Presentation is loading. Please wait.

Presentation is loading. Please wait.

Advance Use of Structures CHAPTER 5. C.10 1 Using Structures with Functions » A function can return only one value back. » Some of the processes may yield.

Similar presentations


Presentation on theme: "Advance Use of Structures CHAPTER 5. C.10 1 Using Structures with Functions » A function can return only one value back. » Some of the processes may yield."— Presentation transcript:

1 Advance Use of Structures CHAPTER 5

2 C.10 1 Using Structures with Functions » A function can return only one value back. » Some of the processes may yield more then one value as a result. » How do we return them back from the function? » You can declare a composite data type with structure, that is including the set of values of process result. » So, you may return these values as a single variable from your function.

3 C.10 2 Using Structures with Functions » You can pass a local variable of “Structure tag” into a function as an argument. » Also, you can return a value in “structure tag” data type from a function. » So the declaration (prototype) of a function can be: structure std_rec calculate_grade( structure std_rec ); » And, the definition of the same function can be: structure std_rec calculate_grade( structure std_rec student ) { … } » Finally we can use this function as: c213[i] = calculate_grade( c213[i] );

4 C.10 3 # include struct std_rec{ char stdno[7], char name[21]; int mt, final, quiz; float grd; }; struct std_rec calc_grd( struct std_rec student) { student.grd = 0.3* student.mt +0.5* student.final + 0.2* student.quiz; return student; } void main() { clrscr(); struct std_rec c213; printf("\n Student no :"); gets(c213.stdno); printf("\n Student name :"); gets(c213.stdno); printf("\n Student mt :"); scanf("%d",&c213.mt); printf("\n Student final:"); scanf("%d",&c213.final); printf("\n Student quiz :"); scanf("%d",&c213.quiz); c213 = calc_grd(c213); printf("\n Grade : %5.2f", c213.grd); getch(); } /* The end of main() */

5 C.10 4 Using Structures with Functions » Example 1: (A function to read the date) struct date read _ date( struct date argdate) { printf(“\n Enter the date (dd/mm/yyyy) :”); scanf(“%d/%d/%d”, &argdate.day, &argdate.month, &argdate.year); return argdate; } » Example 2:(A function to read the inform. of a student) int read_student(void) { struct student_rec temp; printf(“\n Enter the student no:”); gets(temp.stdno); printf(“\n Enter the name :”); gets(temp.name); printf(“\n Enter the Midterm :”); scanf(“%d”,&temp.mt); printf(“\n Enter the Final :”); scanf(“%d”,&temp.final); printf(“\n Enter the Quiz :”); scanf(“%d”,&temp.quiz); temp.grd = 0.3 * temp.mt + 0.5 * temp.final + 0.2 * temp.quiz; return temp; }

6 C.10 5 Using Structures with Functions » An array of structure can be an argument of a function as a simple array: void class_list( struct student_rec [ ], int); // Prototype of the function void class_list( struct student_rec temp[ ], int size) // Definition of the function { int cnt; printf(“\n The Class List \n”); printf(“\n Std-no Name Mt Fin Qui Grd”); printf(“\n ******************************************”); for (cnt = 0; cnt< size; cnt++) printf(“ %7s %21s %3d %3d %3d %5.2f”, temp.stdno, temp.name, temp.mt, temp.final, temp.quiz, temp.grd); printf(“\n ******************************************”); }

7 C.10 6 Using Structures with Functions » An array of structure also can be returned from a function as a simple array: struct student_rec read_students( void); // Prototype of the function struct student_rec read_students( void) // Definition of the function { int cnt; struct student_rec temp[35]; for (cnt = 0; cnt< 35; cnt++) { clrscr(); printf(“\n Student %d”, cnt+1); printf(“\n Enter the student no:”); gets(temp[cnt].stdno); printf(“\n Enter the name :”); gets(temp[cnt].name); printf(“\n Enter the Midterm :”); scanf(“%d”,&temp[cnt].mt); printf(“\n Enter the Final :”); scanf(“%d”,&temp[cnt].final); printf(“\n Enter the Quiz :”); scanf(“%d”,&temp[cnt].quiz); temp[cnt].grd = 0.3 * temp[cnt].mt + 0.5 * temp[cnt].final + 0.2 * temp[cnt].quiz; } return temp; // The first element address of the array is returning back }

8 C.10 7 Sorting an Array of Structure » Question: Write the required functions to sort the array of students with respect to their grades. (from biggest to smallest) struct student_rec { char stdno[7]; char name[21]; int mt, final, quiz; float avg; } c213[35]; // c213 is an array of structure student_rec with 35 elements

