### Working on arrays?

Hello, I need help trying to find the smallest and largest number in a randomized array of 25 numbers. I know that what I have put into my main function is wrong, but can't seem to find the problem. Please help. Any suggestions would be wonderful. Thanks.

 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 #include #include using namespace std; void fillArray(int ar[], int i); void printArray(const int ar[], int i); int main() { int array[25]; fillArray(array, 25) cout << "The smallest number in the array is " << printArray(array, 25) << endl; cout << "The largest number in the array is " << printArray(array, 25) << endl; return 0; } void fillArray(int ar[], int i) { int min = 0, max = 0; for(i = 0; i < 25; i++) { if(ar[i] > ar[max]) max = i; else if(ar[i] < ar[min]) min = i; } } void printArray(const int ar[], int i) { int min = 0, max = 0; for(int i = 0; i < 25; i++) { if(ar[i] > ar[max]) max = i; else if(ar[i] < ar[min]) min = i; } cout << ar[max]; cout << ar[min]; }
line 13 you are missing a ;
cout << "The smallest number in the array is " << printArray(array, 25) << endl; This is invalid because your printArray function dosn't return a value.

Line 26 if(ar[i] > ar[max]) you are checking to see if ar[i] is less than ar[max] but you haven't put anything in the array yet.

You should use more descriptive names for your parameters

use the srand() and rand() functions to create randome numbers
Last edited on
Okay, I've tweaked my code, but I keep producing compiling errors? Not sure what I'm doing wrong.

 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 #include #include using namespace std; void fillArray(int ar[], int size); void printArray(const int ar[], int size); void smallLarge(int &min, int &max); int main() { int arr[25]; fillArray(arr,25); printArray(arr,25); return 0; } void fillArray(int ar[], int size) { for(int i = 0; i <= 100; i++) { int size = rand() % 101; if(size >= 1 && size <= 100) ar[i] = size; } } void printArray(const int ar[], int size) { for(int i = 0; i < size; i++) cout << ar[i] << endl; int big; int small; smallLarge(small, big); cout << "The smallest number in the array is " << small << endl; cout << "The largest number in the array is " << big << endl; } void smallLarge(int &min, int &max) { min = 0; max = 0; for(int i = 0; i < 100; i++) { if(max > min) max = i; else if(min < max) min = i; } return; }
Topic archived. No new replies allowed.