Search code examples
carrayssortingmaxbubble-sort

Sum all max elements of each row of a 2D array


I am trying to make a function that prints the sum of all max elements of each row. I wrote a function that prints 5 max elements of a 2D array, and to calculate the sum I need to put all max elements into one array. Now I have no idea why my code shows me incorrect values, I'll appreciate any help you can give.

#include <stdio.h>

#define MATRIX_SIZE 5


void bubleSort(int arr[MATRIX_SIZE][MATRIX_SIZE])
{
  for (int i = 0; i < MATRIX_SIZE; i++)
  {
    for (int j = 0; j < MATRIX_SIZE; j++)
    {
      for (int h = j + 1; h < MATRIX_SIZE; h++)
      {
        if (arr[j][i] > arr[h][i])
        {
          int tmp = arr[j][i];
          arr[j][i] = arr[h][i];
          arr[h][i] = tmp;
        }
      }
    }
  }
}

int find_max_sum(int arr[MATRIX_SIZE][MATRIX_SIZE]) 
{
  int sum = 0;

  for (int i = 0; i < MATRIX_SIZE; i++) 
  {
    int max = arr[i][0];
    for (int j = 1; j < MATRIX_SIZE; j++) 
    {
      if (max < arr[i][j]) 
      {
        max = arr[i][j];
      }
    }
    printf("%d-th row max = %d\n", i + 1, max);
    sum += max;
  }
  return sum;
}


int printing(int arr[MATRIX_SIZE][MATRIX_SIZE])
{
  for (int i = 0; i < MATRIX_SIZE; i++)
  {
    for (int j = 0; j < MATRIX_SIZE; j++)
    {
      printf("%5.0d", arr[i][j]);
    }
    printf("\n");
  }
  puts("\n");

  return 0;
}

int main() {

  int arr[MATRIX_SIZE][MATRIX_SIZE] = {
    { 12, 7,  23, 13,  4  },
    { 67, 15, 34, -5,  9  },
    { 2,  5,  17, -23, 45 },
    { 26, -6, 23, -5,  -9 },
    { 18, 37, -8, 26,  12 }
  };

  printing(arr);
  printf("\nSum of max elements of 2d array = %d\n", find_max_sum(arr));
  bubleSort(arr);
  printing(arr);
  printf("\nSum of max elements of 2d array = %d\n", find_max_sum(arr));

}

(Updated)

I have a task to sort and print max elements of each row and print their sum but I must print it in in a separate function, I must have minimum 3 function - sort, maxElementOfEachRow, Sum(maxElementOfEachRow), so that's why I asked about pushing max elements into an array to use it in the next function Sum or there is another way to solve this problem?


Solution

  • You don't need to sort the elements to get the max of a row.
    Selecting max element from an array(unordered) is O(n) operation, whereas
    sorting the array takes O(n^2) time (in case of bubble sort) and O(nlogn) if you use quicksort or mergesort.
    Here's the solution without sorting
    Hope this helps

    #include <stdio.h>
    #define ROWS 5
    #define COLS 5
    void bubbleSort(int arr[ROWS][COLS]) {
      // Sorting in ascending order
      for (int row = 0; row < ROWS; row++) {
        for (int i = 0; i < COLS - 1; i++) {
          for (int j = i + 1; j < COLS; j++) {
            if (arr[row][i] > arr[row][j]) {
              int tmp = arr[row][i];
              arr[row][i] = arr[row][j];
              arr[row][j] = tmp;
            }
          }
        }
      }
    }
    void max_element_of_each_row(int arr[ROWS][COLS], int sumArr[ROWS]) {
      for (int i = 0; i < ROWS; i++) {
        sumArr[i] = arr[i][COLS - 1]; // Last element is the max element
      }
    }
    int sum_arr(int sumArr[ROWS]) {
      int sum = 0;
      for (int i = 0; i < ROWS; i++) {
        sum += sumArr[i];
      }
      return sum;
    }
    int find_max_sum(int arr[ROWS][COLS]) {
      int sum = 0;
      for (int i = 0; i < ROWS; i++) {
        int max = arr[i][0];
        for (int j = 1; j < COLS; j++) {
          if (max < arr[i][j]) {
            max = arr[i][j];
          }
        }
        printf("%dth row max = %d\n", i + 1, max);
        sum += max;
      }
      return sum;
    }
    void print_max_element_of_each_row(int sumArr[ROWS]) {
      for (int i = 0; i < ROWS; i++) {
        printf("%dth row max = %d\n", i + 1, sumArr[i]);
      }
    }
    void print(int arr[ROWS][COLS]) {
      for (int i = 0; i < ROWS; i++) {
        for (int j = 0; j < COLS; j++) {
          printf("%d\t", arr[i][j]);
        }
        printf("\n");
      }
    }
    void solution2(int arr[ROWS][COLS]) {
      printf("Before sorting\n");
      print(arr);
      bubbleSort(arr);
      printf("After sorting\n");
      print(arr);
      int sumArr[ROWS];
      max_element_of_each_row(arr, sumArr);
      print_max_element_of_each_row(sumArr);
      printf("Sum of max elements of each row: %d", sum_arr(sumArr));
    }
    void solution1(int arr[ROWS][COLS]) {
      print(arr);
      printf("Max sum of 2d array = %d", find_max_sum(arr));
    }
    int main() {
      int arr[ROWS][COLS] = {12,  7,  23, 13, 4,  67, 15, 34, -5, 9,  2,  5, 17,
                             -23, 45, 26, -6, 23, -5, -9, 18, 37, -8, 26, 12};
      // solution1(arr);
      solution2(arr);
    }
    

    Output(for solution1):
    12 7 23 13 4
    67 15 34 -5 9
    2 5 17 -23 45
    26 -6 23 -5 -9
    18 37 -8 26 12
    1th row max = 23
    2th row max = 67
    3th row max = 45
    4th row max = 26
    5th row max = 37
    Max sum of 2d array = 198


    Output(for solution2):
    Before sorting
    12 7 23 13 4
    67 15 34 -5 9
    2 5 17 -23 45
    26 -6 23 -5 -9
    18 37 -8 26 12
    After sorting
    4 7 12 13 23
    -5 9 15 34 67
    -23 2 5 17 45
    -9 -6 -5 23 26
    -8 12 18 26 37
    1th row max = 23
    2th row max = 67
    3th row max = 45
    4th row max = 26
    5th row max = 37
    Sum of max elements of each row: 198