2D ARRAYS PLZ HELP

I am trying to write a code the calculates couple of things:
1)avg of some students' GPA in a specific subject,
2)or the avg of one student gpa!
The program in not finished yet!
I need to put the information into a file.dat, or whatever it is called.
So,
* how do assign the first row for the first student, just in this example, since the row has 3 columns, and each column represents a subject.
* and how do I find the avg of the students' GPA in one subject. >>> I tried to code this, but when I asked the program to ask the user to input the column number which represents a subject, it failed to debug, because that is not allowed, since the value that goes inside the [] should be constant! HELP PLEASE!

#include <iostream>
#include<iostream>
#include <fstream>
#include <iomanip>
using namespace std;

const int ROW_SIZE = 3;
const int COLUMN_SIZE = 5;

int main()
{
char gradesArray[ROW_SIZE][COLUMN_SIZE];



return 0;
}
void getData(char gradesArray[ROW_SIZE][COLUMN_SIZE])
{
ifstream inputFile;
inputFile.open("grades.dat");


for (int i = 0; i < ROW_SIZE; i++)
{
for (int n = 0; n < COLUMN_SIZE; n++)
{
cout << "Enter your letter grade";
inputFile >> gradesArray[i][n];
}
//cout << endl;
}
}

void display(char gradesArray[ROW_SIZE][COLUMN_SIZE])
{
for (int i = 0; i < ROW_SIZE; i++)
{
for (int n = 0; n < COLUMN_SIZE; n++)
{
cout << gradesArray[i][n];

}
}
}
double calculations(char gradesArray[ROW_SIZE][COLUMN_SIZE])
{
double avg = 0;
int userAns = 0;
for (int i = 0; i < ROW_SIZE; i++)
{
for (int n = 0; n < COLUMN_SIZE; n++)
{
switch (gradesArray[i][n])
{
case 'a': return 4;
case 'b': return 3;
case 'c': return 2;
case 'd': return 1;
default: return 0;
}

cout << "What subject do you want to find the valuavge for? 1) english, 2)History, 3)Math" << endl;
cin >> userAns;

for (COLUMN_SIZE == userAns) ***> ERROR HERE
{
for (int i = 0;i < ROW_SIZE;i++)
avg = gradesArray[userAns][i] / 5;
}
}
Your code is pretty hard to read without code tags, so I'll just answer based on eyeballing it. You are getting the error because COLUMN_SIZE is initialized as const int COLUMN_SIZE = 5; therefore it can't be changed. I honestly don't know what's going on with that for loop you placed there, because it's not iterating through anything, and an if statement check for equivalence doesn't make sense.

how do assign the first row for the first student, just in this example, since the row has 3 columns, and each column represents a subject.


You actually have 3 rows with 5 columns each. If you want to set a value to a specific row and column for example, row 2 column 4, you'll have to access with array[1][3] = data;
Last edited on
since the row has 3 columns,
1
2
3
const int ROW_SIZE = 3;
const int COLUMN_SIZE = 5;
char gradesArray[ROW_SIZE][COLUMN_SIZE];

How many columns per row? You say 3, your code says 5.


1
2
3
4
char gradesArray[ROW_SIZE][COLUMN_SIZE];

for ( int i = 0; i < ROW_SIZE; i++ )
    avg = gradesArray[userAns][i] / 5;

There are COLUMN_SIZE columns, but your loop looks at ROW_SIZE elements of row userAns.


PS. Please use code tags when posting code.
Topic archived. No new replies allowed.