Array user input storing in reverse. Need opposite.

Hello everyone,

One final issue i can't seem to figure out.
My array values are input requested such as 80 82 84.
However, at print they are recovered as 84 82 80.
They seem to be storing in reverse such as last in first out.
I am not sure how to fix this.

Program works completely so please test and see for yourself.

Thanks in advance!
Beauzel

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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
  /* Beau Bruemmer
   Assignment - Program 3 - C++
   Due date - 10/17/14 */

#include "stdafx.h"
#include <iostream> // Allows program to perform input and output.
#include <iomanip>  // Used to manipulate data values using setprecision to 2 decimals

using namespace std; // allows using cin and cout rather then std::cin and std::cout.

/* Function and template pre-loading*/
template <class Avg>
Avg Average(Avg Grade1,Avg Grade2,Avg Grade3);
char printLetterGrade(double AvgGrade);
double avg(double* ary, double SIZE);
double max(double* ary, double SIZE);
double min(double* ary, double SIZE);




int main()
{
	/*Variable declaration*/
	double Students;
	double LtrGrd;
	double Grade1;
	double Grade2;
	double Grade3;
	double AvgValue = 0;
	int StudentNumber = 1;
	int x = 0;
	int y = 0;
	
	/*Requests User data of how many students using an If statement to request new data if input is over the limit of 30*/
	cout << "Enter the number of students in class: "; 
	cin >> Students;  
	if (Students > 30)
	{
		cout << "Maximum number of students allowed is 30.\n";
		cout << "Enter the number of students in class: ";
		cin >> Students;
	}
	/*Initializing maximum array sizes based on parameter of maximum 30 students*/
	int Grades[90];
	double Avgs[30];
	
	/*Input Loop -  Loop used to gather requested data from user and stores values in array "Grade".
	  Loop uses input variables to calculate values for array "Avgs"*/
	for (int n = 1; n <= Students; n++){
		cout << "Please enter three numeric grades for student number " << StudentNumber++ << ". "; 
		cin >> Grade1;
		Grades[x++] = Grade1;
		cin >> Grade2;
		Grades[x++] = Grade2;
		cin >> Grade3;
		Grades[x++] = Grade3;

		Avgs[y++] = Average(Grade1, Grade2, Grade3);
		cout << endl;
	}

	/*output Look - Used to return data from arrays in structured forum that is readable by user.
	  Loop uses if statements to create Visual structure of output.*/
	x = 0 ;
	y = 0 ;
	for (int n = 1; n <= Students; n++){
		if (n <= 1){
			cout << "StudentNumber\t" 	"Grade1\t"  	"Grade2\t"  	"Grade3\t" 	"Average\t" 	"LetterGrade\n";
			cout << "===========================================================" << endl;
		}
		cout << "\t" << n << "\t" << Grades[x++] << "\t" << Grades[x++] << "\t" << Grades[x++] << "\t" << setprecision(2) << fixed << (LtrGrd = Avgs[y++]) << '\t';
		cout << printLetterGrade(LtrGrd) << '\t' << endl;
		
		if (n == Students)
			cout << "===========================================================" << endl;
	}
	
	
	/*Data outputs using array "Avgs" with functions avg, max, min.
	  Precision values set to improve readablity*/
	cout << "Processed: " << setprecision(0) << fixed << Students << " Students." << endl;
	cout << "Class Average: " << setprecision(2) << fixed << avg(Avgs, Students) << endl; 
	cout << "Class highest average: " << max(Avgs, Students) << endl; 
	cout << "Class least average: " << min(Avgs, Students) << endl; 
	cout << endl;

	/*System Pause until for any key before returning of 0 to exit*/
	system("PAUSE"); 
	return 0;
	exit(1);
	
}
/*Function template to take input grades per student from input loop and average the values.
Value is returned and stored in "Avgs" array for use in min/max function and output loop*/
template <class Avg>
Avg Average(Avg Grade1 ,Avg Grade2 ,Avg Grade3){
	
	Avg GradeAvg = ((Grade1 + Grade2 + Grade3) / 3);
	return GradeAvg;
}



/*Function to Take Average of grade input per student from input loop and determines 
the corresponding letter grade. Function returned in output loop*/
char printLetterGrade(double AvgGrade){
	int LetterGrade;
	if (AvgGrade >= 90) // Grades 90 and above as "A"
		LetterGrade = 'A';
	else
		if (AvgGrade >= 80) // Grades 80-89 as "B"
			LetterGrade = 'B';
		else
			if (AvgGrade >= 70) // Grades 70-79 as "C"
				LetterGrade = 'C';
			else
				if (AvgGrade >= 60) // Grades 60-69 as "D"
					LetterGrade = 'D';
				else // Grades 59 and less as "F"
					LetterGrade = 'F';
	
	return LetterGrade;
}

/*Function to find minimum value within array "Avgs"*/
double min(double* ary, double SIZE){
	double min = 100;//Init to max value
	for (int i = 0; i < SIZE; i++)
		if (ary[i] < min)
			min = ary[i];
	return min;
}

/*Function to find maximum value within array "Avgs"*/
double max(double* ary, double SIZE){
	double max = 0;//Init to min value
	for (int i = 0; i < SIZE; i++)
		if (ary[i] > max)
			max = ary[i];
	return max;
}

/*Function to calculate total class average using array "Avgs"*/
double avg(double* ary, double SIZE){
	double avg = 0;
	for (int i = 0; i < SIZE; i++)
		avg += ary[i];
	avg = avg / SIZE;
	return avg;
}
You need to change line 72 to the following:

1
2
3
cout << "\t" << n << "\t" << Grades[x] << "\t" << Grades[x+1] << "\t" << Grades[x+2] << "\t" << setprecision(2) << fixed << (LtrGrd = Avgs[y++]) << '\t';
		cout << printLetterGrade(LtrGrd) << '\t' << endl;
      x += 3;
It's not really storing backwards. It's the way line 72 is being evaluated. It's evaluating from right to left. So the third grade entry is processed first, then the second and then the first. Because you are incrementing x by 1, the third grade is being referred to the first grade, the second grade is being referred to second grade and the first grade is being referred to the third grade.
Note that you should NEVER have multiple x++'s or similar in the same statement. There is no way that you can know how its going to be evaluated - sometimes it would be left to right, other times right to left, sometimes it might be from the middle going out. The point is, you DON'T KNOW. Its called undefined behaviour.

Just thought I should make that clear.
Last edited on
THANK YOU! that has been driving me nuts!
Your explanation makes perfect since, very helpful!
Completely didn't think of it right to left being the issue. Storing that one in brain for future reference.

Again i cant thank you enough!
Beauzel
You're welcome Beauzel. Glad you're unstuck.
Topic archived. No new replies allowed.