Rearranging rows based on values in a column in a 2d array in C
Rearranging rows based on values in a column in a 2d array in C
I am trying to sort a 2d array based on the values of the last column in descending order. My code goes as follows:
#include <stdio.h>
#define totrow 10
#define totcolumn 6
float grade[totrow][totcolumn];
void reArg (float grade[totcolumn]);
void reArg (float grade[totcolumn])
int temp, x, k = 5;
for (int r = 0; r < totrow; r++)
for (int c = 0; c <totcolumn;c++)
if (grade[r][k] > grade[c][k])
for(x = 0; x < totcolumn;x++)
temp = grade[r][x];
grade[r][x]=grade[c][x];
grade[c][x]=temp;
for (int r = 0; r < totrow; r++)
for (int c = 0; c < totcolumn; c++)
printf("%.2ft",grade[r][c]);
printf("n");
After the code compiles I get this for my array:
7899.00 92.00 90.00 88.00 86.00 89.00
6814.00 85.00 86.00 92.00 88.00 87.00
8234.00 77.00 87.00 84.00 98.00 86.00
7654.00 76.00 87.00 84.00 88.00 83.00
3534.00 86.00 81.00 84.00 73.00 81.00
7284.00 56.00 81.00 87.00 98.00 80.00
9901.00 45.00 78.00 79.00 80.00 70.00
6465.00 87.00 54.00 68.00 72.00 70.00
7934.00 76.00 91.00 84.00 65.00 79.00
7234.00 76.00 81.00 84.00 78.00 79.00
The orginal array is:
6814.00 85.00 86.00 92.00 88.00 87.75
7234.00 76.00 81.00 84.00 78.00 79.75
6465.00 87.00 54.00 68.00 72.00 70.25
7899.00 92.00 90.00 88.00 86.00 89.00
9901.00 45.00 78.00 79.00 80.00 70.50
8234.00 77.00 87.00 84.00 98.00 86.50
7934.00 76.00 91.00 84.00 65.00 79.00
7284.00 56.00 81.00 87.00 98.00 80.50
7654.00 76.00 87.00 84.00 88.00 83.75
3534.00 86.00 81.00 84.00 73.00 81.00
Why does the program only sort the first 6 rows?
the complete code is: https://repl.it/@ryanw123/project-4
#define totrow 9 #define totcolumn 5
The original array loses the last two values.
– R doe
38 mins ago
Your code seems incomplete by at least a
}
(assuming it is not a mistake in my edit), please get the shown code compileable, create a Minimal, Complete, and Verifiable example.– Yunnosch
37 mins ago
}
You're right the last line should have a } I didn't include that in my post. The code does compile on my compiler and it will print the results. I believe the problem is within the if statement since r will eventually be less than c.
– R doe
33 mins ago
"it will print the results" I doubt it, without a
main()
and without values in any array. Please create an Minimal, Complete, and Verifiable example to demonstrate your observations.– Yunnosch
31 mins ago
main()
1 Answer
1
The problem is that you were using for(int c = 0; c < totcolumn; c++)
, but, as I figured you were trying to make a Bubble Sort, you need to go through the rows twice, like this: for(int c = 0; c < totrow; c++)
.
for(int c = 0; c < totcolumn; c++)
for(int c = 0; c < totrow; c++)
I created an example to run locally.
#include <stdio.h>
#define totrow 4
#define totcolumn 2
void reArg (float grade[totcolumn])
int temp, x, k = 5;
for (int r = 0; r < totrow; r++)
for (int c = 0; c < totrow; c++)
if (grade[r][k] > grade[c][k])
for(x = 0; x < totcolumn ; x++)
temp = grade[r][x];
grade[r][x]=grade[c][x];
grade[c][x]=temp;
for (int r = 0; r < totrow; r++)
for (int c = 0; c < totcolumn; c++)
printf("%.2ft",grade[r][c]);
printf("n");
int main()
float grade[totrow][totcolumn] = 1,2,3,4,0,0;
reArg(grade);
return 0;
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
What happens if you change to
#define totrow 9 #define totcolumn 5
for an experiment?– Yunnosch
42 mins ago