Question by : how do you write a program in C++ that gives you the argest and the smallest number?
The number of numbers entered is not limited so I know we have to use a loop but how do I compare my number entered with the next one to sort them is the problem.and im using visual studio compiler if that helps.i ll apreciate any help
thank you
Best answer:
Answer by rjay35
1 int main()
2 {
3 cout << "\nThe maximum of 'G' and 'Z' is: " << std::max( 'G', 'Z' );
4 cout << "\nThe maximum of 12 and 7 is: " << std::max( 12, 7 );
5 cout << endl;
6 return 0;
7 }
Give your answer to this question below!









Create two variables, largest and smallest.
Give largest an initial value of a small number and smallest an initial value of a large number.
Now compare each number input to those variables.
If the number is greater than largest then largest becomes this number.
If the number is less than smallest then smallest becomes this number.
Have fun.
Try this code:
#include
cin>>a[i];
cout<<"The smallest number is "<
} // main function
#include
void main()
{
int max,min,i,n;
int a[50];
cout< <"Enter the total number to be entered:";
cin>>n;
cout< <"Enter the numbers:";
for(i=0;i
max=maxfn(a,n);
min=minfn(a,n);
cout< <"The largest number is "<
int maxfn(int a[],int n)
{
{
int i,m=0;
for(i=0;i
if(a[i]>m)
m=a[i];
}
return m;
} // maximum value function
int minfn(int a[],int n)
{
m=a[i];
{
int i,m=0;
for(i=0;i
if(a[i]
}
return m;
} //minimum value function
The thing is the function for getting the largest is almost identical to the the function getting the smallest number all you need to do is change the > sign to the < sign. Still there are quite a few ways to do it, the iterative way or the recursive way. Here is the functions for getting the maximum and minimum recursively all you need to do is enter in the data and use the functions so if you have
5 elements in the array all you need to do to call the findmin function is
min=findmin(data,5,0); and that will return the minimum value
int findmin(int array[], int size, int index)
{ int result;
// This needs to be size - 1 because the array is 0-indexed.
// This is our base case
if (index == size - 1) return array[index];
//Call the function recursively on less of the array
result = findmin(array, size, index + 1);
// Return the max of (the first element we are examining, the max of the
//rest of the array)
if (array[index] < result)
return array[index];
else
return result;
}
int findmax(int array[], int size, int index)
{ int result;
// This needs to be size - 1 because the array is 0-indexed.
// This is our base case
if (index == size - 1) return array[index];
// Call the function recursively on less of the array
result = findmax(array, size, index + 1);
// Return the max of (the first element we are examining, the max of the
// rest of the array)
if (array[index] > result)
return array[index];
else
return result;
}