i have to create a program that allows a user to enter 10 integers into an array and send the array to a function to figure out which number is the largest and which is smallest. these are my instructions "Exercise # 9 on Page 260. Make the array of numbers of type float. Note: The best way to determine the largest and smallest number in a list is to assume that the first number is the smallest and largest. Assign: large= num[0] and small = num[0]
Then use a for loop to move through the array, when a number is larger or smaller reassign large or small to that new number. Can anyone help me with this please, i cant figure out how to write the function.
int main()
{
const float VALUES = 10;
float intValue[VALUE];
float intNum;
int a;
double total = 0;
intValue = 0;
cout<<"You will enter 10 integers."<<endl;
cout<<"Enter value 1: ";
cin>>intValue[intNum];
while (intNum < VALUES)
{
total += intValue[intNum];
++ intNum;
if (intNum < VALUES)
{
cout<<"Enter the next value: ";
cin>>intValue[intNum];
}
}
cout<<"The values you chose are: ";
for (a=0; a< intNum; ++a)
cout<<intValue[a]<<" ";
cout<<endl;
//this is where i nee to insert the function to figure out which number is the largest and which is smallest
Ok, let me show you how it can be done. Follow along, now :-)
The instructions want you to write a function that looks at an array of ten floats. ok...
1 2
void getExtremes(float num[10]){
}
"Assign: large = num[0] and small = num[0]." ok...
1 2 3 4
void getExtremes(float num[10]){
float large = num[0];
float small = num[0];
}
"Then use a for loop to move through the array" ok...
1 2 3 4 5 6
void getExtremes(float num[10]){
float large = num[0];
float small = num[0];
for (int i = 1; i < 10; i++){
}
}
"When a number is larger or smaller reassign large or small to that new number." ok...
1 2 3 4 5 6 7 8
void getExtremes(float num[10]){
float large = num[0];
float small = num[0];
for (int i = 1; i < 10; i++){
if (num[i] > large){large = num[i];}
if (num[i] < small){small = num[i];}
}
}
Then you show the user those values. easy:
1 2 3 4 5 6 7 8 9 10
void getExtremes(float num[10]){
float large = num[0];
float small = num[0];
for(int i = 1; i < 10; i++){
if (num[i] > large){large = num[i];}
if (num[i] < small){small = num[i];}
}
std::cout << "\nThis is the largest number: " << large;
std::cout << "\nThis is the smallest number: " << small;
}