Help with homework

Hey, I dont know how to make this work properly. I dont know how to keep the score array and the name array together. I need to display the scores that go along with the names. The question I got for my homework is:

Modify the program of programming challenge 1 to allow the user to enter name—score pairs. For each student taking a test, the user types a string representing the name of the student followed by an integer representing the students score. Modify both the sorting and average calculating functions so they take arrays of structures with each structure containing the name and score of a single student. In traversing the arrays, use pointers rather than array indices.

Thanks in advanced!!


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

using namespace std;
void printArray(int *s,string *n, int *size);
void sortArray(int score[], string name[], int size);
void averageArray(int *s, int *size);
void sortName(string name[], int size);
int main()
{
	int size;
	int score[size];
	string name[size];
	float average;
	
	cout << "Enter the amount of tests taken: " << endl; //Enter size
	cin >> size;
	
	for(int i = 0; i < size; i++) //Enter scores
	{
		cout << "Enter score and name: ";
		cin >> score[i] >> name[i];
	}
	
	sortName(name, size); // sort names
	sortArray(score, name, size); //Sort scores
	cout << "Sorted Scores and names: " << endl;
	printArray(score, name, &size); //display sorted scores
	cout << "Average: ";
	averageArray(score,  &size); //Find average of scores
	
	return 0;
}
void sortArray(int score[], string name[], int size)
{
	int temp;
	bool madeAswap;
	do
	{	madeAswap = false;
		for(int count = 0; count < (size - 1); count++)
		{
			if(score[count] > score[count + 1] && name[count] > name[count + 1])
			{
				temp = score[count];
				score[count] = score[count +1];
				score[count + 1] = temp;
				madeAswap = true; 
			}
			
		}
	} while(madeAswap);
}
void sortName(string name[], int size)
{
	int startScan, minIndex;
	string minValue;
	
	for(startScan = 0; startScan < (size - 1); startScan++)
	{
		minIndex = startScan;
		minValue = name[startScan];
		
		for(int index = startScan + 1; index < size; index++)
		{
			if(name[index] < minValue)
			{
				minValue = name[index];
				minIndex = index;
			}
		}
		name[minIndex] = name[startScan];
		name[startScan] = minValue;
	}
}
void averageArray(int *s, int *size)
{
	int sum;
	float average;
	for(int i = 0; i < *size; i++)
	{
		sum+=*(s + i);
	}
	average = sum / *size;
	
	cout << average;
}
void printArray(int *s, string *n, int *size)
{
	for(int i = 0; i < *size; i++)
	{
		cout << *(n + i) << *(s + i) << endl;
	}
}
By the way,
Here is the code for programming challenge 1 that is reference in the question, which I wrote, along with the question for challenge 1.

challenge 1:
Write a program that dynamically allocates an array large enough to hold a user-defined number of test scores. Once all the test scores are entered, the array should be passed to a function that sorts them in ascending order another function should be called that calculates the average score. The program should display the sorted list of scores and averages with appropriate headings. Use pointer notation rather than array notation whenever possible.

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

using namespace std;
void printArray(int *s, int *size);
void sortArray(int score[], int size);
void averageArray(int *s, int *size);
int main()
{
	int size;
	int score[size];
	float average;
	
	cout << "Enter the amount of tests taken: " << endl; //Enter size
	cin >> size;
	
	for(int i = 0; i < size; i++) //Enter scores
	{
		cout << "Enter scores: ";
		cin >> score[i];
	}
	
	sortArray(score, size); //Sort scores
	cout << "Sorted Scores: " << endl;
	printArray(score, &size); //display sorted scores
	cout << "Average: ";
	averageArray(score, &size); //Find average of scores
	
	return 0;
}
void sortArray(int score[], int size)
{
	int temp;
	bool madeAswap;
	do
	{	madeAswap = false;
		for(int count = 0; count < (size - 1); count++)
		{
			if(score[count] > score[count + 1])
			{
				temp = score[count];
				score[count] = score[count +1];
				score[count + 1] = temp;
				madeAswap = true; 
			}
		}
	} while(madeAswap);
}
void averageArray(int *s, int *size)
{
	int sum;
	float average;
	for(int i = 0; i < *size; i++)
	{
		sum+=*(s + i);
	}
	average = sum / *size;
	
	cout << average;
}
void printArray(int *s, int *size)
{
	for(int i = 0; i < *size; i++)
	{
		cout << *(s + i) << endl;
	}
}
> Write a program that dynamically allocates an array large enough to hold a user-defined number of test scores.

1
2
3
4
5
int size;
int score[size];

cout << "Enter the amount of tests taken: " << endl; //Enter size
cin >> size;

What size do you think your array is?

1. This isn't even valid C++ (only C permits variable length arrays, and only then in C99).
2. Neither language has lazy evaluation, so your array is cast in stone with the uninitialised size.

In C++, do this if you must use C-style arrays.
1
2
3
4
5
6
7
cin >> size;
int *score = new int[size];

// rest of your code

// clean up when you're done.
delete [] score;


Better yet, use std::vector.
Topic archived. No new replies allowed.