Vous êtes sur la page 1sur 17

GE 2115 SET I 1.

C Program using dynamic memory allocation to sort n numbers in Ascending main( ) { int *ptr; int i, j, n, temp; printf("\nHow many integer numbers you want to sort? "); scanf("%d", &n); printf("\nPlease enter any %d integer numbers.\n", n) ; ptr = (int *)malloc(n*sizeof(int)); for(i=0; i<n;i++) scanf("%d", ptr+i); prin tf("\nUnsorted number : \n"); for(i=0; i<n;i++) printf("%d\n", *(ptr+i)); for(i= 0; i<n;i++) { for(j=i; j<n;j++) { if (*(ptr+i) > *(ptr+j)) { temp = *(ptr+i); *( ptr+i) = *(ptr+j); *(ptr+j) = temp; } } } printf("\nSorted number is : \n"); for (i=0; i<n;i++) printf("%d\n", *(ptr+i)); free(ptr); }

2.C Program using dynamic memory allocation to sort n numbers in Descending main() { int *ptr; int i, j, n, temp; printf("\nHow many integer numbers you wan t to sort in descending? "); scanf("%d", &n); printf("\nPlease enter any %d inte ger numbers.\n", n); ptr = (int *)malloc(n*sizeof(int)); for(i=0; i<n;i++) scanf ("%d", ptr+i); printf("\nUnsorted number is : \n"); for(i=0; i<n;i++) printf("%d \n", *(ptr+i)); for(i=0; i<n;i++) { for(j=i; j<n;j++) { if (*(ptr+i) < *(ptr+j)) { temp = *(ptr+i); *(ptr+i) = *(ptr+j); *(ptr+j) = temp; } } } printf("\nSorted in descending : \n"); for(i=0; i<n;i++) printf("%d\n", *(ptr+i)); free(ptr); }

3.C Program using dynamic memory allocation to sort names in Ascending #include< stdio.h> #include<conio.h> #include<string.h> main() { char *snames[30]; int n,i ; void sort(char *sn[],int n); clrscr(); printf("\nHow many names do u want to s ort"); scanf("%d",&n); snames[n]=(char *)malloc(n*sizeof(char)); printf("\n Ente r all names\n"); //loop to read all values for(i=0;i<n;i++) scanf("%s",snames[i] ); sort(snames,n); //Call function to sort printf("\n the name list in Ascending order"); for(i=0;i<n;i++) printf("\n%s",snames[i]); getch(); } // Function to s ort strings void sort(sn,n) char *sn[]; int n; { int i,j; char *temp; for (i=0;i <n-1;i++) for(j=i+1;j<n;j++) if(strcmp(sn[i],sn[j])<0) { temp=sn[i]; sn[i]=sn[j] ;

sn[j]=temp; } } 4.C Program using dynamic memory allocation to sort names in Descending #include <stdio.h> #include<conio.h> #include<string.h> main() { char *snames[30]; int n, i; void sort(char *sn[],int n); clrscr(); printf("\nHow many names do u want to sort"); scanf("%d",&n); snames[n]=(char *)malloc(n*sizeof(char)); printf("\n Ent er all names \n"); //loop to read all values for(i=0;i<n;i++) scanf("%s",snames[ i]); sort(snames,n); //Call function to sort printf("\n the name list in Descend ing order"); for(i=0;i<n;i++) printf("\n%s",snames[i]); getch(); } // Function t o sort strings void sort(sn,n) char *sn[]; int n; { int i,j;

char *temp=NULL; for (i=0;i<n-1;i++) for(j=i+1;j<n;j++) if(strcmp(sn[i],sn[j])<0 ) { temp=sn[i]; sn[i]=sn[j]; sn[j]=temp; } } 5.C Program using dynamic memory allocation to add two matrices #include<stdio.h > #include<conio.h> #include<stdlib.h> main() { int *a[10],*b[10],*c[10],m,n,i,j ; /* Loop to allocate and initialize matrices */ for(i=0;i<10;i++) { a[i]=(int * )calloc(10,sizeof(int)); b[i]=(int *)calloc(10,sizeof(int)); c[i]=(int *)calloc( 10,sizeof(int)); } printf("\n How many rows and columns ?\n"); scanf("%d%d",&m,& n); /* Loop to read A matrix values*/ printf("\n Enter A matrix values \n"); for (i=0;i<m;i++) for(j=0;j<n;j++) scanf("%d",&a[i][j]); /*Loop to read B matrix val ues*/ printf("\n Enter B matrix values \n"); clrscr();

