Posts

C Program to Insert an Element into Array

Previously we learned to insert elements into empty arrays in the order. But how can we insert a particular element into a desired position of an array? For that we need to initialize a variable 'key' which we may use to indicate the position of PC in program. Let's have a look into the program. //Program #include <stdio.h> void main() { int array[10]; int i, j, n, m, temp, key, pos; printf( "Enter how many elements \n "); scanf( "%d" , &n); printf( "Enter the elements \n "); for (i = 0; i < n; i++) { scanf( "%d" , &array[i]); } printf( "Input array elements are \n "); for (i = 0; i < n; i++) { printf( "%d\n ", array[i]); } for (i = 0; i < n; i++) { for (j = i + 1; j < n; j++) { if (array[i] > array[j]) { temp = array[i]; array[i] = array[j]; array[j] = temp; } } } printf( "Sorted list is \n "); for (i = 0; i &

C Program to Delete the Specified Integer from an Array

This C Program deletes specified integer from an array. It accepts an array and the number to delete. #include <stdio.h>   void main() { int vectorx[10]; int i, n, pos, element, found = 0;   printf( "Enter how many elements\n "); scanf( "%d" , &n); printf( "Enter the elements\n "); for (i = 0; i < n; i++) { scanf( "%d" , &vectorx[i]); } printf( "Input array elements are\n "); for (i = 0; i < n; i++) { printf( "%d\n ", vectorx[i]); } printf( "Enter the element to be deleted\n "); scanf( "%d" , &element); for (i = 0; i < n; i++) { if (vectorx[i] == element) { found = 1; pos = i; break; } } if (found == 1) { for (i = pos; i < n - 1; i++) { vectorx[i] = vectorx[i + 1]; } printf( "The resultant vector is \n "); for (i = 0; i &l

C Program to Seperate Even & Odd Elements of an Array

In this program we find the odd and even elements of an array and insert them into two different arrays. Here the logic of the program to find whether a number is odd or even is used. //Program #include <stdio.h> void main() { long int ARR[10], OAR[10], EAR[10]; int i, j = 0, k = 0, n;   printf( "Enter the size of array AR \n "); scanf( "%d" , &n); printf( "Enter the elements of the array \n "); for (i = 0; i < n; i++) { scanf( "% ld " , &ARR[i]); fflush(stdin); } /* Copy odd and even elements into their respective arrays */ for (i = 0; i < n; i++) { if (ARR[i] % 2 == 0) { EAR[j] = ARR[i]; j++; } else { OAR[k] = ARR[i]; k++; } } printf( "The elements of OAR are \n "); for (i = 0; i < j; i++) { printf( "% ld \n ", OAR[i]); } printf( "The elements of EAR are \n "

C Program to Find The Largest Number in an Array

This program makes the concept of array more clear. As arrays are continuous memory locations, we can access them easily by incrementing the number (variable) in square bracket. This method is used in this program. Here using loop each and every element is comparing it with the other elements. //program #include <stdio .h>   int main ( ) { int array [ 50 ] , size , i , largest ; printf ( " \n Enter the size of the array: " ) ; scanf ( "%d" , & size ) ; printf ( " \n Enter %d elements of the array: " , size ) ; for ( i = 0 ; i < size ; i ++ ) scanf ( "%d" , & array [ i ] ) ; largest = array [ 0 ] ; for ( i = 1 ; i < size ; i ++ ) { if ( largest < array [ i ] ) largest = array [ i ] ; } printf ( " \n largest element present in the given array is : %d" , largest ) ; return 0 ; }

C Program to Calculate The Average of Array Elements

It is as simple as the previous program in which we calculated the sum of array elements. //Program #include <stdio.h> void main() {     int n, i;        float num[100], sum = 0.0, average;     printf("Enter the numbers of elements: ");     scanf("%d", &n);     while (n > 100 || n <= 0)     {         printf("Error! number should in range of (1 to 100).\n");         printf("Enter the number again: ");         scanf("%d", &n);     }     for(i = 0; i < n; ++i)     {         printf("%d. Enter number: ", i+1);         scanf("%f", &num[i]);         sum += num[i];     }     average = sum / n;     printf("Average = %.2f", average);     return 0; }

C Program to Calculate Sum of Array Elements

Array is a mile stone of C Program which we may use almost everywhere. Here is a basic program to understand array, memory locations and structure of array etc. What is an array? An array is a group of continuous memory locations which we can access by the address. An array of a particular datatype can only store values of that data type(often it convert and use the given data). Array initialization The syntax of array initialization in C is : datatype array_name[size]; Algorithm Here the array named 'array' of type 'int' is used to store different numbers and adding them by separately accessing the array elements using for loop. //Program //Program #include <stdio.h< voide main() {     int n, i;        float num[100], sum = 0.0, average;     printf("Enter the numbers of elements: ");     scanf("%d", &n);     while (n > 100 || n <= 0)     {         printf("Error! number should in range of (1 to 100).
C Program to Print an Integer (Entered by the User) //Program written by Nimiya Joseph #include <stdio.h> int main() {     int number;     // printf() dislpays the formatted output      printf("Enter an integer: ");            // scanf() reads the formatted input and stores them     scanf("%d", &number);            // printf() displays the formatted output     printf("You entered: %d", number);     return 0; }