Предмет: Информатика, автор: Умный2чел

В произвольной матрице - отсортировать по убыванию элементы последовательности, расположенные после второго отрицательного числа.
Написать на языке Си

Ответы

Автор ответа: Helper211
1

Ответ:

Если что-то непонятно или не работает - спрашивайте.

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

#define M 4

#define N 4

int main()

{

int A[M][N]; // произвольная матрица

int i, j, indx = -1;

// Автоматически заполняем матрицу ,

// находим индекс второго отрицательного элемента ,

// выводим матрицу на экран

srand((unsigned)time(NULL));

printf("Matrix: \n");

for (i = 0; i < M; i++)

{

 for (j = 0; j < N; j++)

 {

  A[i][j] = (rand() % 201) - 100;

  printf("%5d ", A[i][j]);

  if ((A[i][j] < 0)&&(indx<0))

  {

   indx--;

   if (indx == -3) indx = i * M + j;

  }

 }

 printf("\n");

}

// Сортировка по убыванию элементов, расположенных

// после второго отрицательного числа

for (i = indx + 1; i < M * N - 1; i++)

{

 indx = i;

 for (j = i+1; j < M * N; j++)

  if (A[0][j] > A[0][indx]) indx = j;

 j = A[0][i];

 A[0][i] = A[0][indx];

 A[0][indx] = j;

}

// Вывод результата

printf("\nSort matrix: \n");

for (i = 0; i < M; i++)

{

 for (j = 0; j < N; j++)

  printf("%5d ", A[i][j]);

 printf("\n");

}

return 0;

}


Умный2чел: у меня не выдает ошибку error: ‘N’ undeclared (first use in this function) error: ‘M’ undeclared (first use in this function)
Helper211: Замените все 'N
Helper211: 'N' и 'M' в программе на числа, которые соответствуют кол-ву столбцов и строк (например, 5 и 4).
Похожие вопросы
Предмет: Английский язык, автор: ledy0608

СОКРАТИТЕ ПОЖАЛУЙСТА,ТОЛЬКО ЧТОБЫ СМЫСЛ ОСТАЛСЯ,НЕНУЖНО ДО 1 ПРЕДЛОЖЕНИЯ,ПРОСТО ЧУТЬ-ЧУТЬ ПОМЕНЬШЕ,ЛИШНЕЕ УБРАТЬ.

Nora

I've just done a degree in Drama and education, so I'll probably end up teaching drama in a secondary school, I suppose, but I'm not going to apply for anything this year. Right now, I want to get away from education and do some travelling, probably to the West Indies. I've got family in Trinidad, so I'm hoping to go there for a few months, and maybe find some kind of job there.

Dino

I'm just finishing my final exams in fashion, and it's been incredibly hard work. So first I'm having a holiday with my family back home in California. I'm leaving in five days, I'm so excited! After that I'm planning to do a master's degree in Fashion design, I've applied for a course, and I've been accepted if I get good grades in my exams, so I'm hoping!

Caroline

I'm about to graduate in modern languages, Spanish and Russian and I've already been accepted for a job as a trainee manager with an international clothing company. I'm due to start work at the beginning of September, so I'm having a couple of months' break over the summer. I'm going to find a beach somewhere nice and hot for a couple of weeks in August, hopefully, and then after that I'll start work. I'm really looking forward to it.

Alice

I've just finished a law degree, but I'm definitely not going to be a lawyer. I've realised I hate the law! I've basically got no idea at all what I want to do. I suppose I'll do some kind of temporary job for the next few months; work in a shop, or do telephone sales or something, and then see what happens. Hope that I get some ideas!