reading data from file into arrays and calculating

Hello everyone! I am new to C++ and I'm having major issues with arrays and loops, especially in this assignment.
I have a text file with a series of numbers on it:
1 90
2 100
3 89
4 72
5 68
1 92
2 98
3 85
4 77
4 82
1 97
1 87
1 92
2 86
2 76

The first column is student ids, the second is their corresponding grades. I know for a fact there are a total of 15 grades and 20 numbers all together. According to the assignment, my program needs to have two arrays: one of type int called studentId and the other is of type double called grades. The maximum number of elements supported by the arrays is 20 elements.

Here is part of my code:

[// create the arrays. Maximum number of grades supported is 20
const int MAX_GRADES = 20;
int studentId[MAX_GRADES];
double grades[MAX_GRADES]; // this will contain the actual number of grades read in from the input file
int numberGrades;

ifstream inputFile;
inputFile.open("grades.txt");
int count = 0;
// Read the numbers from file into array
while (count < maxGrades && inputFile)
{
inputFile >> studentId[count] >> grades[count];
count++;
}

//close file
inputFile.close();

//display test to make sure above code is correct
for (count = 0; count < maxGrades; count++)
{
cout << studentId[count] << " " << grades[count] << endl;
}
]

My output is:
1 90
2 100
3 89
4 72
5 68
1 92
2 98
3 85
4 77
4 82
1 97
1 87
1 92
2 86
2 76
-858993460 -9.25596e+061
-858993460 -9.25596e+061
-858993460 -9.25596e+061
-858993460 -9.25596e+061
-858993460 -9.25596e+061


I understand the last 5 lines on the output: each array is trying to fill in the remaining elements with random numbers. I have been slaving over this for two days now and I'm not sure how to solve this problem. Can anybody please help?


Last edited on
you are always printing out the complete array of MAX_GRADES items, regarless of how many you actually read in.

when you are reading the grades in, count is counting them as you go.

use that count as your new maximum, so you only print out the count that you read in.

so your display loop looks like this
1
2
3
4
for (int cnt = 0; cnt < count; cnt++)
{
cout << studentId[cnt] << " " << grades[cnt] << endl;
}
Thanks! Makes sense now!
I have another question:
Since the text I am given isn't in order, I have to sort it out without touching the text file. So if I choose to display grades for student ID 1, I have to display 90, 92, 97, 87, and 92. Logically speaking for every element inside the studentID array with the value 1, that subscript should match the corresponding grade in the grades array

So I did a binary search:


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
//in main function
for (int id = 1; id <= 6; id++)
{
// call displayForStudent to display grades for this student
index = binarysearch(id, studentId, grades, numberGrades);
cout << "id: " << studentId[index] << endl;
cout << "grades: " << grades[index] << endl;
}

int binarysearch(int id, const int studentId[], const double grades[], int numberGrades)
{
	//binary search
	int first = 0,
		last = numberGrades - 1,
		middle,
		position = -1;
	bool found = false;
	while (!found && first <= last)
	{
		middle = (first + last) / 2;
		if (studentId[middle] == id)
		{
			found = true;
			position = middle;
		}
		else if (studentId[middle] > id)
			last = middle - 1;
		else
			first - middle + 1;
	}

	return position;
}



And my output is:
id: 1
grades: 90.00

id: 2
grades: 100.00

id: 3
grades:89.00


It only display up to id 3. The program doesn't even let me press any key to exit the program. What am I doing wrong?


Topic archived. No new replies allowed.