Add a Loop to print the output?

This is a discussion on Add a Loop to print the output? within the C Programming forums, part of the General Programming Boards category; How would I add a loop to print the output of the following code both in ascending and descending order? ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    10

    Smile Add a Loop to print the output?

    How would I add a loop to print the output of the following code both in ascending and descending order? First off, here was the Problem:

    Build a C program (not C++) that uses a single-dimension array to store 10 numbers input by a user. After inputting the numbers, the user should see a menu with two options to sort and print the 10 numbers in ascending or descending order.

    Here is my code so far:
    Code:
    #include<stdio.h>
    
    int main()
    {
      
      int arry[10], y,n,tmp,x,j,i;
      
      printf("\nEnter 10 numbers:\n");
      for(x=1;x<=10;x++)
      {
        scanf("%d",&arry[x]);
      }
      
      printf("\n[Enter your choice]:\n");
      printf("\n1. Acending order: \n2. Decending 
    
    order\n \n");
      scanf("%d",&y);
      if(y==1)
      {
        for (i=1; i<n; i++)   
        {	
          for (j=n; j>i; j--) 
          {
            if (arry[j] < arry[j-1]) 
            {
              tmp = arry[j];
              arry[j] = arry[j-1];
              arry[j-1] = tmp;
            }
          }
        }
      }
      else
      {
        for (i=1; i<n; i++)  
        {	
          for (j=n; j>i; j--) 
          {
            if (arry[j] > arry[j-1]) 
            {
              tmp = arry[j];
              arry[j] = arry[j-1];
              arry[j-1] = tmp;
            }
          }
        }
      }
      return 0;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    First, sort the array. Then, add a loop that prints it from front to back, and another back to front.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    10
    Quote Originally Posted by quzah View Post
    First, sort the array. Then, add a loop that prints it from front to back, and another back to front.


    Quzah.
    I thought that was what the code was doing here:

    Code:
    if(y==1)
      {
        for (i=1; i<n; i++)   
        {	
          for (j=n; j>i; j--) 
          {
            if (arry[j] < arry[j-1]) 
            {
              tmp = arry[j];
              arry[j] = arry[j-1];
              arry[j-1] = tmp;
            }
          }
        }
      }
      else
      {
        for (i=1; i<n; i++)  
        {	
          for (j=n; j>i; j--) 
          {
            if (arry[j] > arry[j-1]) 
            {
              tmp = arry[j];
              arry[j] = arry[j-1];
              arry[j-1] = tmp;
            }
          }
        }
      }
      return 0;
    }
    I'm completely new to programming and still trying to figure out loops and bubble sort in detail.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    That's a pretty strange belief to have. One would think, just from reading, that "First, sort the array. Then, add a loop that prints it from front to back, and another back to front." would require three sections of code, one to sort, one to print forwards, and one to print backwards; not two sections of code, one to sort forwards and one to sort backwards.

    If you know how to go through an array, how much work is it to add a print statement in the middle?

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    10
    Quote Originally Posted by tabstop View Post
    That's a pretty strange belief to have. One would think, just from reading, that "First, sort the array. Then, add a loop that prints it from front to back, and another back to front." would require three sections of code, one to sort, one to print forwards, and one to print backwards; not two sections of code, one to sort forwards and one to sort backwards.

    If you know how to go through an array, how much work is it to add a print statement in the middle?
    Yeah it may be insanely noobish and easy to someone who knows what they are doing and is a programmer, however I am not. Some of this code is like a weeks worth of searching and copying and pasting. I do know what arrays are though, I'm just not any good at loops or bubble sorting.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Do you know how an array works? How its numbering works? Do you know anything about loops? Pick one: for, do-while, while. Try to make a loop that just counts to 10. Show us your code for that.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by big-tony View Post
    Yeah it may be insanely noobish and easy to someone who knows what they are doing and is a programmer, however I am not.
    It should be easy to someone who can count to three. (I mean that recognizing that doing three things requires doing three things, not doing two things.)
    Quote Originally Posted by big-tony View Post
    Some of this code is like a weeks worth of searching and copying and pasting.
    Thought: why not write it yourself?

    That may sound like a sarcastic answer, but it isn't. Why aren't you trying to do it yourself? What is the point of trying to find the exact answer online somewhere, when the point of the class is to help you learn to think?

    Start from the beginning: write down, in words, specifically what you want to do. Once you have that, think for a little bit about how it can be done. So, if you want to print an array in reverse order, what must happen first? Then after that? Then after that?

    Once you have that done, writing the code is easy as 1-2-3.

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    10
    Quote Originally Posted by tabstop View Post
    It should be easy to someone who can count to three. (I mean that recognizing that doing three things requires doing three things, not doing two things.)

    Thought: why not write it yourself?

    That may sound like a sarcastic answer, but it isn't. Why aren't you trying to do it yourself? What is the point of trying to find the exact answer online somewhere, when the point of the class is to help you learn to think?

    Start from the beginning: write down, in words, specifically what you want to do. Once you have that, think for a little bit about how it can be done. So, if you want to print an array in reverse order, what must happen first? Then after that? Then after that?

    Once you have that done, writing the code is easy as 1-2-3.
    I already KNOW what you have to do. I just don't know how to DO it...
    I can't figure out the how to bubble sort for ascending and descending which is the only problem... Some people aren't just made to be programmers..
    I am one of them and my Major is Web Design anyway.. Sure it may have programming involved in it but I am NOT going that route and sticking to basic html for now.
    This is suppose to only be a basic introductory for programming anyway, none of my courses left even involve programming with languages like C.

    But basically I didn't come here to argue about the ethics of doing your own work.. I came here for assistance on the problem.
    Last edited by big-tony; 05-21-2009 at 10:45 PM.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by big-tony View Post
    I already KNOW what you have to do. I just don't know how to DO it...
    I can't figure out the how to bubble sort for ascending and descending which is the only problem... Some people aren't just made to be programmers.. I am one of them and my Major is Web Design anyway.. Sure it may have programming involved in it but I am NOT going that route and sticking to basic html for now. This is suppose to only be a basic introductory for programming anyway, none of my courses left even involve programming with languages like C.
    You're not going to do any PHP, then? Shame.

    It is barely possible for your first sentence to be true, I suppose. But if you've read the homework policy here, then you know that you're not going to get more out of us than you put in yourself. So tell us what you have done, and we'll get on from there.

  10. #10
    Registered User
    Join Date
    May 2009
    Posts
    10
    Quote Originally Posted by tabstop View Post
    You're not going to do any PHP, then? Shame.

    It is barely possible for your first sentence to be true, I suppose. But if you've read the homework policy here, then you know that you're not going to get more out of us than you put in yourself. So tell us what you have done, and we'll get on from there.
    Basically what I have to do is input 10 numbers into an array, print the array in the order it was provided, then have a menu with an option to sort the inputted numbers by ascending and descending order and print the results. It sounds so easy probably to you, But it isn't to me. You can't just go from not knowing jack crap about programming and be expected to learn how to program in like 2 weeks which is how long each unit is..

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by big-tony View Post
    Basically what I have to do is input 10 numbers into an array, print the array in the order it was provided, then have a menu with an option to sort the inputted numbers by ascending and descending order and print the results. It sounds so easy probably to you, But it isn't to me. You can't just go from not knowing jack crap about programming and be expected to learn how to program in like 2 weeks which is how long each unit is..
    Obviously you are in fact expected to do so. Depending on how much time you put in in a day, it's not actually that difficult, at least to get the level this assignment expects.

    Let's look at the code you're using for reading into an array:
    Code:
      for(x=1;x<=10;x++)
      {
        scanf("%d",&arry[x]);
      }
    Tell me what it does -- in specifics, not just "reads numbers into an array". It's going to have some gory detail, but that's fine, that's what we want. You say you know arrays, so this is right up your alley. In doing so, you're almost guaranteed to figure out how to do most of the rest of what you want to do, and hopefully you will also spot the error in the code I copied.

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    This is for all the rest of you readers: Don't spoil it for him. Let him explain it. It's the only way he's going to learn. If you can't put it into your own words, then you probably don't really understand it.


    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    Registered User
    Join Date
    May 2009
    Posts
    10
    Quote Originally Posted by tabstop View Post
    Obviously you are in fact expected to do so. Depending on how much time you put in in a day, it's not actually that difficult, at least to get the level this assignment expects.

    Let's look at the code you're using for reading into an array:
    Code:
      for(x=1;x<=10;x++)
      {
        scanf("%d",&arry[x]);
      }
    Tell me what it does -- in specifics, not just "reads numbers into an array". It's going to have some gory detail, but that's fine, that's what we want. You say you know arrays, so this is right up your alley. In doing so, you're almost guaranteed to figure out how to do most of the rest of what you want to do, and hopefully you will also spot the error in the code I copied.
    That IS what It's doing.. It's assigning each inputted number to each of the 10 variable slots in the array.. and if you read my previous post, you would SEE I said I only knew BASIC knowledge about arrays.. I didn't say I was an expert so there is no point in trying to make me look like a jackass that claims he knows programming and really don't because I don't.
    Last edited by big-tony; 05-21-2009 at 11:15 PM.

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    He's not trying to make you look like a jackass. He wants to know if you can spot what's wrong with it. In short, he's asking you to explain it step by step (walk through the code) to see what part you don't understand.

    By doing so, you'll end up knowing everything you need to know about arrays and a for loop.


    Quzah.
    Hope is the first step on the road to disappointment.

  15. #15
    Registered User
    Join Date
    May 2009
    Posts
    10
    Quote Originally Posted by quzah View Post
    He's not trying to make you look like a jackass. He wants to know if you can spot what's wrong with it. In short, he's asking you to explain it step by step (walk through the code) to see what part you don't understand.

    By doing so, you'll end up knowing everything you need to know about arrays and a for loop.


    Quzah.
    ah.. ok well it's nice to actually see people help people nowadays instead of just cussing them out and telling them to do their own work lol

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 17
    Last Post: 10-23-2008, 10:10 PM
  2. 2d game
    By JordanCason in forum Game Programming
    Replies: 5
    Last Post: 12-08-2007, 10:08 PM
  3. What kind of programs should I start writing?
    By Macabre in forum C++ Programming
    Replies: 23
    Last Post: 04-12-2003, 09:13 PM
  4. Loop output is not quite what I want
    By Zalbik in forum C++ Programming
    Replies: 17
    Last Post: 10-31-2002, 12:35 PM

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