942,788 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 4910
  • C++ RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Nov 17th, 2009
0
Re: print out number in ascending order in c++ WITHOUT USING ARRAY AND FUNCTION
is it something like this exist..??
C++ Syntax (Toggle Plain Text)
  1. if ( a < b && a < c && a < d && a < e)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
din_hilmi is offline Offline
10 posts
since Nov 2009
Nov 17th, 2009
0
Re: print out number in ascending order in c++ WITHOUT USING ARRAY AND FUNCTION
is it possible to use 'switch' or 'for' statement in this problem..??
Reputation Points: 10
Solved Threads: 0
Newbie Poster
din_hilmi is offline Offline
10 posts
since Nov 2009
Nov 17th, 2009
0
Re: print out number in ascending order in c++ WITHOUT USING ARRAY AND FUNCTION
It should be more or less like this :

C++ Syntax (Toggle Plain Text)
  1. if (a<b && b<c && c<d && d<e)
  2. cout<<a<<b<<c<<d<<e;
Nope...u cannot compare or inserting the range when using the switch..
Reputation Points: 6
Solved Threads: 1
Posting Whiz in Training
samsons17 is offline Offline
225 posts
since Oct 2009
Nov 17th, 2009
2
Re: print out number in ascending order in c++ WITHOUT USING ARRAY AND FUNCTION
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?
Featured Poster
Reputation Points: 2606
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,363 posts
since Jan 2008
Nov 18th, 2009
0
Re: print out number in ascending order in c++ WITHOUT USING ARRAY AND FUNCTION
quite true vernon.. any idea to make it more simpler..?? im not allowed to use array and function to settle this assignment.. uhuh.. >.<
Reputation Points: 10
Solved Threads: 0
Newbie Poster
din_hilmi is offline Offline
10 posts
since Nov 2009
Nov 18th, 2009
0
Re: print out number in ascending order in c++ WITHOUT USING ARRAY AND FUNCTION
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.
Reputation Points: 840
Solved Threads: 593
Senior Poster
firstPerson is offline Offline
3,858 posts
since Dec 2008
Nov 18th, 2009
-1
Re: print out number in ascending order in c++ WITHOUT USING ARRAY AND FUNCTION
I didnt get it 1stperson.. mind to show me..??
Reputation Points: 10
Solved Threads: 0
Newbie Poster
din_hilmi is offline Offline
10 posts
since Nov 2009
Nov 18th, 2009
0
Re: print out number in ascending order in c++ WITHOUT USING ARRAY AND FUNCTION
Click to Expand / Collapse  Quote originally posted by din_hilmi ...
I didnt get it 1stperson.. mind to show me..??
In psuedocode of course :

C++ Syntax (Toggle Plain Text)
  1. void insertionSort(int * Array, int size)
  2. {
  3.  
  4. //declare minElem variable to 0
  5.  
  6. //for i = 0 to i < size, increment i by 1
  7. {
  8. //set minElem to i
  9. //for j = i +1 to j < size; increment j by 1
  10. {
  11. //if array[j] is less than array[minElem]
  12. //then set minElem to j
  13. }
  14. //now swap array at i with array at minElem
  15. }
  16. }
Reputation Points: 840
Solved Threads: 593
Senior Poster
firstPerson is offline Offline
3,858 posts
since Dec 2008
Nov 18th, 2009
1
Re: print out number in ascending order in c++ WITHOUT USING ARRAY AND FUNCTION
Click to Expand / Collapse  Quote originally posted by din_hilmi ...
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.
Featured Poster
Reputation Points: 2606
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,363 posts
since Jan 2008
Nov 18th, 2009
0
Re: print out number in ascending order in c++ WITHOUT USING ARRAY AND FUNCTION
Click to Expand / Collapse  Quote originally posted by din_hilmi ...
quite true vernon.. any idea to make it more simpler..?? im not allowed to use array and function to settle this assignment.. uhuh.. >.<
Click to Expand / Collapse  Quote originally posted by din_hilmi ...
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.
Featured Poster
Reputation Points: 2606
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,363 posts
since Jan 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: NIM Game
Next Thread in C++ Forum Timeline: not sure why i am outputting all zeros





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC