A
A
Anthony_19982019-11-13 12:21:20
Arrays
Anthony_1998, 2019-11-13 12:21:20

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");
}               
}

I check for ordering like this:
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) // если строка не упорядочена,
    //то выполняем сортировку

I found the sifting method for the array itself:
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;
  }
}

But how to do it for the matrix?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
Farwisdomer, 2019-11-14
@Farwisdomer

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 question

Ask a Question

731 491 924 answers to any question