mardi 26 mai 2020

Bubble sort 2D array in C

I need a function to bubble sort this randomly generated 2D array. Also with rand() method I wanted it to generate numbers between (1, 1000000) but it doesnt give the required range, any suggestion to find out a solution?

int **matrix()
{

    int **matrix;
    int row, column;
    long s, k;
    int i,j,f,swap;


    srand(time(NULL));
    printf("Number of rows: ");
    scanf("%d", &row);

    printf("Number of column: ");
    scanf("%d", &column);


    matrix = (int **) calloc(row, sizeof(int));


    for(i = 0; i < row; i++)
        matrix[i] = (int *) calloc(column, sizeof(int));


    for(s = 0; s < row; s++)
    {
        for(k = 0; k < column; k++)
        {

            matrix[s][k]=rand()%10000000;
        }
    }

    for(s = 0; s < row; s++)
    {
        for(k = 0; k < column; k++)
            printf("%4d \t\t", matrix[s][k]);

        printf("\n");
    }


    for(i = 0; i < row; i++)
        free((void *) matrix[i]);


    free((void *) matrix);

    return **matrix;

}



Aucun commentaire:

Enregistrer un commentaire