C++/c++ syntax question
Expert: Prince M. Premnath - 10/30/2006
QuestionDear Mr. Premnath,
i need help with the same question which another user had posted ealier about calculating average using voio funtion, and using a value-returning function to calculateGrade, your answer was somewhat helpful but as new users i think itll help us alot if we can get the syntax for that program.
Thanks
Owais
AnswerDear Owais !
Here is the question !
I need to write a program that reads a student’s name together with his or her test scores. The program should then compute the average test score for each student and assign the appropriate grade. The grade scale is as follows: 90-100, A; 80-89, B; 70-79, C; 60-69, D; 0-59, F. The program needs use the following functions:
a. A void function, calculateAverage, to determine the average of the five test scores for each student. Use a loop to read and sum the five test scores. (This function does not output the average test score. The task must be done in the function main.)
b. A value-returning function, calculateGrade, to determine and return each student’s grade. (This function does not output the grade. The task must be done in the function main.)
The program needs to use the following data. Read the data from a file and send the output to a file
/* Since im busy with my examinations there is a delay in responding you */
/* Any how i just managed my time , I think this program will meet your
requirement , Make sure that i have just implemented all the cases found
in the questions posted previously by the other user ,
for your reference i just attached the question along with this program*/
/* Here is the program what you have requested */
#include<stdio.h>
#include<conio.h>
struct stud
{
char name[20];
int m[5];
float avg;
float total;
char grade;
};
struct stud s[100];
int reclen = sizeof(struct stud);
void getinfo(struct stud s[] , int n);
char calcgrade( struct stud x);
FILE* fp1 , *fp2;
void main()
{
int n , i , j ;
struct stud p;
clrscr();
fp1 = fopen("STUDI.TXT" , "w");
if(!fp1)
{
printf("Error : Unable to open input file");
return;
}
printf("Enter how many students :");
scanf("%d" , &n);
printf("Enter the students detail :\n");
getinfo( s , n);
fseek(fp1 , 0 , SEEK_END); /* Seek to end of file */
for( i = 0 ; i < n ; i++){
fwrite(&s[i] , reclen , 1 , fp1);
}
fclose(fp1);
fp1 = fopen("STUDI.TXT" , "r");
fp2 = fopen("STUDO.TXT" , "w");
rewind(fp1);
for( i = 0 ; i < n ; i ++)
{
fread ( &s[i] , reclen , 1 , fp1);
s[i].grade = calcgrade(s[i]);
fwrite( &s[i] , reclen , 1 , fp2);
}
fclose(fp1);
fclose(fp2);
fp1 = fopen("STUDO.TXT" , "r");
rewind(fp1);
for( i = 0 ; i < n ; i++)
{
fread(&p , reclen , 1 , fp1);
printf("%s", p.name);
for( j = 0 ; j < 5 ; j++)
printf(" %d " , p.m[j]);
printf(" %c " , p.grade);
printf(" %f \n" ,p.avg);
}
getch();
}
char calcgrade(struct stud x)
{
float aver;
aver = x.avg;
if( aver >= 90.0 && aver <= 100.0)
return 'A';
else if( aver >= 80.0 && aver <= 89.0)
return 'B';
else if( aver >= 70.0 && aver <= 79.0)
return 'C';
else if( aver >= 60.0 && aver <= 69.0)
return 'D';
else if( aver >= 0.0 && aver <= 59.0)
return 'F';
}
void getinfo( struct stud b[100] , int n)
{
int i , j;
float tot ,avg;
tot = avg = 0;
for( i = 0 ; i < n ; i++)
{
printf("Enter the name of the student :");
scanf("%s" , b[i].name);
printf("Enter 5 subject marks :");
for( j = 0 ; j < 5 ; j++)
scanf("%d" , &b[i].m[j]);
tot = 0;
for( j = 0 ; j < 5 ; j++)
tot += b[i].m[j];
b[i].total = tot;
b[i].avg = (float)tot/ (float)n;
}
}
NOTE :
I have checked this program for umpteen times , its workin fine !
Unfortunately im unable to give you the documentation for this code , if you want ask me in private don't hesitate !
Regards !
Prince M. Premnath