Thread: Beginner: need help with sorting and entering 1 set of numbers into 2 arrays

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    43

    Beginner: need help with sorting and entering 1 set of numbers into 2 arrays

    This is the exact wording for what i am needing to accomplish with my program:

    "Write a program that allows the user to enter 5 numbers from the keyboard. Sort the numbers using any sort routine you wish. The output from your program should be 2 columns of numbers. The left column should be the numbers in the order they were originally entered and the right column should be the sorted list. The columns should be labeled. You will need 2 arrays to accomplish this.
    Use separate functions for input, sorting, and printing"

    My two main problems are that I'm not sure how to get the entered numbers into two arrays. And secondly my sort function is giving me all kinds of grief. So I commented it out to work one step at a time. Any help would be great
    this one has been really tough.

    Code:
    #include <stdio.h>
    # define maxnum 5
    
    
    
    
    void display (int []);
    void bubblesort (int[], int);
    
    
    int main ()
    {
        
        int nums [maxnum];
        int i, j;
        
        printf ("Please enter five numbers\n");
        for (i=0; i<maxnum; i++){
        printf ("Enter a number:");
        scanf ("%d", &nums[i]);
        }
        
        printf ("\n");
        
        display (nums);
        
    system ("PAUSE");
    return 0;
    }
    
    
    void display (int nums[])
    {
         int i;
        printf ("Entered List    Sorted List\n"); 
        printf ("____________    ___________\n");
         for (i=0; i<maxnum; i++){
        printf ("%6d %15d\n", nums[i], nums[i]);
        }
        printf ("\n\n");
    }
    
    
    /*void bubblesort (int num[], int maxnum)
    {
         int i, j, temp;
         
         for (i=0; i<(maxnum-1); i++){
             for (j=1; j<maxnum; j++){
                 if (num[j]<num[j-1])
                 {
                    temp=num[j];
                    num[j]=num[j-1];
                    num[j-1]=temp;
                 }
             }
         }
    
    
    }      */




  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    so declare 2 arrays instead of 1 (nums1 and nums2 for example). to begin with, just read the user input into num1, then copy it to num2. sort num1. now you have an array nums1 that has the sorted order, and nums2 has the original order. print them out as you have done, except print nums1 and nums2

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    43
    Quote Originally Posted by dmh2000 View Post
    so declare 2 arrays instead of 1 (nums1 and nums2 for example). to begin with, just read the user input into num1, then copy it to num2. sort num1. now you have an array nums1 that has the sorted order, and nums2 has the original order. print them out as you have done, except print nums1 and nums2
    Thanks so much for answering

    How would I go about copying it in?
    Last edited by Magi; 10-23-2012 at 05:04 PM.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Make an assignment b[i] = a[i], in the same for loop where the number is entered by the user. (after he enters it into a[i], of course).

  5. #5
    Registered User
    Join Date
    Oct 2012
    Posts
    43
    So I made some changes but when the program runs the numbers in the display function do not display correctly. Am I making the assignment correctly, and entering it in right?

    Code:
    #include <stdio.h>
    # define maxnum 5
    
    
    
    
    void display (int [], int[]);
    void bubblesort (int[], int);
    
    
    int main ()
    {
        
        int nums [maxnum];
        int nums2 [maxnum];
        int i;
        
        printf ("Please enter five numbers\n");
        for (i=0; i<maxnum; i++){
        printf ("Enter a number:");
        scanf ("%d", &nums[i]);
        nums[i]=nums2[i];
        }
        
        printf ("\n");
        
        display (nums, nums2);
        
    system ("PAUSE");
    return 0;
    }
    
    
    void display (int nums[], int nums2[])
    {
         int i;
        printf ("Entered List    Sorted List\n"); 
        printf ("____________    ___________\n");
         for (i=0; i<maxnum; i++){
        printf ("%6d %15d\n", nums[i], nums2[i]);
        }
        printf ("\n\n");
    }
    Last edited by Magi; 10-23-2012 at 05:26 PM.

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Pretty sure that should be

    nums2[i] = nums[i];

    since nums originally contains all the numbers entered. The variable that is supposed to store the value is supposed to be on the left hand side.

  7. #7
    Registered User
    Join Date
    Oct 2012
    Posts
    43
    Quote Originally Posted by whiteflags View Post
    Pretty sure that should be

    nums2[i] = nums[i];

    since nums originally contains all the numbers entered. The variable that is supposed to store the value is supposed to be on the left hand side.
    Thanks a ton that got it to compile correctly, now I'm just having trouble getting my sort function to work. The program does compile, but it doesn't sort nums2 at all.

    Code:
    #include <stdio.h>
    # define maxnum 5
    
    
    
    
    void display (int [], int[]);
    void bubblesort (int[]);
    
    
    int main ()
    {
        
        int nums [maxnum];
        int nums2 [maxnum];
        int i;
        
        printf ("Please enter five numbers\n");
        for (i=0; i<maxnum; i++){
        printf ("Enter a number:");
        scanf ("%d", &nums[i]);
        nums2[i]=nums[i];
        }
        
        printf ("\n");
        
        display (nums, nums2);
        
    system ("PAUSE");
    return 0;
    }
    
    
    void display (int nums[], int nums2[])
    {
         int i;
        printf ("Entered List    Sorted List\n"); 
        printf ("____________    ___________\n");
         for (i=0; i<maxnum; i++){
        printf ("%6d %15d\n", nums[i], nums2[i]);
        }
        printf ("\n\n");
    }
    
    
    void bubblesort (int nums2[])
    {
         int i, j, temp;
         
         for (i=0; i<(maxnum-1); i++){
             for (j=1; j<maxnum; j++){
                 if (nums2[j]<nums2[j-1])
                 {
                    temp=nums2[j];
                    nums2[j]=nums2[j-1];
                    nums2[j-1]=temp;
                 }
             }
         }
    
    
    }
    Last edited by Magi; 10-23-2012 at 05:47 PM.

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Your program never called bubblesort, so nothing could happen.

    That said I think that the algorithm has problems. The loop conditions and the comparison in the if statement need double checking, make sure that you have them right.

  9. #9
    Registered User
    Join Date
    Oct 2012
    Posts
    43
    Thanks to everyone who helped, the program is working great now and, I learned alot!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting random numbers using pointing arrays.
    By jFran in forum C Programming
    Replies: 8
    Last Post: 05-10-2011, 01:56 PM
  2. entering numbers to array in a row
    By transgalactic2 in forum C Programming
    Replies: 4
    Last Post: 12-19-2008, 06:37 AM
  3. Entering floating numbers
    By Ariod in forum C Programming
    Replies: 1
    Last Post: 11-07-2004, 01:58 PM
  4. edit control - entering only numbers
    By Micko in forum Windows Programming
    Replies: 2
    Last Post: 08-20-2004, 11:31 AM
  5. User Entering Numbers
    By ct26torr in forum C Programming
    Replies: 1
    Last Post: 01-26-2003, 01:40 PM

Tags for this Thread