#include <iostream>
using namespace std;
class Matrix
{
int ** matrixInClass;
public:
void setMatrix(int rowAmount, int colAmount);
void changeRowAndColumn(int rowAmount, int colAmount);
};
//==============================================================
void Matrix::setMatrix(int rowAmount, int colAmount) // заполнение массива данными
{
matrixInClass = new int*[rowAmount]; // выделяем память для матрицы
for (int i = 0; i < rowAmount; i++)
{
matrixInClass[i] = new int[colAmount];
}
for (int i = 0; i < rowAmount; i++) // записываем значения в массив
{
cout << " | ";
for (int j = 0; j < colAmount; j++)
{
matrixInClass[i][j] = i + j;
cout << matrixInClass[i][j] << " ";
}
cout << " | " << endl;
}
}
//==============================================================
void Matrix::changeRowAndColumn(int rowAmount, int colAmount)
{
int** tempMatrix = new int*[colAmount]; // выделяем память для временной матрицы
for (int i = 0; i < colAmount; i++)
{
tempMatrix[i] = new int[rowAmount];
}
for (int i = 0; i < colAmount; i++) // копируем столбцы в строки, а строки в столбцы
{
for (int j = 0; j < rowAmount; j++)
{
tempMatrix[i][j] = matrixInClass[j][i];
}
cout << endl;
}
for (int i = 0; i < rowAmount; i++) // Освобождаем память перед выделением новой
{
delete[] matrixInClass[i];
}
delete[] matrixInClass;
matrixInClass = new int*[colAmount]; // выделяем новую память
for (int i = 0; i < colAmount; i++)
{
matrixInClass[i] = new int[rowAmount];
}
for (int i = 0; i < colAmount; i++) // копируем из временной матрицы
{
cout << "|";
for (int j = 0; j < rowAmount; j++)
{
matrixInClass[i][j] = tempMatrix[i][j];
cout << matrixInClass[i][j] << " ";
}
cout << "|" << endl;
}
for (int i = 0; i < colAmount; i++) // Освобождаем память временной матрицы
{
delete[] tempMatrix[i];
}
delete[] tempMatrix;
}
int main()
{
setlocale(LC_ALL, "rus");
int rowAmount;
int colAmount;
cout << "Введите количество строк двумерного массива: ";
cin >> rowAmount;
cout << "Введите количество столбцов двумерного массива: ";
cin >> colAmount;
Matrix Object;
Object.setMatrix(rowAmount, colAmount);
cout << "\nЗамена значений строк на значения столбцов: ";
Object.changeRowAndColumn(rowAmount, colAmount);
return 0;
}
That's how I made the third task:
#include “pch.h”
#include
#include
#include
#include
#include
#include
using namespace std;
class Matrix
{
int rows, cols;
int arr[100][100];
int i, j;
public:
void Fill_array()
{
this->cols = cols;
this->rows = rows;
cout <> rows; cout << endl;
cout <> cols; cout <arr[rows][cols];
for ( i = 0; i < rows; i )
{
for ( j = 0; j < cols; j )
{
arr[i][j] = rand() % 10;
}
}
}
void Change_array()
{
cout << "Matrix\n\n";
for ( i = 0; i < rows; i )
{
for ( j = 0; j < cols; j )
{
cout << setw(4) << arr[i][j];
}
cout << endl;
}
cout << "\nModified matrix\n\n";
for ( j = 0; j < cols; j )
{
for ( i = 0; i < rows; i )
{
cout << setw(4) << arr[i][j];
}
cout << endl;
}
}
};
int main()
{
SetConsoleCP(1251);
SetConsoleOutputCP(1251);
srand(time(NULL));
Matrix a;
a.Fill_array();
a.Change_array();
return 0;
}
//1 the task.
class Tiles
#include
{public:
string brand;
int size_h;
int size_w;
double price; int data;
void setData(int x,int y)
{
size_h=x;
size_w=y;
}
void getData()
{
cout << "\tНазвание = " << brand << "\tВысота= " << size_h << "\tШирина = " << size_w << "\tСтоимость = " << price << endl;
}
void Print()
{
cout <<"\tНазвание = "<< brand << "\tВысота= " << size_h << "\tШирина = " << size_w << "\tСтоимость = " << price << endl;
}
};
int main()
{
setlocale(LC_ALL, "rus");
Tiles Keramika;
Keramika.brand = "Керамика из Италии";
Keramika.price = 2.3;
Tiles bKeramika;
Keramika.setData(5, 6);
Keramika.getData();
bKeramika.price=30.5;
bKeramika.size_h=20;
bKeramika.size_w = 10;
bKeramika.brand = "Керамика из Франции";
bKeramika.Print();
}
//void FillArray-class method. void Exchange-separate function
#include
using namespace std;
int **arr;
class Inverse
{
public:
void FillArray(int raw, int col)
{
arr = new int*[raw];
for (int i = 0; i < raw; i )
{
arr[i] = new int[col]; //assigning to each element of a new line array dyn, responsible for speakers
}
for (int i = 0; i < raw; i )
{
cout << "|";
for (int j = 0; j < col; j )
{
arr[i][j] = rand() % 20;
cout << arr[i][j] << "\t";
}
cout << "|" << endl;
}
}
};
void Exchange(int raw, int col)
{
** = new NARR you're *[col];
for (int i = 0; i < col; i )
{
fool[i] = new int[raw]; //assigning to each element of a new line array dyn, responsible for speakers
}
for (int i = 0; i < col; i )
{
for (int j = 0; j < raw; j )
{
fool[i][j] = arr[j][i];
}
cout << endl;
}
for (int i = 0; i < raw; i )
{
delete[] arr[i];//rid of old data from the array
}
delete[] arr;
arr = new int*[col];
for (int i = 0; i < col; i )
{
arr[i] = new int[raw];
}
for (int i = 0; i < col; i )
{
cout << "|";
for (int j = 0; j < raw; j )
{
arr[i][j] = fool[i][j];
cout << arr[i][j] << "\t";
}
cout << "|" << endl;
}
for (int i = 0; i < col; i )
{
delete[] fool[i];
}
delete[] fool;
}
int main()
{
int raw = 7;
int col = 10;
Inverse a;
a.FillArray(raw, col);
Exchange(raw, col);
return 0;
}
Without pointers, and one method. Who gives a warning, add key -std = c ++ 11 compiler. But work without it.
#include
using namespace std;
class mass
{
int massStroka[5][2]={};
int massCtolbez[2][5]={};
public:
void zapolnMass()
{
int a =0;
int kolvoStr;
int kolvoCtlb;
cout<<" Enter the number of rows. "<>kolvoStr;
cout<<" Enter the number of columns. "<>kolvoCtlb;
for (int i=0;i<kolvoStr;i )
{
cout<<endl;
for(int j =0;j<kolvoCtlb;j )
{
a++;
massStroka[i][j] = a;
massCtolbez[j][i] = a ;
cout << "|" ;
cout<<massStroka[i][j];
cout << "|" ;
}
}
cout<<endl;
for (int z=0;from<kolvoCtlb;with ++)
{
cout<<endl;
cout << "|";
for(int k =0;k<kolvoStr;k )
{
cout<<massCtolbez[from][k];
}
cout << "|";
}
}
};
int main ()
{
setlocale(LC_ALL, "rus");
mass a1;
a1.zapolnMass();
return 0;
}
3 the task. Prompt, all is good?
#include
#include
using namespace std;
class Matrix
{
private:
int arr[3][4];
public:
void vivodnorn()
{
for (int i = 0; i < 3; i )
{
for (int k = 0; k < 4; k )
{
arr[i][k] = rand() % 132;
cout << arr[i][k] << "\t";
}
cout << endl;
}
}
void vivodfuc()
{
for (int i = 0; i < 4; i )
{
for (int k = 0; k < 3; k )
cout << arr[k][i] << "\t";
cout << endl;
}
}
};
int main(int vage)
{
setlocale(0, "");
srand(time(0));
Matrix a;
a.vivodnorn();
cout << endl << endl;
a.vivodfuc();
return 0;
}
In the third task, a memory leak without a destructor
Through the class constructor 1 the task is easier to do
#include
#include
using namespace std;
class Tiles {
private:
double size_h;
double size_w;
double price;
string Brend;
public:
Tiles(double size_h1, double size_w1, double price1, string Brend1) {
size_h = size_h1;
size_w = size_w1;
price = price1;
Brand = Brand1;
}
void Getobject() {
cout << size_h << " X " << size_w << endl << "$" << price << endl << "Bramnd " << Brand << endl;
}
};
void main() {
Tiles a(12,14.5,20.5,"EPTA");
a.Getobject();
}
Second number
#include
#include
#include
using namespace std;
class Children {
private:
int age;
string name;
string surname;
public:
void SetChildren() {
cin >> age;
cin >> name;
cin >> surname;
}
void Print() {
cout << "имя "<<name<<endl<<"Фамилия "<<surname<<endl<<"Возраст"<<age<<endl;
}
};
void main() {
SetConsoleCP(1251);
SetConsoleOutputCP(1251);
Children a;
a.SetChildren();
a.Print();
}