
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.