• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

programming question

Page 2 - Seeking answers? Join the AnandTech community: where nearly half-a-million members share solutions and discuss the latest tech.
Originally posted by: DaveSimmons
yep, that should work fine, though I like my way better -- only 4 "if" and 1 "while" 🙂

yours is the next-best way to do it, and reducing the unsorted set size by one each time was a good approach (done any recursion before?)


mine works with 3 'if' and a while, unless that was a typo


edit: Text

I don't acutally have to hand this in, just do an algorithm for it.
 
Originally posted by: trek8900
Originally posted by: DaveSimmons
yep, that should work fine, though I like my way better -- only 4 "if" and 1 "while" 🙂

yours is the next-best way to do it, and reducing the unsorted set size by one each time was a good approach (done any recursion before?)


mine works with 3 'if' and a while, unless that was a typo


edit: Text

I don't acutally have to hand this in, just do an algorithm for it.
yes, that's how I was doing it -- I didn't bother to actually put it on paper so I was thinking N compares per iteration instead of N-1.

Then there are 3 ways to control the iteration:
(a) fixed loop like you did (then "for" might make more sense than "while")
(b) flag variable that gets cleared before the compares, set if any swaps took place
(c) while test use a test for sorted, i.e. while ( not (w1 > w2 && w2 > w3 && w3 > w4 ) )

This was actually a fun little problem to do without arrays 🙂
 
hmm, are you comparing strings or numbers ?

Comparing/sorting string might be a little harder. (in string 9>10)

and I didn't know C/c++ have native overloaded operators to compare strings (string1 > string2). CString is nice but that is MFC and still leaves problem of (9 > 10).
 
"sorting 4 words in alphabetical order "

So no "9", "10" sorting issues. And Pascal does have built-in string compares, no strcmp() needed.
 
oh ok, didn't realize this was pascal problem 🙂, thanks.

word is just a smaller string isn't it ? You have to compare each character of the word unless the built in string compare takes care of things.
 
pascal compare works like strcmp() so for "alphabetical order" you can use "if (a < b)" just like you'd use "if (strcmp(a,b) < 0)" in c++.
 
Originally posted by: trek8900
so now it turns out im not allowed any loops at all.... oh well
That's annoying -- but at least you can revert back to the version before, it was as efficient as you can get without loops.

I guess you'll have to start tracking exactly which tiny subset of Pascal it's OK to use, and eagerly await the arrival of loops and arrays like they were packages from Newegg.
 
Back
Top