T
T
tmtimuchin2021-05-24 23:20:39
OOP
tmtimuchin, 2021-05-24 23:20:39

How to translate code into OOP c++?

There is a simple C++ program that, using a given formula, counts the elements of two arrays if they correspond to the positive elements of the third array. Here is the code:

#include <iostream>
#include <cmath>

using namespace std;

int main(void)
{
    int i = 0;
    int n = 10;
    float avrg = 0;
    int sum = 0;
    
    
    // Определяю массивы
    float a[n] = { -7, 17, 69, 25, 88, 14, 84, 36, -4, 75 };
    float b[n] = { 16, -30, 11, 39, 15, -36, 12, 6, -30, 5 };
    float c[n] = { -19, 35, -6, 15, -9, 13, 8, 12, -33, 42 };
    
    cout << "Первый массив: ";
    for (i = 0; i < n; i++)
        cout << a[i] << " ";
    cout << "\nВторой массив: ";
    for (i = 0; i < n; i++)
        cout << b[i] << " ";
    cout << "\nТретий массив: ";
    for (i = 0; i < n; i++)
        cout << c[i] << " ";
    
    
    // Определяю массив y
    int y[n];
    int yn = 0;
    
    for (i = 0; i < 10; i++) {
        if (c[i] > 0) {
            y[i] = a[i] - b[i];
            yn++;
        } else {
            y[i] = pow(10, 23);
        }
    }
    
    cout << "\nЧетвёртый массив: ";
    for (i = 0; i < n; i++) {
        if (y[i] > -100) {
            cout << y[i] << " ";
        }
    }
    
    
    // Нахожу средние арифметические
    for (i = 0; i < n; i++) {
        if (y[i] > -100) {
            sum += y[i];
        }
    }
    avrg = sum / yn;
    cout << "\nСреднее арифметические: " << avrg << endl;
    
    
    // Дисперсия массива y
    int d = 0;
    for (i = 0; i < n; i++) {
        if (y[i] > -100) {
            d += (pow((y[i] - avrg), 2)) / (6 - 1);
        }
    }
    cout << "Дисперсия: " << d;
    
    // Минимальный элемент массива y
    // float min = y[0];
    
    // for (i = 0; i < n; ++i) {
    //     if (y[i] < min) {
    //         min = y[i];
    //     }
    // }
    // cout << "\nМинимальный элемент массива: " << min;
    
    return 0;
}


Please help translate this program into OOP!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergei Nazarenko, 2021-05-24
@nazares

Books on OOP, C++

1
12rbah, 2021-05-25
@12rbah

Please help translate this program into OOP!

Is this some kind of assignment? If not, then there is not much point in transferring to OOP. You can create a class, the constructor takes an array of ints as input, and the class has getAverage functions, and so on, which you call from the object. You can put as many functions as you want in there, but programs of this type generally do not require any complex architectural approach, because very easy to scale.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question