#include <stdio.h> int main() { int x[a][w]; int k, l, n, tmp; printf("Enter the order of the array:"); scanf("%d", &n); printf("Enter your entries for the input mat:\n"); for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { scanf("%d", &mat[i][j]); } } /* sort the contents of the two dimensional array */ for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { tmp = x[i][j]; l = j + 1; for (k = i; k < n; k++) { l++; } l = 0; } } } /* print the result */ printf("\n"); printf("Resultant array:\n"); for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { printf("%d ", mat[i][j]); } printf("\n"); } getch ( ); } please fix the code thanks :) input : How many column number : 4 how many row number: 5 generated number: 3 9 6 7 1 7 9 0 3 2 5 6 2 1 4 6 2 1 3 4 sorted : 3 6 7 9 0 1 7 9 2 3 5 6 1 2 4 6 1 2 3 4
int main() {
int x[a][w];
int k, l, n, tmp;
printf("Enter the order of the array:");
scanf("%d", &n);
printf("Enter your entries for the input mat:\n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
scanf("%d", &mat[i][j]);
}
}
- Declared variables 'a' and 'w' as constant of type integers with size 100.
- A prompt asking user to enter the desired size of array.
- Input the array in variable 'x' and not an undeclared variable 'mat'.
- For sorting only two loops were used which is correct for 1 dimensional array but not for 2 dimensional array, therefore used 3 loops for sorting the array.
#include <stdio.h>
#include <windows.h>
const int a = 100;
const int w = 100;
int main () {
int x[a][w],mb[a][w];
int i,j,k,m,n;
int temp;
printf ("Enter the order of the matrix \n");
scanf ("%d %d", &m,&n);
printf ("Enter elements of the matrix \n");
for (i=0;i<m;++i) {
for (j=0;j<n;++j) {
scanf ("%d",&x[i][j]);
mb[i][j] = x[i][j];
}
}
printf ("The given matrix is \n");
for (i=0;i<m;++i) {
for (j=0;j<n;++j) {
printf (" %d",x[i][j]);
}
printf ("\n");
}
printf ("After soring the array in ascending order\n");
for (i=0;i<m;++i) {
for (j=0;j<n;++j) {
for (k=(j+1);k<n;++k) {
if (x[i][j] > x[i][k]) {
temp = x[i][j];
x[i][j] = x[i][k];
x[i][k] = temp;
}
}
}
}
/* End of outer for loop*/
for (i=0;i<m;++i) {
for (j=0;j<n;++j) {
printf (" %d",x[i][j]);
}
printf ("\n");
}
system("pause");
return 0;
}
Step by step
Solved in 3 steps with 2 images