Thread: Sorting a set of numbers based on what the user inputs?

1. Sorting a set of numbers based on what the user inputs?

I've looked around for some help on this but I can only find how to sort for a specific array size. The directions are as follows:

Write a program that finds the largest in a series of numbers entered by the user. The program must prompt the user to enter numbers one by one. When the user enters a 0 or a negative number, the program must display the largest nonnegative number entered.

Here is what I have so far:
Code:
```int main(int argc, char *argv[])
{
/*This program will find the largest number in a series of positive numbers */
/*entered one number at a time. Enter a 0 or negative number to end the loop*/

int n, temp;
int counter = 1;
int largest = 0;
printf("Enter a number:");
scanf("%d", &n);
while(n > 0)
{
printf("Enter a number:");
scanf("%d", &n);
}
while(n <= 0)
{
while(counter <= n)
{
if(n > largest)
{
temp = 0;
temp = largest;
largest = n;
}
else if(n < largest);
{
temp = 0;
temp = largest;
largest = largest;
}

counter++;

printf("The largerst number is: %d", &largest);
}
}
system("PAUSE");
return 0;
}```
I am using bloodshed if that makes any difference, please let me know what I need to change, thanks.

2. Sorry for the bad syntax, I thought I entered it in correctly, I'm trying to fix it.

3. You don't need that second while loop, at all. put the test for largest, right below the user's input, up above.

You don't need to set temp to zero. You don't need to set largest = largest.

No else portion is needed. Just if( n > largest) largest = n;

4. Your solution is correct, and complicated. Here's a much simpler solution.
Code:
```#include <stdio.h>

int main(void) {
int n, big;
big = 0;

// Continue accepting user input
// while scanf() successfully assigns user input
// to variable "n" AND while this input is greater then 0.
while ( scanf("%d", &n) == 1 && n > 0) {

// Is the current input greater then standing maximum?
if (n > big) big = n;
}

printf("Largest number entered: %d\n", big);

return 0;
}```

5. msh, please do not do others' homework for them. You just helped someone cheat!

6. He's not helping me cheat, he was showing me a simpler solution. I clearly did the work myself, the codes up there...

7. And thanks to everyone that helped me, I appreciate it!

8. Originally Posted by iMalc
msh, please do not do others' homework for them. You just helped someone cheat!
I see this as teaching by example. He has clearly put some work into this, and I do not see the harm of showing a good example of doing it differently.

I understand why you would object to this, but, in this case, I respectfully disagree. This is hardly a "just give me the codez"-type topic, and it will be a sad day when I can't post some code as an answer to a question out of fear that someone could use it in their homework.

9. Originally Posted by msh
I see this as teaching by example. He has clearly put some work into this, and I do not see the harm of showing a good example of doing it differently.

I understand why you would object to this, but, in this case, I respectfully disagree. This is hardly a "just give me the codez"-type topic, and it will be a sad day when I can't post some code as an answer to a question out of fear that someone could use it in their homework.
His code did NOT work correctly. First make sure he has a valid answer. Once he has, you can propose a better answer, even though I still wouldn't write it for them but rather tell them what to change.
See, now he still won't learn about what he did have wrong in his code, so you're not doing him a favour either. For his next homework, he still won't understand the problem, will make the same mistakes again and, then, the problem will be harder to solve.

10. You've made your point.

11. Msh, I'm with you on this point.

We do a lot of tutoring/teaching for students and learners, and somehow, someway, the joy of just coding something, gets yanked out of us, and in it's place, we get stuffed with a bunch of "here's the right way to tutor/teach so the student can't cheat".

If a student wants to cheat, and they're on the net, -- hey, they can cheat. And it's NOT our duty to play plagerism monitor/policeman.

