Две задачи в одной программе:
#include <iostream>
#include <iomanip>
#include <ctime>
using namespace std;
int randInt(int min, int max){
return min + (rand() % (max - min + 1));
}
int main(int argc, char *argv[]){
const int DIM = 5;
int matrix[DIM][DIM];
const int Z = 0;
srand(time(0));
for (int i = 0; i < DIM; i++) {
for (int j = 0; j < DIM; j++) {
matrix[i][j] = randInt(-5, 5);
cout << setw(4) << matrix[i][j] << ' ';
cout << endl;
cout << "x > " << Z << ":" << endl;
int count = 0;
int sum = 0;
if (matrix[j][i] > Z)
count++;
if (matrix[i][j] < 0)
sum += matrix[i][j];
cout << setw(4) << count << ' ';
count = 0;
cout << "Sum: " << sum << endl;
return 0;
По отдельности:
1)
2)
Copyright © 2024 SCHOLAR.TIPS - All rights reserved.
Answers & Comments
Две задачи в одной программе:
#include <iostream>
#include <iomanip>
#include <ctime>
using namespace std;
int randInt(int min, int max){
return min + (rand() % (max - min + 1));
}
int main(int argc, char *argv[]){
const int DIM = 5;
int matrix[DIM][DIM];
const int Z = 0;
srand(time(0));
for (int i = 0; i < DIM; i++) {
for (int j = 0; j < DIM; j++) {
matrix[i][j] = randInt(-5, 5);
cout << setw(4) << matrix[i][j] << ' ';
}
cout << endl;
}
cout << endl;
cout << "x > " << Z << ":" << endl;
int count = 0;
int sum = 0;
for (int i = 0; i < DIM; i++) {
for (int j = 0; j < DIM; j++) {
if (matrix[j][i] > Z)
count++;
if (matrix[i][j] < 0)
sum += matrix[i][j];
}
cout << setw(4) << count << ' ';
count = 0;
}
cout << endl;
cout << endl;
cout << "Sum: " << sum << endl;
cout << endl;
return 0;
}
По отдельности:
1)
#include <iostream>
#include <iomanip>
#include <ctime>
using namespace std;
int randInt(int min, int max){
return min + (rand() % (max - min + 1));
}
int main(int argc, char *argv[]){
const int DIM = 5;
int matrix[DIM][DIM];
const int Z = 0;
srand(time(0));
for (int i = 0; i < DIM; i++) {
for (int j = 0; j < DIM; j++) {
matrix[i][j] = randInt(-5, 5);
cout << setw(4) << matrix[i][j] << ' ';
}
cout << endl;
}
cout << endl;
cout << "x > " << Z << ":" << endl;
int count = 0;
for (int i = 0; i < DIM; i++) {
for (int j = 0; j < DIM; j++) {
if (matrix[j][i] > Z)
count++;
}
cout << setw(4) << count << ' ';
count = 0;
}
cout << endl;
int sum = 0;
for (int i = 0; i < DIM; i++) {
for (int j = 0; j < DIM; j++) {
if (matrix[i][j] < 0)
sum += matrix[i][j];
}
}
cout << "Sum: " << sum << endl;
cout << endl;
return 0;
}
2)
#include <iostream>
#include <iomanip>
#include <ctime>
using namespace std;
int randInt(int min, int max){
return min + (rand() % (max - min + 1));
}
int main(int argc, char *argv[]){
const int DIM = 5;
int matrix[DIM][DIM];
const int Z = 0;
srand(time(0));
for (int i = 0; i < DIM; i++) {
for (int j = 0; j < DIM; j++) {
matrix[i][j] = randInt(-5, 5);
cout << setw(4) << matrix[i][j] << ' ';
}
cout << endl;
}
cout << endl;
int sum = 0;
for (int i = 0; i < DIM; i++) {
for (int j = 0; j < DIM; j++) {
if (matrix[i][j] < 0)
sum += matrix[i][j];
}
}
cout << "Sum: " << sum << endl;
cout << endl;
return 0;
}