Hello all,

For this program (assignment), the user enters the names of five students, and their respective four test scores. The program will then average the scores, and display them for the user.

Part of the requirement for the program is that we use five single-dimensional arrays of 'doubles' to store the sets of test scores (I'm sure a two-dimensional array would be easier, but again, a requirement for the assignment).

Where I'm stuck is how I can use a loop (with cin) to store the data entered by the user into the separate arrays. So far, the program will store the test scores in the first array, but that's it.

This is my first programming class, so please be gentle ;-)

Thanks!

vandadm

#include <iostream>
using namespace std;


 int main()
 {
	const int STUDENT_NAME = 5;				//number of names
	const int NUM_SCORES = 4;				//number of scores entered by user
	const int STRING_SIZE = 15;				//maximum numer of characters
	char names[STUDENT_NAME][STRING_SIZE];	//array for the names of the students
	char letter_grade[5];					//array for the letter grade
	double score1[NUM_SCORES], 
		   score2[NUM_SCORES], 
		   score3[NUM_SCORES], 
		   score4[NUM_SCORES];
	double average;
	
	//Loop that will get the name of the student, test scores, and average them.
	 for (int index = 0; index < STUDENT_NAME; index++)
	 {
		 cout << "Enter the name of student " << (index + 1) << " : ";
		 cin >> names[index];
		 cout << "Enter the test scores for " << names[index] << ":" << endl;
		 for (index = 0; index < NUM_SCORES; index++)
		 {
			 cin >> score1[index];
		 }
	 }

	 

	return 0;
 }

Recommended Answers

All 5 Replies

struct Student  {
        double grades[4];
        std::string name;
        int average;

        void Assign_Name()  {
           std::cin >> name;
         }
        void Assign_Grades()  {
           for (int n=0;n<4;n++)  {
               std::cin >> grades[n];
             }
         }
        void Display_Average()  {
           average=0;
           for (int x=0;x<4;x++)  {
              average+=grades[x];
            }
           std::cout << name << "\t" << average << "\n";
         }
      };

     Student students[5];
     for (int x=0;x<6;x++)   {
         students[x].Assign_Name();
         students[x].Assign_Grades();
         students[x].Display_Average();
       }

@caut_baia - he said the data types that he had to use so I don't think using a structure would be following those guide lines.

@op - Not sure if you are allowed to use a pointer but I threw it in with a switch otherwise you would have to copy paste a bunch of code since the score arrays cannot be used in a for loop effectively. I also added another score array in because you only had 4 and changed some variable names to (ex NUM_STUDENTS instead of STUDENT_NAME)

#include <iostream>
using namespace std;

 int main()
 {
	const int NUM_STUDENTS = 5;				//number of students
	const int NUM_SCORES = 4;				//number of scores entered by user
	const int STRING_SIZE = 15;				//maximum numer of characters
	char names[NUM_STUDENTS][STRING_SIZE];	//array for the names of the students
	char letter_grade[NUM_STUDENTS];		//array for the letter grade
	double score0[NUM_SCORES],
		   score1[NUM_SCORES],
		   score2[NUM_SCORES],
		   score3[NUM_SCORES],
		   score4[NUM_SCORES];
	double average[NUM_SCORES];

	double *score = 0; //points to the score based on the student

	//Loop that will get the name of the student, test scores, and average them.
	 for( int studentIndex = 0; studentIndex < NUM_STUDENTS; studentIndex++ )
	 {
		average[studentIndex] = 0;
		cout << "Enter the name of student " << (studentIndex + 1) << " : ";
		cin >> names[studentIndex];
		cout << "Enter the test scores for " << names[studentIndex] << ":" << endl;

		switch(studentIndex)
		{
			case 0:
				score = score0;
				break;
			case 1:
				score = score1;
				break;
			case 2:
				score = score2;
				break;
			case 3:
				score = score3;
				break;
			case 4:
				score = score4;
				break;
		}

		for( int scoreIndex = 0; scoreIndex < NUM_SCORES; scoreIndex++ )
		{
			cin >> score[scoreIndex];
			average[studentIndex] += score[scoreIndex];
		}
		average[studentIndex] /= NUM_SCORES;
		cout << average[studentIndex] << endl;

	}
	score = 0;
	//add a pause if needed

	return 0;
}

You still have to add in the letter grade part

Thanks for the info. It's much appreciated.

@caut_baia - sfuo is correct. I don't even know what a structure is, yet.

@sfuo - Unfortunately, I haven't learned pointers yet, but can see how much easier this code is.

Can you think of another way to get populate the scores array, other than a for loop? You mentioned copying and pasting code. Would a series of if / else statements work for the population of the arrays?

Thanks again!

vandadm

>>Can you think of another way to get populate the scores array, other than a for loop?
Thats pretty simple to code. But sice you asked

#include<iostream>
using namespace std;

int main() {
	double	marks1[1],marks2[1],marks3[1],marks4[1],marks5[1];
	int x=1;
	
	while(x<=5)
	{
		int total=0;
		int average;
		cout<<"Enter 5 marks for student"<<" "<<x<<":"<<endl;
		cin>>marks1[0];
		total+=marks1[0];
		cin>>marks2[0];
		total+=marks2[0];
		cin>>marks3[0];
		total+=marks3[0];
		cin>>marks4[0];
		total+=marks4[0];
		cin>>marks5[0];
		total+=marks5[0];
		average=total/5;
		cout<<x<<" "<<"student's Average: "<<average<<endl;
		++x;
	}
	return 0;
}

+++ deleted +++
Sorry, you aren't allowed to use two-dimensional arrays, so my posting is useless !
+++ deleted +++

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.