# Thread: Homework: Arrays & Functions - Print Two Arrays on Same Table After Sorting

1. ## Homework: Arrays & Functions - Print Two Arrays on Same Table After Sorting

Hello again!

Having trouble with homework involving (title). Here is the assignment:

Write a program that allows the user to enter 10 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.

So, I have to use separate functions for each of these. I would think it would be easiest to do the input in Main() and then the sorting and printing in another function, but of course since you can't return arrays I am kind of stuck on how I return the new array after sorting. I thought about doing the sorting in Main(), but then I would need to still do the original arrays input in a function and would still run into the same problem.

Here is what I have so far:

Code:
```//Cameron Taylor

#include <stdio.h>

#define MAXARRAY 10

int highLow(int[], int);
int print(int[], int[], int);

int main (){

int unsorted[MAXARRAY], i, j, temp;
int sorted, sortedArray[MAXARRAY], printArrays;

for (i = 0; i < MAXARRAY; i++){
printf("Enter 10 numbers to be sorted");
scanf("%d", &unsorted[i]);
}

sorted = highLow(unsorted, MAXARRAY);
sorted = sortedArray[MAXARRAY];

printArrays = print(unsorted, temp, MAXARRAY);

printf("end");

return 0;
}

int highLow(int unsorted[], int sortA){

int i, j, temp, sortedArray[MAXARRAY];
for (i = 0; i < (sortA - 1); ++i){

for (j = 0; j < sortA - 1 - i; ++j){
if (unsorted[j] > unsorted[j+1]){
temp = unsorted[j+1];
unsorted[j+1] = unsorted[j];
unsorted[j] = sortedArray[j];
sortedArray[j] = temp;
}
}
}
return (temp);
}

int print(int unsorted[], int sortedArray[], int sortB){

int i, j;

for (i = 0; i < sortB; i++){
printf("%d %d", unsorted[i], sortedArray[i]);
}

return 0;

}```
I know it seems simplistic right now, but I am just trying to get it to work first and then go back and beautify it up a bit.

Thanks! 2. It's not true that "you can't return arrays" from a function. You could dynamically allocate one (or return a static one!).

But you don't need that here. You just have to pass in both arrays, which become pointers to the first elements of the original arrays and can therefore not only be used to read but also to change the original values.

Try structuring your program like this:
Code:
```void print(int src[], int dst[], int sz) {
// ...
}

void mysort(int src[], int dst[], int sz) {
// ...
}

int getInput(int a[]) {
// ...
}

int main() {
int src[SIZE], dst[SIZE], sz = 0;

sz = getInput(src);
mysort(src, dst, sz);
print(src, dst, sz);

return 0;
}``` 3. Ok, I hope this is sort of what you were looking for:

Code:
```//Cameron Taylor

#include <stdio.h>

#define MAXARRAY 10

void print(int[], int[], int);
void highLow(int[], int[], int);

void print(int unsorted[], int sortedArray[], int sz){

int i, j;

for (i = 0; i < sz; i++){
printf("%d %d", unsorted[i], sortedArray[i]);
}

}

void highLow(int unsorted[], sortedArray[], int sz) {

int i, j;

for (i = 0; i < (sz - 1); ++i){
for (j = 0; j < sz - 1 - i; ++j){
if (unsorted[j] > unsorted[j+1]){
temp = unsorted[j+1];
unsorted[j+1] = unsorted[j];
unsorted[j] = sortedArray[j];

}
}
}
}

int getInput(int a[]){

int unsorted[MAXARRAY], i;

for (i = 0; i < MAXARRAY; i++){
printf("Enter 10 numbers to be sorted");
scanf("%d", &unsorted[i]);
}
}

int main(){
int unsorted[MAXARRAY], sortedArray[MAXARRAY], sz = 0;

sz = getInput(unsorted);
highLow(unsorted, sortedArray, sz);
print(unsorted, sortedArray, sz);

return 0;
}```
Getting this compiler error though:

commiedic@localhost Project13\$ make Exercise1
cc Exercise1.c -o Exercise1
Exercise1.c:21:30: error: unknown type name ‘sortedArray’
make: *** [Exercise1] Error 1 4. Originally Posted by Cameron Taylor Ok, I hope this is sort of what you were looking for:

