tags:

views:

168

answers:

4

I have tried to write my program, but it doesnt come out the way I want it, can some one please revise this source code fully to show how the corrections fit in. Heres the problem: Write a program that sorts three integers. The integers are entered from the console and stored in variables numi, num2, num3, respectively. The program sorts the numbers so that num 1 equal to or less than num 2 equal to or less than num 3.

Here is what Ive done so far. Thnx for help and support.

#include <iostream>

using namespace std;
int main()

 if (n1 > n2)
  {

    int temp = n1;
    n1 = n2;
    n2 = temp;
  }

  if (n2 > n3)
  {

    int temp = n2;
    n2 = n3;
    n3 = temp;
  } 

  if (n1 > n2)
  {

    int temp = n1;
    n1 = n2;
    n2 = temp;
  }
{
    cout<<"Enter an integer: ";
    int int1;
    cin>>int1;
    cout<<"Enter another integer: ";
    int int2;
    cin>>int2;
    cout<<"Enter a last integer: ";
    int int3;
    cin>>int3;

}
+5  A: 

You have some serious problems with the code. First and foremost, you're attempting to do the sort before you're even reading the numbers in. You should refactor the sorting code to a function and call that function AFTER you've read the numbers, else you wouldn't know what numbers to sort.

As for the sorting itself, you'd also want to refactor the swap to its own method, so that the logic of the sorting itself becomes clear and more readable.

These are but a few of the mistakes in your code.


Pseudocode

Here's a solution in pseudocode; you should know enough C++ to be able to translate this. If you don't, then it's time to pick up a book/online tutorial and learn. This is really nothing but refactoring of your own code.

ref denotes a pass-by-reference semantics.

PROCEDURE swap(ref x, ref y)
# swaps two numbers
   DECLARE temp = x
   x = y
   y = temp
END

PROCEDURE sort2(ref x, ref y)
# sorts 2 numbers
   IF (x > y)
      SWAP(x, y)
END      

PROCEDURE sort3(ref a, ref b, ref c)
# sorts 3 numbers
   sort2(a, b)
   sort2(b, c)
   sort2(a, b)
END

PROGRAM main
   DECLARE x, y, z

   PROMPT "Enter value for x"
   READ x
   PROMPT "Enter value for y"
   READ y
   PROMPT "Enter value for z"
   READ z

   sort3(x, y, z)
   PROMPT "Sorted values are ", x, y, z
END

The algorithm

It is worth noting that the sorting algorithm here is essentially bubble sort. It's too slow for sorting a lot of numbers, but it's arguably the best for sorting only 3 values. It's also a beginner-friendly sorting algorithm.

polygenelubricants
Ahem... there is a missing { bracket at the beginning of main, n1, n2 and n3 are undeclared, int1..int3 aren't participating in sort, main doesn't return value...
SigTerm
@SigTerm: `main` doesn't have to return a value explicitly. And the `{` isn't missing, it just went for a walk.
Mike Seymour
@SigTerm: I'm trying to write more about the big things more than the little things. Mastering syntax is easy (though obviously OP has yet to do that); learning how to write good, readable, maintainable programs is the hard part.
polygenelubricants
@polygenelubricants: I think that before writing a novel (i.e. program), people should first learn alphabet (syntax). Still, I upvoted your answer because of pseudocode - at least this way dude will (be forced to) learn something. However, I'm not sure if it was worth the effort. I don't remember ever being completely clueless during learning (even basics - it was fun to learn new stuff and discover new things, and get to understand how it works), so I'm not sure if a person that gets lost with syntax is cut out to be programmer. Still, only time will tell. SigTerm out.
SigTerm
A: 

Two problems there:

  1. You are testing n1, n2 etc before you have them
  2. You never print out any results.
Kate Gregory
A: 

Your algorithm seems right. The only problem is that you have the beginning (inputting the numbers) at the end, and you don't output the results.

James Curran
A: 
#include <iostream> 
using namespace std;
void main(int argc, char* argv[])
{
    cout<<"Enter an integer: "; 
    int n1; 
    cin>>n1; 
    cout<<"Enter another integer: "; 
    int n2; 
    cin>>n2; 
    cout<<"Enter a last integer: "; 
    int n3; 
    cin>>n3;
    if (n1 > n2) 
    { 

        int temp = n1; 
        n1 = n2; 
        n2 = temp; 
    } 

    if (n2 > n3) 
    { 

        int temp = n2; 
        n2 = n3; 
        n3 = temp; 
    }  

    if (n1 > n2) 
    { 

        int temp = n1; 
        n1 = n2; 
        n2 = temp; 
    }
    cout << n1 << endl;
    cout << n2 << endl;
    cout << n3 << endl;
}

Enter an integer: 2
Enter another integer: 3
Enter a last integer: 1
1
2
3
Press any key to continue . . .
Gabriel
Don't give away homework answers.
GMan