C/C++
Написать программу на языке C++
Дана матрица A[m×n]. Массиву B[m] присвойте сумму элементов каждой строки и найдите его минимальный элемент.
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
int n,m,min;
int ** A, * B;
do
{
cout << "Введите m: ";
cin >> m;
}while(m < 1);
do
{
cout << "Введите n: ";
cin >> n;
}while(n < 1 || n == m);
A = new int*[m];
for(int i = 0; i < m; i++)
A[i] = new int[n];
B = new int[m];
srand(time(nullptr));
rand();
cout << "A:" << endl;
for(int x = 0; x < m; x++)
{
B[x] = 0;
for(int y = 0; y < n; y++)
{
A[x][y] = rand() % 9 + 1; // [1;9]
cout << A[x][y] << '\t';
B[x] += A[x][y];
}
cout << endl;
}
cout << endl;
cout << "B: ";
for(int z = 0; z < m; z++)
cout << B[z] << ' ';
cout << endl << endl;
min = B[0];
for(int u = 0; u < m; u++)
if(B[u] < min) min = B[u];
cout << "Минимальный элемент B равен " << min << '.' << endl;
if(B != (int*) nullptr)
{
delete [] B;
B = (int*) nullptr;
}
else return 1;
if(A != (int**) nullptr)
{
for(int i = 0; i < m; i++)
{
if(A[i] != (int*) nullptr)
{
delete [] A[i];
A[i] = (int*) nullptr;
}
else return 1;
}
delete [] A;
A = (int**) nullptr;
}
else return 1;
return 0;
}
#include <ctime>
using namespace std;
int main()
{
int n,m,min;
int ** A, * B;
do
{
cout << "Введите m: ";
cin >> m;
}while(m < 1);
do
{
cout << "Введите n: ";
cin >> n;
}while(n < 1 || n == m);
A = new int*[m];
for(int i = 0; i < m; i++)
A[i] = new int[n];
B = new int[m];
srand(time(nullptr));
rand();
cout << "A:" << endl;
for(int x = 0; x < m; x++)
{
B[x] = 0;
for(int y = 0; y < n; y++)
{
A[x][y] = rand() % 9 + 1; // [1;9]
cout << A[x][y] << '\t';
B[x] += A[x][y];
}
cout << endl;
}
cout << endl;
cout << "B: ";
for(int z = 0; z < m; z++)
cout << B[z] << ' ';
cout << endl << endl;
min = B[0];
for(int u = 0; u < m; u++)
if(B[u] < min) min = B[u];
cout << "Минимальный элемент B равен " << min << '.' << endl;
if(B != (int*) nullptr)
{
delete [] B;
B = (int*) nullptr;
}
else return 1;
if(A != (int**) nullptr)
{
for(int i = 0; i < m; i++)
{
if(A[i] != (int*) nullptr)
{
delete [] A[i];
A[i] = (int*) nullptr;
}
else return 1;
}
delete [] A;
A = (int**) nullptr;
}
else return 1;
return 0;
}
#include "iostream"
#include "algorithm"
#include "ctime"
#include "cstdlib"
using namespace std;
int main(){
int n,m; cout<<"N M: "; cin>>n>>m; srand(time(NULL));
int **a=new int*[n]; for(int i=0;i< n;i++)a[i]=new int[m]; int *b=new int[n];
for(int i=0;i< n;i++)for(int j=0;j< m;j++)a[i][j]=rand()%199-99;
for(int i=0;i< n;i++){for(int j=0;j< m;j++)cout<<'\t'<< a[i][j]; cout<<'\n';}
for(int i=0;i< n;i++)b[i]=accumulate(a[i],a[i]+m,0); cout<<'\n';
cout<<"b=\t"; for(int i=0;i< n;i++)cout<< b[i]<<'\t';
cout<<"\n\nmin="<<*min_element(b,b+n)<<'\n';}
#include "algorithm"
#include "ctime"
#include "cstdlib"
using namespace std;
int main(){
int n,m; cout<<"N M: "; cin>>n>>m; srand(time(NULL));
int **a=new int*[n]; for(int i=0;i< n;i++)a[i]=new int[m]; int *b=new int[n];
for(int i=0;i< n;i++)for(int j=0;j< m;j++)a[i][j]=rand()%199-99;
for(int i=0;i< n;i++){for(int j=0;j< m;j++)cout<<'\t'<< a[i][j]; cout<<'\n';}
for(int i=0;i< n;i++)b[i]=accumulate(a[i],a[i]+m,0); cout<<'\n';
cout<<"b=\t"; for(int i=0;i< n;i++)cout<< b[i]<<'\t';
cout<<"\n\nmin="<<*min_element(b,b+n)<<'\n';}
Похожие вопросы
- Здравствуйте, помогите пожалуйста написать программы на языке C++.
- Написать Программу на языке C++.
- Нужно написать программу на языке C++
- Вычислить значение функции F(x), разложенной в ряд и сходящейся в указанной области. Написать программу на языке C++
- Написать программу на языке c++
- Помогите написать программу в языке C++.
- Написать программу на языке Си
- Набрать текст программы на языке (C++) реализующий уравнение
- Написать программу на языке программирования C++
- Написал код для языка C, но работает не правильно