Sorting variable length Rows using Array of Pointers

Viewed 168

hi this is my code can you help me out not printing output properly how to print output given in description , i am not getting way to sort the given output, output should be sorted from given average.
Implement fragments using array of pointers.
Rows are static and columns are dynamic. Fixed no.of rows and columns will vary for each row.
Example:
Read no.of rows from user and allocate the memory statically for rows.
Read no.of columns for each row and allocate the memory dynamically.
Let us Assume, Row = 3.
Row[0] = 4 columns, Row[1] = 3 columns and Row[2] = 5 columns.
While allocating the memory for columns you have allocate for no.of columns + 1 dynamically.
After that read the values from user and calculate the average for each row separately and store that average in that extra memory block which you added while allocating the memory.

Then sort the array based on the average.
Print the output on the screen.
Example is given below.

ENTER THE NUMBER OF ARRAY YOU WANT TO GIVE AS INPUT: 3
ENTER NO. OF COLUMNS IN ROW[0]: 3
ENTER NO. OF COLUMNS IN ROW[1]: 3
ENTER NO. OF COLUMNS IN ROW[2]: 3
ENTER 3 VALUES OF ROW[0]: 2 3 5
ENTER 3 VALUES OF ROW[1]: 2 4 7
ENTER 3 VALUES OF ROW[2]: 2 1 0

BEFORE SORTING OUTPUT IS: 
2 3 5 3.33333
2 4 7 4.33333
2 1 0 1
AFTER SORTING OUTPUT IS: 
2 1 0 1 
2 3 5 3.33333 
2 4 7 4.33333 

My code so far:

#include <stdio.h>
#include <stdlib.h>

int fragments(int rows, int *array)
{
    int i, j;
    int pos[rows];
    float *arr[rows], avg, sum;
    char *temp[rows];
    for (i = 0; i < rows; i++)
    {
        arr[i] = malloc(sizeof(float*) * array[i]+1);
        pos[i] = i;
        printf("Enter %d values for row[%d]: ", array[i], i);
        for (j = 0; j< array[i]; j++)
        {
            scanf(" %f", &arr[i][j]);
        }
    }
    printf("Before Sorting output is:\n");
    for (i = 0; i < rows; i++)
    {
        sum = 0;
        for (j = 0; j < array[i]; j++)
        {
            printf("%f ", arr[i][j]);
            sum = sum + arr[i][j];
        }
        printf("%f\n", arr[i][j] = (sum / j));
    }
    for (i = 0; i < rows - 1; i++)
    {
        for (j = 0; j < rows - i - 1; j++)
        {
            if (arr[j][array[j]] > arr[j+1][array[j+1]])
            {
                temp[j][j] = arr[j][array[j]];
                arr[j][array[j]] = arr[j+1][array[j+1]];
                arr[j+1][array[j+1]] = temp[j][j];
            }
        }
    }
    for (i = 0; i < rows; i++)
    {
        for (j = 0; j < array[i] + 1; j++)
        {
            printf("%f ", arr[i][array[j]]);
        }
        printf("\n");
    }
}

int main()
{
    int rows, i, j;
    printf("Enter no of rows: ");
    scanf("%d", &rows);
    int array[rows];
    int pos[rows];
    for (i = 0; i < rows; i++)
    {
        printf("Enter no of columns in row[%d]:", i);
        scanf("%d", &array[i] );
    }
    fragments(rows, array); //`enter code here`
}
1 Answers

There are multiple problems in your code:

  • The allocation size is incorrect: arr[i] = malloc(sizeof(float*) * array[i]+1); should read:

    arr[i] = malloc(sizeof(float) * (array[i] + 1));
    
  • the swapping code should swap the array pointers and the array sizes. The current code does not make sense. Use this instead:

              float *temp = arr[j];
              arr[j] = arr[j+1];
              arr[j+1] = temp;
              int temp1 = array[j];
              array[j] = array[j+1];
              array[j+1] = temp1;
    
  • the final print loop is incorrect: instead of printf("%f ", arr[i][array[j]]); you should write:

       printf("%f ", arr[i][j]);
    

Here is a modified version:

#include <stdio.h>
#include <stdlib.h>

int fragments(int rows, int *array) {
    float *arr[rows];

    for (int i = 0; i < rows; i++) {
        arr[i] = malloc(sizeof(float) * (array[i] + 1));
        printf("Enter %d values for row[%d]: ", array[i], i);
        for (int j = 0; j < array[i]; j++) {
            if (scanf("%f", &arr[i][j]) != 1)
                return;
        }
    }
    printf("Before Sorting output is:\n");
    for (int i = 0; i < rows; i++) {
        float sum = 0;
        for (j = 0; j < array[i]; j++) {
            printf("%f ", arr[i][j]);
            sum += arr[i][j];
        }
        printf("%f\n", arr[i][j] = sum / j);
    }
    for (int i = 0; i < rows - 1; i++) {
        for (int j = 0; j < rows - i - 1; j++) {
            if (arr[j][array[j]] > arr[j+1][array[j+1]]) {
                float *temp = arr[j];
                arr[j] = arr[j+1];
                arr[j+1] = temp;
                int temp1 = array[j];
                array[j] = array[j+1];
                array[j+1] = temp1;
            }
        }
    }
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < array[i] + 1; j++) {
            printf("%f ", arr[i][j]);
        }
        printf("\n");
    }
    for (int i = 0; i < rows; i++) {
        free(arr[i]);
    }
}

int main() {
    int rows;
    printf("Enter no of rows: ");
    if (scanf("%d", &rows) != 1)
        return 1;
    int array[rows];
    for (int i = 0; i < rows; i++) {
        printf("Enter no of columns in row[%d]:", i);
        if (scanf("%d", &array[i]) != 1)
            return 1;
    }
    fragments(rows, array);
    return 0;
}
Related