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

1. ## 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;

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. 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. Originally Posted by dmh2000
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

How would I go about copying it in?

4. 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. 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;

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");
}```

6. 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. Originally Posted by whiteflags
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;

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;
}
}
}

}```

8. 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. Thanks to everyone who helped, the program is working great now and, I learned alot!