partially filled arrays

Im having problems figuring how to Calculate and print the highest value, lowest, sum, and average of the partially filled array.

The partially filled array is < int cnt = 0 ;
cout << "Enter the length of the array: " ;
cin >> cnt ;
if( cnt > LENGTH ) cnt = LENGTH ;
for( int i = 0; i < cnt ; ++i )
array[i] = 1 + rand() % 100;

for( int i = 0; i < cnt ; ++i )
cout << array[i] << ' ' ;
cout << '\n' ;>
looks like you have the correct loop idea, from 0 to count

you just need to do the calculations.

sum = 0;
high = array[0];
low = array[0];

for dx = 0; dx < cnt; dx++
{
sum+= array[dx];
if(array[dx] > high) high = array[dx];
..etc for low
}
and average is just sum over count, right?


actually I'm creating a menu, and the first option prints random integers, and the second option will find the high, low, and average of those random numbers created. I created this, the first part works, but I'm having difficulties getting the high, low, and sum.

< const int LENGTH =100;
int array[LENGTH];
srand(((unsigned)time(0)));
if(input==0){
int cnt = 0 ;
cout << "Enter the length of the array: " ;
cin >> cnt ;
if( cnt > LENGTH ) cnt = LENGTH ;
for( int i = 0; i < cnt ; ++i )
array[i] = 1 + rand() % 100;

for( int i = 0; i < cnt ; ++i )
cout << array[i] << ' ' ;
cout << '\n' ;}
if (input ==1){
int sum = 0;
int average = 0;
int high = array[0];
int low = array[0];
int cnt=0;

for (int dx = 0; dx < cnt; dx++)
{
sum += array[dx];
if(array[dx] > high) high = array[dx];
if(array[dx] < low) low = array[dx];
average = sum/ cnt;
}
cout << high << low << average << endl;
}

return 0;
}

average is computed outside the loop, once -- it only works once you have found SUM. (it should be ok as-is but it is not really 'right' to do it every iteration).

otherwise it looks more or less correct. What is not working when you run it?



Last edited on
Topic archived. No new replies allowed.