1) Why ?
2) need compile time variable meaning there will be a fixed amount
of inputs to get
3) Need to implement your own sorting method and use it inside main,
so technically it won't be a function. If not then you will need a lot
of if/else or you can use trees.
4) Bad Idea.
firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608
Yes, your if/else is incorrect, only 1 of them gets evaluated.
firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608
Five numbers can be ordered 5! or 120 ways. The no-brainer way of writing this code would thus be writing an if statement with 119 "else if" statements, one for each possible ordering. You should be using <= instead of < (what if two elements are the same?). There may be some clever nested-if ways of doing this that don't require 120 if statements of some sort. I suppose that might be interesting to have a contest where you had to sort 5 numbers without using arrays or functions and people might come up with creative answers.
But the larger question is this: why are you adding these stipulations? There's a reason why all the sorting algorithms out there use arrays and/or helper functions/recursion. Why would anyone want to write 120 if-statements?
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
well since since there are 5! possible combination, the minimum
number of if stated you will use is 2^5 = 32.
That will suck.
Just use insertion sort, the idea behind it is to mid a max* element
and swap it with the last element. Then repeat it with the last element
decreasing by 1 each time.
firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608
I didnt get it 1stperson.. mind to show me..??
In psuedocode of course :
void insertionSort(int * Array, int size)
{
//declare minElem variable to 0
//for i = 0 to i < size, increment i by 1
{
//set minElem to i
//for j = i +1 to j < size; increment j by 1
{
//if array[j] is less than array[minElem]
//then set minElem to j
}
//now swap array at i with array at minElem
}
}
firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608
quite true vernon.. any idea to make it more simpler..?? im not allowed to use array and function to settle this assignment.. uhuh.. >.<
"Simple", "efficient", "correct", and "not tedious" are different concepts. Your "easiest", most brain-dead method is to make 120 "if" statements, not taking advantage of the results of previous tests. The CORRECT way is to use an array, function, or whatever is needed. Back to my original question. WHY are you limited with this restriction? What's your experience level? One can always avoid arrays by using pointer arithmetic in lieu of the [] operator, which is basically the same thing, so what's the point? Sounds like one of those classes where they require you to do it the WRONG way so you'll appreciate the RIGHT way when they get to teaching it. If that's the case, just write your 120 if-statements and get it over with, I guess.
By the way, have you written it for sorting two, three, and four numbers? That comes before five. Get the pattern down.
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
quite true vernon.. any idea to make it more simpler..?? im not allowed to use array and function to settle this assignment.. uhuh.. >.<
I didnt get it 1stperson.. mind to show me..??
I'm looking at the post times. This one comes two posts after firstPerson's post. Quite fast. I wouldn't "get it" either if I had to learn the Insertion Sort in two minutes. Since it uses an array, you can't use it anyway. No one's going to give you the code without some effort on your part.
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711