Help with Arrays?

The instructions were:

Create a program that will input 10 names of students, each student have 5 quizzes USING ARRAY
Sample output:
Enter name of student 1:
Enter quiz1:
Enter quiz2:
Enter quiz3:
Enter quiz4:
Enter quiz5:
The average is:


And my code is:

--------------------------------------------


#include "stdafx.h"
#include <conio.h>
#include <stdio.h>
#include <iostream>
#include <string>


using namespace std;


void main()
{

float gradeArray [5];
string studentName;
float studentAverage;

printf("Enter name of student 1: ");
scanf("%s", studentName);

printf("Enter quiz 1: ");
scanf(" %f", gradeArray);

printf("Enter quiz 2: ");
scanf(" %f", gradeArray);

printf("Enter quiz 3: ");
scanf(" %f", gradeArray);

printf("Enter quiz 4: ");
scanf(" %f", gradeArray);

printf("Enter quiz 5: ");
scanf(" %f", gradeArray);

studentAverage= ((gradeArray[0]+ gradeArray[1]+ gradeArray[2] + gradeArray[3] + gradeArray[4]) / (5));
printf("The student's average is: %f", studentAverage);



getch();

}
-------------------------------------------------------------


My program allows me to input the name and grades, but the average is SO FAR OFF, it even reaches a negative result when I put all of the grades into 10's and I don't understand why. :( If someone could help me I would greatly appreciate it. :)
You have a few issues the main thing is you are using c and c++ code and it seems better to pick one. Here is how I would write your program in c++ let me know if you have any questions.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <string>

using namespace std;

const int MAXSTUDENTS = 5;

int main()
{

    struct {
        float grade;
        string studentName;
    } student[MAXSTUDENTS];


    for (int i = 0; i < MAXSTUDENTS; i++) {
        cout << "please enter student " << i+1 << ". name: " ;
        cin >> student[i].studentName;
        cout << "please enter student " << i+1 << ". grade: " ;
        cin >> student[i].grade;
    }

    int averageGrade = 0;
    int totalStudentScore = 0;
    for (int i = 0; i < MAXSTUDENTS; i++) {
        totalStudentScore = totalStudentScore+ student[i].grade;

    }

    averageGrade = totalStudentScore/MAXSTUDENTS;

    cout << "Average student grade is: " << averageGrade;

    return 0;

}


EDIT
I realize my program works a bit differently but there should be enough information for you to fill in the blanks. Please feel free to ask any questions!
Last edited on
You need to initialize the studentaverage to zero.
So change float studentAverage=0;
@Bdanielz Thanks for your reply! Though ti worked differently it's easily "editable". But I was just wondering, would this be possible to do without the data structures? :)
You can do it with out the struct

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45

#include <iostream>
#include <string>

using namespace std;

const int MAXSTUDENTS = 5;
const int MAXTESTS = 5;

int main()
{

  
    float grade[MAXSTUDENTS][MAXTESTS];
    string studentName[MAXSTUDENTS];


    for (int i = 0; i < MAXSTUDENTS; i++) {
        cout << "please enter student " << i+1 << ". name: " ;
        cin >> studentName[i];
        for (int k = 0;  k < MAXTESTS; k++) {
            cout << "please enter student " << i+1 << ". test  "<< k+1 << " : " ;

         cin  >>  grade[i][k];
            
        }
    }

    int averageGrade = 0;
    int totalStudentScore = 0;
    for (int i = 0; i < MAXSTUDENTS; i++) {
        for (int k = 0; k < MAXTESTS; k++) {
            totalStudentScore = totalStudentScore + grade[i][k];
            
        }

    }

    averageGrade = totalStudentScore/MAXSTUDENTS;

    cout << "Average student grade is: " << averageGrade;

    return 0;

}


but i think using a struct makes the code more readable.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <string>

using namespace std;

const int MAXSTUDENTS = 5;
const int MAXTESTS = 5;

int main()
{
    
    struct {
        float grade[MAXTESTS];
        string studentName;
    } student[MAXSTUDENTS];
    
    
    for (int i = 0; i < MAXSTUDENTS; i++) {
        cout << "please enter student " << i+1 << ". name: " ;
        cin >> student[i].studentName;
      
        for (int k = 0; k < MAXTESTS; k++) {
            cout << "please enter student " << i+1 << ". grade to test " << k+1 ;
            cin >> student[i].grade[k];
        }
        
    }
    
    int averageGrade = 0;
    int totalStudentScore = 0;
    for (int i = 0; i < MAXSTUDENTS; i++) {

        for (int k = 0; k < MAXTESTS; k++) {
            totalStudentScore = totalStudentScore+ student[i].grade[k];

        }
        
    }
    
    averageGrade = totalStudentScore/MAXSTUDENTS;
    
    cout << "Average student grade is: " << averageGrade;
    
    return 0;
    
}

I just realized I did not calculate your averages correctly. I think you need to average each student then average all of those averages.
Topic archived. No new replies allowed.