Parallell array

I need help with this assignment.
Program 7
You are to write a program that will compute your CS1428 quiz average after you have selected a quiz to drop.
The program should read input from a data file named “quizinfo.txt” where each line of the file contains
quiz number maximum points on the quiz points earned on the quiz
The above are separated by tabs.
Note: Both point values should be float or double as some quizzes may have ½ points earned
You can assume that there will be no more than 12 quizzes. (Use a Named Constant for this)
The end of the data is indicated by a negative quiz number and 0s for the other two data values on the line.
Do not allow your program to run off the end of arrays!!
Note: This is a sentinel value read. Please do it correctly.
The data is displayed to the user where they can then select a quiz number to 'drop'. The quiz average with the
dropped quiz is then displayed to the nearest whole number. A statement similar to the following should be used to
output the average:
“If you drop quiz <quiz #> then your quiz average will be <calculated average>.”
The user should be allowed to repeat the process with another quiz selection until they elect to exit the program
by entering a negative value for the quiz number.
You should validate the input file open.
The usual style requirements of header comment format, variable description, indentation, etc. should be followed.
All output is to the screen/console.
This program is due on THURSDAY, November 1, by the start of class.
Advice: On paper – list the tasks the program should perform in the order they need to be performed. Then (in
English or pseudo Code/English) expand each task.
Example of input file content:
0 27 14
1 14 11
2 30 24
3 34 31
4 9 7
5 14 10
-1 0 0




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

using namespace std;

int main()
{
	const int VALUES= 13; // the array that will hold the values
	int Quiz[VALUES]; // quiz number
	double MaxPoints[VALUES]; // max points on quiz
	double PointsEarned[VALUES]; // points earned on quiz
	const int SENTINEL = -1; // sentinel value
	int Count =0; // counts the number of quizzes
	double SumTotal =0; // the total number of points possible
	double SumEarned =0;
	double average;
	int QuizNum;
	ifstream input;
	input.open("quizinput.txt");


	if(Quiz[Count])
	{
		input >> Quiz[Count];

		while ( QuizNum != SENTINEL && Quiz[Count <= 13])
		{
			Count ++;



			for(int i =0; i <= Quiz[13]; i++)
			{
				input >> MaxPoints[i];
				SumTotal += MaxPoints[i];
				input >> PointsEarned[i];
				SumEarned += PointsEarned[i];

			}


			for(int i =0; i<= Count; i ++)
			{
				input >> Quiz[i-1];
				input >> MaxPoints[i-1];
				input >> PointsEarned[i-1];
				cout << Quiz[i] << "    " << MaxPoints[i]<<
				"    " << PointsEarned[i] <<endl << endl;


			}
			cout << "What Quiz would you like to drop ?" << endl;
			cin >> QuizNum;

			average =  static_cast<double> (SumTotal - MaxPoints[QuizNum])/
				SumEarned - PointsEarned[QuizNum];
			cout << "If you drop quiz " << QuizNum <<
				" then your quiz average will be "<< average << endl;
		}
	}

	else
	{
		cout << " The input file did not open ";
		return -1;

	}

	return 0;
}

Please help as quickly as possible
Last edited on
Whats the problem?

Please use code tags, the <> button, for code.
the sentinel is not working , the averaging is not working, it is not inputing correctly
1
2
3
4
5
6
7
for(int i =0; i <= Quiz[13]; i++)
{
input >> MaxPoints[i];
SumTotal += MaxPoints[i];
input >> PointsEarned[i];
SumEarned += PointsEarned[i];
}


Why are you using the 13th element of the Quiz array as the comparison to i in this for loop? I think you wanted to simply use the total number of array elements, which was Count. BUT, you initialize Count to 0 when you declare it but then do nothing to change that.

Also:

1
2
3
4
5
6
7
for(int i =0; i<= Count; i ++)
{
input >> Quiz[i-1];
input >> MaxPoints[i-1];
input >> PointsEarned[i-1];
cout << Quiz[i] << " " << MaxPoints[i]<<
" " << PointsEarned[i] <<endl << endl;


Here you initialize i to equal 0, so when you are using Quiz[i-1] the first iteration through the loop, you are telling it to look at Quiz[-1] which it outside the array since the first element of any array is array[0]. Therefore, Quiz[-1] is looking at undefined data. Also, if you want to iterate through an array remember if you have an array of say, size 13, you declare it as array[13]. But remember the first element of the array is array[0]. The last element will be array[12]. So saying i <= 13 in any loop that is supposed to iterate through each element of the array will run to array[13] which is also outside the array and therefore contains undefined data.

while ( QuizNum != SENTINEL && Quiz[Count <= 13])

Here, the Quiz[Count <= 13] is invalid code. I doubt that would even compile. Again I think you simply meant to use the array size as the comparison which should be simply the Count variable.

Last edited on
I think both of my for loops are wrong.
Especially the read one


1
2
3
4
5
6
7
for(int i =0; i<= Count; i ++)
			{
				input >> Quiz[i-1];
				input >> MaxPoints[i-1];
				input >> PointsEarned[i-1];
				cout << Quiz[i] << "    " << MaxPoints[i]<<
				"    " << PointsEarned[i] <<endl << endl;
Thanks reezzor, how am i able to change my count variable to math with the number of quizzes
this is my out put right now

27 14 1

14 11 2

30 24 3

34 31 4

9 7 5

14 10 -1

0 0 8.05308e-315

0 4.05805e+159 4.18838e-312

0 8.71808e+159 1.80992e+159

1180054 2.12331e-314 2.03713e+159

4 6.86218e-308 3.24665e-312

0 6.86216e-308 6.86194e-308

1629956524 8.04053e-315 1.3907e-309

0 2.122e-314 3.23791e-319

0 2.97079e-313 14

2 7.21479e-313 11

1629101814 2.97079e-313 24

2674688 0 31

2665544 2.50407e-308 7

13 1.97626e-323 10

1629683785 8.05306e-315 0

1629101814 4.24399e-314 4.05805e+159

152 6.90121e-308 8.71808e+159

0 2.75873e-313 2.12331e-314

537035256 5.73808e+159 6.86218e-308

1629101791 7.5098e-322 6.86216e-308

2665720 5.738e+159 8.04053e-315

1627419957 1.80768e+159 2.122e-314

What Quiz would you like to drop ?
The input needs to make sure it hasn't reached the end of the file:
1
2
3
int Count = 0;
while (input >> Quiz[Count] >> MaxPoints[Count] >> PointsEarned[Count])
  Count++;


You can also put the SENTINAL in the while condition:
1
2
3
while (input >> Quiz[Count] >> MaxPoints[Count] >> PointsEarned[Count]  &&
      Quiz[Count] != SENTINAL)
  Count++;


Count will point to the sentinals index, so Count--

Any loop you use to go through the arrays:
for (int i = 0; i < Count; i++) // do things with array[i]
Last edited on
Thanks i think that fixed the sentinel.
My for loop is still messed up

1
2
3
4
5
6
7
8
9
10
for(int i =0; i<= Count; i ++)
	{
		input >> Quiz[i];
		input >> MaxPoints[i];
		input >> PointsEarned[i];

		cout << Quiz[i] << "    " << MaxPoints[i]<<
		"    " << PointsEarned[i] <<endl << endl;

	}


and my output is
14 11 2

30 24 3

What Quiz would you like to drop ?
it did not fix the sentinel and i want count to be the number of quizzes in the array
Topic archived. No new replies allowed.