C Program to Sort Rows of a 2D Array
C Program to Sort Rows of a 2D Array
A small school table has two rows and two columns. The sort within each row operation should preserve what each row and column means, rather than treating the table as four unrelated values.
Sort each row independently rather than globally sorting every table element.
Before this exercise, review array indexes and bounds and nested loops. The worked program below applies those ideas to this specific problem.
Explain
Explain why sort within each row uses this sequence of operations.
Trace
Trace the sample values and predict the printed result.
Apply
Order each row descending while keeping values in their original row.
Sort Within Each Row: follow the data
Given data
A small school table has two rows and two columns. The sort within each row operation should preserve what each row and column means, rather than treating the table as four unrelated values.
Rule
Sort each row independently rather than globally sorting every table element.
Observed result
1 4 2 3
Understand the operation
Each two-element row needs at most one comparison and swap. For wider rows, apply a full row-sorting algorithm without exchanging elements between different rows.
The example fixes both dimensions at two so every index can be checked directly. A general matrix interface must carry dimensions, require compatible shapes for its operation and validate storage for each referenced element.
Read the complete C program
The input values are written into this complete C17 example so the calculation can be reproduced without an unavailable service. Change the named data, rebuild and compare the actual output with your prediction.
C17 · complete program
#include <stdio.h>
int main(void) {
int matrix[2][2] = {{1, 2}, {3, 4}};
matrix[0][0] = 4; matrix[0][1] = 1; matrix[1][0] = 3; matrix[1][1] = 2;
for (int row = 0; row < 2; row++) {
if (matrix[row][0] > matrix[row][1]) { int saved = matrix[row][0]; matrix[row][0] = matrix[row][1]; matrix[row][1] = saved; }
}
for (int row = 0; row < 2; row++) {
for (int col = 0; col < 2; col++) printf("%d%s", matrix[row][col], col == 1 ? "\n" : " ");
}
return 0;
}
Expected output
1 4
2 3
Run in C compiler →Follow the values through the code
C17 · code used in this explanation
#include <stdio.h>
int main(void) {
int matrix[2][2] = {{1, 2}, {3, 4}};
matrix[0][0] = 4; matrix[0][1] = 1; matrix[1][0] = 3; matrix[1][1] = 2;
for (int row = 0; row < 2; row++) {
if (matrix[row][0] > matrix[row][1]) { int saved = matrix[row][0]; matrix[row][0] = matrix[row][1]; matrix[row][1] = saved; }
}
for (int row = 0; row < 2; row++) {
for (int col = 0; col < 2; col++) printf("%d%s", matrix[row][col], col == 1 ? "\n" : " ");
}
return 0;
}
The first row 4,1 becomes 1,4; the second row 3,2 becomes 2,3.
Read the resulting rows in order; moving a value to another row or column is part of the operation’s meaning, not merely an output-format choice.
Follow the worked calculation
Start with the sample
The first row 4,1 becomes 1,4; the second row 3,2 becomes 2,3.
Apply the rule
Read the resulting rows in order; moving a value to another row or column is part of the operation’s meaning, not merely an output-format choice.
Check the result
1 4 2 3
1 4 2 3
Try a small variation
C17 · code used in this explanation
#include <stdio.h>
int main(void) {
int matrix[2][2] = {{1, 2}, {3, 4}};
matrix[0][0] = 4; matrix[0][1] = 1; matrix[1][0] = 3; matrix[1][1] = 2;
for (int row = 0; row < 2; row++) {
if (matrix[row][0] > matrix[row][1]) { int saved = matrix[row][0]; matrix[row][0] = matrix[row][1]; matrix[row][1] = saved; }
}
for (int row = 0; row < 2; row++) {
for (int col = 0; col < 2; col++) printf("%d%s", matrix[row][col], col == 1 ? "\n" : " ");
}
return 0;
}
Order each row descending while keeping values in their original row.
Worked answer
The complete worked answer uses the same valid two-by-two input. Trace its index rule independently; compare each printed result with the source element or accumulation that produced it.
C17 · complete program
#include <stdio.h>
int main(void) {
int matrix[2][2] = {{1, 2}, {3, 4}};
matrix[0][0] = 4; matrix[0][1] = 1; matrix[1][0] = 3; matrix[1][1] = 2;
for (int row = 0; row < 2; row++) {
if (matrix[row][0] < matrix[row][1]) { int saved = matrix[row][0]; matrix[row][0] = matrix[row][1]; matrix[row][1] = saved; }
}
for (int row = 0; row < 2; row++) {
for (int col = 0; col < 2; col++) printf("%d%s", matrix[row][col], col == 1 ? "\n" : " ");
}
return 0;
}
Expected output
4 1
3 2
Run in C compiler →Recognize the important boundaries
Sorting marks within a row may detach a mark from its subject column. Decide whether columns are freely reorderable values before applying the operation.
Larger or untrusted dimensions require checked storage-size calculations. Integer products and accumulated sums also need a representable-range contract.
Check your understanding
Choose the answer, then compare the reasoning for every option.
Use the idea independently
Sort each row independently rather than globally sorting every table element.
Continue with array boundaries.
Continue with function bounds contracts.
Language reference: WG14 C11 committee draft. These examples use the C17-compatible subset of the explained language features.
Reaching the recap and completing the quizzes are separate milestones.