C/C++
Задача по матрице C++
Получить матрицу, в которой крайние элементы равны 1, а остальные 0.
cout << введите число строк и столбцов;
cin >> str >> сol;
int **arr = new int *[str];
for (int i=0; i<str; i++)
arr[i] = new int [col]; // не забульте удалить по окончании
for (int i=0; i<str; i++)
for (int j=0; j<col; j++)
{
if (i==0 || i==str-1 || j==0 || j== col-1)
arr [i][j] = 1; else arr [i][j] = 0;
}
// если j = кранйие значения или i = крайние значения, то присвоить 1
// иначе присвоить 0.
cin >> str >> сol;
int **arr = new int *[str];
for (int i=0; i<str; i++)
arr[i] = new int [col]; // не забульте удалить по окончании
for (int i=0; i<str; i++)
for (int j=0; j<col; j++)
{
if (i==0 || i==str-1 || j==0 || j== col-1)
arr [i][j] = 1; else arr [i][j] = 0;
}
// если j = кранйие значения или i = крайние значения, то присвоить 1
// иначе присвоить 0.
#include <iostream>
#include <iomanip>
using namespace std;
unsigned integer(const char* msg) {
cout << msg;
unsigned value;
cin >> value;
cin.ignore(cin.rdbuf()->in_avail());
return value;
}
int main() {
const auto n = integer(" Rows: ");
const auto m = integer(" Columns: ");
puts("");
auto matrix = new int* [n];
const auto length = m * sizeof(matrix[0][0]);
for (auto i = 0U; i < n; ++i) {
matrix[i] = new int[m];
memset(matrix[i], 0, length);
}
for (auto i = 0U; i < n; ++i) {
for (auto j = 0U; j < m; ++j) {
if (!i || !j || i == n - 1) {
matrix[i][j] = 1;
} else {
matrix[i][m - 1] = 1;
break;
}
}
}
for (auto i = 0U; i < n; ++i) {
for (auto j = 0U; j < m; ++j) {
cout << setw(2) << matrix[i][j];
}
puts("");
}
for (auto i = 0U; i < n; ++i) delete[] matrix[i];
delete[] matrix;
system("pause > nul");
}
#include <iomanip>
using namespace std;
unsigned integer(const char* msg) {
cout << msg;
unsigned value;
cin >> value;
cin.ignore(cin.rdbuf()->in_avail());
return value;
}
int main() {
const auto n = integer(" Rows: ");
const auto m = integer(" Columns: ");
puts("");
auto matrix = new int* [n];
const auto length = m * sizeof(matrix[0][0]);
for (auto i = 0U; i < n; ++i) {
matrix[i] = new int[m];
memset(matrix[i], 0, length);
}
for (auto i = 0U; i < n; ++i) {
for (auto j = 0U; j < m; ++j) {
if (!i || !j || i == n - 1) {
matrix[i][j] = 1;
} else {
matrix[i][m - 1] = 1;
break;
}
}
}
for (auto i = 0U; i < n; ++i) {
for (auto j = 0U; j < m; ++j) {
cout << setw(2) << matrix[i][j];
}
puts("");
}
for (auto i = 0U; i < n; ++i) delete[] matrix[i];
delete[] matrix;
system("pause > nul");
}
Андрей Борисевич
Выдает ошибку
main.cpp:27:1: error: ‘memset’ was not declared in this scope
27 | memset(matrix[i], 0, length);
| ^~~~~~
main.cpp:11:1: note: ‘memset’ is defined in header ‘’; did you forget to ‘#include ’?
10 | #include
+++|+#include
11 | using namespace std;
main.cpp:27:1: error: ‘memset’ was not declared in this scope
27 | memset(matrix[i], 0, length);
| ^~~~~~
main.cpp:11:1: note: ‘memset’ is defined in header ‘’; did you forget to ‘#include ’?
10 | #include
+++|+#include
11 | using namespace std;
Андрей Борисевич
Спасибо, но слишком тяжело написано, даже не способен понять)
Серёга Зарубин
Один из создателей кода для windows Vista )
int a[3][3] = {1, 1, 1, 1, 0, 1, 1, 1, 1};
Я только одну матрицу знаю
Похожие вопросы
- Помогите новичку с программированием и матрицами C++
- Задача по программированию C++
- Помогите решить задачу пожалуйста, в C++
- Элементы Матрицы C++
- Задача на языке C.
- Помощь с задачей на Си (C)
- Задача на языке C++
- Помогите пожалуйста сделать задачу по программированию C++
- Задача про строки C++(выполнить нужно внутри 1ой функции)
- Задача по программированию C++
#include
#include
using namespace std;
int main()
{
int a,b;
cout <<a<> a >> b;
int **arr = new int *[a];
for (int i=0; i<a; i++)
arr[i] = new int [b]; // не забульте удалить по окончании
for (int i=0; i<a; i++)
for (int j=0; j<b; j++)
{
if (i==0 || i==a-1 || j==0 || j== b-1)
arr [i][j] = 1; else arr [i][j] = 0;
}
}
using namespace std;
int main()
{
setlocale(0, "");
int a, b;
cout << "Введите размер массива а и б" << endl;
cin >> a >> b;
int** arr = new int* [a];
for (int i = 0; i < a; i++)
arr[i] = new int[b]; // не забульте удалить по окончании
for (int i = 0; i < a; i++)
for (int j = 0; j < b; j++)
{
if (i == 0 || i == a - 1 || j == 0 || j == b - 1)
arr[i][j] = 1; else arr[i][j] = 0;
}
for (int i = 0; i < a; i++)
{
for (int j = 0; j < b; j++)
cout << arr[i][j] << " ";
cout << endl;
}
for (int i = 0; i < a; i++)
delete[] arr[i];
delete[] arr;
}