Populate a parallel array in one function and output the data in a separate function

Hello. I am working on an assignment that requires three functions, one for populating arrays, one for getting a letter grade, and one for outputting the data. I cannot figure this out, and the textbook is complete garbage when it comes to examples.

Any help would be appreciated. I can do it in one function, but it requires three. This is what I have so far:



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

// function prototypes
void getData(string [], string [], int []);
void calculateGrade(char []);
void printResult(string [], string [], int [], char [], int);

int main()
{
	// define 4 parallel arrays
	const int NO_OF_STUDENTS = 5;
	string studentFNames[NO_OF_STUDENTS];
	string studentLNames[NO_OF_STUDENTS];
	int testScores[NO_OF_STUDENTS];
	char letterGrades[NO_OF_STUDENTS];

	// call getData() to populate three of the four parallel arrays
	//getData(studentLNames, studentFNames, testScores, NO_OF_STUDENTS);
	getData(studentFNames, studentLNames, testScores);
	// call calculateGrade() to provide values for the fourth parallel array
	calculateGrade(letterGrades);

	// call printResult() to display report form the parralel arrays
	

	return 0;
}

// function definition getData()
void getData(string fName[], string lName[], int scores[])
{
	// the follow arrays are used for test data (do not modify)
	string fNameTest[5] = {"Humpty", "Jack", "Mary", "Jack", "King"};
	string lNameTest[5] = {"Dumpty", "Horner", "Lamb", "Sprat", "Cole"};
	int scoresTest[5] = {59, 88, 100, 75, 60};
	

	// use a suitable loop to populate the appropriate "empty" arrays
	// with values from the three initialized test arrays
	for(int index = 0; index < 5; index++)
	{
		cout << lNameTest[index] << " " << fNameTest[index] << " " << scoresTest[index] << endl;
	}
}

// function definition for calculateGrade()
void calculateGrade(char letter[])
{
	int grade;
	char gradeLetter[5] = {'A', 'B', 'C', 'D', 'F'};

		if(grade > 89)
		{
			cout << gradeLetter[0];
		}
		else if(grade > 79)
		{
			cout << gradeLetter[1];
		}
		else if(grade > 69)
		{
			cout << gradeLetter[2];
		}
		else if(grade > 59)
		{
			cout << gradeLetter[3];
		}
		else
		{
			cout << gradeLetter[4];
		}
	
	
}

