Multiple functions taking input from two arrays

I am trying to write a program that takes the values from two arrays to display several different things. This is the what the lab asks for.

Video Game Player Program): Determine the average score for a group of players and then determine who scored below average. Write your program to do the following:
In main, declare a PlayerName Array and a Score Array. Declare the size of the arrays to be 100.

In the InputData function, input the player name and score into the arrays for an unknown number of players up to 100.
In the DisplayPlayerData function, display the name and score of each player.
In the CalculateAverageScore function, calculate the average score and return it by value.
In the DisplayBelowAverage function, display the name and score for any player who scored below the average. Do not use global variables.

This is how the program is supposed to look.
Output from Program:

Enter Player Name (Q to quit): Bob
Enter score for Bob: 3245
Enter Player Name (Q to quit): Sue
Enter score for Sue: 1098
Enter Player Name (Q to quit): Dave
Enter score for Dave: 8219
Enter Player Name (Q to quit): Pat
Enter score for Pat: 3217
Enter Player Name (Q to quit): Q

Name Score
Bob 3245
Sue 1098
Dave 8219
Pat 3217

Average Score: 3944.75

Players who scored below average
Name Score
Bob 3245
Sue 1098
Pat 3217
Press any key to continue . . .


I do not know what to put in the for loop for my value of i on line 42. I also feel like each of the arrays in the different functions are different despite the fact that they have the same name. I feel really lost on this program.


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
#include <iostream>
#include <string>

using namespace std;

// Input data prototype
void InputData(string PlayerName[], int Score[]);
// Display Player prototype
void DisplayPlayerData(string PlayerName[], int Score[]);

int i=0;
int main()
{
	
	string PlayerName[100];
	int Score[100];
	InputData(PlayerName,Score);
	DisplayPlayerData(PlayerName, Score);
	return 0;
}
void InputData(string PlayerName[],int Score[])
{
	
	
	while (PlayerName[i] != "q")
	{
		i = i + 1;
		cout << "Enter Player Name (Q to quit) : ";
		cin >> PlayerName[i];
		cout << endl;
		cout << "Enter score for " << PlayerName[i] << ": ";
		cin >> Score[i];
		cout << endl;
		
	}
	cout << endl;
	return;
}
void DisplayPlayerData(string PlayerName[],int Score[])
{
	cout << "Name\t" << "Score" << endl;
	for (i = 0; i < ; i++)
	{
		cout << PlayerName[i] << "\t" << Score[i] << endl;
	}
	return;
}

I have not written anything for the last two functions, because I feel like I am way off track with what I have so far. I think I might be able to do those once I figure out what is wrong with what I have written so far.
you need a third parameter for DisplayPlayerData(...) with the number of elements. InputData(...) is supposed to return this number.

In your case [unfortunately] the global variable i contains the number of elments, hence you cannot use i as the index of your loop on line 42. It may look like this:
1
2
3
4
	for (j = 0; j < i; j++) // Note: j instead of i
	{
		cout << PlayerName[j] << "\t" << Score[j] << endl;
	}


Does this mean that Input data should not a be a void function, so it can return a value to plug into the Display function?
I have tried labeling the function several different things besides void so it can return a value. Since it is has both string and int I do not know how to label it.
Got the code working. Going to display it in case it can help someone else down the line.
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
#include <iostream>
#include <string>

using namespace std;

// Input data prototype
void InputData(string PlayerName[], int Score[]);
// Display Player prototype
void DisplayPlayerData(string PlayerName[], int Score[]);

double CalculateAverage(string PlayerName[], int Score[]);
void DisplayBelowAverage(string PlayerName[], int Score[], double avg);


int i = 0;
int main()
{

	string PlayerName[100]; // we declare an array of strings to store names of all the players 
	int Score[100]; //declaring an array of int to store scores of all the players
	//such that ith position playerName will have ith position Score
	InputData(PlayerName, Score); //call function to input names and scores
	DisplayPlayerData(PlayerName, Score); //display names and scores
	double avg = CalculateAverage(PlayerName, Score); //calculates average of all scores
	//and stores in avg
	DisplayBelowAverage(PlayerName, Score, avg); //displays all player names with score who fall  //below average                      
	return 0;
}
void InputData(string PlayerName[], int Score[])
{


	while (PlayerName[i] != "q")
	{

		cout << "Enter Player Name (Q to quit) : ";
		cin >> PlayerName[i];
		if (PlayerName[i] == "q")
		{
			break;

		}
		cout << endl;
		cout << "Enter score for " << PlayerName[i] << ": ";
		cin >> Score[i];
		cout << endl;
		i = i + 1;

	}
	cout << endl;

}
void DisplayPlayerData(string PlayerName[], int Score[])
{
	int j;
	cout << "Name\t" << "Score" << endl;
	for (j = 0; j <i; j++)
	{
		cout << PlayerName[j] << "\t" << Score[j] << endl;
	}
	return;
}


double CalculateAverage(string PlayerName[], int Score[])
{
	int sum = 0;
	for (int j = 0; j<i; j++)
	{
		sum = sum + Score[j]; // here we calculate sum of all scores
	}

	return sum / i;  // and we return sum divide by total number of scores to get the average
}

void DisplayBelowAverage(string PlayerName[], int Score[], double avg)


{
	cout << endl << "average is ::" << avg;
	cout << endl << "players below average are::" << endl;
	for (int j = 0; j<i; j++)
	{
		if (Score[j] < avg)   //now we compare all scores with average
			cout << PlayerName[j] << "::" << Score[j] << endl;
		//here we display all the players name with scores who have scores less than average
	}
}
Topic archived. No new replies allowed.