Create a 2D Array to Create a Gradebook

My assignment is to create a 2D array to create a grade book so that each student's name can be displayed next to all of the assignments.
The text file I am reading into my program to create the grade book is as follows:
Student1 98 71 73 71 82 88 77
Student2 87 83 98 76 76 80 75
Student3 82 92 85 80 88 79 87
Student4 84 95 89 70 86 90 99
Student5 96 90 87 84 100 72 73
Student6 86 75 98 71 75 89 72
Student7 70 84 85 80 90 90 78
Student8 77 96 97 75 77 73 71
Student9 89 79 87 86 70 77 84
Student10 74 79 87 96 96 94 73
Student11 90 88 95 95 90 86 76
Student12 92 87 80 87 98 71 76
Student13 79 94 75 93 86 88 93
Student14 82 90 92 83 88 94 84
Student15 79 78 72 72 81 90 90
Student16 100 84 79 95 78 77 95
Student17 77 98 98 98 74 80 89
Student18 83 84 96 75 98 75 99
Student19 94 98 89 86 98 76 84
Student20 87 76 98 83 76 72 88
Student21 98 89 93 82 73 84 96
Student22 81 98 85 73 73 99 76
Student23 98 70 80 84 76 80 82
Student24 81 73 74 74 77 86 87
Student25 94 76 83 83 72 76 100
Student26 70 70 82 74 85 85 94
Student27 75 77 72 84 89 90 84
Student28 78 88 81 85 87 72 82
Student29 98 99 88 70 71 88 70
Student30 95 100 77 79 70 89 89
Student31 75 91 93 93 71 74 77
Student32 72 90 76 98 88 90 82
Student33 82 84 75 75 100 88 93
Student34 97 100 74 93 84 82 97
Student35 92 93 74 93 72 96 92
Student36 100 96 77 91 95 78 90
Student37 90 75 82 96 98 82 76
Student38 81 72 80 84 86 84 88
Student39 71 71 96 94 76 81 85
Student40 85 93 99 94 91 83 95

The next step is to create a function, calcaverage, to calculate the average grade for each student. I.e. there should be a display line of each student and their respective average of all of the test grades. I can't figure out how to properly create the 2D array, let alone get on to the average for each student.

This is what I have right now, but it's not working properly so any help would be greatly appreciated. Thanks in advance.

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
  
#include "pch.h"
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;

const int STUDENTS = 40, ASSIGNMENTS = 8, TOTAL = 350;

int main()
{
	ifstream gradebook;
	string filename, students[TOTAL];
	int grades[ASSIGNMENTS][STUDENTS], i = 0;

	cout << "Enter the filename:" << endl;
	getline(cin, filename);

	gradebook.open(filename);
	int row, col;
	for (row = 0; row < STUDENTS; row++)
		for (col = 0; col < ASSIGNMENTS; col++)
			gradebook >> grades[ASSIGNMENTS][STUDENTS];
	gradebook.close();
	return(0);
}

Forgot to add in. When I type a cout for gradebook[2][4] it should display Student 3 88.
Last edited on
I think I need to create an array that is just the students and one that is just the grades, and then combine them into one, but how do I skip over elements from the file? Like I can create a 1D array that is the whole list of data, but how would I be able to skip 7 elements between reading the students' names? I'm pretty sure it involves ignore or getline but I'm not sure how to implement them.
Last edited on
I can't make sense of what you are asking, you may need to try again.

there are 3 ways to deal with this in c++. (and a number of similar ways that do the same idea with alternative syntax)

1) parallel arrays. so you would have an array of names and an array of arrays of grades. Looks like string students[maxstudents]; and int grades[students][7];

students[0] is first student. grades[0][2] is 73, the third grade for the first student.


2) a custom type (class or struct). this puts the 2 above items into a single thing:
struct student
{
string name;
int grades[maxstudents][7];
}; and then you make an array of that:

student everyone[maxstudents];
everyone[0].name; everyone[0].grades[0][2]; //same data as above


and
3) some form of byte-hack. This is very much not recommended.
eg
union u{string s; int i[7]};
u everything[maxstudents][2];
enum{name,grade}; //not required but for your own sanity.
everything[0][name].s = "student's name";
everything[0][grade].i[0] = 86; //everything[0] is which student. .i[which grade]. [grade] and [name] should be self explaining.
a fourth way is to use a built in container of some sort. a map for example where the name is the key and the grades are a vector of data, but this is way past what you know so far.

#2 is your best solution unless you are not yet allowed to use class/struct.
#4 is your true best answer, but most professors frown upon letting the language do all the work for you this early in the game.
#3 is an example of 'just because you can do something does not make it a great idea'

once you have a way to store the data, you just loop reading...
filevariable >> namestorage >> firstgrade>>secondgrade>> …. etc
for the #2 solution
filevariable>> everyone[i].name >> everyone[i].grades[0] >> everyone[i].grades[1] .. and so on
Last edited on
Yes we have learned structs, I thought of implementing them some way after my original post, but #1 makes the most sense intuitively to me.
Last edited on
I highly recommend 2 though. 1 is ok here because you only really have 2 pieces of data, but if you had 20 things, 20 arrays not connected to each other are a mess.

a really simple example of that is just using a function on all the data at once, say you did have 20 things in your struct vs 20 parallel arrays.

void foo( mytype &data); //struct

void foo(type1 *array1, type2 *array2, type3 *array3 … … … type19 *array19, type20 *array20)

and the array one called in code..
foo(array1,array2,array3,... … array20); //Big mess, see? vs foo(data);
Last edited on
I see the point you make. I did do another assignment with struct that I didn't understand as well but I think you've cleared up some confusion for me there.

So my while would be:
1
2
3
4
while (gradebook >> everyone[i].sname >> everyone[i].sscores[0] ... everyone[i].sscores[7])
{
i++
}
Last edited on
Topic archived. No new replies allowed.