Students DO learn by examples (even by bad one's, since they fail). So while I do want to see the OP put up some code to show they're trying, I see nothing wrong with throwing out a bit of code, either.

In this case, I WAS trying to "lure" the OP into re-doing their own code, and THEN refining it for him, if needed, but there was no way Msh would know that, being new and not "reading between the lines", yet.

So good on ya, Msh. Just know that when you see us posting up descriptions or pseudo code for a poster, what we're hoping for is that they will post back with some better code, first.

That way we don't feel guilty about any plagerism, and the forum doesn't become "homework central", either.

And I have to add, I've "spoon fed" many posters, because I thought their problem was interesting. It doesn't bother me to be a relatively easy conduit for programming info. In fact, I rather like it, and I can tell you do, as well.

So good on us, both.

@EVOEx:

You're claiming that you know what the OP will learn - which you do not know.
You're claiming that you know what the OP will NOT learn - which again, you do not know.

What you are claiming are your own opinions, and they should not be posted as facts. They are based on nothing more, in this instance case, than your own pre-conceptions.

12. Originally Posted by Adak
@EVOEx:

You're claiming that you know what the OP will learn - which you do not know.
You're claiming that you know what the OP will NOT learn - which again, you do not know.

What you are claiming are your own opinions, and they should not be posted as facts. They are based on nothing more, in this instance case, than your own pre-conceptions.
I'm not claiming the OP will learn. Well, he's trying to learn C, obviously, otherwise he wouldn't here, whether it'd be homework or not. I expect it's homework, but it could just be a personal attempt to learn C as well, but the same will still hold.
No, the OP will per definition NOT learn if he is provided with an answer. Maybe he will understand the answer, but he failed the necessary step of practising the process of thinking of his own answer. Even if he removes the example and re-writes it, it will be with the provided answer in the back of his mind. The global solution is likely to remain the same.
And in the next practice he will get, he will have less stuff to practice before, making it harder for him to proceed.

It is a fact that being provided with the answer will go at the cost of your own practice. How much practice I can't say. Maybe it's negligible, but it might not be.

The only non-fact that I provided as a fact was the fairly safe and likely assumption this was homework. But it wasn't important for the rest of the post anyway.

13. No, the OP will per definition NOT learn if he is provided with an answer. Maybe he will understand the answer, but he failed the necessary step of practising the process of thinking of his own answer. Even if he removes the example and re-writes it, it will be with the provided answer in the back of his mind. The global solution is likely to remain the same.
And in the next practice he will get, he will have less stuff to practice before, making it harder for him to proceed.
You couldn't possibly BE more wrong. Of course humans learn by example!

Consider yourself - were you shown LOTS of examples of proper programming, while you were learning C? Of course you were! Whether you learned by examples on a chalk board, in a book, or on the net, it makes no difference - WE LEARN BY EXAMPLES. (That's why good examples in our lives, are so important.)

We also learn by doing, and practice is important. Your comments here are simply so far away from the obvious truth you can see in the world around us, it's amazing.

The unvarnished fact here, is that you know almost nothing about the OP.

14. Originally Posted by Adak
You couldn't possibly BE more wrong. Of course humans learn by example!

Consider yourself - were you shown LOTS of examples of proper programming, while you were learning C? Of course you were! Whether you learned by examples on a chalk board, in a book, or on the net, it makes no difference - WE LEARN BY EXAMPLES. (That's why good examples in our lives, are so important.)

We also learn by doing, and practice is important. Your comments here are simply so far away from the obvious truth you can see in the world around us, it's amazing.

The unvarnished fact here, is that you know almost nothing about the OP.
Programming is learned by reviewing examples and then practising the process of translating requirements into a programming language. You can't learn to code with just examples (well, maybe there are some exceptional people that can), and you can't learn to code without examples either.
In whatever way he is learning C, be it school, a book, or even self-taught, it seemed that now was the point he needed practice, and he would have to have seen enough examples to finish the assignment.
Here's the key point: If the resource he's learning from is remotely adequate, he would have to have seen enough examples to finish the assignment. Now, it was time to practice, to make sure he understood the examples properly.
He failed coding the assignment, so obviously, he did not properly understand the examples. The best way to learn from there is to review everything you've been taught before, review the examples he's been shown before.

But now, he has received yet another example. Will he properly understand this example? The previous examples (assuming there have been, otherwise the learning resource sucks) didn't do the trick. What are the odds this example does?

15. You have no idea what the OP has been exposed to in their programming education. You have no idea what environment he had, at that time. You have no idea what his educational foundation is, or it's history. You have no idea of what he did last week, will do today, or will do in the future, in programming.

You don't know. You don't have any idea of these facts. Your pontifications can not be made true, no matter how elegantly you rationalize them. You have no facts in this case, to base them upon.

It is a fact that being provided with the answer will go at the cost of your own practice. How much practice I can't say. Maybe it's negligible, but it might not be.
Is that why so many programming books have answers to their problems or exercises, included in the book?

If a New Caledonian Crow can learn to use a tool by example, I am supremely confident that our forum members, can learn by example, as well.

Popular pages Recent additions