Table of Contents
Practice Programs on Arrays
1. Merge two arrays of same size sorted in descending order:
#include <stdio.h> void main() { int arr1[100], arr2[100], arr3[200]; int s1, s2, s3; int i, j, k; printf("\n\nMerge two arrays of same size sorted in decending order.\n"); printf("------------------------------------------------------------\n"); printf("Input the number of elements to be stored in the first array :"); scanf("%d",&s1); printf("Input %d elements in the array :\n",s1); for(i=0;i<s1;i++) { printf("element - %d : ",i); scanf("%d",&arr1[i]); } printf("Input the number of elements to be stored in the second array :"); scanf("%d",&s2); printf("Input %d elements in the array :\n",s2); for(i=0;i<s2;i++) { printf("element - %d : ",i); scanf("%d",&arr2[i]); } /* size of merged array is size of first array and size of second array */ s3 = s1 + s2; /*----------------- insert in the third array------------------------------------*/ for(i=0;i<s1; i++) { arr3[i] = arr1[i]; } for(j=0;j<s2; j++) { arr3[i] = arr2[j]; i++; } /*----------------- sort the array in decending order ---------------------------*/ for(i=0;i<s3; i++) { for(k=0;k<s3-1;k++) { if(arr3[k]<=arr3[k+1]) { j=arr3[k+1]; arr3[k+1]=arr3[k]; arr3[k]=j; } } } /*--------------- Prints the merged array ------------------------------------*/ printf("\nThe merged array in decending order is :\n"); for(i=0; i<s3; i++) { printf("%d ", arr3[i]); } printf("\n\n"); }
*****************************************************************
2. Count the frequency of each element of an array:
#include <stdio.h> void main() { int arr1[100], fr1[100]; int n, i, j, ctr; printf("\n\nCount frequency of each element of an array:\n"); printf("------------------------------------------------\n"); printf("Input the number of elements to be stored in the array :"); scanf("%d",&n); printf("Input %d elements in the array :\n",n); for(i=0;i<n;i++) { printf("element - %d : ",i); scanf("%d",&arr1[i]); fr1[i] = -1; } for(i=0; i<n; i++) { ctr = 1; for(j=i+1; j<n; j++) { if(arr1[i]==arr1[j]) { ctr++; fr1[j] = 0; } } if(fr1[i]!=0) { fr1[i] = ctr; } } printf("\nThe frequency of all elements of array : \n"); for(i=0; i<n; i++) { if(fr1[i]!=0) { printf("%d occurs %d times\n", arr1[i], fr1[i]); } } }
***************************************************************
3. Sort elements of array in ascending order:
#include <stdio.h> void main() { int arr1[100]; int n, i, j, tmp; printf("\n\nsort elements of array in ascending order :\n "); printf("----------------------------------------------\n"); printf("Input the size of array : "); scanf("%d", &n); printf("Input %d elements in the array :\n",n); for(i=0;i<n;i++) { printf("element - %d : ",i); scanf("%d",&arr1[i]); } for(i=0; i<n; i++) { for(j=i+1; j<n; j++) { if(arr1[j] <arr1[i]) { tmp = arr1[i]; arr1[i] = arr1[j]; arr1[j] = tmp; } } } printf("\nElements of array in sorted ascending order:\n"); for(i=0; i<n; i++) { printf("%d ", arr1[i]); } printf("\n\n"); }
*******************************************************************
4. Insert New value in the array (sorted list):
#include <stdio.h> void main() { int arr1[100],i,n,p,inval; printf("\n\nInsert New value in the sorted array :\n"); printf("-----------------------------------------\n"); printf("Input the size of array : "); scanf("%d", &n); /* Stored values into the array*/ printf("Input %d elements in the array in ascending order:\n",n); for(i=0;i<n;i++) { printf("element - %d : ",i); scanf("%d",&arr1[i]); } printf("Input the value to be inserted : "); scanf("%d",&inval); printf("The exist array list is :\n "); for(i=0;i<n;i++) printf("% 5d",arr1[i]); /* Determine the position where the new value will be insert.*/ for(i=0;i<n;i++) if(inval<arr1[i]) { p = i; break; } /* move all data at right side of the array */ for(i=n;i>=p;i--) arr1[i]= arr1[i-1]; /* insert value at the proper position */ arr1[p]=inval; printf("\n\nAfter Insert the list is :\n "); for(i=0;i<=n;i++) printf("% 5d",arr1[i]); printf("\n"); }
************************************************************************
5. Delete an element at desired position from an array:
#include <stdio.h> void main() { int arr1[50],i,pos,n; printf("\n\nDelete an element at desired position from an array :\n"); printf("---------------------------------------------------------\n"); printf("Input the size of array : "); scanf("%d", &n); /* Stored values into the array*/ printf("Input %d elements in the array in ascending order:\n",n); for(i=0;i<n;i++) { printf("element - %d : ",i); scanf("%d",&arr1[i]); } printf("\nInput the position where to delete: "); scanf("%d",&pos); /*---- locate the position of i in the array -------*/ i=0; while(i!=pos-1) i++; /*---- the position of i in the array will be replaced by the value of its right */ while(i<n) { arr1[i]=arr1[i+1]; i++; } n--; printf("\nThe new list is : "); for(i=0;i<n;i++) { printf(" %d",arr1[i]); } printf("\n\n"); }
*********************************************************************
6. Addition of two Matrices:
#include <stdio.h> void main() { int arr1[50][50],brr1[50][50],crr1[50][50],i,j,n; printf("\n\nAddition of two Matrices :\n"); printf("------------------------------\n"); printf("Input the size of the square matrix (less than 5): "); scanf("%d", &n); /* Stored values into the array*/ printf("Input elements in the first matrix :\n"); for(i=0;i<n;i++) { for(j=0;j<n;j++) { printf("element - [%d],[%d] : ",i,j); scanf("%d",&arr1[i][j]); } } printf("Input elements in the second matrix :\n"); for(i=0;i<n;i++) { for(j=0;j<n;j++) { printf("element - [%d],[%d] : ",i,j); scanf("%d",&brr1[i][j]); } } printf("\nThe First matrix is :\n"); for(i=0;i<n;i++) { printf("\n"); for(j=0;j<n;j++) printf("%d\t",arr1[i][j]); } printf("\nThe Second matrix is :\n"); for(i=0;i<n;i++) { printf("\n"); for(j=0;j<n;j++) printf("%d\t",brr1[i][j]); } /* calculate the sum of the matrix */ for(i=0;i<n;i++) for(j=0;j<n;j++) crr1[i][j]=arr1[i][j]+brr1[i][j]; printf("\nThe Addition of two matrix is : \n"); for(i=0;i<n;i++){ printf("\n"); for(j=0;j<n;j++) printf("%d\t",crr1[i][j]); } printf("\n\n"); }
**************************************************************
7. Multiplication of two Matrices:
#include <stdio.h> void main() { int arr1[50][50],brr1[50][50],crr1[50][50],i,j,k,r1,c1,r2,c2,sum=0; printf("\n\nMultiplication of two Matrices :\n"); printf("----------------------------------\n"); printf("\nInput the rows and columns of first matrix : "); scanf("%d %d",&r1,&c1); printf("\nInput the rows and columns of second matrix : "); scanf("%d %d",&r2,&c2); if(c1!=r2) { printf("Mutiplication of Matrix is not possible."); printf("\nColumn of first matrix and row of second matrix must be same."); } else { printf("Input elements in the first matrix :\n"); for(i=0;i<r1;i++) { for(j=0;j<c1;j++) { printf("element - [%d],[%d] : ",i,j); scanf("%d",&arr1[i][j]); } } printf("Input elements in the second matrix :\n"); for(i=0;i<r2;i++) { for(j=0;j<c2;j++) { printf("element - [%d],[%d] : ",i,j); scanf("%d",&brr1[i][j]); } } printf("\nThe First matrix is :\n"); for(i=0;i<r1;i++) { printf("\n"); for(j=0;j<c1;j++) printf("%d\t",arr1[i][j]); } printf("\nThe Second matrix is :\n"); for(i=0;i<r2;i++) { printf("\n"); for(j=0;j<c2;j++) printf("%d\t",brr1[i][j]); } //multiplication of matrix for(i=0;i<r1;i++) for(j=0;j<c2;j++) crr1[i][j]=0; for(i=0;i<r1;i++) //row of first matrix { for(j=0;j<c2;j++) //column of second matrix { sum=0; for(k=0;k<c1;k++) sum=sum+arr1[i][k]*brr1[k][j]; crr1[i][j]=sum; } } printf("\nThe multiplication of two matrices is : \n"); for(i=0;i<r1;i++) { printf("\n"); for(j=0;j<c2;j++) { printf("%d\t",crr1[i][j]); } } } printf("\n\n"); }
**************************************************************
8. Transpose of a Matrix:
#include <stdio.h> void main() { int arr1[50][50],brr1[50][50],i,j,r,c; printf("\n\nTranspose of a Matrix :\n"); printf("---------------------------\n"); printf("\nInput the rows and columns of the matrix : "); scanf("%d %d",&r,&c); printf("Input elements in the first matrix :\n"); for(i=0;i<r;i++) { for(j=0;j<c;j++) { printf("element - [%d],[%d] : ",i,j); scanf("%d",&arr1[i][j]); } } printf("\nThe matrix is :\n"); for(i=0;i<r;i++) { printf("\n"); for(j=0;j<c;j++) printf("%d\t",arr1[i][j]); } for(i=0;i<r;i++) { for(j=0;j<c;j++) { brr1[j][i]=arr1[i][j]; } } printf("\n\nThe transpose of a matrix is : "); for(i=0;i<c;i++) { printf("\n"); for(j=0;j<r;j++) { printf("%d\t",brr1[i][j]); } } printf("\n\n"); }
*******************************************************
9. Find the missing number from a given array:
#include <stdio.h> int pickMissNumber(int *arr1, int ar_size) { int i, sum = 0, n = ar_size + 1; for(i = 0; i < ar_size; i++) { sum = sum + arr1[i]; } return (n*(n+1))/2 - sum; } int main() { int i; int arr1[] = {1, 3, 4, 2, 5, 6, 9, 8}; int ctr = sizeof(arr1)/sizeof(arr1[0]); printf("The given array is : "); for(i = 0; i < ctr; i++) { printf("%d ", arr1[i]); } printf("\n"); printf("The missing number is : %d \n", pickMissNumber(arr1, ctr)); return 0; }
*************************************************************
10. Find two elements whose sum is closest to zero:
#include <stdio.h> #include <math.h> #include <stdlib.h> void findMinSumPair(int *arr1, int arr_size) { int i, j, sum, minSum, min1Pair, min2Pair; if(arr1 == NULL || arr_size < 2) return; min1Pair = arr1[0]; min2Pair = arr1[1]; minSum = min1Pair + min2Pair; for(i = 0; i < arr_size-1; i++) { for(j = i+1; j < arr_size; j++) { sum = arr1[i] + arr1[j]; if(abs(sum) < abs(minSum)) { minSum = sum; min1Pair = arr1[i]; min2Pair = arr1[j]; } } } printf("[%d, %d]\n", min1Pair, min2Pair); } int main() { int arr1[] = {38, 44, 63, -51, -35, 19, 84, -69, 4, -46}; int ctr = sizeof(arr1)/sizeof(arr1[0]); int i; //------------- print original array ------------------ printf("The given array is : "); for(i = 0; i < ctr; i++) { printf("%d ", arr1[i]); } printf("\n"); //------------------------------------------------------ printf("The Pair of elements whose sum is minimum are: \n"); findMinSumPair(arr1, ctr); return 0; }
************************************************
For more practice questions, go to Questionnare.