Code:
```//Cameron Taylor

#include <stdio.h>

#define MAXARRAY 10

void print(int[], int[], int);
void highLow(int[], int[], int);

void print(int unsorted[], int sortedArray[], int sz){

int i, j;

for (i = 0; i < sz; i++){
printf("%d %d", unsorted[i], sortedArray[i]);
}

}

void highLow(int unsorted[], sortedArray[], int sz) {

int i, j, temp;

for (i = 0; i < (sz - 1); ++i){
for (j = 0; j < sz - 1 - i; ++j){
if (unsorted[j] > unsorted[j+1]){
temp = unsorted[j+1];
unsorted[j+1] = unsorted[j];
unsorted[j] = temp;

}
}
}
}

int getInput(int a[]){

int unsorted[MAXARRAY], i;

for (i = 0; i < MAXARRAY; i++){
printf("Enter 10 numbers to be sorted");
scanf("%d", &unsorted[i]);
}
}

int main(){
int unsorted[MAXARRAY], sortedArray[MAXARRAY], sz = 0;

sz = getInput(unsorted);
highLow(unsorted, sortedArray, sz);
print(unsorted, sortedArray, sz);

return 0;
}```
Getting this compiler error though:

You never declare SortedArray's type in your function parameters. 5. lol forgot the "int"... 6. Code:
```//Cameron Taylor

#include <stdio.h>

#define MAXARRAY 10

void print(int[], int[], int);
void highLow(int[], int[], int);

void print(int unsorted[], int sortedArray[], int sz){

int i, j;

for (i = 0; i < sz; i++){
printf("%d %d", unsorted[i], sortedArray[i]);
}

}

void highLow(int unsorted[], int sortedArray[], int sz) {

int i, j, temp;

for (i = 0; i < (sz - 1); ++i){
for (j = 0; j < sz - 1 - i; ++j){
if (unsorted[j] > unsorted[j+1]){
temp = unsorted[j+1];
unsorted[j+1] = unsorted[j];
unsorted[j] = sortedArray[j];

}
}
}
}

int getInput(int a[]){

int unsorted[MAXARRAY], i;

for (i = 0; i < MAXARRAY; i++){
printf("Enter 10 numbers to be sorted");
scanf("%d", &unsorted[i]);
}
}

int main(){
int unsorted[MAXARRAY], sortedArray[MAXARRAY], sz = 0;

sz = getInput(unsorted);
highLow(unsorted, sortedArray, sz);
print(unsorted, sortedArray, sz);

return 0;
}```
Well at least it is running :P

commiedic@localhost Project13\$ make Exercise1
cc Exercise1.c -o Exercise1
commiedic@localhost Project13\$ ./Exercise1
Enter 10 numbers to be sorted1
Enter 10 numbers to be sorted2
Enter 10 numbers to be sorted3
Enter 10 numbers to be sorted4
Enter 10 numbers to be sorted5
Enter 10 numbers to be sorted6
Enter 10 numbers to be sorted7
Enter 10 numbers to be sorted8
Enter 10 numbers to be sorted9
Enter 10 numbers to be sorted10
0 -1233512952
commiedic@localhost Project13\$ 7. Originally Posted by Cameron Taylor Code:
```//Cameron Taylor

#include <stdio.h>

#define MAXARRAY 10

void print(int[], int[], int);
void highLow(int[], int[], int);

void print(int unsorted[], int sortedArray[], int sz){

int i, j;

for (i = 0; i < sz; i++){
printf("%d %d", unsorted[i], sortedArray[i]);
}

}

void highLow(int unsorted[], int sortedArray[], int sz) {

int i, j, temp;

for (i = 0; i < (sz - 1); ++i){
for (j = 0; j < sz - 1 - i; ++j){
if (unsorted[j] > unsorted[j+1]){
temp = unsorted[j+1];
unsorted[j+1] = unsorted[j];
unsorted[j] = sortedArray[j];

}
}
}
}

int getInput(int a[]){

int unsorted[MAXARRAY], i;

for (i = 0; i < MAXARRAY; i++){
printf("Enter 10 numbers to be sorted");
scanf("%d", &unsorted[i]);
}
}

int main(){
int unsorted[MAXARRAY], sortedArray[MAXARRAY], sz = 0;

sz = getInput(unsorted);
highLow(unsorted, sortedArray, sz);
print(unsorted, sortedArray, sz);

return 0;
}```
Well at least it is running :P
Not to do your homework for you, but you made a ton of errors in your code. I decided to fix it for you and then explain why.

