LAB SHEET 11

NAME-Yogesh Parth SC11B128 Q-1 FILE I\O PROGRAM CODE: #include<conio.h> #include<stdio.h> void sort(int a[20]); void swap(int *p1, int *p2); int main() { FILE *f1,*f2; f1=fopen("data.txt","r"); int a[20],i; for(i=0;i<20;i+=2) { fscanf(f1,"%d%d",&a[i],&a[i+1]); } fclose(f1); sort(a); f2=fopen("new.txt","w"); for(i=0;i<20;i++) { fprintf(f2,"%d\n",a[i]); } fclose(f2); getch(); } void swap(int *p1, int *p2) { int c; c=*p1; if(c<*p2) { *p1=*p2; *p2=c; } } void sort(int a[20]) { int j=0,i; while(j<15) { for(i=15-j;i>0;i--) { swap(a+i,a+i-1); } j++; } } OUTPUT: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 AVIONICS C PROGRAMMING LAB

fp=fopen("alpha.h> #include<stdio. fp).i<=26. clrscr()."w"). FILE *fp."r"). f2=fopen(a[2].h> int main(int x.txt".15 16 17 18 19 20 Discussion: A FILE NAMED “RESULT” GETS CREATED IN WHICH INTEGERS ARE SORTED AS ABOVE. char *a[]) { FILE *f1."r").c.h> #include<ctype. } fclose(fp).txt". for(i=1.h> int main(){ long i=0. char c=fgetc(f1).txt”. } OUTPUT: Enter a number between 0 and 25 10 output: J j Discussion: Numerals are getting converted into alphabets and saved in file “alpha. return 0.i++){ fputc(i+64. printf("Output: %c %c\n". char d=tolower(c). getch(). Q-3 Command line Argument #include<conio."w").h> #include<stdio. f1=fopen(a[1].d). Q-2 RANDOM ACCESS TO FILE #include<conio.*f2. f1=fopen("alpha.i.&i). printf("Enter a number between 0 and 25:\n"). .0). FILE *f1. scanf("%ld". fclose(f1). fseek(f1.

getch().cn).rn[8]. printf("Enter roll number:\n").txt").f2).while(!(feof(f1))) { fputc(fgetc(f1). } OUTPUT: ENTER FIRST NAME: yogesh Enter last name: parth Enter the roll number sc11b128 Enter contact number 0123456789 ”OUTPUT” FILE GETS CREATED Yogesh parth Sc11b128 . printf("Enter last name:\n").rn. } fclose(f1)."%s %s\n%s\n%s\n". gets(ln). #include<conio. return 0. fclose(f2). } DISCUSSION: The contents of file1 are copied in file2 using command line arguments. return 0. gets(rn). char fn[20]. FILE *f1.h> int main(){ printf("Enter first name:\n"). fprintf(f1.fn.ln. printf("Enter contact number:\n"). gets(cn).h> #include<stdio. f1=fopen("output. char cn[10]. Q-4 MISC. ln[20]. fclose(f1). gets(fn).

i++) { c[i]=a[i]+b[i].h> #include<alloc. } OUTPUT: enter the size of array 5 enter array elements 25678 enter the elements of b 14567 the sum is 3 9 11 13 15 Discussion: Dynamic memories are stored in HEAP memory.c[i]).*b. printf("%d\n". for(i=0.i++) { scanf("%d". for(i=0. for(i=0.i++) { scanf("%d".i<n.h> void main() { int *a.i.0123456789 Q-5DYNAMIC MEMORY ALLOCATION #include<stdio. printf("enter the elements of b"). printf("enter the size of array").i<n. scanf("%d". } c=(int *)malloc(n*2).&b[i]).&a[i]). } getch().&n).n.*c.h> #include<conio. } b=(int *)malloc(n*2). a=(int *)malloc(n*2). .i<n. clrscr(). printf("enter the array elements").

Sign up to vote on this title
UsefulNot useful