Search code examples
cfunctionbubble-sortcs50

Trying to implement a simple bubble sort function on a list of self declared arrays


Newbie trying to make an arrays then sort them using a function called "sort" that i created at the bottom of the code, using bubble sort..

I'm pretty sure the printing part is ok, just the functions that I'm having trouble with, can someone help a newbie out please?..

#include <stdio.h>
#include <cs50.h>

void sort(int arrayRack[], int NumberOfArrayElements);

int main(void)

{
//variable declarations
int NumberOfArrayElements = 0;


//Get number of arrays
printf("number of arrays you'd like?\n");
NumberOfArrayElements = GetInt();

//variable array declaration
int arrayRack[NumberOfArrayElements];


//Get number for each int
for(int i = 0; i < NumberOfArrayElements; i++)
    {
        printf("give me an int for the %i th array\n", i + 1);
        arrayRack[i] = GetInt();
    }

//print what is currently in the arrayRack
for(int j = 0; j < NumberOfArrayElements; j++)
    printf("{%i}<-- number in %i-th array (unsorted)\n", arrayRack[j], j + 1);



for(int i = 0; i < NumberOfArrayElements; i++)
    {
        sort(&arrayRack[i], NumberOfArrayElements);
        printf("{%i}<-- number in %i-th array (sorted)\n", arrayRack[i], i + 1);

    }
    return 0;
}


//this is the funciton, like sort(haystack, size);
void sort(int arrayRack[], int NumberOfArrayElements)
{
//declare variable
int swap = 0;
        for(int k = 0; k < NumberOfArrayElements; k++)
        {
            //if [1] is higher, swap with [2]
            if(arrayRack[k] > arrayRack[k + 1])
            {
                swap = arrayRack[k];
                arrayRack[k] = arrayRack[k + 1];
                arrayRack[k + 1] = swap;
            }
        }

}

Solution

  • You pass a pointer to element &arrayRack[i] but in the parameters of sort you have declared the first parameter to be an array so this doesn't match. You could simply write:

    In main:

    sort(arrayRack, NumberOfArrayElements);
    for(int i = 0; i < NumberOfArrayElements; i++)
        {
            printf("{%i}<-- number in %i-th array (sorted)\n", arrayRack[i], i + 1);
    
        }
    

    And in sort function:

    void sort(int arrayRack[], int NumberOfArrayElements)
    {
    //declare variable
    int swap = 0;
            for(int k = 0; k < NumberOfArrayElements-1; k++)
            {
    
               for(int j=0; j<NumberOfArrayElements-1-k;j++)
    
                if(arrayRack[j] > arrayRack[j + 1])
                {
                    swap = arrayRack[j];
                    arrayRack[j] = arrayRack[j + 1];
                    arrayRack[j + 1] = swap;
                }
            }
    
    }
    

    Also I would suggest scanf("%d",&NumberOfArrayElements); instead of NumberOfArrayElements = GetInt(); and scanf("%d",&arrayRack[i]); instead of arrayRack[i] = GetInt();

    Example:

     number of arrays you'd like?
        5
        give me an int for the 1 th array
        5 
        give me an int for the 2 th array
        7
        give me an int for the 3 th array
        1
        give me an int for the 4 th array
        8
        give me an int for the 5 th array
        3
        {5}<-- number in 1-th array (unsorted)
        {7}<-- number in 2-th array (unsorted)
        {1}<-- number in 3-th array (unsorted)
        {8}<-- number in 4-th array (unsorted)
        {3}<-- number in 5-th array (unsorted)
        {1}<-- number in 1-th array (sorted)
        {3}<-- number in 2-th array (sorted)
        {5}<-- number in 3-th array (sorted)
        {7}<-- number in 4-th array (sorted)
        {8}<-- number in 5-th array (sorted)