Вопрос Перевернуть все нечётные строки матрицы. C++

Регистрация
4 Апр 2013
Сообщения
80
Репутация
0
Спасибо
1
Монет
0
Вот создали матрицу, дальше не знаю как:

#include "stdafx.h"
#include "iostream"
#include "math.h"
#include "stdio.h"
#include "conio.h"
#include "ctime"
#include "cstdlib"
#include "vector"
#include "deque"

using namespace std;

void mass(int **arr,int d, int c)
{
for (int i=1;i<d+1;i++)
for (int j=1;j<c+1;j++)
arr[j]=((rand()%10));
cout <<"Матрица: "<<endl;
for (int i=1;i<d+1;i++)
{
for(int j=1;j<c+1;j++)
cout << arr[j] <<" ";
cout << endl;
}
}

int main()
{
srand(time(NULL));
setlocale(LC_ALL, "Russian");
system("color F4");
int n,m;
cout <<"Введите длину строки: ";
cin >> m;
cout <<"Введите высоту столбика: ";
cin >> n;
cout << endl;
int **mess = new int * [n+1];
for (int i=1; i<n+1; i++)
mess = new int [m];
mass(mess,n,m);
_getch();
return 0;

}
 
Вот так: #include #include "math.h" #include "stdio.h" #include "conio.h" #include "ctime" #include "cstdlib" #include "vector" #include "deque" void print_array(int** arr,int Ylen,int Xlen) { for(int i = 0;i<Ylen;i++) { for(int j = 0;j<Xlen;j++) std::cout<<arr[j]<<" "; std::cout<<std::endl; } } int main(int argc, char**argv) { int Ylen = 5; int Xlen = 5; int** arr = new int*[Ylen]; for(int i = 0; i < Ylen; i++) { arr = new int[Xlen]; for(int j=0;j<Xlen;j++) { arr[j]= (rand()%10); } } //Первый вывод std::cout<<"Old matrix:"<<std::endl; print_array(arr,Ylen,Xlen); for(int i = 0; i < Ylen; i++) { if((i + 1) % 2 == 1) { // проверяем строку for(int j = 0; j < Xlen; j++) { // реверсим int elem = arr[(Xlen - 1) - j]; arr[(Xlen - 1) - j] = arr[j]; arr[j] = elem; if(j == (Xlen - 1) - j) break; } } } std::cout<<"New matrix:"<<std::endl; //второй вывод print_array(arr,Ylen,Xlen); return 0; }
 
Назад
Сверху