• 0

c++ help


Question

hi, my teacher told us to create a program that would ask for 3 numbers and put them in order from least to greatest.. i am having trouble doing the least to greatest part. please help asap. here is where im up to..

#include <iostream.h>

int main()

{

int a, b, c;

cout<<"what is the first number:";

cin>>a;

cout<<"what is the 2nd number:";

cin>>b;

cout<<"what is the 3rd number:";

cin>>c;

cout<<"You have entered:"<<a<<b<<c<<endl;

if ((a<=c) || (c>=b))

cout<<"the smallest number is:"<<a;

cout<<endl;

if ((a<=b) || (b<=c))

cout<<"the middle number is:"<<b;

cout<<endl;

if ((b<=c) || (c<=a))

cout<<"the largest number is:"<<c;

cout<<endl;

}

Link to comment
Share on other sites

9 answers to this question

Recommended Posts

  • 0

If input is 1, 2, 3

what is the first number:1
what is the 2nd number:2
what is the 3rd number:3
You have entered:123
the smallest number is:1
the middle number is:2
the largest number is:3
Press any key to continue

if input is 3, 2, 1

what is the first number:3
what is the 2nd number:2
what is the 3rd number:1
You have entered:321


the largest number is:1
Press any key to continue

Link to comment
Share on other sites

  • 0

you have logic errors no matter if your if() tests become true or not

they will always display smallest = a

middle = b

etc...

if (a &lt;= b &amp;&amp; a &lt;= c)
 ?cout &lt;&lt; "The smallest number is " &lt;&lt; a &lt;&lt; endl;
	else if (b &lt;= a &amp;&amp; b &lt;= c)
 ?cout &lt;&lt; "The smallest number is " &lt;&lt; a &lt;&lt; endl;
	else if (c &lt;= b &amp;&amp; c &lt;= a)
 ?cout &lt;&lt; "The smallest number is " &lt;&lt; a &lt;&lt; endl;

that will tell you the smallest

gives you a chance to figure the rest for yourself

Link to comment
Share on other sites

  • 0
you have logic errors no matter if your if() tests become true or not

they will always display smallest = a

middle = b

etc...

if (a &lt;= b &amp;&amp; a &lt;= c)
 ?cout &lt;&lt; "The smallest number is " &lt;&lt; a &lt;&lt; endl;
	else if (b &lt;= a &amp;&amp; b &lt;= c)
 ?cout &lt;&lt; "The smallest number is " &lt;&lt; a &lt;&lt; endl;
	else if (c &lt;= b &amp;&amp; c &lt;= a)
 ?cout &lt;&lt; "The smallest number is " &lt;&lt; a &lt;&lt; endl;

that will tell you the smallest

gives you a chance to figure the rest for yourself

um.......your code does not seem to work very well. because if i put in 2,3,1 its different and also 3,2,1 or 2,1,3

Link to comment
Share on other sites

  • 0

// Sort a, b and c
if (a &gt; b) {
  // a &amp; b are not in the good order; exchange the two numbers
  int temp = a;
  a = b;
  b = temp;
}
if (b &gt; c) {
  // b &amp; c are not in the good order; exchange the two numbers
  int temp = b;
  b = c;
  c = temp;
}
if (a &gt; b) {
  // a &amp; b are not in the good order; exchange the two numbers
  int temp = a;
  a = b;
  b = temp;
}

// here, a, b and c are in order... just print them

http://en.wikipedia.org/wiki/Bubble_sort

Edited by Mouton
Link to comment
Share on other sites

  • 0
um.......your code does not seem to work very well. because if i put in 2,3,1 its different and also 3,2,1 or 2,1,3

yes sorry i forgot the change the cout statements to

CODE ?
if (a &lt;= b &amp;&amp; a &lt;= c)
 cout &lt;&lt; "The smallest number is " &lt;&lt; a &lt;&lt; endl;
else if (b &lt;= a &amp;&amp; b &lt;= c)
 cout &lt;&lt; "The smallest number is " &lt;&lt; b &lt;&lt; endl;
else if (c &lt;= b &amp;&amp; c &lt;= a)
 cout &lt;&lt; "The smallest number is " &lt;&lt; c &lt;&lt; endl;

Link to comment
Share on other sites

  • 0

this is for you, hngmin12

I think this is the simplest way to complete

you can use either by multiple if statements or implementing a temp variable to sort them out

int main()
{
      int a, b, c;     
      cout &lt;&lt; "what is the first number:";
      cin &gt;&gt; a;
      cout &lt;&lt; "what is the second number:";
      cin &gt;&gt; b;
      cout &lt;&lt; "what is the third number:";
      cin &gt;&gt; c;
      cout &lt;&lt; "You have entered: " &lt;&lt; a &lt;&lt; " " &lt;&lt; b &lt;&lt; " " &lt;&lt; c &lt;&lt; endl;

      //if statements to determine smallest number
      if (a &lt;= b &amp;&amp; a &lt;= c)
       cout &lt;&lt; "The smallest number is " &lt;&lt; a &lt;&lt; endl;
      else if (b &lt;= a &amp;&amp; b &lt;= c)
       cout &lt;&lt; "The smallest number is " &lt;&lt; b &lt;&lt; endl;
      else if (c &lt;= b &amp;&amp; c &lt;= a)
       cout &lt;&lt; "The smallest number is " &lt;&lt; c &lt;&lt; endl;

      //if statements to determine middle number
      if (((a &gt;= b) &amp;&amp; (a &lt;= c)) || ((a &gt;= c) &amp;&amp; (a &lt;= b)))
       cout &lt;&lt; "The middle number is " &lt;&lt; a &lt;&lt; endl;
      if (((b &gt;= a) &amp;&amp; (b &lt;= c)) || ((b &gt;= c) &amp;&amp; (b &lt;= a)))
            cout &lt;&lt; "The middle number is " &lt;&lt; b &lt;&lt; endl;
      if (((c &gt;= a) &amp;&amp; (c &lt;= b)) || ((c &gt;= b) &amp;&amp; (c &lt;= a)))
       cout &lt;&lt; "The middle number is " &lt;&lt; c &lt;&lt; endl;

      //if statements to determine largest number
      if (a &gt;= b &amp;&amp; a &gt;= c)
       cout &lt;&lt; "The largest number is " &lt;&lt; a &lt;&lt; endl;
      else if (b &gt;= a &amp;&amp; b &gt;= c)
       cout &lt;&lt; "The largest number is " &lt;&lt; b &lt;&lt; endl;
      else if (c &gt;= b &amp;&amp; c &gt;= a)
       cout &lt;&lt; "The largest number is " &lt;&lt; c &lt;&lt; endl;



      return 0;
}

or

Link to comment
Share on other sites

  • 0
true but wouldnt a bubble sort be a little advanced for hngmin12 at this time

I suggested 3 if statements, and swapping 2 variables. very basic stuff.

Takes about 1 min to understand even if u never saw it. It uses the same way your would sort 3 numbers if u had them on a table before u.

U suggest 9 if statements, with multiple && and ||.....

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.