求和二维数组每一行的所有最大元素

时间:2018-10-11 17:50:59

标签: c arrays sorting max bubble-sort

我正在尝试创建一个函数,该函数打印每行的所有最大元素的总和。我编写了一个函数,该函数可打印2D数组的5个最大元素,并计算总和,我需要将所有最大元素放入一个数组中。现在我不知道为什么我的代码会向我显示错误的值,感谢您提供的任何帮助。

#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));

}

(已更新)

我有一个任务来排序和打印每行的最大元素并打印它们的总和,但是我必须将其打印在单独的函数中,我必须至少具有3个函数-sort,maxElementOfEachRow,Sum(maxElementOfEachRow),所以这就是原因我问过将max元素放入数组以在下一个函数Sum中使用它的方法,还是有另一种方法可以解决此问题?

3 个答案:

答案 0 :(得分:1)

您无需对元素进行排序即可获得一行的最大值。
从数组中选择最大元素(无序)是O(n)操作,而对数组进行排序则需要O(n ^ 2)时间(在冒泡排序的情况下)和O(nlogn)(如果使用quicksort或mergesort) 。
这是无需排序的解决方案
希望对您有所帮助

#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);
}

输出(用于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
第一排最大= 23
第二排最大= 67
第三排最大= 45
第四排最大= 26
第5行上限= 37
2d数组的总和= 198


输出(用于solution2):
排序之前
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
排序后
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
第一排最大= 23
第二排最大= 67
第三排最大= 45
第四排最大= 26
第5行上限= 37
每行的最大元素总数:198

答案 1 :(得分:0)

每行的最大值及其总和可以在一对循环中找到。

#include <stdio.h>
#include <limits.h>

#define ROWS 5
#define COLS 5

int main(void) {

    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 }
    };

    int maxsum = 0;
    for(int row = 0; row < ROWS; row++) {
        int rowmax = INT_MIN;
        for(int col = 0; col < COLS; col++) {
            if(rowmax < arr[row][col]) {
                rowmax = arr[row][col];
            }
        }
        printf("Row %d max = %d\n", row + 1, rowmax);
        maxsum += rowmax;
    }
    printf("Sum of max = %d\n", maxsum);
}

程序输出:

Row 1 max = 23
Row 2 max = 67
Row 3 max = 45
Row 4 max = 26
Row 5 max = 37
Sum of max = 198

答案 2 :(得分:0)

#include <stdio.h>

#define MATRIX_SIZE 5

void bubleSort(int arr[MATRIX_SIZE][MATRIX_SIZE]) // sorting an array by buble sort in descending order
{
  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 tempt = arr[j][i];
          arr[j][i] = arr[h][i];
          arr[h][i] = tempt;
        }
      }
    }
  }
}

int find_max_elm(int arr[MATRIX_SIZE][MATRIX_SIZE], int sumArr[MATRIX_SIZE]) // findind max element in each row of a 2d array
{
  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];
      }
    }
    sumArr[i] = max;
  }
}

int sum_arr(int sumArr[MATRIX_SIZE]) // calculating the summ of all max elements in each row of a 2d array
{
  int sum = 0;
  for (int i = 0; i < MATRIX_SIZE; i++)
  {
    sum += sumArr[i];
  }
  return sum;
}

void print_max_element_of_each_row(int sumArr[MATRIX_SIZE]) // printing all max elements in each row of a 2d array
{
  for (int i = 0; i < MATRIX_SIZE; i++)
  {
    printf("%d-th row max = %d\n", i + 1, sumArr[i]);
  }
}

int printing(int arr[MATRIX_SIZE][MATRIX_SIZE]) // printing an array = matrix
{
  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 }
  };


  printf("Before sorting\n");
  printing(arr);

  bubleSort(arr);

  printf("After sorting\n");
  printing(arr);

  int sumArr[MATRIX_SIZE];

  find_max_elm(arr, sumArr);
  print_max_element_of_each_row(sumArr);
  printf("\nSum of max elements of each row: %d", sum_arr(sumArr));
}