Posts

Showing posts from May, 2020

All Operations on Singly Linked List

All Operations on Singly Linked Link List I have combined all the operation that could be performed on a singly linked link list and mashed it up into a single program. All the operations are separate and independent with the use of separate function for each operations. The operations which have been used are : - 1. Initializing node and adding. 2. Inserting node at any position. 3. Deletion of Node at any position. 4. Counting the total number of nodes. 5. Traversing and printing a linklist(Iteratively) 6. Traversing a link-list and printing it backward(Recursively) 7. Inversing a Singly Linked List using Iteration. 8. Inversing a Singly Linked List using recursion. The program in C

Palindrome Number Checker

What is a Palindrome Number ? A  palindromic number  (also known as a  numeral palindrome  or a  numeric palindrome ) is a number that remains the same when its digits are reversed. Like 16461, for example, it is "symmetrical". The term  palindromic  is derived from  palindrome , which refers to a word (such as  rotor  or  racecar ) whose spelling is unchanged when its letters are reversed. The first 30 palindromic numbers (in  decimal ) are: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, 66, 77, 88, 99, 101, 111, 121, 131, 141, 151, 161, 171, 181, 191, 202, … (sequence  A002113  in the  OEIS ). Palindromic numbers receive most attention in the realm of  recreational mathematics . A typical problem asks for numbers that possess a certain property  and  are palindromic. For instance: The  palindromic primes  are 2, 3, 5, 7, 11, 101, 131, 151, … (sequence  A002385  in the  OEIS ). The palindromic  square numbers  are 0, 1, 4, 9, 121, 484, 676, 10201, 12321, … (se

Snake's Game

Snake Snake Score: 0 Game Over press space to begin a new game settings Settings new game Speed: Slow Normal Fast Wall: On Off Snake new game settings

Delete a character in a string in C

How to delete a character in a string For example : ' School ' is the string and 'c ' is the word to be replaced, then the word would become 'shool ' . Similarly if we remove the letter ' o ' from the string ' school ', then the string would become ' schl ' removing all the ' o ' s present.  The program for doing this in C.

Matrix Multiplication in C

Image
The Concept behind Matrix Multiplication Two matrixes of order ( m x n ) and ( o x p ) can only be multiplied if ( n = p ). Other-wise matrix multiplication is not possible. So, the matrix-es must full-fill the condition before going forward. Following the multiplication pattern the syntax for matrix multiplication is as follows.   As ( n = p ), we consider the order of the matrix ( m x n ) and ( n x p ). So the generalized syntax of the matrix multiplication will be :  For [1st Matrix - A ]( m x n ) X ( n x p )[Second Matrix - B ] matrix : Resultant Matrix - C ( m x p ) Sum - temporary varible to store iterations. for (i=0;i<m;i++) { for (j=0;j<p;j++) { sum = 0; for (k=0;k<n;k++) { sum = sum + A[i][k] * B[k][j]; } C[i][j] = sum; } } Matrix Multiplication program in C: 

Matrix Addition in C

Matrix multiplication is one of the most easiest operation that could be done on matrix. Passing two 2D-Arrays ( matrix ) in a function and returning their sum to the main function and printing it. General Way of Matrix Addition. Matrix Addition using Pointers Matrix Addition using Array of Pointers

Free Function ( Dynamic Memory Allocation Part - 5 )

Definition Compile time storage of a variable is allocated and is released after being used. But in the case of dynamical memory allocation as the memory is stored in the stack it is the responsibility of the user to free the memory space occupied by malloc or calloc functions after completing their operation. Storage is scarce so freeing memory when it is not being used is very important. So comes the free function. Syntax of the free function free ( pointer );  Some important points to remember of the free function : The pointer which has been originally used to store the malloc or calloc adresses must be used. Using invalid pointers will return a null pointer which may result in crashing of the system. It is not the pointer that is being released, it the memory at which the pointer points to. The pointer is stored in the stack and will be erased after completion of the function where pointer is a local variable.  To release an array of memory in the calloc we need to

Realloc Function ( Dynamic Memory Allocation Part - 4 )

Defination Sometimes we may discover that the previously allocated memory is not enough or there is a wastage of memory space which we want to end. Realloc function is a one way solution to it. We can reallocate memory and can increase or decrease the amount of memory allocation by the realloc function in the heap memory.  Some key points to note of the realloc function.  If any previous data is stored in the memory allocated by malloc or calloc though a pointer, the previous data will surely be restored as the program surely guarantees that. In order to store a value in the malloc or calloc variable use strcpy in case of strings. Pass the same pointer to the realloc which has been passed to create memory blocks in calloc or malloc functions. If the function is unsuccessful in allocation of memory, it return a null pointer to the original block and the previous data will be lost, which is a big downside of the realloc function. The general syntax of the calloc function i

Calloc Function ( Dynamic Memory Allocation Part -3 )

Definition Calloc function allocates  series of similar bock of elements in the heap which is executed and allotted at the runtime of the program execution. As the program instructions cannot directly access heap memory, so a pointer is required which points to the memory address of the first memory byte allocated by the calloc function in heap.  The calloc returns a void pointer which is to be type-casted to the convenient data type as in use by the user.  Malloc function is declared under stdlib.h library. So include this in the program in the section of the header files, to avoid getting a warning or syntax error in certain compilers. The general syntax of the calloc function is : < data_type> *pointer;   pointer  = ( < type casting * > ) calloc(< number of elements >,< byte_size >)   For storing float in the dynamic memory we can use : float *pointer; pointer= ( float* )calloc(5,sizeof( float )) A program using calloc function

Exit Function in C

 Exit ( ) Function in C The exit() function is used to terminate program execution and to return to the operating system. The return code "0" exits a program without any error message, but other codes indicate that the system can handle the error messages. Exit ( 0 ) - This indicates that the program exits without returning any error message. Exit ( 1 ) - This indicates that the program exits, indicating that something went wrong. *** Exit function is defined under the header stdlib.h Syntax of the exit function void exit(int return_code); Example of exit ( ) function

Concept of Stack & Heap ( Dynamic Memory Allocation Part - 1 )

Image
Dynamic Memory Allocation Definition  :-  The process of allocating memory during  runtime of the program  is known as dynamic memory  allocation.  Although C does not inheritively has this property but there are four  " memory management function "  that can be used to allocate memory during runtime.  malloc( ) -  Allocates required size of memory bytes and returns the pointer to the first byte of the allocated space. calloc( )  -  Allocates an array of memory bytes and each memory block corresponds to a malloc memory allocation and then initialized them to zero and returns the pointer.   realloc( ) -  Increasing the size of the previously allocated space. free ( ) -  Frees previously allocated memory. Memory allocation Process in C A diagrammatic representation of the memory allocation in c. The Concept Of Permanent Storage Area A diagrammatic representation of the memory allocation in c. Memory in C can be divided into three mai

Malloc Function ( Dynamic Memory Allocation Part - 2 )

Defination The malloc allocates a block of memory in the heap and a pointer is to be declared at the stack of the memory which would contain the base address of the heap's first memory byte and would permanently point at it. During runtime of the program the memory is accessed not directly but through pointers which have declared in the stack. The address returned is in the form of a void pointer address, which is to be type casted according to convenience of the user. So malloc returns a null pointer. Malloc function is declared under stdlib.h library. So include this in the program in the section of the header files, to avoid getting a warning or syntax error in certain compilers. The general syntax of the malloc function is : < data_type> *pointer; pointer  = ( < type casting * > ) malloc(< byte_size >) For storing float in the dynamic memory we can use :  float *pointer; pointer= ( float* )malloc(5*sizeof( float )) A program using malloc

ssasa

sasasasas

Shell Sort in ( C and Python 3 )

Shell Sorting Shell sort is a highly efficient sorting algorithm and is based on insertion sort algorithm. This algorithm avoids large shifts as in case of insertion sort, if the smaller value is to the far right and has to be moved to the far left. This algorithm uses insertion sort on a widely spread elements, first to sort them and then sorts the less widely spaced elements. This spacing is termed as  interval . This interval is calculated based on Knuth's formula as − Time Complexity Best Case Scenario : O( n ) Average Case Scenario : O( n log n ) Worst Case Scenario : O( n ^ 1.25 )    Knuth's Formula h = h * 3 + 1 where − h is interval with initial value 1 This algorithm is quite efficient for medium-sized data sets as its average and worst-case complexity of this algorithm depends on the gap sequence the best known is Ο(n), where n is the number of items. And the worst case space complexity is O(n). Shell Sorting in C Shell Sorting

Tic Tac Toe game in ( Python 3 )

Image
Tic Tac Toe game Tic-tac-toe  ( American English ),  noughts and crosses  ( British English ), or  Xs and Os  is a  paper-and-pencil game  for two players,  X  and  O , who take turns marking the spaces in a 3×3 grid. The player who succeeds in placing three of their marks in a horizontal, vertical, or diagonal row is the winner. Tic Tac Toe in Python3 (Instructions to play the game)   0 0    0 1   0 2       ----> Use these co-ordinates to play  1 0    1 1   1 2               the game, here in the program   2 0    2 1   2 2               below.( use spaces in between                                       the   numbers i.e for ex: 0 0 not 00 )

Date Time Correcter in C

Question Develop a program to enter a date and check if it correct, keeping the days of each month and the concept of Leap year in mind.

Merge Sort ( C & Python 3 )

Image
Merge Sort  Merge sort is one of the most efficient sorting algorithms. It works on the principle of Divide and Conquer. Merge sort repeatedly breaks down a list into several sublists until each sublist consists of a single element and merging those sublists in a manner that results into a sorted list. Algorithm:- Conceptually, a merge sort works as follows : Divide the unsorted list into n sublists, each containing 1 element (a list of 1 element is considered sorted). Repeatedly merge sublists to produce new sorted sublists until there is only 1 sublist remaining. This will be the sorted list. A pictorial representation of Merge Sort :- Speed of Merge Sort : -  Best case Scenario   : - θ( n log n ) Worst Case  Scenario  : - θ( n log n ) So, merge sort is one of the most efficient ways of sorting.  The main algorithm of Merging explained with the help of a program in Python 3. ( Not the complete program ) """ The programming is about taking tw

Type Casting in C

Type Casting in C Type Casting refers to changing of the variable data type from one form to another after being initialized. The compiler will automatically change the type of the data into another type if it makes sense. There are two types of Type Casting:  1. Implicit Type Conversion. 2. Explicit Type Conversion. Implicit Type Conversion. I mplicit type conversion is only known as automatic type conversion. It is done by the computer on it's own without any interference of the user or without triggering anything.  All the data types of the variables are upgraded to the data type of the variable with largest data type. bool -> char -> short int -> int -> unsigned int -> long -> unsigned -> long long -> float -> double -> long double Example of Type Implicit Conversion: / * Implicit Type Convertion @uther- Abhishek */ #include <stdio.h> int main() { int a=5,c; char b='z'; float