?
Solved

Beginner programming array question

Posted on 2003-04-01
5
Medium Priority
?
195 Views
Last Modified: 2010-04-01
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
Comment
Question by:douggr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
5 Comments
 
LVL 18

Expert Comment

by:bobbit31
ID: 8248318
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
 
LVL 18

Accepted Solution

by:
bobbit31 earned 300 total points
ID: 8248353
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
 
LVL 2

Expert Comment

by:Francoz
ID: 8259487
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
 
LVL 11

Expert Comment

by:bcladd
ID: 9587458
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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question