Docsity

C Programming: Array Traversal, Matrix Addition, Linear Search, Min/Max, and Pointers, Thesis of Data Structures and Algorithms

Partial preview of the text

Download C Programming: Array Traversal, Matrix Addition, Linear Search, Min/Max, and Pointers and more Thesis Data Structures and Algorithms in PDF only on Docsity!

[1] Write a program to traverse and display all the elements of n sized array. Program :- #include<stdio.h> #include<conio.h> void main() { int a[30],i,n; clrscr(); printf("Enter the number of element: "); scanf("%d",&n); printf("\n Enter the elements: \n"); for(i=1;i<=n;i++) { scanf("%d",&a[i]); } for(i=1;i<=n;i++) { printf("Element[%d]=%d \n",i,a[i]); } getch(); }

Output :-

[2] Write a program to add two matrix. Program :- #include<stdio.h> #include<conio.h> void main() { int a[10][10],b[10][10],c[10][10],i,m,n,j; clrscr(); printf("Enter the order of matrix:\n"); scanf("%d%d",&m,&n); printf("Enter the elements of first matrix:\n"); for(i=0;i<m;i++) { for(j=0;j<n;j++)

getch(); }

Output :-

[3]Write a program to search an element from an array using linear search. Program :- #include<stdio.h> #include<conio.h> void main() { int a[30],data,c=0,n,i; clrscr(); printf("Enter the value of n \n ");

scanf("%d",&n); printf("Enter the array Element \n"); for(i=1;i<=n;i++) scanf("%d",&a[i]); printf("Enter the Element which you want to search \n"); scanf("%d",&data); i=1; while(i<=n) { if(a[i]==data) { printf("The Element found at : %d",i); } i++; } if(c==0) printf("Element not found at any location"); getch(); }

Output :-

printf("Maximum Element in array is %d",max); min=a[0]; for(i=0;i<n;i++) { if(a[i]<min) { min=a[i]; } } printf("\nThe minimum element in array is %d",min); getch(); }

Output :-

[5] Write a program to declare and use pointer variable in the program. Program :-

#include<stdio.h> #include<conio.h> void main() { int a; int ptr; clrscr(); printf("Enter any integer(a):"); scanf("%d",&a); ptr=&a; printf("The value of a is %d \n",a); printf("The address of %d is %x \n",a,&a); printf("The value of pointer ptr is %d \n",ptr); printf("The address of pointer ptr is %x \n",ptr); getch(); }

Output :-

scanf("%d",&a[i]); p[i]=&a[i]; } for(i=0;i<n;i++) { printf("The value of a[%d]=%d \n",i,a[i]); printf("The address of %d=%x \n",a[i],p[i]); } getch(); }

Output :-

[7] Write a program to dynamically allocate and diallocate memory. Program :-

#include<stdio.h> #include<conio.h> #include<stdlib.h> void main() { int n,i,a; int *p; clrscr(); printf("How many values you want to enter:"); scanf("%d",&n); printf("Enter the elements of array: \n"); p=(int )malloc(nsizeof(int)); if(p!=NULL) { for(i=1;i<=n;i++) { scanf("%d",&p[i]); } printf("\n Elements of array are:\n"); for(i=1;i<=n;i++) { printf("%d\n",p[i]); } free(p); } else {