#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;
}
#include
using namespace std;
class Matrix {
private:
int **matrix;
public:
void setData(int row, int col) {
matrix = new int* [row];
for (int i = 0; i < row; i ) {
matrix[i] = new int[col];
}
for (int i = 0; i < row; i ) {
cout << '\n';
for (int j = 0; j < col; j ) {
matrix[i][j] = 1 + rand() % 9;
cout << matrix[i][j] << '\t';
}
}
cout << "\n====================================================\n";
};
void getData(int row,int col) {
for (int i = 0; i < col; i ) {
cout << '\n';
for (int j = 0; j < row; j ) {
cout << matrix[j][i] << '\t';
}
}
for (int i = 0; i < row; i ) {
delete[]matrix[i];
}
delete[]matrix;
};
};
int main() {
Matrix a;
a.setData(3,6);
a.getData(3,6);
return 0;
}
I am a beginner, and here is my code:
#include
using namespace std;
class ArrayConverter{
public:
int numbers[7][3] = {
0,0,0,
0,0,0,
0,0,0,
0,0,0,
0,0,0,
0,0,0,
0,0,0
};
int convertedNumbers[3][7] = {
0,0,0,0,0,0,0,
0,0,0,0,0,0,0,
0,0,0,0,0,0,0
};
void SetRandomElements(){
for(int i = 0; i < 7; i ){
for(int j = 0; j < 3; j ){
numbers[i][j] = rand() % 11;
}
}
}
void ShowOriginalArray(){
for(int i = 0; i < 7; i ){
for(int j = 0; j < 3; j ){
cout << numbers[i][j] << " ";
}
cout << endl;
}
}
void ConvertColumnsToRows(){
for(int i = 0; i < 3; i ){
for(int j = 0; j < 7; j ){
convertedNumbers[i][j] = numbers[j][i];
}
}
}
void ShowConvertedArray(){
for(int i = 0; i < 3; i ){
for(int j = 0; j < 7; j ){
cout << convertedNumbers[i][j] << " ";
}
cout << endl;
}
}
ArrayConverter(){
SetRandomElements();
}
};
int main(){
ArrayConverter converter;
converter.ShowOriginalArray();
converter.ConvertColumnsToRows();
cout << endl;
convert.ShowConvertedArray();
return 0;
}
#include
using namespace std;
class ChangeArr
{
public:
void setArr();
void change();
private:
int** arr;
int row = 0;
int col = 0;
};
void main()
{
setlocale(LC_ALL, “ru”);
srand(time(NULL));
ChangeArr arr;
arr.setArr();
arr.change();
}
void ChangeArr::setArr()
{
cout << "Укажите размер массива:" <> row;
cin >> col;
arr = new int* [row];
for (int i = 0; i < row; i )
{
arr[i] = new int[col];
}
cout << "Заполните массив:" << endl;
for (int i = 0; i < row; i )
{
for (int j = 0; j < col; j )
{
cout << "Значение ячейки – [" << i << "]" << "[" << j << "]" <> arr[i][j];
}
}
cout << endl;
cout << "Ваш массив:" << endl<<endl;
for (int i = 0; i < row; i )
{
for (int j = 0; j < col; j )
{
cout << arr[i][j] << " ";
}
cout << endl;
}
}
void ChangeArr::change()
{
int** tempArr = new int* [col];
for (int i = 0; i < col; i )
{
tempArr[i] = new int[row];
}
for (int i = 0; i < row; i )
{
for (int j = 0; j < col; j )
{
tempArr[j][i] = arr[i][j];
}
}
cout << "Замена строчек на столбци " << endl << endl;
for (int i = 0; i < col; i )
{
for (int j = 0; j < row; j )
{
cout << tempArr[i][j] << " ";
}
cout << endl;
}
for (int i = 0; i < col; i )
{
delete[] arr[i];
}
delete[]arr;
arr = nullptr;
for (int i = 0; i < col; i )
{
delete[] tempArr[i];
}
delete[]tempArr;
tempArr = nullptr;
}
#include
#include
using namespace std;
class Matrix
{
private:
int** arr;
int row;
int col;
you** bufArr;
int bufRow;
int bufCol;
public:
void Fill_array(int rows_value, int cols_value)
{
row = rows_value;
col = cols_value;
bufRow = rows_value;
bufCol = cols_value;
arr = new int* [row];
for (int i = 0; i < row; i )
{
arr[i] = new int[col];
}
for (int i = 0; i < row; i )
{
for (int j = 0; j < col; j )
{
arr[i][j] = rand() % 90 + 10;
}
}
}
void Change_rows_with_cols()
{
bufArr = new int* [row];
for (int i = 0; i < row; i )
{
bufArr[i] = new int[col];
}
for (int i = 0; i < row; i )
{
for (int j = 0; j < col; j )
{
bufArr[i][j] = arr[i][j];
}
}
int buf = row;
row = col;
col = buff;
for (int i = 0; i < col; i )
{
delete[] arr[i];
}
delete[] arr;
arr = new int* [row];
for (int i = 0; i < row; i )
{
arr[i] = new int[col];
}
for (int i = 0; i < row; i )
{
for (int j = 0; j < col; j )
{
arr[i][j] = bufArr[j][i];
}
}
}
void PrintArr()
{
for (int i = 0; i < row; i )
{
for (int j = 0; j < col; j )
{
cout << arr[i][j] << " ";
}
cout << endl;
}
}
~Matrix()
{
for (int i = 0; i < bufRow; i )
{
delete[] bufArr[i];
}
delete[]bufArr;
for (int i = 0; i < row; i )
{
delete[] arr[i];
}
delete[]arr;
}
};
int main()
{
srand(time(NULL));
Matrix first;
first.Fill_array(4, 2);
first.PrintArr();
cout << "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n" << endl;
first.Change_rows_with_cols();
first.PrintArr();
}
class Massive {
private:
you massive[2][4]{
{11,33,55,77},
{22,44,66,88}
};
public:
void vertical() {
std::cout << solid[0][0] << " " << solid[1][0] << std::endl;
std::cout << solid[0][1] << " " << solid[1][1] << std::endl;
std::cout << solid[0][2] << " " << solid[1][2] << std::endl;
std::cout << solid[0][3] << " " << solid[1][3] << std::endl;
}
void horizon() {
std::cout << solid[0][0] << " " << solid[0][1] << " " << solid[0][2] << " " << solid[0][3] << " " << std::endl;
std::cout << solid[1][0] << " " << solid[1][1] << " " << solid[1][2] << " " << solid[1][3] << " " << std::endl;
}
};
int main() {
setlocale(LC_ALL, "rus");
Massively massive_two;
std::cout << "было:" << std::endl;
massiv_two.vertical();
std::cout << "стало:" << std::endl;
massiv_two.gorizont();
}
Explain why memory is allocated here at all? It's absolutely needed here?