You are here:

C++/Help with a C++ project

Advertisement


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.  PLease do not use any global variables.  Use the appropriate parameters to pass values in and out of functions.

Johnson 85 83 77 91 76
Aniston 80 90 95 93 48
Cooper 78 81 11 90 73
Gupta 92 83 30 69 87
Blair 23 45 96 38 59
Clark 60 85 45 39 67
Kennedy 77 31 52 74 83
Bronson 93 94 89 77 97
Sunny 79 85 28 93 82
Smith 85 72 49 75 63  

Answer
Dear Mckale!
  Many students requested the same program  , no option to make , i have to write the program for all  , so here is the program what you requested  , i have checked this program with lot many data's and its workin fine  , due to my tight schedule i have no time to write the documentation for this program  if  you want ask a follow up later !


/* 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;
}
}



Regards !
Prince M. Premnath.

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Prince M. Premnath

Expertise

Years of research in C/C++ Will bring you all facts regarding , OOPS , SVGA , MOUSE , BIOS , IVT , Feel free to query me :-)

Experience

More than 5 years

Education/Credentials
MCA

©2016 About.com. All rights reserved.