how do you write a program in C++ that gives you the argest and the smallest number?

April 9, 2012
how do you write a program in C++ that gives you the argest and the smallest number?

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!

3 Comments »

  1. AnalProgrammer April 9, 2012 at 3:13 am -

    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.

  2. Jefferson Daniel April 9, 2012 at 3:52 am -

    Try this code:

    #include
    #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 cin>>a[i];
    max=maxfn(a,n);
    min=minfn(a,n);
    cout< <"The largest number is "< cout<<"The smallest number is "< } // main function

    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)
    {
    int i,m=0;
    for(i=0;i {
    if(a[i] m=a[i];
    }
    return m;
    } //minimum value function

  3. Ciaron April 9, 2012 at 4:29 am -

    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;
    }

how do you write a program in C++ that gives you the argest and the smallest number?

how do you write a program in C++ that gives you the argest and the smallest number?

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 }

What do you think? Answer below!

3 Comments »

  1. AnalProgrammer April 9, 2012 at 3:13 am -

    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.

  2. Jefferson Daniel April 9, 2012 at 3:52 am -

    Try this code:

    #include
    #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 cin>>a[i];
    max=maxfn(a,n);
    min=minfn(a,n);
    cout< <"The largest number is "< cout<<"The smallest number is "< } // main function

    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)
    {
    int i,m=0;
    for(i=0;i {
    if(a[i] m=a[i];
    }
    return m;
    } //minimum value function

  3. Ciaron April 9, 2012 at 4:29 am -

    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;
    }