Jump to content

Comparing 4 numbers...C++

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
6 replies to this topic

#1
mindsurfer

mindsurfer

    Learning Programmer

  • Members
  • PipPipPip
  • 39 posts
hi ...
i am trying to find the smallest and the largest of four numbers.. but if,for example, i input a value like 100000000 , it shows me a result "Largest number is 1e+009".. can someone please tell me why it doesn't show "1000000000".. sorry for asking this kind of question as i am new to C++..

someone advised me to use <limits.h> but i dont know how to use that in my code... some one please help me..

# include <iostream>


using namespace std;


int main()


{

    float number1,number2,number3,number4;


    cout << "Enter the value of number 1 : ";

    cin >>number1;


    cout << "Enter the value of number 2 : ";

    cin >>number2;


    cout << "Enter the value of number 3 : ";

    cin >>number3;


    cout <<"Enter the value of number 4 : ";

    cin >>number4;


    if (number1 > number2 && number1 > number3 && number1 > number4)

        cout << "Largest number is : " <<number1 <<endl;


    else if (number2 > number3 && number2 > number4)

        cout << "Largest number is : " <<number2 <<endl;


    else if (number3 > number4)

        cout << "Largest number is : " <<number3 <<endl;


    else

        cout << "Largest number is : " <<number4 <<endl;


    if (number1 < number2 && number1 < number3 && number1 < number4)

        cout << "Smallest number is : " <<number1 <<endl;


    else if (number2 < number3 && number3 < number4)

        cout << "Smallest number is : " <<number2 <<endl;


    else if (number3 < number4)

        cout << "Smallest number is : " <<number3 <<endl;


    else

        cout << "Smallest number is : " <<number4 <<endl;


    return 0;


}

Thanks in advance..
MindSurfer

#2
G_Morgan

G_Morgan

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 537 posts
Using floats you will have to use format modifiers on the output stream to change it from standard form. 1e+009 is the equivalent of the mathematical equation 1 x 10^9. Look up formatted output.

BTW I'd have done the problem slightly differently.

float numbers[4] = {145465, 3243254, 236567, 234343658};
float largest = numbers[0];
float smallest = numbers[0];
for(int i = 1; i <= 4; i++) {
    if(numbers[i] > largest)
        largest = numbers[i];
    if(numbers[i] < smallest)
        smallest = numbers[i];
}

cout << "The largest number is " << largest << endl;
cout << "The smallest number is " << smallest << endl;

Another potential modification is doing something like

int number_terms;
cin >> number_terms;
float numbers[number_terms];
for(int i = 0; i < number_terms; i++) {
    cin >>  numbers[i];
}

This way the first argument states how many numbers you are reading.

//edit - look at this Handout - Floating point output format //

#3
G_Morgan

G_Morgan

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 537 posts
BTW, if you have any questions about anything I've posted above just ask. It can be easy to forget how annoying all this stuff was when we all started.

#4
lord_guxtavo

lord_guxtavo

    Newbie

  • Members
  • Pip
  • 6 posts
from my experience, it would be easier if you made an array that captures the 4 numbers, then use whatever method you like to put them in ascendant/descendant order, and display the top and the bottom one. You could've written much less code :)

#5
kkelly

kkelly

    Learning Programmer

  • Members
  • PipPipPip
  • 49 posts
I believe this is an exercise in insertion. The previous post is a good idea, or possibly a binary tree.

#6
mokszyk

mokszyk

    Learning Programmer

  • Members
  • PipPipPip
  • 33 posts
What about:
cout.precision(x) //where x , means number
Hmm?

#7
DevilsCharm

DevilsCharm

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 884 posts
I guess C++ automatically deals with large numbers in scientific notation? I know that Python divides numbers and uses remainders instead of decimals... kind of weird but that's how it works.