Now into Arrays

Ok... so I've got a new assignment and this was the question:
"Write a programs that stores 20 numbers, each of which is between 10 and 100, inclusive. As each number is read, check that the number is within the range and store it in the array. After storing all the values, display the average of the 20 values."

this is my code so almost finish code:
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
#include <iostream.h>
int main()
{
int array[20];
int i=0;
int average;
int sum;

cout << "Enter 20 numbers which are between 10 and 100\n";
while(i<20)
{
cin >> array[i];
if(array[i] >= 10 && array[i] <= 100)
{
i++;
}
else
{
cout << "Number entered is wrong. Please try again.\n";
}
if(array[i] >= 10 && array[i] <= 100)
{
sum=array[i]+array[i];
average = sum/20;
i++;
cout << "average is:" << average;
}
}
return 0;
}


My problem here is that it does not display the average....although it already stores the values..
You've got an if statement trying to act like a for loop. It doesn't work. You have to actually iterate through the array,
for (int i = 0; i < 20; ++i) {
add all of the values to a central value,
sum += array[i];
and then average them and output them.
std::cout << "Average: " << sum/20 << std::endl;

EDIT:

Read my code, understand it, and then apply it to your program. If you just copy and paste it, you're going to have more problems.
Last edited on
I tried what you said
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
#include <iostream.h>
int main()
{
int array[20];
int i=0;
int average;
int sum;

cout << "Enter 20 numbers which are between 10 and 100\n";
while(i<20)
{
cin >> array[i];
if(array[i] >= 10 && array[i] <= 100)
{
i++;
}
else
{
cout << "Number entered is wrong. Please try again.\n";
}
for (int i = 0; i < 20; ++i)
{
sum += array[i];
cout << "Average: " << sum/20 <<endl;
}
}
return 0;
}


the result was 20 negative numbers....
like:
Average: -42949672
Average: -85899345
etc..
Last edited on
Sum is never assigned a value, thus its starting value is kept (and altered through sum += ..). The starting value can be anything (often the largest negative number, for some reason).

To get proper results, set sum and average to 0 at the beginning.
I knew you were going to do that!

That's exactly why I did it that way, I told you to understand what I was doing, and then add code in that was similar to what I put in, and not to just copy and paste my code, which you obviously did.
Well, the code could look the following way

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

using namespace std;

int main()
{
   const int N = 20;
   int array[N];

   const int MIN = 10;
   const int MAX = 100;


   cout << "Enter " << N << " numbers which are between " << MIN << " and " << MAX << endl;

   int i = 0;

   while( i < N )
   {
      cin >> array[i];

      if ( array[i] < MIN || array[i] > MAX )
      {
         cout << "Number entered is wrong. Please try again.\n";
         continue;
      }

      i++;
   }

   int sum = 0;

   for ( i = 0; i < N; i++ ) sum += array[i];

   cout << "average is:" << sum / N << endl;
   
   return 0;
}


In fact sum could be calculated inside the while loop and in this case the array is redundant
-1 vlad: His assignment is to learn arrays. Despite the fact that the array is redundant, he needs to use arrays in order to learn their uses.
ciphermagi,

sometimes it is useful to see how code could be written by others which know array usage.:)
Topic archived. No new replies allowed.