I wanted to find the sum of each row and then sort it. I managed to do this but to make it more complicated I decided to find from which row the min sum
and the max sum
was originated.
Example: The row with the min sum
is the 3rd and the row with the max sum
is is the 5th.
The main issue in my approach that I attempted was that the b[] array (in the sortfunction
below) which I used to store the initial sum
of each row is now replaced with the sorted values and I cannot work with the unsorted values which would show the rows that are needed.
#include<stdio.h>
#define ROWS 5
#define COLS 3
void printnumbers(int arr[][COLS]);
void sortfunction(int arr[][COLS]);
int main() {
int arr[ROWS][COLS] = {{1, 2, 3},
{2, 3, 4},
{3, 1, 1},
{5, 5, 6},
{35, 335, 6}};
printnumbers(arr);
sortfunction(arr);
return 0;
}
void printnumbers(int arr[][COLS]){
int i,j;
for(i = 0; i<ROWS; i )
{
printf("\n");
for(j = 0; j<COLS; j )
{
printf("%d\t", arr[i][j]);
}
}
}
//swap function below
void swap(int* xp, int* yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
// sort function
void sortfunction(int arr[][COLS]) {
int i, j, total, k, b[ROWS];
k = 0;
printf("\n");
for (i = 0; i < ROWS; i ) {
total = 0;
for (j = 0; j < COLS; j ) {
total = arr[i][j];
}
if (j == COLS) {
b[k] = total;
k = 1;
}
}
for (k = 0; k < ROWS; k ) {
printf("the sum of the %dst row is %d\n", k 1, b[k]);
}
int a, q, min_idx;
int n = sizeof(b) / sizeof(b[0]);
// One by one move boundary of unsorted subarray
for (q = 0; q < n - 1; q ) {
// Find the minimum element in unsorted array
min_idx = q;
for (a = q 1; a < n; a )
if (b[a] < b[min_idx])
min_idx = a;
// Swap the found minimum element
// with the first element
swap(&b[min_idx], &b[q]);
}
for (i = 0; i < n; i )
printf("%d ", b[i]);
printf("\n");
//Now I want to find which row has the least sum and which has the max sum but the b[] array is already sorted.
}
This prints:
the sum of the 1st row is 6
the sum of the 2st row is 9
the sum of the 3st row is 5
the sum of the 4st row is 16
the sum of the 5st row is 376
5 6 9 16 376 //it got sorted
and it needs to print also this:
The row with the min sum is in the 3rd row
The row with the max sum us un the 5th row.
CodePudding user response:
One solution would be to make b
an array of a struct with the sum and row numbers. The swap()
and sortfunction()
functions would then be as follows (including some minor simplifications):
typedef struct
{
int sum;
int row;
} tRowSum;
//swap function below
void swap(tRowSum* xp, tRowSum* yp)
{
tRowSum temp = *xp;
*xp = *yp;
*yp = temp;
}
// sort function
void sortfunction(int arr[][COLS]) {
int i, j, total;
tRowSum b[ROWS];
printf("\n");
for (i = 0; i < ROWS; i ) {
total = 0;
for (j = 0; j < COLS; j ) {
total = arr[i][j];
}
b[i].sum = total;
b[i].row = i;
}
for (i = 0; i < ROWS; i ) {
printf("the sum of the %dst row is %d\n", i 1, b[i].sum);
}
int a, q, min_idx;
// One by one move boundary of unsorted subarray
for (q = 0; q < ROWS - 1; q ) {
// Find the minimum element in unsorted array
min_idx = q;
for (a = q 1; a < ROWS; a )
if (b[a].sum < b[min_idx].sum)
min_idx = a;
// Swap the found minimum element
// with the first element
swap(&b[min_idx], &b[q]);
}
for (i = 0; i < ROWS; i ) {
printf("%d (%d)", b[i].sum, b[i].row);
printf("\n");
}
}
After sorting b
with respect to the sum, the original row numbers are then available for each entry.