Answer the question
In order to leave comments, you need to log in
How to work with matrices in C?
Hello. Please help with the task: Check whether the elements of the matrix columns are ordered. If not, then sort them in ascending order by sifting.
Here is the I/O matrix I did:
#include <stdio.h>
#include <math.h>
#include <conio.h>
#include <stdlib.h>
int main ()
{
int a[10][10];
int i, j, n, m, r, k, p;
printf ("Vvedite razmer matrici n=");
scanf ("%d", &n);
k=0;
for (i=0; i<n; i++)
for (j=0; j<n; j++)
{
printf("a[%d][%d]=", i, j);
scanf("%d", &a[i][j]);
}
for (i=0; i<n; i++)
{
for (j=0; j<n; j++)
printf("%2d", a[i][j]);
printf("\n");
}
}
for (i = 0; i < n; i++) //просматриваем каждую строку и
// проверяем, упорядочена ли она
{
k = p = 0;
for (j = 0; j < n - 1; j++)
if (A[i][j] <= A[i][j + 1])
k++;//упорядочена ли по возрастанию
else
p++;// упорядочена ли по убыванию
if (k != n - 1 && p != n - 1) // если строка не упорядочена,
//то выполняем сортировку
for (i=0;i<=n-2;i++)
if (A[i]>A[i+1])
{
r=A[i];
A[i]=A[i+1];
A[i+1]=r;
j=i;
while (A[j]<A[j-1]&&j>0)
{
r=A[j];
A[j]=A[j-1];
A[j-1]=r;
j=j-1;
}
}
Answer the question
In order to leave comments, you need to log in
The matrix is an array of arrays, run for each each line of the matrix separately.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question