for(i=0;i<m;i++) for(j=0;j<n;j++) scanf("%d",&b[i][j]); /*Loop to add matrices * / for(i=0;i<m;i++) for(j=0;j<n;j++) c[i][j]=a[i][j] + b[i][j]; /*loop to print t he result*/ printf("\nAddition of A and B Matrices\n"); for(i=0;i<m;i++) { for(j =0;j<n;j++) printf("%5d",c[i][j]); printf("\n"); } } 6.C Program using dynamic memory allocation to Subtract two matrices #include<st dio.h> #include<conio.h> #include<stdlib.h> main() { int *a[10],*b[10],*c[10],m, n,i,j; /* Loop to allocate and initialize matrices */ for(i=0;i<10;i++) { a[i]=( int *)calloc(10,sizeof(int)); b[i]=(int *)calloc(10,sizeof(int)); c[i]=(int *)ca lloc(10,sizeof(int)); } printf("\n How many rows and columns ?\n"); scanf("%d%d" ,&m,&n); /* Loop to read A matrix values*/ printf("\n Enter A matrix values \n") ; clrscr();

for(i=0;i<m;i++) for(j=0;j<n;j++) scanf("%d",&a[i][j]); /*Loop to read B matrix values*/ printf("\n Enter B matrix values \n"); for(i=0;i<m;i++) for(j=0;j<n;j++ ) scanf("%d",&b[i][j]); /*Loop to sub matrices */ for(i=0;i<m;i++) for(j=0;j<n;j ++) c[i][j]=a[i][j] - b[i][j]; /*loop to print the result*/ printf("\nSubstracti on of A and B Matrices\n"); for(i=0;i<m;i++) { for(j=0;j<n;j++) printf("%5d",c[i ][j]); printf("\n"); } } 7.C Program using dynamic memory allocation to transpos e matrices #include<stdio.h> #include<conio.h> void main() { int *a[10],m,i,j,te mp; for(i=0;i<10;i++) { a[i]=(int *)calloc(10,sizeof(int)); } printf("\n Enter t he Size of the matrix "); scanf("%d",&m); /*Loop to read the matrix*/ printf("\n enter the values of the Matrix");

for(i=0;i<m;i++) for(j=0;j<m;j++) scanf("%d",&a[i][j]); /*Loop to Transpose the Matrix*/ for(i=1;i<m;i++) for(j=0;j<i;j++) { temp=a[i][j]; a[i][j]=a[j][i]; a[j] [i]=temp; } /* Loop to print the Tranposed Matrix*/ printf("\nThe Transposed Mat rix is\n"); for(i=0;i<m;i++) { printf("\n"); for(j=0;j<m;j++) printf("%d\t",a[i] [j]); } getch(); } 8. C Program using dynamic memory allocation to perform multiplication of matric es #include<stdio.h> #include<conio.h> void main() { int *a[10],*b[10],*c[10],m, i,j,k,temp; for(i=0;i<10;i++) { a[i]=(int *)calloc(10,sizeof(int)); b[i]=(int *) calloc(10,sizeof(int));

c[i]=(int *)calloc(10,sizeof(int)); } printf("\n Enter the Size of the matrix ") ; scanf("%d",&m); /*Loop to read the matrix A*/ printf("\n Enter the values of t he Matrix A"); for(i=0;i<m;i++) for(j=0;j<m;j++) scanf("%d",&a[i][j]); /*Loop to read the matrix B*/ printf("\n Enter the values of the Matrix B"); for(i=0;i<m; i++) for(j=0;j<m;j++) scanf("%d",&b[i][j]); /*Loop to Multiply the Matrices*/ fo r(i=0;i<m;i++) for(j=0;j<m;j++) { c[i][j]=0; for(k=0;k<m;k++) c[i][j]=c[i][j]+a[ i][k]*b[k][j]; } /* Loop to print the Resultant Matrix*/ printf("\nThe Resultant Matrix is\n"); for(i=0;i<m;i++) { for(j=0;j<m;j++) printf("%6d",c[i][j]); print f("\n"); } getch(); }

9. C Program using dynamic memory allocation to find the sum of elements of a ma trix #include<stdio.h> #include<conio.h> void main() { int *a[10],m,i,j,n,s=0; f or(i=0;i<10;i++) { a[i]=(int *)calloc(10,sizeof(int)); } printf("\n Enter the no of rows and columns of the matrix "); scanf("%d%d",&m,&n); /*Loop to read the m atrix A*/ printf("\n Enter the values of the Matrix A"); for(i=0;i<m;i++) for(j= 0;j<n;j++) scanf("%d",&a[i][j]); /*Loop to add all elements in the matrix*/ for( i=0;i<m;i++) for(j=0;j<n;j++) s=s+a[i][j]; printf("\n Sum of all elements in the matrix=%d",s); }

