1. C Program to implement initialization of array and perform traversal operations in both the directions. PROGRAM: #include<stdio.h> void main() { int n,i,a[100]; printf(\"Enter how many values you want to read : \"); scanf(\"%d\",&n); for(i=0;i<n;i++) { printf(\"Enter the value of a[%d] : \",i); scanf(\"%d\",&a[i]); } printf(\"The array elements are : \"); for(i=0;i<n;i++) { printf(\"%d \",a[i]); } printf(\"\\n\"); printf(\"Elements in backward direction: \"); for(i=n-1;i>=0;i--) { printf(\"%d \",a[i]); } } 2. C Program to implement searching operation on array using Linear Search. #include<stdio.h> void main() { int n,i,a[100],key,flag; printf(\"Enter value of n : \"); scanf(\"%d\",&n); for(i=0;i<n;i++) { printf(\"Enter element for a[%d] : \",i); scanf(\"%d\",&a[i]); } printf(\"Enter key element : \"); scanf(\"%d\",&key); for(i=0;i<n;i++) { if(a[i]==key) { flag=1; break; } } if(flag==1) { printf(\"The key element %d is found at the position %d\\n\",key,i); } else
{ printf(\"The key element %d is not found in the array\\n\",key); } } 3. C Program to display the count of occurrences of every number in an array. #include<stdio.h> void main() { int n,a[100],i,j,count,freq[100]; printf(\"Enter the size of array (n<=10): \"); scanf(\"%d\",&n); printf(\"Enter %d Elements:\\n\",n); for(i=0;i<n;i++) { scanf(\"%d\",&a[i]); freq[i]=-1; } for(i=0;i<n;i++) { count=1; for(j=i+1;j<n;j++) { if(a[i]==a[j]) { count++; freq[j]=0; } } if (freq[i]!=0) { freq[i]=count; } } for(i=0;i<n;i++) { if(freq[i]!=0) { printf(\"%d occurs %d times \\n\",a[i],freq[i]); } } } 4. C Program to implement searching operation on array using Binary Search. #include<stdio.h> void main() { int a[100],i,j,flag,n,temp,key; printf(\"Enter value of n : \"); scanf(\"%d\",&n); for(i=0;i<n;i++) { printf(\"Enter element for a[%d] : \",i); scanf(\"%d\",&a[i]); }
printf(\"Enter key element : \"); scanf(\"%d\",&key); for(i=0;i<n;i++) { for(j=i+1;j<n;j++) { if(a[i]>a[j]) { temp=a[i]; a[i]=a[j]; a[j]=temp; } } } printf(\"After sorting the elements in the array are\\n\"); for(i=0;i<n;i++) { printf(\"Value of a[%d] = %d\\n\",i,a[i]); } for(i=0;i<n;i++) { if(key==a[i]) { flag=1; break; } } if(flag==1) { printf(\"The key element %d is found at the position %d\\n\",key,i); } else { printf(\"The Key element %d is not found in the array\\n\",key); } } 5. Write a C program to insert an element in an array. #include<stdio.h> void main() { int n,i,a[100],pos,item; printf(\"Enter no of elements in array: \"); scanf(\"%d\",&n); printf(\"Enter %d elements: \",n); for(i=0;i<n;i++) { scanf(\"%d\",&a[i]); } printf(\"Enter the position where you want to insert: \"); scanf(\"%d\",&pos); printf(\"Enter the value into that position: \"); scanf(\"%d\",&item); i++; for(i=n-1;i>=pos-1;i--) { a[i+1]=a[i]; }
a[pos-1]=item; n++; printf(\"Final array is: \"); for(i=0;i<n;i++) { printf(\"%d \",a[i]); } printf(\"\\n\"); } 6. Write a program to delete an element #include<stdio.h> void main() { int n,a[100],del,i; printf(\"Enter number of elements in array: \"); scanf(\"%d\",&n); printf(\"Enter %d elements\\n\",n); for(i=0;i<n;i++) { scanf(\"%d\",&a[i]); } printf(\"Enter the location where you wish to delete element: \"); scanf(\"%d\",&del); if(del>=n+1) { printf(\"Deletion not possible.\\n\"); } else { printf(\"Resultant array is\\n\"); for(i=del-1;i<n-1;i++) { a[i]=a[i+1]; } for(i=0;i<n-1;i++) { printf(\"%d\\n\",a[i]); } } } 7. Write a program to represent and display sparse matrix. #include<stdio.h> void main() { int i,j,r,c,a[100][100],count; printf(\"Enter the size of matrix (rows, columns): \"); scanf(\"%d %d\",&r,&c); printf(\"Enter elements of matrix\\n\"); for(i=0;i<r;i++) { for(j=0;j<c;j++) {
printf(\"[%d][%d]: \",i,j); scanf(\"%d\",&a[i][j]); if(a[i][j]!=0) { count++; } } } printf(\"Sparse form - list of 3 triples\\n\"); printf(\"%d %d %d\\n\",r,c,count); for(i=0;i<r;i++) { for(j=0;j<c;j++) { if(a[i][j]!=0) { printf(\"%d %d %d\\n\",i,j,a[i][j]); } } } } 8. Write a program to implement initialization of arrays and traversal operation with DMA #include<stdio.h> //#include<alloc.h> void main() { int i,n; int *p; printf(\"Enter number of elements:\\n\"); scanf(\"%d\",&n); p=(int*)malloc(n*sizeof(int)); printf(\"Enter %d elements into array:\\n\",n); for(i=0;i<n;i++) { scanf(\"%d\",&p[i]); } printf(\"Elements of array are as follow:\\n\"); for(i=0;i<n;i++) { printf(\"%d \",p[i]); } } 9. #include<malloc.h> void main(){ int **m1; int **m2; int o1,o2,i,j; printf(\"Enter the order of the matrix 1 & 2: \\n\"); scanf(\"%d%d\",&o1,&o2); printf(\"Enter values into 2D array of rows=%d, cols=%d\\n\",o1,o2); m1=(int**)malloc(o1*sizeof(int*)); for(i=0;i<o1;i++) {
m1[i]=(int*) malloc (o2*sizeof(int)); } m2=(int**) malloc(o1*sizeof(int*)); for(i=0;i<o1;i++) { m2[i]=(int*) malloc(o2*sizeof(int)); } for(i=0;i<o1;i++) { for(j=0;j<o2;j++) { scanf(\"%d\",&m1[i][j]); } } printf(\"Enter values into 2D array of rows=%d, cols=%d\\n\",o1,o2); for(i=0;i<o1;i++) { for(j=0;j<o2;j++) { scanf(\"%d\",&m2[i][j]); } } printf(\"Matrix-1 is as follow:\\n\"); for(i=0;i<o1;i++) { for(j=0;j<o2;j++) { printf(\"%d \",m1[i][j]); } printf(\"\\n\"); } printf(\"Matrix-2 is as follow:\\n\"); for(i=0;i<o1;i++) { for(j=0;j<o2;j++) { printf(\"%d \",m2[i][j]); } printf(\"\\n\"); } printf(\"Resultant Sum of 2 Matrices is as follow:\\n\"); for(i=0;i<o1;i++) { for(j=0;j<o2;j++) } printf(\"\\n\"); } printf(\"Resultant difference of 2 Matrices is as follow:\\n\"); for(i=0;i<o1;i++) { for(j=0;j<o2;j++) { printf(\"%d \",m1[i][j]-m2[i][j]); } printf(\"\\n\"); } }
10. Write a program to implement Stack Operations using arrays. #include<stdio.h> int main() { int n,in,i,a[100],top=-1; printf(\"Enter the choice for stack operation: \\n\"); printf(\"press 1 for push\\npress 2 for pop\\npress 3 for display\\npress 4 for exit\\n\"); scanf(\"%d\",&n); do { // printf(\"Enter the choice for stack operation: \"); //scanf(\"%d\",&n); if(n==1) { top++; // printf(\"Enter the choice for stack operation: \"); //scanf(\"%d\",&n); printf(\"Enter item to insert: \"); scanf(\"%d\",&in); a[top]=in; printf(\"Item inserted = %d\\n\",a[top]); } else if(n==2) { if(top==-1) { printf(\"Stack underflow\\n\"); } else { printf(\"Item deleted = %d\\n\",a[top]); top--; } } else if(n==3) { if(top!=-1) { printf(\"Stack are\\n\"); for(i=top;i>=0;i--) { printf(\"%d \",a[i]); } printf(\"\\n\"); } else { printf(\"No more data\\n\"); } } else { exit(0); } printf(\"Enter the choice for stack operation: \"); scanf(\"%d\",&n); }
Search
Read the Text Version
- 1 - 8
Pages: