partially filled arrays

Write your question here.
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;
}>
Hello cash,

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

Your program is missing a bit of code. I can not comment on what is missing, but what is there works after I defied "input".

I changed your "srand" to srand(size_t(time(NULL)));. "size_t" is another name for "unsigned int" which is what it should be. Just saying "unsigned" leaves one guessing if it is an "unsigned char", "unsigned short", "unsigned int" or something else.

Personally I would change "Enter the length of the array: " to something like "How many numbers to work with? Maximum 100: ". It is more descriptive.

With the program the way it is there is no way to get to the second if statement because the first if statement needs to be one first. The program need some work to make use of both if statements.

Hope that helps,

Andy
Topic archived. No new replies allowed.