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

This is a discussion on Sorting a set of numbers based on what the user inputs? within the C Programming forums, part of the General Programming Boards category; I've looked around for some help on this but I can only find how to sort for a specific array ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    7

    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.
    Last edited by vmckoon2; 04-28-2010 at 10:14 PM. Reason: Bad syntax

  2. #2
    Registered User
    Join Date
    Apr 2010
    Posts
    7
    Sorry for the bad syntax, I thought I entered it in correctly, I'm trying to fix it.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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. #4
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    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. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,302
    msh, please do not do others' homework for them. You just helped someone cheat!

  6. #6
    Registered User
    Join Date
    Apr 2010
    Posts
    7
    He's not helping me cheat, he was showing me a simpler solution. I clearly did the work myself, the codes up there...

  7. #7
    Registered User
    Join Date
    Apr 2010
    Posts
    7
    And thanks to everyone that helped me, I appreciate it!

  8. #8
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Quote Originally Posted by iMalc View Post
    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. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by msh View Post
    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. #10
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    You've made your point.

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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.
    Last edited by Adak; 04-29-2010 at 04:36 AM.

  12. #12
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by Adak View Post
    @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. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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. #14
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by Adak View Post
    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. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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.

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting numbers in descending order
    By Yunasnk in forum C++ Programming
    Replies: 2
    Last Post: 11-23-2003, 04:55 PM
  2. Sorting Random Numbers
    By kid kash in forum C++ Programming
    Replies: 4
    Last Post: 12-07-2002, 03:47 AM
  3. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  4. Replies: 6
    Last Post: 04-12-2002, 08:33 AM
  5. Action Based On User Input
    By Stealth in forum C++ Programming
    Replies: 2
    Last Post: 10-03-2001, 05:38 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21