9 C.10 8 int findmax(void) { float max = -1.0; int cnt, pos; for (cnt=0; cnt<35; cnt++) { if (c213[cnt].grd > max) { max = c213[cnt].grd; pos = cnt; } return pos; } void sort_grd(void) { struct student_ rec temp[35]; int cnt, pos; for (cnt=0; cnt<35; cnt++) { pos = findmax(); temp[cnt]= c213[pos]; c213[pos].grd = -1000.0; // Blockade the position with a very small num. } for (cnt=0; cnt<35; cnt++) c213[cnt]= temp[cnt]; // Rewrite the sorted elements into c213 }

10 C.10 9 Sorting an Array of Structure » Question: Write the required functions to sort the array of students with respect to their names. » In order to sort an array of strings, first you have to have the following functions: » String Compare (strcmp), that is comparing two strings and returning a result value as : 0 : Two strings are same 1 : The first string is bigger (or coming after in an alphabetic order) -1 : The second string is bigger (or coming after in an alphabetic order) » String Copy (strcpy), copy a string from an array to another.

11 C.10 10 int strcomp( char str1[], char str2[]) { int cnt=0, big=0; for(;;) { if ( ((*(str1+cnt))!='\0') && ((*(str2+cnt))!='\0') && (cnt< 21)) { if ((*(str1+cnt)) > (*(str2+cnt))) { big = 1; break; } if ((*(str1+cnt)) < (*(str2+cnt))) { big = 2; break; } cnt ++; } else break; // a “\0” is found. } // To evaluate two strings like ‘cem’ and ‘cemal’ if ((big == 0) && (*(str1+cnt)!= '\0')) big = 1; if ((big == 0) && (*(str2+cnt)!= '\0')) big = 2; return big; }

12 C.10 11 void strcpy( char str1[], char str2[]) { int cnt=0; while (*(str1+cnt)!='\0') { *(str2+cnt) = *(str1+cnt); cnt++; } *(str2+cnt)='\0'; } » Now, we can start to write the program that is asked to sort the array of students on their name field. » We will write those functions: min_name sortname

13 C.10 12 int min_name(void) { int cnt, pos; char tstr[21]=“zzzzzzzzzzzzzzzzzzzz”; for (cnt=0; cnt<35; cnt++) { if (strcmp(c213[cnt].name, tstr)= = -1) // is it smallerthen tstr ? { strcpy(c213[cnt].name, tstr); pos=cnt; } return pos; } void sortname(void) { struct student_rec temp;int cnt, pos; char dummy[21] = “zzzzzzzzzzzzzzzzzzzz”; for (cnt=0; cnt<35; cnt++) { pos = min_name(); temp[cnt] = c213[pos]; strcpy(c213[pos].name, dummy); } for (cnt=0; cnt<35; cnt++) c213[cnt] = temp[cnt] ; }

14 C.10 13 Searching an Array of Structure » Question: Write a functions to search a student record within an array of structures. » The student number (string) has to be given as an argument to the function and it will return either : –1 : If student is not found n : the offset of the array element (if the student is found).

15 C.10 14 int find_name(char target[21]) { int cnt, pos= -1; for (cnt=0; cnt<35; cnt++) { if (strcmp(c213[cnt].stdno, target)==0) // are they equal ? { pos=cnt;// Get the position of the found student break;// Stop the search (break the for loop) } return pos;// Return the position of the student } Searching An Array of Structure

16 C.10 15 » The content of an array is kept in RAM (Primary Memory). » What you write in an arrays is not durable. » The content of an array is lost, when the program is stopped. » You have to save the content of the array into a file which uses the SECONDARY STORAGE. » On the next run, the content of the file can be re-load into the array to be processed. Files with Array of Structures

17 C.10 16 » Question : Write a function to save the records of students from the array c213 to the file “C213.TXT”. void std_save(void) { int cnt; FILE *fp; fp = fopen(“C213.TXT”,”w”); for (cnt=0; cnt<35; cnt++) { fprintf(fp,“%s\n”,c213[cnt].stdno); fprintf(fp,“%s\n”,c213[cnt].name); fprintf(fp,“%d\n”,c213[cnt].mt); fprintf(fp,“%d\n”,c213[cnt].final); fprintf(fp,“%d\n”,c213[cnt].quiz); fprintf(fp,“%f”,c213[cnt].grd); } fclose(fp); } Files with Array of Structures

18 C.10 17 » Question : Write a function to read the content of the file “C213.TXT” into the array of structure c213. void std_restore(void) { int cnt; FILE *fp; fp = fopen(“C213.TXT”,”r”); for (cnt=0; cnt<35; cnt++) { fgets(c213[cnt].stdno, 7, fp); fgets(c213[cnt].name, 21, fp); fscanf(fp, “%d”&,c213[cnt].mt); fscanf(fp, “%d”&,c213[cnt].final); fscanf(fp, “%d”&,c213[cnt].quiz); fscanf(fp, “%f”&,c213[cnt].grd); } fclose(fp); } Files with Array of Structures


Download ppt "Advance Use of Structures CHAPTER 5. C.10 1 Using Structures with Functions » A function can return only one value back. » Some of the processes may yield."

Similar presentations


Ads by Google