Both your min and max variables are uninitialized. Set them both to the first item in the array. As for the average problem, you're printing the average inside the loop rather than after it completes. The loop should only be summing the items:
for ( int i = 0; i < 12; i++ ) {
sum += temp[i];
}
average = sum / 12;
cout<<"Average: "<< sum <<'\n';
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
gurdeep kaur, how does your post help Lenny19 with his problem? Or do you have a new question which needs to be asked in it's own thread?
Before starting your thread, read some of the posts that explain how to post your question properly, which includes The Rules.
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
>I don't understand the theory behind this step??
>Why r u setting them both to the first item in the array?
Why not? The algorithm starts at the beginning and moves to the end. The min and max variables represent the smallest and largest values encountered so far. If the only item we've encountered is the first one, doesn't it make sense that that item is both the smallest and the largest?
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>but here's the thing num[3, 4 and 8] are smaller than 10...
Yes, num[3], num[4], and num[8] are indeed smaller than 10. But that's quite irrelevant at that point because you've changed the value in min from 10 to 3. When you get to num[3], you'll replace the value from 3 to 0. And when you get to num[8] (skipping over num[4] because 0 is less than 1), you'll replace the value from 0 to -19. At the end of the loop, min will have a value of -19, which is correct.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401