Thread: Finishing Off My Program

  1. #1
    Registered User
    Join Date
    Mar 2014
    Posts
    20

    Finishing Off My Program

    Hey guys, I posted here a couple times about helping me with me program. I started from scratch and I came up with the program I needed, my only problem is that I want to output each array entered before I put them in descending order. Right now the user enters each term, then it outputs them in descending order. I want the program to output each term entered and then put them in descending order. I attached my program. Could someone point me in the right direction on how to start this or simply start me on what I have to do. The program as is compiles and runs perfect. Thanks guys!
    Code:
    #include
    <stdio.h>
    int
     main()
    {
        
    int data[10],n,i,j,hold,k;
        printf(
    "Enter number of Expected Times: ");
        scanf(
    "%d",&n);
        printf(
    "Enter Expected Times: ");
        
    for(i=0;i<=n-1;++i)
        {
           scanf(
    "%d",&data[i]);
        }
        
    for(i=1;i<=n-1;++i)
        {
        
    for(j=0;j<i;++j)
           
    if(data[j]<data[i])
    	printf(
    "The Expected Times Are: ");
    	scanf(-----This is 
    where I am stuck...
           {
               hold=data[i];
               k=i;
               
    while(k!=j)
               {
                   data[k]=data[k-1];
                   --k;
               }
               data[j]=hold;
           }
        }
        printf(
    "In descending Order: ");
        
    for(i=0;i<=n-1;++i)
         {
           printf(
    "%d    ",data[i]);
        }
        
    return 0;
    }
    


  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You need to indent your code properly. For example:
    Code:
    #include <stdio.h>
    
    int main()
    {
        int data[10], n, i, j, hold, k;
    
        printf("Enter number of Expected Times: ");
        scanf("%d", &n);
    
        printf("Enter Expected Times: ");
        for (i = 0; i <= n - 1; ++i)
        {
            scanf("%d", &data[i]);
        }
    
        for (i = 1; i <= n - 1; ++i)
        {
            for (j = 0; j < i; ++j)
                if (data[j] < data[i])
                    printf("The Expected Times Are: ");
    
            scanf(-----This is where I am stuck...
    
            {
                hold = data[i];
                k = i;
    
                while (k != j)
                {
                    data[k] = data[k - 1];
                    --k;
                }
                data[j] = hold;
            }
        }
    
        printf("In descending Order: ");
        for (i = 0; i <= n - 1; ++i)
        {
            printf("%d    ", data[i]);
        }
    
        return 0;
    }
    Quote Originally Posted by Steveqb14
    I started from scratch and I came up with the program I needed, my only problem is that I want to output each array entered before I put them in descending order. Right now the user enters each term, then it outputs them in descending order. I want the program to output each term entered and then put them in descending order.
    I think that what you mean is that at every step of the sorting algorithm, you want to print the array as it is. Is that correct? If so, then there's no sense having that scanf in that part. Rather, you should print similiar to how you printed the array at the end.

    By the way, instead of writing i <= n - 1, write i < n.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2014
    Posts
    20
    Yes thats what I mean, once the user puts in all of the numbers, I want the program to output each expected activity time in descending order

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Right. My guess is that you're confused here because in your loop to print the numbers at the end, you use i as the loop index. i has already been used as the loop index in the loop to perform the sorting.

    One simple way out is to write a function to print the array. Do you know how to write a function other than the main function?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Mar 2014
    Posts
    20
    No main is the only function i am familiar with.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Right. Then, either learn how to write a function, or write a similiar loop but with a different (presumably new) loop index.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Mar 2014
    Posts
    20
    Hmmm… alright Thank you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C Program, Need help with finishing touches.
    By bgarner151 in forum C Programming
    Replies: 5
    Last Post: 12-10-2012, 06:30 PM
  2. help finishing this program
    By j.ward91 in forum C Programming
    Replies: 16
    Last Post: 04-26-2012, 10:44 AM
  3. Finishing up Mastermind game program
    By Charak in forum C Programming
    Replies: 5
    Last Post: 02-17-2011, 02:49 AM
  4. SO close to finishing chat program...!
    By Nakeerb in forum C++ Programming
    Replies: 13
    Last Post: 10-26-2002, 12:24 PM
  5. finishing up the sorting program
    By jk in forum C Programming
    Replies: 2
    Last Post: 03-19-2002, 07:43 PM