Hi y'all!
I need to sort an input of integers, long as we wish (not defined, depending on the user), which ends up by -1, and print it to the screen sorted. I can't think of anything that is simple enough to use (a beginners level, all we learned in the 4 lectures that we had was to the loops level, nothing too complicated). I know how to do it with arrays, or lists etc., but not without it. Any suggestion?
Thanks

If you have variables a, b and c it would be not too difficult with some if statements. But the more variables you have the greater difficulty to sort them without an array or linked list.

>Any suggestion?
I'd suggest that you confirm the need to do this without any kind of list. This sounds more like make-work with unreasonable restrictions than a beginner's project. Since you claim that it's a beginner's project, it's more likely that you just didn't fully understand the requirements and have made it more complicated than it needs to be.

Hello again!
I just wanted you to know that I solved it, I forgot a tiny detail that makes all the difference: I have to sort numbers from 0 to 9. That means that I can sum the times a number appears and print it from the lowest to the highest, according to the number of times it appeared. It actually has a name, called BUCKET SORT.
Hope that it will be useful anyway.
Cheers.

Hello again!
I just wanted you to know that I solved it, I forgot a tiny detail that makes all the difference: I have to sort numbers from 0 to 9. That means that I can sum the times a number appears and print it from the lowest to the highest, according to the number of times it appeared. It actually has a name, called BUCKET SORT.
Hope that it will be useful anyway.
Cheers.

Well, yet another triumph of the most fundamental programming principle: only even number of errors can produce correct result...
It's interesting:
1. Why you have decided that your task specifications have forbidden you array using?
2. Can you make a bucket sort without arrays, lists, trees and other data structures?

Well, yet another triumph of the most fundamental programming principle: only even number of errors can produce correct result...
It's interesting:
1. Why you have decided that your task specifications have forbidden you array using?
2. Can you make a bucket sort without arrays, lists, trees and other data structures?

Well, first, I didn't decide so, my tutor did :-)
second, you CAN use bucket sort. I guess that's the name of it... You know what your variables are, you just don't know how many times the user will print them, if any.
I have 10 variables, names zero_counter, one_counter,..., nine_counter, all initialized to zero, every time the user prints a legal number in that range, the compatible counter increases. You already know the order between them, all that's left to do is to print zeros the number of times needed, according to the zero_counter value (if zero_counter equals 0, you don't need to print zeros at all, obviously...), continue doing that to the one_counter, etc.
It's like you Do have a data structure of some kind, just not as one unified unit (not an array, a list etc.).
Still wanna prove me wrong? :-)

this will end well.

:|

>Still wanna prove me wrong? :-)
You're still using an array, just without the convenience of array notation or the convenience of semantic guarantees that an array provides. Given a strict definition of "no arrays", your solution is invalid.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.