r/C_Homework • • 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

9 comments sorted by

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/MassiveMorph Apr 03 '18

Sorry man, i managed to format it so it is accurate to my code.

It is meant to print the Bubblesort Function - specifically the Ascending part, so it prints out all the numbers in the array into Ascending order. The only problem is that it actually doesn't print any numbers.

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.

DisplayArray(array,arrayHeight);
int DisplayArray(int array[100][2],int arrayHeight, int swap)

You're calling DisplayArray with 2 parameters but it wants 3.

The cause of your problem is that you're never calling BubbleSort.

1

u/MassiveMorph Apr 03 '18

Thanks man, originally it kept jumping around. So as you literally mentioned calling the Bubblesort, i realised that it had to go at the bottom of the MainFunction.

But still throws the incorrect numbers from the array

1

u/jedwardsol Apr 03 '18
for (j = i+1; j < 3; ++j) {

Why 3?

if (array[i][0] > array[i][1])  {

This is comparing the x-coordinate with the y-coordinate. You need to be comparing separate 2 points and swapping them (after deciding what it means for 1 point to be "bigger" than another)

1

u/MassiveMorph Apr 03 '18

It's 3 because the original size of Position J in the Array was 3. So I'm not what I am meant to put instead of that

1

u/jedwardsol Apr 03 '18

Bubblesort moves the biggest element in the remaining part of the array to the end in each pass.

1 5 4 3 -> 1 4 3 5

1 4 3 5 -> 1 3 4 5

1 3 4 5 -> 1 3 4 5

So the outer loop can go down from N to 1 representing the final position of the sorted element.

And the inner loop can go up from 0 to N-1 representing the bubble.

1

u/MassiveMorph Apr 03 '18

Thank you. When you say 'N' do you mean the ArraySize or something else?

1

u/jedwardsol Apr 03 '18

Yes, the array size.