Incorrect result (arrays)

There are 3 methods. measureTime() works correctly. But meanTime() and stdTime() give me something like -3.13558e+67. How can I fix it?

statistics.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include "statistics.h"
#include "cmath"
#include <iostream>
 
double means{ 0 };
 
double statistics::meanTime(double* array, size_t z){
    double sum1{ 0 };
 
    for (size_t i = 0; i < z; ++i) sum1 += array[i];
    means = sum1 / z;
    std::cout << "\t\tAverage time is " << sum1 / z << " sec" << std::endl;
    return means;
}
 
double statistics::stdTime(double* array, size_t z){
    double stds{ 0 };   
    double sum2{ 0 };
 
    for (int i = 1; i <= z; i += 1) sum2 += ((array[i] - means) * (array[i] - means));
    stds = sqrt(sum2 / (z - 1));
    std::cout << "\t\tStandard deviation of time is " << stds << std::endl;
    return  stds;
}


experiment.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
double experiment::measureTime(int n) {
    steady_clock::time_point t1 = steady_clock::now();
    foo(n);
    steady_clock::time_point t2 = steady_clock::now();

    duration<double> time_span = duration_cast<duration<double>>(t2 - t1);
    std::cout << "It took " << time_span.count() << " seconds\n";
    
    return time_span.count();
}

void experiment::Experiment(double* arguments, size_t n, size_t z, double* means, double* stds){
 
    std::cout << "How many numbers would you like to input?" << std::endl;
    if (!(std::cin >> z >> n)) {
 
        std::cout << "Input was not valid";
    }
    else {
        if ((z < 1 || z > 10) && (n < 1000 || n > 10000)) {
            Experiment(arguments, n, z, means, stds);
        }
        for (size_t j{ 1 }; j <= z; j += 1) {
            std::cout << "[" << j << "]" << std::endl;
            for (size_t i{ 1000 }; i <= n; i += 1000) {
                arguments[i] = measureTime(i);
            }
            statistics stat;
 
            means[j] = stat.meanTime(arguments, z);
            stds[j] = stat.stdTime(arguments, z);
           
        }
    }
}


main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include "experiment.h"
#include "statistics.h"
 
int main() {
 
    size_t z{ 10 };
    size_t n{ 10000 };
 
    double* arguments = new double[n];
    double* means = new double[z];
    double* stds = new double[1100000];
 
    experiment ex;
    ex.Experiment(arguments, n, z, means, stds);
    
    delete[] arguments;
    delete[] means;
    delete[] stds;
}
Last edited on
- get rid of global `means'.
- get rid of all your input/output operations inside functions, limit them to main(). (one function, one purpose)
- learn to use the parameters and return values (¿why are «n» and «z» arguments of Experiment()?)
- array indices start at 0 and end at size-1
- use meaningful names for your variables (¿what's «z»?)

- explain your loop at lines 25-27
- explain lines 10-12 at main.cpp, ¿how big is each array? ¿what's their purpose?
Topic archived. No new replies allowed.