Homework: Arrays & Functions - Print Two Arrays on Same Table After Sorting
Hello again!
Having trouble with homework involving (title). Here is the assignment:
Quote:
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!