I need an explanation on how to use 2 dimensional arrays displaying names. And single dimensional arrays for displaying letter and number grades. I need to make a self input gradebook for my students and do not know how to function it. I want them to input their names and their 4 test grades, drop the lowest and average the others to display to them. Any help would be appreciated.

Recommended Answers

All 8 Replies

Lots of different ways to do all of this. Some require using one dimensional arrays, some require using two dimensional arrays, some requiring no arrays at all.

I don't know why you'd want a 2-D array here unless you are using C-Style strings. Use C++ style strings and you only need a 1-D array. Use a vector instead of an array and you don't have to bother with arrays at all.

The advice you get on this depends on what you already know. If you already know how to use C++ classes, that's probably the best way. If not, it would be pointless to suggest a class-based solution. So you'll have to get more specific on exactly what your skill level is and what aspect of this assignment you need help on.

That is my problem. I don't know much about C++ myself and have read about vectors, and believe that is too complicated for me right now, I have read up on arrays but have not grasped the concept on how 2-D works. You don't have to give me an example on what I want it for, just give me a random example to see if I grasp it better, please? I have been coding for the past hour and have sort of a better understanding of single dimensional arrays now.

That is my problem. I don't know much about C++ myself and have read about vectors, and believe that is too complicated for me right now, I have read up on arrays but have not grasped the concept on how 2-D works. You don't have to give me an example on what I want it for, just give me a random example to see if I grasp it better, please? I have been coding for the past hour and have sort of a better understanding of single dimensional arrays now.

Don't worry about 2-D arrays for names. I don't see why you would need one here. You might need a 2-D array for their test scores, so we'll use that. Let's say you have 10 students. Each has a name and 4 scores. Scores range from 0 to 100. We'll make them integers.

string names[10];
int scores[10][4];

// now read them in using a nested loop.
for(int i = 0; i < 10; i++)
{
    cout << "Enter the name of the student : ";
    cin >> names[i];

    // now read in the grades.
    for(int j = 0; j < 4; j++)
    {
       cout << "Enter grade " << j << " for " << names[i] << " : ";
       cin >> scores[i][j];
    }
}

Now you have all the data in your arrays.

I see, you have to run i(rows) then j(cols), opposite from Excel. But I would like to know when it comes to displaying as a chart wouldn't a 2-D array be easier? Considering that I don't know know much code. Maybe I can make it modular and start putting the inputed scores in a col for each student, avg, low, high and so on. See where I am getting at?

>> But I would like to know when it comes to displaying as a chart wouldn't a 2-D array be easier?

Easier than what? Chart displaying what? You should have the raw grades stored in a 2-D array, as per my example. Everything else gets CALCULATED from the scores array. In other words, generally, very much UNLIKE Excel, you do not store everything you display. You store the raw grades, then from that, you would most likely calculate the average or whatever, display it, then once you didn't need it anymore, you'd throw it away. Or not. It completely depends on what you are doing and how and there's more than one way to do everything. Those with familiarity with C++ would use an struct/class and you'd likely think of things as a database rather than a spreadsheet. If you end up displaying a chart, you're almost certainly not going to have a 2-D array with strings and numbers in it. Arrays are going to be the same type (i.e. a 2-D array of integers or a 2-D array of strings). What you DON'T do is this:

Name Grade 1 Grade 2
Bob  54      87
Sam  78      90

You're not going to have a 2-D array where "Bob" is a string and 54 is an integer and they're stored in the same 2-D array because that's what ends up being displayed. I'm not sure if that's how you're thinking about it, but if so, that's not the way to think about it. To display the above, you would not store the data in a 3 x 3 array.

Some people don't think the same way. All I want to do is write a program that uses a two dimensional array of characters to hold the student names, a single-dimensional array of characters to hold each student's letter grade, and five single dimensional arrays of four doubles to hold each student's set of test scores. The program should allow my students to enter each student's name and his/her four test scores. It should then find the lowest test score for each student, drop it and then calculate the average for the remaining. And ideally should also display each student's average test score and a letter grade based on the average, even though I would also like it to show the inputs aswell.

>> five single dimensional arrays of four doubles

That's a 2-D array of doubles.

>> two dimensional array of characters

Those are C-style strings rather than C++ style strings


Change my skeleton slightly assuming maximum name length is 20. 5 is number of students.

char names[5][21]; // 21 is 1 more than 20, need room for NULL terminator
double scores[5][4];
char grades[5];

// now read them in using a nested loop.
for(int i = 0; i < 5; i++)
{
    cout << "Enter the name of the student : ";
    cin >> names[i];

    // now read in the grades.
    for(int j = 0; j < 4; j++)
    {
       cout << "Enter grade " << j << " for " << names[i] << " : ";
       cin >> scores[i][j];
    }

    // calculate the grade from the scores and stick it in grades[i]
    // calculate average, display the inputs, whatever you want based on the scores
}

I see, I have to two more questions. Would I use a for loop for calculating and discarding the low, or a while loop? And how would I display the answers using the 2-D array as I have tried it and it divides my input names into single elements. Making a mess of everything.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.