Code:
```//Cameron Taylor

#include <stdio.h>

#define MAXARRAY 10

void print(int[], int[]);
void highLow(int[], int[]);

void print(int unsorted[], int sortedArray[])
{
int i;

for (i = 0; i < MAXARRAY; i++)
{
fprintf(stdout, "\t%d\t%d\n", unsorted[i], sortedArray[i]);
}

return;
}

void highLow(int unsorted[], int sortedArray[])
{
int i, j, temp;

for ( i = 0; i < MAXARRAY; i++)
{
sortedArray[i] = unsorted[i];
}

for (i = 0; i < MAXARRAY; i++){
for (j = 0; j < MAXARRAY; j++){
if (sortedArray[j] > sortedArray[j+1]){
temp = sortedArray[j];
sortedArray[j] = sortedArray[j+1];
sortedArray[j+1] = temp;
}
}
}

return;
}

void getInput(int a[])
{
int i;

fprintf(stdout, "\tEnter 10 numbers to be sorted :\n");

for (i = 0; i < MAXARRAY; i++)
{
scanf("%d", &a[i]);
}

return;
}

int main()
{
int unsorted[MAXARRAY], sortedArray[MAXARRAY];

getInput(unsorted);
highLow(unsorted, sortedArray);
print(unsorted, sortedArray);

return 0;
}```
The first thing I did is remove all the unused variables, many of which were unneeded variables, ( j was not used in one function ). Then I fixed the formatting issue with calls to fprintf( I like using it better ) to make columns of numbers.

Code:
`fprintf(stdout, "\t%d\t%d\n", unsorted[i], sortedArray[i]);`
In your sort function, you used sortedArray uninitialized, that caused you to have many unpredictable results, because you were getting whatever values were on the stack at that time. So I ended up initializing it.

Code:
```    for ( i = 0; i < MAXARRAY; i++)
{
sortedArray[i] = unsorted[i];
}```
Then the bubble sort also had a lot of problems. It seemed like you tried to make it into rocket science with confusing loop comparisons with that sz variable. You really just needed to use MAXARRAY instead of all that useless math you kept doing.

Code:
```        for (i = 0; i < MAXARRAY; i++){
for (j = 0; j < MAXARRAY; j++){```
Also, you messed up the sort. You just kept setting it to the same values without changing anything. You really needed to do just this

Code:
```                    temp = sortedArray[j];
sortedArray[j] = sortedArray[j+1];
sortedArray[j+1] = temp;```
In the GetInput() function, you created a local array and never changed the array you passed. I fixed it easily.

Code:
```void getInput(int a[])
{
int i;

fprintf(stdout, "\tEnter 10 numbers to be sorted :\n");

for (i = 0; i < MAXARRAY; i++)
{
scanf("%d", &a[i]);
}

return;
}```
There were some other changes I made, but it's a long list. Just be careful to know what variables you are setting next time.

Sample output :

Code:
```        Enter 10 numbers to be sorted :
100
50
20
30
50
20
10
40
100
30
100     10
50      20
20      20
30      30
50      30
20      40
10      50
40      50
100     100
30      100```
If you have any questions on what I did feel free to ask. 8. Why this:
Code:
`fprintf(stdout, "\t%d\t%d\n", unsorted[i], sortedArray[i]);`
Why use fprintf and stdout instead of just a normal printf("%d %d", unsorted[i], sortedArray[i]); 9. Originally Posted by Cameron Taylor Why this:
Code:
`fprintf(stdout, "\t%d\t%d\n", unsorted[i], sortedArray[i]);`
Why use fprintf and stdout instead of just a normal printf("%d %d", unsorted[i], sortedArray[i]);
Good question, it's mainly because printf() really calls fprintf() behind the scenes. In my opinion, you might as well do it for the program. Also, fprintf() gives you more control, whereas printf() doesn't so much. If you ever wanted to use stderr to print a error message when you redirected stdout to a file, you should use fprintf(). As for the \t ( tab ) and the \n ( newline ) escape characters, those just make it much easier to read the results. 10. I think easier to read will be something like
Code:
`printf("%20d %20d\n", ...);`
both code and result, since numbers will be aligned right Popular pages Recent additions 