calculation with arrays & loops within for loops

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
Hi, this is the program i am making. I'm not a student of c++ i received this
task from a friend who is. I'm doing this for fun, just to clarify that. However, with this program, i have errors that i have no idead why are there.
1. While running this program, sometimes the stack around variable nrofdays, mintemp and maxtemp are corrupted, or lost. That is beyond me.
2. I'd also like some advice as to how the calculation at the end is'nt working. This is obviously a beginner problem, that has to do with understanding arrays.


// Program shall prompt the user how many days it is in the month the user is stat tracking. ( has to be 28-31)
// mintemp has to be between -70 and 70, while maxtemp has to be between mintemp and 70. Downpour has to be from 0 to 200.
// Then, the program will state the average minimal temperature, average maximal temperature, and average downpour.

#include <iostream>
 using namespace std;

 int main() {
	 
	 int nrofdays = 0;
	 int mintemp[] = {0};
	 int maxtemp[] = {0};
	 int totaldownpour[] = {0}; 


	 do {
	 cout << "How many days is there in the month you are keeping stats for? (28 - 31)" << endl; 
	 cin >> nrofdays;
	 if (nrofdays > 31)
		 nrofdays = 0;
 }
	 while ( nrofdays < 28);

for (int i = 0; i < nrofdays; i++) { // This is a for loop with 3 do-while loops in it to ensure error free inputs

	do {
	cout << "Enter minimum temperature for day " << i + 1 << ". (-70 - 70)" << endl;
	cin >> mintemp[i];
	if (mintemp[i] > 70)
		 mintemp[i] = 0;
}
while (mintemp[i] < -70);

do {
	cout << "Enter max temperature for day " << i + 1 << "." << " (" << mintemp[i] << " - 70)" << endl;
	cin >> maxtemp[i];
	if  (maxtemp[i] < mintemp[i])
		maxtemp[i] = 0;
	else if (maxtemp[i] > 70)
		maxtemp[i] = 0;
}
while ( maxtemp[i] == 0);

do {
	cout << "Enter total downpour for day " << i + 1 << ". (0 - 200)" << endl;
	cin >> totaldownpour[i];
	if (totaldownpour[i] > 200)
		totaldownpour[i] = 0;
}
while ( totaldownpour[i] <= 0);
 }

cout << "The average minimal temperature was " << ((mintemp[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]) / nrofdays) << endl;
cout << "The average maximal temperature was " << ((maxtemp[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]) / nrofdays) << endl;
cout << "The average downpour was " << ((totaldownpour[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]) / nrofdays) << endl;
 system("pause");
 return 0;
 }
@Terje Gundersen

Your main problem, lies in
1
2
3
int mintemp[] = {0};
	 int maxtemp[] = {0};
	 int totaldownpour[] = {0}; 


You didn't specify the array sizes, It probably made the size 1, since you initialized it with a 0. Also, the cout of the mintemp[], maxtemp[] and totaldownpour[], isn't doing what what you are intending. You don't really need those 3 arrays in the first place, since your aren't needing to access certain days.
int mintemp = 0, maxtemp = 0, totaldownpour = 0, min,max; is all that's needed.
Then..
1
2
3
4
5
6
do {
	cout << "Enter minimum temperature for day " << i + 1 << ". (-70 to 70)" << endl;
	cin >> min;
	if (min > 70)
		 min = 0;
mintemp+=min;

and
1
2
3
4
5
6
7
cout << "Enter max temperature for day " << i + 1 << "." << " (" << min << " to 70)" << endl;
	cin >> max;
	if  (max < min)
		max = 0; // or have max equal min instead
	else if (max > 70)
		max = 70; // makes more sense if max would equal the highest possible id higher amount entered
maxtemp+=max;


And just add up the total rain each time. At the end, divide the totals by the nrofdays, on each one, to get your answers.

Hope this helps you.


Last edited on
Sorry for the late answer, this did help me in the end and i got the program to work.
Topic archived. No new replies allowed.