Can anyone fix this code for me? An error accures when calculating the average s
ID: 641378 • Letter: C
Question
Can anyone fix this code for me? An error accures when calculating the average score. The problem is from Starting out with C++ Ch11 PC12.
#include <iostream>
using namespace std;
const int columns = 4;
struct StuDent
{
int id[6];
char names[6][20];
int scores[6][4];
double avg[6];
char grade[6];
}; //semi-colon required
//pass by reference
void calculateAverage(StuDent&);
char letterGrade(StuDent&);
int main()
{
const int rows = 6;
//declare and allocate
StuDent r;
//enter student id
cout<<"Student id: ";
for(int i=0; i<6 ;i++)
cin>>r.id[i];
//enter student+scores
for(int i=0; i<6 ;i++)
{
cout<<"Enter Name of student "<<i+1<<": ";
cin>>r.names[i];
cout<<endl;
}
for(int i=0; i<rows; i++)
{
cout<<"Enter "<<columns<<" scores for student "<<i+1<<": ";
for (int j=0; j<columns; j++)
{
cin>>r.scores[i][j];
}
cout<<endl;
}
//display
cout<<"Student id: "<<r.id<<endl;
cout<<"Scores: ";
for(int i=0; i<4 ; i++)
cout<<r.scores<<" ";
cout<<endl;
//compAvg
calculateAverage(r);
cout<<"Avg Score: "<<r.avg<<endl;
letterGrade(r);
cout<<"Letter Grade: "<<r.grade<<endl;
}//endmain
void avg(StuDent& r)
{
int sum=0;
for(int i=0; i<6 ; i++)
sum+=r.scores[columns][i];
r.avg=static_cast<double>(sum)/4;
//error: incompatible types in
//assignment of 'double' to 'double [6]'
//
}
char grade(StuDent& r)
{
int sum = 0;
char grade;
for(int j=0; j<6; j++)
{
sum += r.scores[columns][j];
}
double avg = sum/4;
cout<<"Avg: "<<avg;
if(avg >= 90)
grade = 'A';
if(avg >= 80 && avg < 90)
grade = 'B';
if(avg >= 70 && avg < 80)
grade = 'C';
if(avg >= 60 && avg < 70)
grade = 'D';
if(avg < 60)
grade = 'F';
return grade;
}
Explanation / Answer
use double r.avg=(double)((sum)/4);