Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 202
  • Last Modified:

Beginner programming array question

I'm in the middle of trying to write a program for my C++ class and am stuck on a certain part. I need to write a program, using a two dimensional array, where I am creating a student data base. I need to enter 10 students names with 5 grades per student after which I have to display each students average as well as the class average. What I'm having a problem is is with the array that stores the grades. It appears that each time I enter a new students grades, it over writes the previous students grades. So what prints out is that every student has the same exact grades. How do I get it so that I'm not over writing the previous grades. Here's what I have wriiten for code so far.


//HOMEWORK 5. C++ PROGRAMMING DOUGLAS GREIG

#include<iostream>
#include<iomanip>


using namespace std;

int main(){

     //two dimensional array of charachters
     
     const int numNames = 2;                        //max number of names entered
     const int maxltr = 80;
     const int numgrades = 5;
     const int maxltrg = 10;
   
     char names[numNames][maxltr] = {0};                     //sets the array "names"
    char grades[numgrades][maxltrg] = {0};                  //sets the array "grades"
   

     //get the names and grades

     for(int i = 0; i < numNames; i++){
          cout << "ENTER THE STUDENTS NAME: ";
          cin.getline(names[i], maxltr);
          for(int k = 0; k < numgrades; k++){
               cout << "ENTER THE STUDENTS GRADES: ";
               cin.getline(grades[k], maxltrg);
          }

                   
         
         
     }

     //calulate the students average





     //print them out

     cout << "\n\n\n";

     cout << "    NAME   GRADES                    AVERAGE" << endl;
     cout << "--------------------------------------------" << endl;

     for(i = 0; i < numNames; i++){
             cout << setw(8) << names[i];
          for(int k = 0;k < numgrades; k++){
               cout << setw(5) << grades[k];
          }          
     cout << endl;
     }

     cout << "\n\n\n";

     int average = 0;
     int total = 0;

     total = ;

     average = (total / numgrades);


     cout << names[1] << average << endl;

     
     return 0;
}
0
douggr
Asked:
douggr
  • 2
1 Solution
 
bobbit31Commented:
i won't finish the whole thing for you, but take a look at what i'm doing here:

#include<iostream>
#include<iomanip>

using namespace std;

int main(){

     //two dimensional array of charachters

     const int numNames = 2;                        //max number of names entered
     const int maxltr = 80;
     const int numgrades = 5;
     const int maxltrg = 10;

     char names[numNames][maxltr] = {0};                     //sets the array "names"
     char grades[numNames*numgrades][maxltrg] = {0};         //sets the array "grades"

     //get the names and grades

     for(int i = 1; i <= numNames; i++){
          cout << "ENTER THE STUDENTS NAME: ";
          cin.getline(names[i-1], maxltr);
          for(int k=(i*numgrades)-numgrades; k < (i*numgrades); k++){
               cout << "ENTER THE STUDENTS GRADES: ";
               cin.getline(grades[k], maxltrg);
          }
     }


     //calulate the students average





     //print them out

     cout << "\n\n\n";

     cout << "    NAME   GRADES                    AVERAGE" << endl;
     cout << "--------------------------------------------" << endl;

     for(int i = 1; i <= numNames; i++){
             cout << setw(8) << names[i-1];
          for(int k=(i*numgrades)-numgrades;k < (i*numgrades); k++){
               cout << setw(5) << grades[k];
          }
     cout << endl;
     }

     cout << "\n\n\n";

     int average = 0;
     int total = 0;

     // total = ;

     average = (total / numgrades);


     cout << names[1] << average << endl;


     return 0;
}
0
 
bobbit31Commented:
basically, the way you had it, your grades array was an array of characters (up to maxltrg in size) that had only numgrades empty spaces. (that's why you were overwriting the values)

i changed it so that grades had numNames*numgrades empty spaces:

grades[0]
grades[1]
grades[2]
grades[3]
grades[4]

are the grades for name 0

grades[5]
grades[6]
grades[7]
grades[8]
grades[9]

are for names 1

actually, you'd be better off creating a Student class and then just having an array of students... but assuming this is a homework problem, i tried to keep to the subject at hand.

good luck
0
 
FrancozCommented:
The problem is with your second for loop to compute avarage,

for(int k = 0;k < numgrades; k++)

Here you've reinitializing the variable used to get the grade array to zero. This is wrong. Instead of that you should compute the index in htis way

for (int i = 0 ; i < numNames ; i++)
{
 //do with name array

 for(int k = i*5; k < (i+1)*5 ; k++)
 {
  //do with grades array
 }
}

I found one more mistake that you're not using the 0th elt. present in Names array since you've started index with 1.

See whether my understandings are correct or not.
0
 
bcladdCommented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Answered: Points to bobbit31

Please leave any comments here within the next seven days. Experts: Silence
means you don't care.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

-bcl (bcladd)
EE Cleanup Volunteer
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now