10. C Program using dynamic memory allocation to develop mark sheet of 5 student s main() { struct student { int rollno; char name[20]; int m1,m2,m3; int total; int avg; }; struct student *sptr; int i,n,total; float a=10; printf("\n Enter th e no of Students:"); scanf("%d", &n); sptr = (struct student *)malloc(sizeof(str uct student)*n); printf("\n"); for(i=0; i<n; i++) { printf("Enter student #%d ro llno,name,mark1,mark2 and mark3 : \n", i+1); scanf("%d %s %d %d %d", &(sptr+i)-> rollno, &(sptr+i)->name, &(sptr+i)->m1, &(sptr+i)->m2, &(sptr+i)->m3); } printf( "\n STUDENT DETAILS \n"); printf("********************************************** ****************\n"); printf("\nRollno\tName \t\tMark1\tMark2\tMark3 \t Total\tA verage\n"); printf("************************************************************ ***\n"); for(i=0; i<n;i++) { (sptr+i)->total=(sptr+i)->m1+(sptr+i)->m2+(sptr+i)>m3; (sptr+i)->avg=(sptr+i)->total/3; printf("\n%d\t%s\t\t%d\t%d\t%d\t%d\t%d\t%s ",(sptr+i)->rollno, (sptr+i)->name, (sptr+i)->m1,

(sptr+i)->m2, (sptr+i)->m3, (sptr+i)->total, (sptr+i)->avg); } free(sptr); } 11. C Program using dynamic memory allocation to develop salary details of 5 emp loyees main() { struct employee { int empno; char name[20]; int bsal; int da; in t hra; int gross; }; struct employee *eptr; int i,n; float a=10; printf("\n Ente r the no of employees:"); scanf("%d", &n); eptr = (struct employee *)malloc(size of(struct employee)*n); printf("\n"); for(i=0; i<n; i++) { printf("Enter student #%d employeeno,name,basicsalary: \n", i+1); scanf("%d%s%d", &(eptr+i)->empno, & (eptr+i)->name, &(eptr+i)->bsal); } printf("\n EMPLOYEE DETAILS \n"); printf("** ************************************************************\n"); printf("\nEmpn o\tName \t\t Basic\tDA \t HRA \t Gross \n"); printf("*************************** ************************************\n");

for(i=0; i<n;i++) { (eptr+i)->da=(eptr+i)->bsal/100*50; (eptr+i)->hra=(eptr+i)-> bsal/100*10; (eptr+i)->gross=(eptr+i)->bsal+(eptr+i)->hra+(eptr+i)->da; printf(" \n%d\t%s\t\t%d\t%d\t%d\t%d",(eptr+i)->empno, (eptr+i)->name, (eptr+i)->bsal, (ep tr+i)>da, (eptr+i)->hra, (eptr+i)->gross); } free(eptr); } 12. C Program using dynamic memory allocation to find minimum and maximum of a s et of numbers #include<math.h> #include<stdlib.h> main() { int *ptr; int i, j, n, temp ,max,mi n; printf("\nHow many integer numbers:"); scanf("%d", &n); printf("\nPlease ente r any %d integer numbers.\n", n); ptr = (int *)malloc(n*sizeof(int)); for(i=0; i <n;i++) scanf("%d", ptr+i); /*Assume first value as max*/ /*loop to find the big gest by comparing from second number onwards*/

max=*ptr; for(i=1; i<n;i++) if(*(ptr+i)>max) max=*(ptr+i); /*Assume first value as min*/ /*loop to find the biggest by comparing from second number onwards*/ mi n=*ptr; for(i=1; i<n;i++) if(*(ptr+i)<min) min=*(ptr+i); printf("\n The Maximum value is: %d",max); printf("\n The Minimum value is: %d",min); } 13. C Program to find sum of n numbers using function #include<stdio.h> int i; main() { int sum(int b[],int n); int sum_num,n,a[20]; printf("\nEnter the total no's:") ; scanf("%d",&n); printf("Enter numbers i:"); for(i=0;i<n;i++) scanf("%d",&a[i]) ; sum_num=sum(a,n); printf("The Sum of Numbers is = %d",sum_num); } int sum(int b[],int n) { int s=0;

for(i=0;i<n;i++) { s+=b[i]; } return s; } 14. C program using pointers to reallo cate memory #include<stdlib.h> #include<string.h> main() { char *p; p=(char *)ma lloc(11); strcpy(p,"University"); printf("Memory contains:%s\n",p); p=realloc(p, 23); strcpy(p,"University Examinations"); printf("Memory now contains:%s\n",p); } 15. C program using pointers to perform file copy operation #include <stdio.h> #include <stdlib.h> int main() { FILE *fp1,*fp2; int c; char fname1[40],fname2[ 40]; printf("\n\n\t\t To copy the contents of one file to another \n"); printf(" \n\n Enter the source file:"); gets(fname1); printf("\n\n Enter the target file: "); gets(fname2); fp1=fopen(fname1,"r"); fp2=fopen(fname2,"w");

if(fp1==NULL) { printf("\n\n cannot open %s for reading \n",fname1); exit(1); } else if(fp2== NULL) { printf("\n\n Cannot open %s for writing \n",fname2); exit( 1); } else { c=getc(fp1); while(c!=EOF) { putc(c,fp2); c=getc(fp1); } fclose(fp1 ); fclose(fp2); printf("\n\n File Sucessfully copied\n"); } }