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;
}
Другие языки программирования и технологии
Сформировать двумерный массив C++
#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;
}
а добавление сама напишешь )
#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;
}
а добавление сама напишешь )
жди. Если нужно сообщи
Рафаэль Гафурбаев
матрица произвольного размера типа int?
Похожие вопросы
- Дан двумерный массив C(m,n). Найти произведение элементов, больших среднего значения. Помогите пожалуйста!!!
- Дан двумерный массив C(m,n). Найти произведение элементов, больших среднего значения. Помогите пожалуйста!!! На паскале
- помогите срочно надо Квадратные массивы тема: Двумерные массивы на языке C++
- .помогите пожалуйста двумерный массив на языке c++
- Каким образом в c++ можно передать двумерный массив в фунцкию?
- дан двумерный массив С(3,4).Получите новый массив А путём увеличения всех элементов исходного массива на число С.
- Необходимо упорядочить строки двумерного массива, по возрастанию первого эл-та. СИ.
- Как можно передать ДВУМЕРНЫЙ массив в функцию в С++, не создавая его, как глобальный. Пример ниже:
- Что такое Двумерный массив?
- Ассемблер двумерный массив
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;
}
}
}