Другие языки программирования и технологии

Сформировать двумерный массив C++

1)заполнить матрицу рандомными числами и вывести на экран
2)удалить 5 последних элементов
3)добавить в начало 3 элемента со значением M[i+1]-2
4)распечатать массив
Здесь создание, заполнение и вывод. Помогите дописать остальное, пожалуйста, очень срочно надо!!
#include
#include
#include

using namespace std;

int main()
{
setlocale(LC_CTYPE, "Russian");
int rows, cols;
cout << "Введите количество строк: ";
cin >> rows;
cout << "Введите кол-во столбцов: ";
cin >> cols;
//выделение
int** arr = new int*[rows];
for (int i = 0; i < rows; i++)
arr[i] = new int[cols];

//Заполнение массва//
for (int row = 0; row < rows; ++row) {
for (int column = 0; column < cols; ++column) {
arr[row][column] = rand()%10;
}
}
//вывод массива
for (int row = 0; row < rows; ++row) {
for (int column = 0; column < cols; ++column) {
cout << arr[row][column] << " ";
}
cout << endl;
}

//освобождение памяти
for (int i = 0; i < rows; i++)
delete[] arr[i];
delete[] arr;

system("pause");
return 0;
}
#include
#include
#include
using namespace std;

int** initArray(int rows, int cols)
{
//выделение
int** arr = new int*[rows];
for (int i = 0; i < rows; i++)
arr[i] = new int[cols];

//Заполнение массва//
for (int row = 0; row < rows; ++row) {
for (int column = 0; column < cols; ++column) {
arr[row][column] = rand()%10;
}
}
return arr;
}
void printArray(int** arr, int rows, int cols, int last_row = -1)
{
//вывод массива
if(arr == nullptr) cout << "Array empty"<<endl;
cout << endl;
for (int row = 0; row < rows; ++row)
{
for (int column = 0; column < cols; ++column)
{
cout << arr[row][column] << " ";
if(row == rows - 1 && column == last_row - 1)break;
}
cout << endl;
}

}
void removeFromArray(int** &arr, int &rows, int &cols, int& last_row, int cnt = 5)
{
int trows = rows - cnt/cols;
int tlast_row = -1;
int** temp = nullptr;
if(trows > 0)
{
tlast_row = cols - cnt%cols;
temp = new int*[trows];

for (int row = 0; row < trows; row++)
{
if(row != trows - 1)
temp[row] = new int[cols];
else
temp[row] = new int[tlast_row];
for (int column = 0; column < cols; column++)
{
temp[row][column] = arr[row][column];
if(row == trows - 1 && column == tlast_row - 1)break;
}

}
}

for (int i = 0; i < rows; i++)
delete[] arr[i];
arr = temp;
rows = trows;
last_row = tlast_row;

}

int main()
{
setlocale(LC_CTYPE, "Russian");
int rows, cols, last_row = -1;
cout << "Введите количество строк: ";
cin >> rows;
cout << "Введите кол-во столбцов: ";
cin >> cols;
int** arr = initArray(rows, cols);
printArray(arr, rows, cols);
removeFromArray(arr, rows, cols,last_row, 5);

printArray(arr, rows, cols, last_row);
//освобождение памяти
for (int i = 0; i < rows; i++)
delete[] arr[i];
delete[] arr;

system("pause");
return 0;
}

а добавление сама напишешь )
Илья Кузнецов
Илья Кузнецов
20 058
Лучший ответ
Рафаэль Гафурбаев http://pastebin.com/C4UM06D9 динамические массивы с классом
Денис Кузнецов Спасибо Вам большое) Можете объяснить условие if (row == rows - 1 && column == last_row - 1)break;
Денис Кузнецов И я немного не понимаю как тут происходит все
void removeFromArray(int** &arr, int &rows, int &cols, int& last_row, int cnt = 5)
{
int trows = rows - cnt / cols;
int tlast_row = -1;
int** temp = nullptr;
if (trows > 0)
{
tlast_row = cols - cnt%cols;
temp = new int*[trows];

for (int row = 0; row < trows; row++)
{
if (row != trows - 1)
temp[row] = new int[cols];
else
temp[row] = new int[tlast_row];
for (int column = 0; column < cols; column++)
{
temp[row][column] = arr[row][column];
if (row == trows - 1 && column == tlast_row - 1)break;
}

}
}
жди. Если нужно сообщи
Рафаэль Гафурбаев матрица произвольного размера типа int?