Sei sulla pagina 1di 23

//Exp no. : 8.

a //Date :

C Programming Pointer and Functions

Name : Roll no. : 99

1. Write a Program using pointers and dynamic memory allocation to perform the copy of one array into another array in reverse order. #include<stdio.h> #include<stdlib.h> #include<conio.h> void fun(int *a, int *b, int n) { int i, j; for (i=0, j=n-1; i<n; i++, j--) { *(b+j)=*(a+i); } } int main() { int *a, *b; char ch; int n, i; do { printf("\n Enter the number of elements : "); scanf("%d", &n); a=(int *) malloc(sizeof(int)*n); b=(int *) malloc(sizeof(int)*n); printf("\n Enter the elements :\n"); for (i=0; i<n; i++) scanf("%d", &(*(a+i))); fun(a, b, n); printf("\n The elements are :\n"); for (i=0; i<n; i++) printf("%d ", *(b+i)); printf("\n Enter 'y' to continue : "); scanf("%s", &ch); }while(ch=='y'); getch(); } Output : Enter the number of elements : 4 Enter the elements : 1 2 3 4 The elements are : 4321 Enter 'y' to continue : n

//Exp no. : 8.b //Date :

C Programming Pointer and Functions

Name : Roll no. : 99

2. Write a Program using pointers and dynamic memory allocation to perform matrix addition. #include<stdio.h> #include<stdlib.h> #include<conio.h> void add(int **a, int **b, int **c, int m, int n) { int i, j, k; k=m*n; for (i=0; i<m; i++) { for (j=0; j<n; j++) *(*(c+i)+j)=*(*(b+i)+j)+*(*(a+i)+j); } } int main() { int i, j, m, n, **a, **b, **c; char ch; do { printf("\n Enter the number of rows and columns : "); scanf("%d %d", &m, &n); a=(int **) malloc(sizeof(int) *m); for (i=0; i<m; i++) a[i]=(int *) malloc(sizeof(int)*n); b=(int **) malloc(sizeof(int)*m); for (i=0; i<m; i++) b[i]=(int *) malloc(sizeof(int)*n); c=(int **) malloc(sizeof(int)*m); for (i=0; i<m; i++) c[i]=(int *) malloc(sizeof(int)*n); printf("\n Enter the elements of matrix A :\n"); for (i=0; i<m; i++) for (j=0; j<n; j++) scanf("%d", &(*(*(a+i)+j))); printf("\n Enter the elements of matrix B:\n"); for (i=0; i<m; i++) for (j=0; j<n; j++) scanf("%d", &(*(*(b+i)+j))); add(a, b, c, m, n);

printf("\n The added matrix is :\n"); for (i=0; i<m; i++) { for (j=0; j<n; j++) printf("%4d ", *(*(c+i)+j)); printf("\n"); } printf("\n Enter 'y' to continue : "); scanf("%s", &ch); }while(ch=='y'); getch(); } Ouput : Enter the number of rows and columns : 2 3 Enter the elements of matrix A : 1 2 3 4 5 6 Enter the elements of matrix B: 1 2 3 4 5 6 The added matrix is : 2 4 6 8 10 12 Enter 'y' to continue : n

//Exp no. : 8.c //Date :

C Programming Pointer and Functions

Name : Roll no. : 99

3. Write a Program using pointers and functions to perform the following operations in the given string without using inbuilt functions. a) Find length b) Copy to another string c) Reverse the string d) Word Count #include<stdio.h> #include<process.h> #include<stdlib.h> #include<string.h> #include<conio.h> int length(char *a) { int i, l; i=0, l=0; while (*(a+i)!='\0') { i++; l++; } return l; } int copy(char *a, char *b) { int i; for (i=0; *(a+i)!='\0'; i++) { *(b+i)=*(a+i); } *(b+i)='\0'; } int wcount(char *a) { int c=0, i, w=0, k; for (i=0; *(a+i)!='\0'; i++) { if ((*(a+i)==' ') &&(*(a+i+1)==' ')) {

for (k=i+1; *(a+k)!='\0'; k++) *(a+k)=*(a+k+1); *(a+k)='\0'; i--; } } //for (i=0; *(a+i)!='\0'; i++); //*(a+i)=' '; for (i=0; *(a+i)!='\0'; i++) { if (*(a+i)==' ') c++; } return (c+1); } void reverse(char *a, char *b) { int i, j; j=length(a)-1; for (i=0; *(a+i)!='\0'; i++, j--) { *(b+j)=*(a+i); } *(b+i)='\0'; } int main() { char *a, *b; int ch, l, i, flag=0; b=NULL; a =(char *) malloc(sizeof(char)*30); b =(char *) malloc(sizeof(char)*30); printf("\n Enter the string : "); gets(&(*a)); do { printf(" \n \n 1. Length \n 2. Copy \n 3. Wordcount\n 4. Reverse\n 5. Enter new string\n 6. Exit\n Enter your choice :"); scanf("%d", &ch); switch(ch) { case 1 : { l=length(a); printf("\n The length of the string is : %d", l); break;

} case 2 : { copy(a, b); printf("\n The copied string is : "); for (i=0; i<length(b); i++) printf("%c", *(b+i)); break; } case 3 : { l=wcount(a); printf("\n The no. of words in the string is : %d", l); break; } case 4 : { reverse(a, b); printf("\n The reversed string is : "); for (i=0; *(b+i)!='\0'; i++) printf("%c", *(b+i)); break; } case 5 : { a =(char *) malloc(sizeof(char)*30); printf("\n Enter the string : "); gets(&(*a)); gets(&(*a)); break; } case 6 : { printf("\n Program is terminating! \n"); getch(); exit(0); } default : { printf("\n Invalid choice! \n"); } } }while(1); getch(); } Ouput : Enter the string : hey hello 1. Length 2. Copy 3. Wordcount

4. Reverse 5. Enter new string 6. Exit Enter your choice :1 The length of the string is : 9 1. Length 2. Copy 3. Wordcount 4. Reverse 5. Enter new string 6. Exit Enter your choice :2 The copied string is : hey hello 1. Length 2. Copy 3. Wordcount 4. Reverse 5. Enter new string 6. Exit Enter your choice :3 The no. of words in the string is : 2 1. Length 2. Copy 3. Wordcount 4. Reverse 5. Enter new string 6. Exit Enter your choice :4 The reversed string is : olleh yeh 1. Length 2. Copy 3. Wordcount 4. Reverse 5. Enter new string 6. Exit Enter your choice :5 Enter the string : hey hi

1. Length 2. Copy 3. Wordcount

4. Reverse 5. Enter new string 6. Exit Enter your choice :6 Program is terminating! //Exp no. : 8.d //Date : C Programming Pointer and Functions Name : Roll no. : 99

4. Write a Program to discuss about the size of various data types and their pointers. #include<stdio.h> #include<conio.h> int main() { char c, *cp; int i, *ip; short s, *sp; long l, *lp; double d, *dp; signed int si, *sip; unsigned int ui, *uip; float f, *fp; printf(" Char : %d \t Char pointer : %d ", sizeof(c), sizeof(*cp)); printf("\n Int : %d \t Int pointer : %d ", sizeof(i), sizeof(*ip)); printf("\n Short : %d \t Short pointer : %d ", sizeof(s), sizeof(*sp)); printf("\n Long : %d \t Long pointer : %d ", sizeof(l), sizeof(*lp)); printf("\n Double : %d \t Double pointer : %d ", sizeof(d), sizeof(*dp)); printf("\n Signed int : %d Signed int pointer : %d ", sizeof(si), sizeof(*sip)); printf("\n Unsigned : %d \t Unsigned pointer : %d ", sizeof(ui), sizeof(* uip)); printf("\n Float : %d \t Float pointer : %d ", sizeof(f), sizeof(*fp));\ getch(); } Ouput: Char : 1 Int : 4 Short : 2 Long : 4 Double : 8 Signed int : 4 Unsigned : 4 Float : 4

Char pointer : 1 Int pointer : 4 Short pointer : 2 Long pointer : 4 Double pointer : 8 Signed int pointer : 4 Unsigned pointer : 4 Float pointer : 4

//Exp no. : 8.e //Date :

C Programming Pointer and Functions

Name : Roll no. : 99

5. Write a Program using functions and pointers to do the following. a) Area & Perimeter of circle b) Area & volume of a cube #include<stdio.h> #include<conio.h> #include<stdlib.h> void circle(float *a, float *area, float *peri) { float x=2*3.14*(*a); float y=(3.14*(*a)*(*a)); *area=x; *peri=y; } float cube(float *b, float *area, float *vol) { float x=(6*(*b)*(*b)); float y=((*b)*(*b)*(*b)); *area=x; *vol=y; } int main() { float *r, *area, *peri, *l, *vol; r=(float*) malloc(sizeof(float)); l=(float*) malloc(sizeof(float)); area=(float*) malloc(sizeof(float)); peri=(float*) malloc(sizeof(float)); vol=(float*) malloc(sizeof(float)); char ch; do { printf("\n Enter the radius of the circle : "); scanf("%f", &(*r)); circle(r, area, peri);

printf(" Perimeter = %f Area = %f ", *peri, *area); printf("\n Enter the side of the cube : "); scanf("%f", &(*l)); cube(l, area, vol); printf(" Volume = %f Area = %f ", *vol, *area); printf("\n Enter 'y' to continue : "); scanf("%s", &ch); }while(ch=='y'); getch(); } Ouput : Enter the radius of the circle : 2 Perimeter = 12.560000 Area = 12.560000 Enter the side of the cube : 3 Volume = 27.000000 Area = 54.000000 Enter 'y' to continue : n

//Exp no. : 9.a //Date :

C Programming Pointer and Structures

Name : Roll no. : 99

1. Write a Program to define a structure named train, which contains train name, train number, source, destination, no.of seats allotted, total seats, time of departure. Write program to get n number of trains information and do the following. a) Search by train name b) Allot n tickets in a train using train no. c) Display all the trains to a particular destination from a particular source. #include<stdio.h> #include<conio.h> #include<stdlib.h> #include<process.h> struct train { char name[30]; int num; char source[30]; char dest[30]; int nos; int ts; char tod[6]; }; void scan(struct train *s) { printf("\n Train Name : "); scanf("%s", &(*s->name)); printf("\n Train Number : "); scanf("%d", &(s->num)); printf("\n Source : "); scanf("%s", &(*s->source)); printf("\n Destination : "); scanf("%s", &(*s->dest)); printf("\n No. of seats alloted : "); scanf("%d", &(s->nos)); printf("\n Total no. of seats : "); scanf("%d", &(s->ts)); printf("\n Time of departure : ");

scanf("%s", &(*s->tod)); } void print(struct train *s) { printf("\n -------------------------------- "); printf("\n Train Name : "); printf("%s", s->name); printf("\n Train Number : "); printf("%d", s->num); printf("\n Source : "); printf("%s", s->source); printf("\n Destination : "); printf("%s", s->dest); printf("\n No. of seats alloted : "); printf("%d", s->nos); printf("\n Total no. of seats : "); printf("%d", s->ts); printf("\n Time of departure : "); printf("%s", s->tod); printf("\n -------------------------------- \n"); } int cmp(char *a, char *b) { int i, flag=0, l1, l2, l; for (i=0; *(a+i)!='\0'; i++); l1=i; for (i=0; *(b+i)!='\0'; i++); l2=i; if (l1>l2) l=l1; else l=l2; for (i=0; i<l ; i++) { if (*(a+i)!=*(b+i)) { flag=1; return 1; } } if (flag==0) return 0; } int search(char *se, struct train *s ) {

if (cmp(s->name, se)==0) return 1; else return 0; } int allot( struct train *s, int *se, int al) { if (s->num==*se) { if (al > (s->ts - s->nos)) { return 2; } else { s->nos +=al; return 1; } } else return 0; } int display(struct train *s, char *s1, char *s2) { if ((cmp(s->source, s1)==0) && (cmp(s->dest, s2)==0)) return 1; else return 0; } int main() { struct train *s; int i, n, c, t; printf("\n Enter the number of trains : "); scanf("%d", &n); s=(struct train *) malloc(sizeof(struct train)*n); printf("\n Enter the details of trains : \n"); for (i=0; i<n; i++) { scan(s+i); } do { printf("\n ***********YOU CAN****************");

printf("\n 1. Search by train name \n"); printf(" 2. Allot 'n' tickets \n"); printf(" 3. Display trains fram particular source and dest \n"); printf(" Press any number to exit \n"); printf("\n Your choice : "); scanf("%d", &c); switch(c) { case 1 : { char *se; se=(char *) malloc(sizeof(char) *30); int flag=0; printf("\n Enter the train name to be searched : "); scanf("%s", &(*se)); for (i=0; i<n; i++) { t=search(se, s+i); if (t==1) { flag=1; print(s+i); } } if (flag==0) printf("\n No such train found !\n"); break; } case 2 : { int *se; int flag=0, d; se=(int *) malloc(sizeof(int)); printf("\n Enter the train number to be alloted : "); scanf("%d", &(*se)); printf("\n Enter the number of seats to be alloted : "); scanf("%d", &d); for (i=0; i<n; i++) { t=allot(s+i, se, d); if (t==1) { flag=1; print(s+i); printf(" %d seats are allotted \n", d); } else if (t==2) { flag=1;

print(s+i); printf("\n There are no sufficeint seats. Only %d seats are available \n", (s+i)->ts(s+i)->nos); } } if (flag==0) printf("\n No such train found !\n"); break; } case 3 : { char *s1, *s2; int flag=0; s1=(char *) malloc(sizeof(char) *30); s2=(char *) malloc(sizeof(char) *30); printf("\n Enter the Source of the train : "); scanf("%s", &(*s1)); printf("\n Enter the Destination of the train : "); scanf("%s", &(*s2)); for (i=0; i<n; i++) { t=display(s+i, s1, s2); if (t==1) { flag=1; print(s+i); } } if (flag==0) printf("\n No such train found !\n"); break; } default : { exit(0); } } }while(1); getch(); } Ouput : Enter the number of trains : 3 Enter the details of trains : Train Name : shathabdhi Train Number : 4177 Source : chennai

Destination : vijayawada No. of seats alloted : 423 Total no. of seats : 500 Time of departure : 12.30 Train Name : pinakini Train Number : 2177 Source : chennai Destination : hyderabad No. of seats alloted : 233 Total no. of seats : 400 Time of departure : 6.30 Train Name : charminar Train Number : 2434 Source : tirupathi Destination : chennai No. of seats alloted : 320 Total no. of seats : 340 Time of departure : 14.50 ***********YOU CAN**************** 1. Search by train name 2. Allot 'n' tickets 3. Display trains fram particular source and dest Press any number to exit Your choice : 1 Enter the train name to be searched : charminar -------------------------------Train Name : charminar Train Number : 2434 Source : tirupathi Destination : chennai No. of seats alloted : 320 Total no. of seats : 340 Time of departure : 14.50 -------------------------------***********YOU CAN**************** 1. Search by train name 2. Allot 'n' tickets 3. Display trains fram particular source and dest Press any number to exit Your choice : 2 Enter the train number to be alloted : 2177 Enter the number of seats to be alloted : 30 --------------------------------

Train Name : pinakini Train Number : 2177 Source : chennai Destination : hyderabad No. of seats alloted : 263 Total no. of seats : 400 Time of departure : 6.30 -------------------------------30 seats are allotted ***********YOU CAN**************** 1. Search by train name 2. Allot 'n' tickets 3. Display trains fram particular source and dest Press any number to exit Your choice : 3 Enter the Source of the train : tirupathi Enter the Destination of the train : chennai -------------------------------Train Name : charminar Train Number : 2434 Source : tirupathi Destination : chennai No. of seats alloted : 320 Total no. of seats : 340 Time of departure : 14.50 -------------------------------***********YOU CAN**************** 1. Search by train name 2. Allot 'n' tickets 3. Display trains fram particular source and dest Press any number to exit Your choice : 1 Enter the train name to be searched : golconda No such train found ! ***********YOU CAN**************** 1. Search by train name 2. Allot 'n' tickets 3. Display trains fram particular source and dest Press any number to exit Your choice : 2 Enter the train number to be alloted : 4177 Enter the number of seats to be alloted : 90 -------------------------------Train Name : shathabdhi

Train Number : 4177 Source : chennai Destination : vijayawada No. of seats alloted : 423 Total no. of seats : 500 Time of departure : 12.30 -------------------------------There are no sufficeint seats. Only 77 seats are available ***********YOU CAN**************** 1. Search by train name 2. Allot 'n' tickets 3. Display trains fram particular source and dest Press any number to exit Your choice : 7 //Exp no. : 9.b //Date : C Programming Pointer and Structures Name : Roll no. : 99

2. Write a Program to define a structure named student with needed details. Write program to get n number of students information and display them in rank order. Use structure pointers. #include<stdio.h> #include<conio.h> #include<stdlib.h> struct student { int rno; char name[30]; int sem; int m[5]; int total; int rank; }; void max marks(struct student *s ,int n ) { int void total(struct student *s, int n) (s+i)->total=(s+i)->m[0]+(s+i)->m[1]+(s+i)->m[2]+(s+i)->m[3]+(s+i)->m[4]; } void failures(struct student *s, int n) { for (int i=0; i<n; i++) { if (((s+i)->m[0]<40) ||((s+i)->m[1]<40) || ((s+i)->m[2]<40) || ((s+i)->m[3]<40) ||((s+i)>m[4]<40))

(s+i)->rank=-1; } } void sort( struct student *s, int n) { int i, j; student *t; t=(struct student *) malloc(sizeof(struct student)); for (i=0; i<n; i++) { for (j=0; j<n-i-1; j++) { if ((s+j)->total < (s+j+1)->total) { *t=*(s+j); *(s+j)=*(s+j+1); *(s+j+1)=*t; } } } } void ranksort( struct student *s, int n, int r) { int i, j; student *t; t=(struct student *) malloc(sizeof(struct student)); for (i=0; i<n; i++) if ((s+i)->rank==-1) (s+i)->rank=r; for (i=0; i<n; i++) { for (j=0; j<n-i-1; j++) { if ((s+j)->rank > (s+j+1)->rank) { *t=*(s+j); *(s+j)=*(s+j+1); *(s+j+1)=*t; } } } } int rank(struct student *s, int n) {

int r=1, i, j; for (i=0; i<n; i++) { if ((s+i)->rank==-1) { r--; } else (s+i)->rank=r; for (j=i+1; j<n; j++) { if ((s+i)->total==(s+j)->total) { if ((s+i)->rank!=-1) (s+j)->rank=r; i++; } } r++; } return (r+1); } void print(struct student *s, int r) { printf("\n Student Name : "); printf("%s", s->name); printf("\n Roll no : "); printf("%d", s->rno); for (int i=0; i<5; i++) { printf("\n Mark %d : ", i+1); printf("%d", s->m[i]); } printf("\n Total : "); printf("%d", s->total); printf("\n Rank : "); if (s->rank==r) printf(" F "); else printf("%d", s->rank); printf("\n -------------------------------- \n"); } void scan(struct student *s) {

printf("\n Student Name : "); scanf("%s", &(*s->name)); printf("\n Roll no : "); scanf("%d", &(s->rno)); printf("\n Semester : "); scanf("%d", &(s->sem)); for (int i=0; i<5; i++) { printf("\n Mark %d : ", i+1); scanf("%d", &(s->m[i])); } } int main() { struct student *s; int n, i, r; printf("\n Enter the number of students : "); scanf("%d", &n); s=(struct student *) malloc(sizeof(struct student)*n); printf("\n Enter the student details : \n"); for (i=0; i<n; i++) scan(s+i); total(s, n); sort(s, n); failures(s, n); r=rank(s, n); ranksort(s, n, r); printf("\n The details of students in rank order : \n"); printf("\n -------------------------------- "); for (i=0; i<n; i++) print(s+i, r); getch(); } Ouput : Enter the number of students : 3 Enter the student details : Student Name : abc Roll no : 1 Semester : 2 Mark 1 : 45 Mark 2 : 23 Mark 3 : 67 Mark 4 : 78 Mark 5 : 90

Student Name : def Roll no : 2 Semester : 2 Mark 1 : 67 Mark 2 : 89 Mark 3 : 90 Mark 4 : 99 Mark 5 : 89 Student Name : ghi Roll no : 3 Semester : 2 Mark 1 : 56 Mark 2 : 65 Mark 3 : 67 Mark 4 : 78 Mark 5 : 77 The details of students in rank order : -------------------------------Student Name : def Roll no : 2 Mark 1 : 67 Mark 2 : 89 Mark 3 : 90 Mark 4 : 99 Mark 5 : 89 Total : 434 Rank : 1 -------------------------------Student Name : ghi Roll no : 3 Mark 1 : 56 Mark 2 : 65 Mark 3 : 67 Mark 4 : 78 Mark 5 : 77 Total : 343 Rank : 2 -------------------------------Student Name : abc Roll no : 1 Mark 1 : 45 Mark 2 : 23 Mark 3 : 67 Mark 4 : 78

Mark 5 : 90 Total : 303 Rank : F --------------------------------

Potrebbero piacerti anche