• B правой части каждого сообщения есть стрелки и . Не стесняйтесь оценивать ответы. Чтобы автору вопроса закрыть свой тикет, надо выбрать лучший ответ. Просто нажмите значок в правой части сообщения.

  • Курсы Академии Кодебай, стартующие в мае - июне, от команды The Codeby

    1. Цифровая криминалистика и реагирование на инциденты
    2. ОС Linux (DFIR) Старт: 16 мая
    3. Анализ фишинговых атак Старт: 16 мая Устройства для тестирования на проникновение Старт: 16 мая

    Скидки до 10%

    Полный список ближайших курсов ...

Лабораторная

  • Автор темы kirkl
  • Дата начала
K

kirkl

задание
1.Используя функции сформировать с помощью ДСЧ двумерный массив и вывести его
на печать
2.Все четные строки матрицы сдвинуть циклически на К элементов вправо

первое задание я сделал а вот второе не понимаю=(

[ code=cpp ]#include <conio.h>
#include <stdlib.h>
#include <math.h>
#include <iostream>
#include <iomanip>
#include <time.h>
using namespace std;

void mass(int n, int m)
{
int a[100][100];
int i;
cout <<"ishedni massiv:"<<"\n";
srand(time(NULL));
for (i=0; i<n; i++)
{
for (int j=0; j<m; j++)
{
a[j]=rand()%50;
cout << a[j] << "\t";
}
cout <<"\n";
}
}


int main()
{
int n,m;
cout << "vvedite chislo strok: ";
cin >> n;
cout << "vvedite chislo stolbcov: ";
cin >> m;
mass(n,m);
getch();
return 0;
}
[ / code ]
 
R

rlib

Ну, я бы вот так...

main.c
C++:
#include <stdio.h>
#include "matrix.h"

#define ROWS 4
#define COLS 2
int main(void)
{
Matrix m, m1;
m=CreateMatrix(ROWS,COLS);
m[0][0]=2.1;
m[0][1]=4.85;
m[1][0]=5.2;
m[1][1]=7.2;
m[2][0]=5.4;
m[2][1]=6.5;
m[3][0]=9.8;
m[3][1]=10.8;
printf("Initial matrix:\n");
PrintMatrix(m,ROWS,COLS);
m1=RightCycleEvenRows(m,ROWS,COLS,1);
printf("Shifted matrix:\n");
PrintMatrix(m1,ROWS,COLS);
DestroyMatrix(m,ROWS);
DestroyMatrix(m1,ROWS);
return 0;
}

Matrix.h
C++:
#ifndef MATRIX_H
#define MATRIX_H

#define _ELEMTYPE_ 'd'

#if _ELEMTYPE_ == 'd'
#define ELEMTYPE double
#define MYPRINT printf ("%f ", m[i][j] )


#elif _ELEMTYPE_ == 'f'
#define ELEMTYPE float
#define MYPRINT printf ("%f ", m[i][j] )

#elif _ELEMTYPE_ == 'i'
#define ELEMTYPE int
#define MYPRINT printf ("%i ", m[i][j] )

#endif



typedef ELEMTYPE ElemType;
typedef ElemType **Matrix;
typedef unsigned int uint;

Matrix RightCycleAllRows(Matrix m, uint rows, uint cols, uint shift);
Matrix RightCycleEvenRows(Matrix m, uint rows, uint cols, uint shift);
Matrix CreateMatrix(uint rows, uint cols);
Matrix CopyMatrix(Matrix src, uint rows, uint cols);
void DestroyMatrix(Matrix m, uint rows);
void PrintMatrix(Matrix m, uint rows, uint cols);
#endif // MATRIX_H

Matrix.c
C++:
#include "matrix.h"
#include <malloc.h>
#include <stdio.h>
Matrix CreateMatrix(uint rows, uint cols) {
uint i;
Matrix m = (Matrix)malloc(sizeof(ElemType*)*rows);
for (i=0; i<rows; ++i)
m[i] = (ElemType*)malloc(sizeof(ElemType)*cols);
return m;
}

void DestroyMatrix(Matrix m, uint rows) {
uint i;
for (i=0; i<rows; ++i)
free(m[i]);
free(m);
}

void PrintMatrix(Matrix m, uint rows, uint cols) {
uint i,j;
for (i=0; i<rows; ++i)
for (j=0; j<cols; ++j) {
MYPRINT;
if (j==cols-1)
printf("\n");
}
}

Matrix RightCycleAllRows(Matrix m, uint rows, uint cols, uint shift) {
Matrix nm; // new matrix
uint i,j,ni, nj;
nm = CreateMatrix(rows, cols);

for (i=0; i<rows;++i) {
for (j=0;j<cols;++j) {
// calculate new coordinates
nj = (j+shift)%cols;
ni = ((j+shift)/cols+i)%rows;
nm[ni][nj] = m[i][j];
}
}
return nm;
}

Matrix CopyMatrix(Matrix src, uint rows, uint cols) {
Matrix res;
uint i, j;
res = CreateMatrix(rows, cols);
for (i=0; i<rows; ++i)
for (j=0; j<cols; ++j)
res[i][j]=src[i][j];
return res;
}

Matrix RightCycleEvenRows(Matrix m, uint rows, uint cols, uint shift) {
Matrix res, erm, serm;
uint i,j,k, evenrows;
res = CopyMatrix(m, rows, cols); // result matrix;
evenrows = rows/2;
erm = CreateMatrix(evenrows, cols); // even rows matrix
for (i=1, k=0; i<rows; i+=2) {
for (j=0; j<cols; ++j) {
erm[k][j] = m[i][j]; // copy even rows
}
k+=1;
}
serm = RightCycleAllRows(erm, evenrows, cols, shift);
DestroyMatrix(erm, evenrows);
for (i=1, k=0; i<rows; i+=2) {
for (j=0; j<cols; ++j) {
res[i][j]=serm[k][j];
}
k+=1;
}
DestroyMatrix(serm, evenrows);
return res;
}





задание
1.Используя функции сформировать с помощью ДСЧ двумерный массив и вывести его
на печать
2.Все четные строки матрицы сдвинуть циклически на К элементов вправо

первое задание я сделал а вот второе не понимаю=(

[ code=cpp ]#include <conio.h>
#include <stdlib.h>
#include <math.h>
#include <iostream>
#include <iomanip>
#include <time.h>
using namespace std;

void mass(int n, int m)
{
int a[100][100];
int i;
cout <<"ishedni massiv:"<<"\n";
srand(time(NULL));
for (i=0; i<n; i++)
{
for (int j=0; j<m; j++)
{
a[j]=rand()%50;
cout << a[j] << "\t";
}
cout <<"\n";
}
}


int main()
{
int n,m;
cout << "vvedite chislo strok: ";
cin >> n;
cout << "vvedite chislo stolbcov: ";
cin >> m;
mass(n,m);
getch();
return 0;
}
[ / code ]
 
Мы в соцсетях:

Обучение наступательной кибербезопасности в игровой форме. Начать игру!