GE 2115 Set I: 1.C Program Using Dynamic Memory Allocation To Sort N Numbers in Ascending
GE 2115 Set I: 1.C Program Using Dynamic Memory Allocation To Sort N Numbers in Ascending
SET I
1.C Program using dynamic memory allocation to sort n numbers in Ascending
main()
{
int *ptr;
int i, j, n, temp;
main()
{
int *ptr;
int i, j, n, temp;
#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 Enter 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();
}
#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 Enter 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 Descending order");
for(i=0;i<n;i++)
printf("\n%s",snames[i]);
getch();
}
#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));
} clrscr();
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 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("\nSubstraction 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 transpose matrices
#include<stdio.h>
#include<conio.h>
void main()
{
int *a[10],m,i,j,temp;
for(i=0;i<10;i++)
{
a[i]=(int *)calloc(10,sizeof(int));
}
printf("\n Enter the Size of the matrix ");
scanf("%d",&m);
#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);
#include<stdio.h>
#include<conio.h>
void main()
{
int *a[10],m,i,j,n,s=0;
for(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);
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;
11. C Program using dynamic memory allocation to develop salary details of 5 employees
main()
{
struct employee
{
int empno;
char name[20];
int bsal;
int da;
int hra;
int gross;
};
struct employee *eptr;
int i,n;
float a=10;
12. C Program using dynamic memory allocation to find minimum and maximum of a set of
numbers
#include<math.h>
#include<stdlib.h>
main()
{
int *ptr;
int i, j, n, temp ,max,min;
printf("\nHow many integer numbers:");
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);
}
int sum(int b[],int n)
{
int s=0;
for(i=0;i<n;i++)
{
s+=b[i];
}
return s;
}
#include<stdlib.h>
#include<string.h>
main()
{
char *p;
p=(char *)malloc(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");
}
}