Skip to content

Instantly share code, notes, and snippets.

@gluschenko
Created December 17, 2019 08:44
Show Gist options
  • Save gluschenko/914865d9909bc8a82e2b07421af037cd to your computer and use it in GitHub Desktop.
Save gluschenko/914865d9909bc8a82e2b07421af037cd to your computer and use it in GitHub Desktop.
#include <iostream>
#include <math.h>
#include <cstdlib>
using namespace std;
double** read(int m, int n){
double** A = (double**)malloc(m * sizeof(double*));
for(int i = 0; i < m; i++){
A[i] = (double*)malloc(n * sizeof(double));
}
for(int i = 0; i < m; i++){
for(int k = 0; k < n; k++){
cout << "A[" << i << ", " << k << "] = ";
cin >> A[i][k];
}
}
return A;
}
double calcAvg(double** A, int m, int n){
double sum = 0;
int all = m * n;
for(int i = 0; i < m; i++){
for(int k = 0; k < n; k++){
sum += A[i][k];
}
}
double result = sum / all;
return result;
}
int getCount(double** A, int m, int n, double avg){
int count = 0;
for(int i = 0; i < m; i++){
for(int k = 0; k < n; k++){
if(A[i][k] > avg){
count++;
}
}
}
return count;
}
double* getC(double** A, int m, int n, double avg, int count){
double* C = (double*)malloc(count * sizeof(double));
int z = 0;
for(int i = 0; i < m; i++){
for(int k = 0; k < n; k++){
if(A[i][k] > avg){
C[z] = A[i][k];
z++;
}
}
}
return C;
}
int main() {
setlocale(LC_ALL, "Russian");
int n, m, count;
cout << "n = ";
cin >> n;
cout << "m = ";
cin >> m;
double** A;
double* C;
A = read(m, n);
double avg = calcAvg(A, m, n);
if(avg >= 0){
count = getCount(A, m, n, avg);
if(count > 0){
C = getC(A, m, n, avg, count);
for(int i = 0; i < count; i++){
cout << "C[" << i << "] = " << C[i] << endl;
}
}
else{
cout << "Условие не выполнено" << endl;
}
}
else{
cout << "Условие не выполнено (avg < 0)" << endl;
}
system("pause");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment