Você está na página 1de 6

/* WAP to illustrate the use of pointers in arithmetic operations.*/ #include<stdio.h> #include<conio.

h> void main() { char *p="RAVIKUMAR"; clrscr(); printf("Before pointer arithmetic operation %s\n",p); p++; printf("After pointer arithmetic operation %s\n",p); getch(); }

/* OUTPUT Before pointer arithmetic operation RAVIKUMAR After pointer arithmetic operation AVIKUMAR */

/* WAP to illustrate different parameter passing mechanisms: call by value and call by reference.*/ #include<stdio.h> #include<conio.h> void main() { int a,b; clrscr(); printf("Enter the value of a and b\n"); scanf("%d%d",&a,&b); printf("Before call by value\n"); printf("a=%d\tb=%d\n",a,b); call_value(a,b); printf("After call by value \n"); printf("a=%d\tb=%d\n",a,b); call_reference(&a,&b); printf("After call by reference\n"); printf("a=%d\tb=%d\n",a,b); getch(); } call_value(int x,int y) { x=300; y=400; } call_reference(int*x,int*y) { *x=1000; *y=2000; } /* OUTPUT Enter the value of a and b 10 20 Before call by value a=10 b=20 After call by value a=10 b=20 After call by reference a=1000 b=2000 */

/*To find the smallest element in an array of 10 elements using pointers*/ #include<stdio.h> #include<conio.h> void main() { int a[10],i,*small; clrscr(); printf("Enter the 10 elements of an array\n"); for(i=0;i<10;i++) scanf("%d",&a[i]); small=a; for(i=0;i<10;i++) { if(a[i]<*small) small=&a[i]; } printf("The smallest element in an array = %d\n",*small); getch(); }

/* OUTPUT Enter the 10 elements of an array 74 85 42 52 61 32 12 82 92 53 The smallest element in an array = 12 */

/* To count the number of characters in a given file.*/ #include<stdio.h> #include<conio.h> #include<stdlib.h> void main() { FILE *fp; char ch; int count=0; clrscr(); fp=fopen("SAMPLE.DAT","r"); if(fp==NULL) { printf("Error in opening file\n"); exit(0); } ch=fgetc(fp); while(ch!=EOF) { putchar(ch); count++; ch=fgetc(fp); } printf("\nThe number of characters in a given file =%d",count); getch(); }

/*OUTPUT Data Structures Lab The number of characters in a given file =19 */

/*To sort using bubble sort.*/ #include<stdio.h> #include<conio.h> void main() { int i,j,n,a[10],temp; clrscr(); printf("Ente the size of the array\n"); scanf("%d",&n); printf("Enter the array elemets\n"); for(i=0;i<n;i++) scanf("%d",&a[i]); for(i=0;i<n;i++) for(j=0;j<n-1;j++) { if(a[j]>a[j+1]) { temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } } { printf("The sorted list are\n"); for(i=0;i<n;i++) printf("%d\n",a[i]); } getch(); }

/* OUTPUT Ente the size of the array 5 Enter the array elemets 78 45 34 23 12 The sorted list are 12 23 34 45 78 */

Você também pode gostar