r/C_Homework • u/MassiveMorph • Apr 03 '18
Ascending order using Functions in Arrays.. Not Printing
Hi, i am not sure if this is the correct Subreddit - But i am wondering if someone could possibly emphasize on why my functions are being called/printing.
I have my first function printing the contents of the array, but my second function does not print the Ascending order..Could anyone emphasize? Thank you.
int main() {
/* 2D array declaration and size of each Array in the Programme*/ int arrayHeight, array[100][2], xCoord, yCoord, i; printf ("***** Bubble Sort ***** \n"); /*Counter variables for the loop*/ printf("How many items of data do you wish to enter? "); scanf("%d",&arrayHeight); for(i=0; i<arrayHeight; i++) /*if ('-1' = close) Primarily going to be used to close programme on command */ { printf("Please enter in the X coordinate: "); scanf("%d", &xCoord); printf("Please enter in the Y coordinate: "); scanf("%d", &yCoord); array[i][0] = xCoord;/* Name of XCoordinate and position within Array*/ array[i][1] = yCoord;/*Name of YCoordinate and position within Array*/ } DisplayArray(array,arrayHeight); Bubblesort(array,arrayHeight); } int DisplayArray(int array[100][2],int arrayHeight, int swap) { /*Displaying Array elements*/ int i, j; printf("\n The 2-D Array contains : \n"); for(i=0; i<arrayHeight; i++) { printf("[%d][%d]\n\r", array[i][0], array[i][1]); } } int BubbleSort(int array[100][2], int arrayHeight) /*Start of the Function Usage*/ { /*Sorts the Array elements into appropriate chosen sorting order - Ascending*/ int k, i,j, swap; /*for(k =0; k< arrayHeight; k++) {*/ for (i = 0; i <arrayHeight; i++) { for (j = i+1; j < 3; ++j) { if (array[i][0] > array[i][1]) { swap = array[i][0]; array[i][0] = array[i][1]; array[i][1] = swap; } } } /*Prints out the Organsied Ascending Order from both Arrays */ printf("\n Printing in Asending Order: "); for (i=0; i<arrayHeight; i++) { for (j=0; j<arrayHeight; j++) /* This would be the same Code as Displaying Array*/ { printf("\n %d ", swap); } } BubbleSort(array, swap); }
2
Upvotes
1
u/barryvm Apr 03 '18
You should edit your post, the last bit isn't displayed as source code.
What part of the program does not print and what should it print ?
1
u/jedwardsol Apr 03 '18
Put 4 spaces in front of every line to format it properly. For the lines that are formatted properly, please fix your indentation.
You're calling DisplayArray with 2 parameters but it wants 3.
The cause of your problem is that you're never calling
BubbleSort.