// function definition for printResults()
void printResult(string lName[], string fName[], int score[], char letter[], int size)
{
	cout << setw(15) << left << "Student Name" << setw(10) << "Test Score" << setw(5) << right << "Grade" << endl;


The getData() function was provided by the professor, and that cannot be changed. How it is programmed currently is wrong with what he wants. He writes: "Four 'empty' parallel arrays have been defined in main(). Three of these arrays are to be populated by a call to the getData() function. Note that the three arrays defined in getData() have already been initialized with the test data to be loaded into main()'s arrays using a suitable loop.

I wrote the loop. Any help would be appreciated. I don't know how to populate, pretty sure I don't know how to print it properly with the letter grade associated with it.

Thank you for your help.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

void getData(string fName[], string lName[], int scores[])
{
	// the follow arrays are used for test data (do not modify)
	string fNameTest[5] = {"Humpty", "Jack", "Mary", "Jack", "King"};
	string lNameTest[5] = {"Dumpty", "Horner", "Lamb", "Sprat", "Cole"};
	int scoresTest[5] = {59, 88, 100, 75, 60};
	

	// use a suitable loop to populate the appropriate "empty" arrays
	// with values from the three initialized test arrays
	for(int index = 0; index < 5; index++)
	{
         fName[ index ] = fNameTest[ index ];
         // repeat with the other arrays
	}
}


Something similar for the grade

He's actually shown you how to print:
 
cout  << lNameTest[index];


Though, he used his local array for that. You'd choose the array you populated.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <iostream>
#include <iomanip>
#include <string>

const int NO_OF_STUDENTS = 5;

// function declarations prototypes
void getData( std::string first_names[], std::string last_names[], int scores[] );
void calculateGrade( char grades[], const int scores[] ); // make it const-correct
void printResult( const std::string first_names[], const std::string last_names[],
                  const int scores[], const char grades[] ); // make it const-correct

int main()
{
        // define 4 parallel arrays
	std::string studentFNames[NO_OF_STUDENTS];
	std::string studentLNames[NO_OF_STUDENTS];
	int testScores[NO_OF_STUDENTS] = {0} ;
	char letterGrades[NO_OF_STUDENTS] = {0} ;

	// call getData() to populate three of the four parallel arrays
	//getData(studentLNames, studentFNames, testScores);
	getData( studentFNames, studentLNames, testScores );

	// call calculateGrade() to provide values for the fourth parallel array
	calculateGrade( letterGrades, testScores );

	// call printResult() to display report form the parralel arrays
	printResult( studentFNames, studentLNames, testScores, letterGrades );
}

// function definition getData()
void getData( std::string first_names[], std::string last_names[], int scores[] )
{
	// the follow arrays are used for test data (do not modify)
	// string fNameTest[5] = {"Humpty", "Jack", "Mary", "Jack", "King"};
	// string lNameTest[5] = {"Dumpty", "Horner", "Lamb", "Sprat", "Cole"};
	// int scoresTest[5] = {59, 88, 100, 75, 60};

	// do modify. (make it const-correct and avoid magic numbers.)
	static const std::string fNameTest[NO_OF_STUDENTS] = {"Humpty", "Jack", "Mary", "Jack", "King"};
	static const std::string lNameTest[NO_OF_STUDENTS] = {"Dumpty", "Horner", "Lamb", "Sprat", "Cole"};
	static const int scoresTest[NO_OF_STUDENTS] = {59, 88, 100, 75, 60};

	// use a suitable loop to populate the appropriate "empty" arrays
	// with values from the three initialized test arrays
	for( int index = 0; index < NO_OF_STUDENTS ; ++index )
	{
		first_names[index] = fNameTest[index] ;
		last_names[index] = lNameTest[index] ;
		scores[index] = scoresTest[index] ;
	}
}

static char grade( int score ) // given a score, return corresponding grade
{
    const int NGRADES = 5 ;
    static const char gradeLetter[NGRADES] = { 'A', 'B', 'C', 'D', 'F' };

    if( score > 89 ) return gradeLetter[0] ;
    if( score > 79 ) return gradeLetter[1] ;
    if( score > 69 ) return gradeLetter[2] ;
    if( score > 59 ) return gradeLetter[3] ;
    return gradeLetter[4] ;
}

// function definition for calculateGrade()
void calculateGrade( char grades[], const int scores[] )
{
    for( int index = 0; index < NO_OF_STUDENTS ; ++index ) grades[index] = grade( scores[index] ) ;
}

static void printOneResult( std::string first_name, std::string last_name, int score, char grade )
{
    using namespace std ;
	cout << setw(15) << left << ( last_name + ", " + first_name )
	     << setw(5) << right << score << ' ' << grade << '\n' ;
}

// function definition for sprintResults()
void printResult( const std::string first_names[], const std::string last_names[],
                  const int scores[], const char grades[] )
{
    for( int index = 0; index < NO_OF_STUDENTS ; ++index )
        printOneResult( first_names[index], last_names[index], scores[index], grades[index] ) ;
}

http://coliru.stacked-crooked.com/a/4689322cdb596bae
Hey, thanks for the help guys! Crash, that did the trick. JLB, unfortunately I have to use the three functions only. But I worked with what you put and I'm starting to figure it out. The only thing I can't get is the letter grades to show up. The program compiles, just no luck on the actual output.

Thanks again. I'm close enough that I'm sure I can figure it out.
Still having an issue returning the letter grade. It's returning something, but they are odd symbols and a few letters (not A - F, though lol).

Any ideas?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94


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

// function prototypes
void getData(string [], string [], int []);
char calculateGrade(char [], int []);
void printResult(string [], string [], int [], char [], int);

int main()
{
	// define 4 parallel arrays
	const int NO_OF_STUDENTS = 5;
	string studentFNames[NO_OF_STUDENTS];
	string studentLNames[NO_OF_STUDENTS];
	int testScores[NO_OF_STUDENTS];
	char letterGrades[NO_OF_STUDENTS];

	// call getData() to populate three of the four parallel arrays
	getData(studentFNames, studentLNames, testScores);

	// call calculateGrade() to provide values for the fourth parallel array
	calculateGrade(letterGrades, testScores);

	// call printResult() to display report form the parralel arrays
	printResult(studentFNames, studentLNames, testScores, letterGrades, NO_OF_STUDENTS);

	return 0;
}

// function definition getData()
void getData(string fName[], string lName[], int scores[])
{
	// the follow arrays are used for test data (do not modify)
	string fNameTest[5] = {"Humpty", "Jack", "Mary", "Jack", "King"};
	string lNameTest[5] = {"Dumpty", "Horner", "Lamb", "Sprat", "Cole"};
	int scoresTest[5] = {59, 88, 100, 75, 60};


	// use a suitable loop to populate the appropriate "empty" arrays
	// with values from the three initialized test arrays
	for(int index = 0; index < 5; index++)
	{
		fName[index] = fNameTest[index];
		lName[index] = lNameTest[index];
		scores[index] = scoresTest[index];
	}
}

// function definition for calculateGrade()
char calculateGrade(char letter[], int score[])
{
	char gradeLetter[5] = {'A', 'B', 'C', 'D', 'F'};

	for(int i = 0; i < 5; i++)
	{

		if(score[i] > 89)
		{
			return gradeLetter[i];
		}
		if(score[i] > 79)
		{
			return gradeLetter[i];
		}
		if(score[i] > 69)
		{
			return gradeLetter[i];
		}
		if(score[i] > 59)
		{
			return gradeLetter[i];
		}


        return gradeLetter[i];

	}

}

// function definition for printResults()
void printResult(string lName[], string fName[], int score[], char letter[], int size)
{
	cout << setw(15) << left << "Student Name" << setw(9) << right << "Test Score" << " " << setw(5) << "Grade" << endl << endl;
	for(int index = 0; index < size; index++)
	{
		cout << setw(15) << left << (lName[index] + ", " + fName[index]);
		cout << setw(9) << right << score[index] << " " << setw(5) << letter[index] << endl;
	}
}


I know I'm not matching the correct elements with the char function, but I'm honestly at a loss.

Again, any help is appreciated. Just need a point in the right direction. I am limited to the three functions and there isn't a way for me to alter or add any constants.

Thank you.
1
2
3
4
5
6
7
8
9
10
11
12
13
// function definition for calculateGrade()
char void calculateGrade( char letter[], const int score[] )
{
	for( int i = 0; i < NO_OF_STUDENTS ; ++i )
	{

		if( score[i] > 89 ) letter[i] ='A' ; 
		else if(score[i] > 79 ) letter[i] ='B' ;
		// etc. 

        return gradeLetter[i];
	}
}
Topic archived. No new replies allowed.