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.
#include <iostream>
#include <cmath>
usingnamespace std;
void fillArray(int ar[], int i);
void printArray(constint 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;
elseif(ar[i] < ar[min])
min = i;
}
}
void printArray(constint ar[], int i)
{
int min = 0, max = 0;
for(int i = 0; i < 25; i++)
{
if(ar[i] > ar[max])
max = i;
elseif(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
#include <iostream>
#include <cmath>
usingnamespace std;
void fillArray(int ar[], int size);
void printArray(constint 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(constint 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;
elseif(min < max)
min = i;
}
return;
}