Cycle for

Pages: 12
What does your original exercise actually say? (Not your paraphrasing of it).
What does your original exercise actually say? (Not your paraphrasing of it).
It looks like:

The main.cpp file should contain only the main () function, which will be responsible for interacting with the help, entering data and outputting it to the console and to a text file. The user determines how many times to run, examine the function on the same value of the argument (z in experiment parameters (), see below), the boundaries of the argument values ​​(enters from what and to what value, with what step). The function must form an array of arguments by dynamically allocating memory for it using the new [] operator. Be sure to free the memory at the end of the program (using the delete [] operator). Similarly, it should transfer memory of the same size for future mean time and standard deviation in time, which will be given as arguments to the experiment function.

statistics.h contains two function declarations:
meanTime - accepting an array of times (double *) and its size (size_t) as an argument and returning the average time over the array (double).

statistics.cpp contains implementations of the above two functions

experiment.h contains declarations of the following functions:
i. foo is the function being tested for speed. Takes an integer value (int) as an argument, returns an int. Please note that it must somehow use the results of its work (for example, output to the console), otherwise its running time will always be zero (why?).
ii. measureTime is a function that measures the running time of foo (). Receives an
argument for foo (int) as input. It starts foo with the given argument and returns the running time (double). To measure time, you should use the clock () function from the <ctime> library (analogous to the c-library <time.h>). Alternatively, you can use the steady_count class methods from the <chrono> library (note that it needs C ++ 11 support to work)
iii. experiment is a function that does most of the work in our numerical experiment. It takes as an input an array of values ​​of the function argument foo (int []), its size (size_t), the number of function starts with the same argument (size_t), an array into which the average running times will be written (double []) and an array for root-mean-square deviations of the operating time for a given number of starts for each value of the argument of the function under study (double []). The function must not return anything explicitly (void). Its type will look like this:
void experiment (int * arguments, size_t n, size_t z, double * means, double * stds)

It will change the contents of the means and stds arrays. Note that the size of these arrays must be equal to the number of different argument values ​​(the size n of the arguments array). Each cell of the means [i] array should contain the average running time function foo (measured by measureTime) over z starts with given argument arguments [i]. Likewise for stds [i], which should contain the standard deviation over time at z runs of foo for a fixed argument arguments [i].
experiment.cpp should contain the implementation of all the functions from the previous paragraph.
ATTENTION! As a reminder, you should point out that each header file (statistics.h, experiment.h) should contain the following pieces of code:
#ifndef LIBRARY_NAME
#define LIBRARY_NAME
// ... connecting the necessary libraries, declaring functions ...
#endif

What should be taken as the investigated function? A function foo that takes an input argument n and computes the sum ΣΣi*j of the odds is obviously O (n^2). You need to verify that this is indeed the case. Take the range of input values ​​n from 1000 to 10000, step equal to 1000, the number of repeated starts z = 10. Having received a table of average times and standard deviations from n using your program, plot graphs of these dependencies (for example, in excel program). Make sure the mean time is indeed quadratic. Estimate the coefficients of the parabola.
Last edited on
sum ΣΣi*j of the odds

What does that mean?
What does that mean?


Mistake

A function foo that takes an input argument n and calculates the sum ΣΣi*j. Its complexity is obviously O (n^2)
Last edited on
Where did you get your "expected output" from, then. It does not correspond to this question.
Where did you get your "expected output" from, then. It does not correspond to this question.


I thouht that program give me result and I only need to change the limit
alexandr2222 wrote:
A function foo that takes an input argument n and computes the sum ΣΣi*j of the odds is obviously O (n^2).

What exactly does this mean? It says that it is "obviously O (n^2)." How can a function be "O (n^2)"??? It isn't obvious to me...
What exactly does this mean? It says that it is "obviously O (n^2)." How can a function be "O (n^2)"??? It isn't obvious to me...



Probably, this mystery is known only for my teacher...)
Ah, well then I would ask him what it means.
IF the inner loop runs n times for each of the n times that the outer loop runs then you will have done whatever's inside it n*n or n2 times.

In general, the scheme is O(n2) if the number of operations is <= C.n2 for some number C independent of n.

So, ΣΣi*j for i=1,...,n and j=1,...,n would clearly be O(n2).

We come back to ... why you keep saying that your output should be
0
55   
165  
330  
550  
825  
1155 
1540 
1980 
2475 
3025

when that clearly doesn't correspond to your instructions. Somebody must have given you that output ... mustn't they?
when that clearly doesn't correspond to your instructions. Somebody must have given you that output ... mustn't they?


1
2
3
4
5
6
7
8
9
10
int experiment::foo(int n) {
    int s{ 0 };
    for (int i = 0; i <= n; i += 1) {
        for (int j = 1; j <= n; j += 1) {
            s += i * j;
        }
    }
    std::cout << "Sum is " << s << ".\t";
    return 0;
}


But how should I write foo? I wrote it so I should that result is right. How can I write foo else?
Last edited on
There's nothing wrong with foo (other than the fact that i=0 is a pointless loop, and your latest version doesn't actually return the sum.) What is wrong is what you claim the expected output to be.
There's nothing wrong with foo (other than the fact that i=0 is a pointless loop, and your latest version doesn't actually return the sum.) What is wrong is what you claim the expected output to be.



Well, I understood
But how can I do then?
 0*1+0*2+0*3
+1*1+1*2+1*3
+2*1+2*2+2*3
+3*1+3*2+3*3

is same as (and first row, the 0*... is useless)
 1*(1+2+3)
+2*(1+2+3)
+3*(1+2+3)

is same as
(1+2+3)*(1+2+3)

In other words, every result should be a square of x, where x = (n+1)*n/2
Your "expected results" are not squares of integers.
Your "expected results" are not squares of integers.


Thank you very much!) I've already understood it
How can I rewrite my program?
Rewrite to do what?

i. foo is the function being tested for speed. Takes an integer value (int) as an argument, returns an int. Please note that it must somehow use the results of its work (for example, output to the console), otherwise its running time will always be zero (why?).

I'd say the wording is misleading: "it must somehow use".
I bet the the it does not refer to foo(), but to the program as whole.

If I were the teacher, I would expect:
1
2
3
4
5
int foo(int);

int main() {
  std::cout << foo(42);
}

and definitely not:
1
2
3
4
5
6
7
8
int foo(int n) {
  std::cout << "Hello world\n";
  return 0;
}

int main() {
  foo(42);
}
If I were the teacher, I would expect


I rewrite program. If I make the graph of the dependence of the average value and standard deviation of time I will have a parabola (I checked it). What can you say?

experience.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
#include "experiment.h"
#include <chrono>
#include <iostream>
#include <thread>

using namespace std::chrono;

int experiment::foo(int n) {
    int s{ 0 };
    for (int i = 1; i <= n; i += 1) {
        for (int j = 1; j <= n; j += 1) {
            s += i * j;
        }
       std::cout << "Sum is " << s << std::endl;
    }
    return 0;
}

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(int* arguments, size_t n, size_t z, double* means, double* stds){

}


statistics.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include "statistics.h"
#include "cmath"
#include <iostream>

double x = 0;

double statistics::meanTime(double* t, size_t z){  
    for (size_t i = 1; i <= z; ++i) x += t[i]; 
    std::cout << "\t\tAverage time is " << x / z << " sec\n" << std::endl;
    return x / z;
}

double statistics::stdTime(double* t, size_t z){
    double STD{ 0 };   
    for (int i = 1; i <= z; i += 1)  STD += (pow((t[i] - (x / z)), 2))/z;     
    std::cout << "\t\tStandard deviation of time is " << STD << std::endl;
	return STD;
}


main.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
#include <iostream>
#include "experiment.h"
#include "statistics.h"

int main() {
    size_t n{ 10 };
    size_t z{ 10 };
    experiment ex;
    statistics stat;

    double* arguments = new double[11];
    double* means = new double[11];
    double* stds = new double[11];

    for (size_t j = 1; j <= 10; ++j) {
        std::cout << "[" << j << "]" << std::endl;
        for (size_t i = 1; i <= 10; i += 1) {
            arguments[i] = ex.measureTime(i);
        }
        means[j] = stat.meanTime(arguments, z);
        stds[j] = stat.stdTime(arguments, z);
    }
    
    delete[] arguments;
    delete[] means;
    delete[] stds;
}


Result:


[1]
Sum is 1
It took 0.0065248 seconds
Sum is 3
Sum is 9
It took 0.0005157 seconds
Sum is 6
Sum is 18
Sum is 36
It took 0.0006858 seconds
Sum is 10
Sum is 30
Sum is 60
Sum is 100
It took 0.0012032 seconds
Sum is 15
Sum is 45
Sum is 90
Sum is 150
Sum is 225
It took 0.0018505 seconds
Sum is 21
Sum is 63
Sum is 126
Sum is 210
Sum is 315
Sum is 441
It took 0.0024608 seconds
Sum is 28
Sum is 84
Sum is 168
Sum is 280
Sum is 420
Sum is 588
Sum is 784
It took 0.578849 seconds
Sum is 36
Sum is 108
Sum is 216
Sum is 360
Sum is 540
Sum is 756
Sum is 1008
Sum is 1296
It took 0.320817 seconds
Sum is 45
Sum is 135
Sum is 270
Sum is 450
Sum is 675
Sum is 945
Sum is 1260
Sum is 1620
Sum is 2025
It took 0.41821 seconds
Sum is 55
Sum is 165
Sum is 330
Sum is 550
Sum is 825
Sum is 1155
Sum is 1540
Sum is 1980
Sum is 2475
Sum is 3025
It took 0.149976 seconds
                Average time is 0.148109 sec

                Standard deviation of time is 0.0416072
[2]
Sum is 1
It took 0.0041349 seconds
Sum is 3
Sum is 9
It took 0.0040938 seconds
Sum is 6
Sum is 18
Sum is 36
It took 0.0468383 seconds
Sum is 10
Sum is 30
Sum is 60
Sum is 100
It took 0.0121621 seconds
Sum is 15
Sum is 45
Sum is 90
Sum is 150
Sum is 225
It took 0.0401639 seconds
Sum is 21
Sum is 63
Sum is 126
Sum is 210
Sum is 315
Sum is 441
It took 0.0947693 seconds
Sum is 28
Sum is 84
Sum is 168
Sum is 280
Sum is 420
Sum is 588
Sum is 784
It took 0.0676067 seconds
Sum is 36
Sum is 108
Sum is 216
Sum is 360
Sum is 540
Sum is 756
Sum is 1008
Sum is 1296
It took 0.988829 seconds
Sum is 45
Sum is 135
Sum is 270
Sum is 450
Sum is 675
Sum is 945
Sum is 1260
Sum is 1620
Sum is 2025
It took 0.112987 seconds
Sum is 55
Sum is 165
Sum is 330
Sum is 550
Sum is 825
Sum is 1155
Sum is 1540
Sum is 1980
Sum is 2475
Sum is 3025
It took 0.104183 seconds
                Average time is 0.295686 sec

                Standard deviation of time is 0.102052
[3]
Sum is 1
It took 0.0041388 seconds
Sum is 3
Sum is 9
It took 0.014026 seconds
Sum is 6
Sum is 18
Sum is 36
It took 0.387297 seconds
Sum is 10
Sum is 30
Sum is 60
Sum is 100
It took 0.0423102 seconds
Sum is 15
Sum is 45
Sum is 90
Sum is 150
Sum is 225
It took 0.150148 seconds
Sum is 21
Sum is 63
Sum is 126
Sum is 210
Sum is 315
Sum is 441
It took 0.511041 seconds
Sum is 28
Sum is 84
Sum is 168
Sum is 280
Sum is 420
Sum is 588
Sum is 784
It took 0.0348538 seconds
Sum is 36
Sum is 108
Sum is 216
Sum is 360
Sum is 540
Sum is 756
Sum is 1008
Sum is 1296
It took 0.265801 seconds
Sum is 45
Sum is 135
Sum is 270
Sum is 450
Sum is 675
Sum is 945
Sum is 1260
Sum is 1620
Sum is 2025
It took 0.684713 seconds
Sum is 55
Sum is 165
Sum is 330
Sum is 550
Sum is 825
Sum is 1155
Sum is 1540
Sum is 1980
Sum is 2475
Sum is 3025
It took 0.217861 seconds
                Average time is 0.526905 sec

                Standard deviation of time is 0.136355
[4]
Sum is 1
It took 0.0015297 seconds
Sum is 3
Sum is 9
It took 0.0065616 seconds
Sum is 6
Sum is 18
Sum is 36
It took 0.0059755 seconds
Sum is 10
Sum is 30
Sum is 60
Sum is 100
It took 0.0142431 seconds
Sum is 15
Sum is 45
Sum is 90
Sum is 150
Sum is 225
It took 0.0145488 seconds
Sum is 21
Sum is 63
Sum is 126
Sum is 210
Sum is 315
Sum is 441
It took 0.0141732 seconds
Sum is 28
Sum is 84
Sum is 168
Sum is 280
Sum is 420
Sum is 588
Sum is 784
It took 0.0255628 seconds
Sum is 36
Sum is 108
Sum is 216
Sum is 360
Sum is 540
Sum is 756
Sum is 1008
Sum is 1296
It took 0.0379447 seconds
Sum is 45
Sum is 135
Sum is 270
Sum is 450
Sum is 675
Sum is 945
Sum is 1260
Sum is 1620
Sum is 2025
It took 0.0973989 seconds
Sum is 55
Sum is 165
Sum is 330
Sum is 550
Sum is 825
Sum is 1155
Sum is 1540
Sum is 1980
Sum is 2475
Sum is 3025
It took 0.0438889 seconds
                Average time is 0.553088 sec

                Standard deviation of time is 0.278364
[5]
Sum is 1
It took 0.0042041 seconds
Sum is 3
Sum is 9
It took 0.0099989 seconds
Sum is 6
Sum is 18
Sum is 36
It took 0.0239921 seconds
Sum is 10
Sum is 30
Sum is 60
Sum is 100
It took 0.0660612 seconds
Sum is 15
Sum is 45
Sum is 90
Sum is 150
Sum is 225
It took 0.0851271 seconds
Sum is 21
Sum is 63
Sum is 126
Sum is 210
Sum is 315
Sum is 441
It took 0.015434 seconds
Sum is 28
Sum is 84
Sum is 168
Sum is 280
Sum is 420
Sum is 588
Sum is 784
It took 0.0155635 seconds
Sum is 36
Sum is 108
Sum is 216
Sum is 360
Sum is 540
Sum is 756
Sum is 1008
Sum is 1296
It took 0.0305899 seconds
Sum is 45
Sum is 135
Sum is 270
Sum is 450
Sum is 675
Sum is 945
Sum is 1260
Sum is 1620
Sum is 2025
It took 0.0868472 seconds
Sum is 55
Sum is 165
Sum is 330
Sum is 550
Sum is 825
Sum is 1155
Sum is 1540
Sum is 1980
Sum is 2475
Sum is 3025
It took 0.0277033 seconds
                Average time is 0.58964 sec

                Standard deviation of time is 0.306773
[6]
Sum is 1
It took 0.0033071 seconds
Sum is 3
Sum is 9
It took 0.0031104 seconds
Sum is 6
Sum is 18
Sum is 36
It took 0.0049866 seconds
Sum is 10
Sum is 30
Sum is 60
Sum is 100
It took 0.008319 seconds
Sum is 15
Sum is 45
Sum is 90
Sum is 150
Sum is 225
It took 0.0121811 seconds
Sum is 21
Sum is 63
Sum is 126
Sum is 210
Sum is 315
Sum is 441
It took 0.0172088 seconds
Sum is 28
Sum is 84
Sum is 168
Sum is 280
Sum is 420
Sum is 588
Sum is 784
It took 0.221754 seconds
Sum is 36
Sum is 108
Sum is 216
Sum is 360
Sum is 540
Sum is 756
Sum is 1008
Sum is 1296
It took 0.0244327 seconds
Sum is 45
Sum is 135
Sum is 270
Sum is 450
Sum is 675
Sum is 945
Sum is 1260
Sum is 1620
Sum is 2025
It took 0.0294854 seconds
Sum is 55
Sum is 165
Sum is 330
Sum is 550
Sum is 825
Sum is 1155
Sum is 1540
Sum is 1980
Sum is 2475
Sum is 3025
It took 0.0669646 seconds
                Average time is 0.628815 sec

                Standard deviation of time is 0.351709
Last edited on

[7]
Sum is 1
It took 0.0005066 seconds
Sum is 3
Sum is 9
It took 0.0037864 seconds
Sum is 6
Sum is 18
Sum is 36
It took 0.0061842 seconds
Sum is 10
Sum is 30
Sum is 60
Sum is 100
It took 0.0136314 seconds
Sum is 15
Sum is 45
Sum is 90
Sum is 150
Sum is 225
It took 0.0082269 seconds
Sum is 21
Sum is 63
Sum is 126
Sum is 210
Sum is 315
Sum is 441
It took 0.0218842 seconds
Sum is 28
Sum is 84
Sum is 168
Sum is 280
Sum is 420
Sum is 588
Sum is 784
It took 0.0165372 seconds
Sum is 36
Sum is 108
Sum is 216
Sum is 360
Sum is 540
Sum is 756
Sum is 1008
Sum is 1296
It took 0.0215945 seconds
Sum is 45
Sum is 135
Sum is 270
Sum is 450
Sum is 675
Sum is 945
Sum is 1260
Sum is 1620
Sum is 2025
It took 0.0202296 seconds
Sum is 55
Sum is 165
Sum is 330
Sum is 550
Sum is 825
Sum is 1155
Sum is 1540
Sum is 1980
Sum is 2475
Sum is 3025
It took 0.0648342 seconds
                Average time is 0.646556 sec

                Standard deviation of time is 0.395707
[8]
Sum is 1
It took 0.0015781 seconds
Sum is 3
Sum is 9
It took 0.0030599 seconds
Sum is 6
Sum is 18
Sum is 36
It took 0.0065246 seconds
Sum is 10
Sum is 30
Sum is 60
Sum is 100
It took 0.0117297 seconds
Sum is 15
Sum is 45
Sum is 90
Sum is 150
Sum is 225
It took 0.0114924 seconds
Sum is 21
Sum is 63
Sum is 126
Sum is 210
Sum is 315
Sum is 441
It took 0.0189183 seconds
Sum is 28
Sum is 84
Sum is 168
Sum is 280
Sum is 420
Sum is 588
Sum is 784
It took 0.0153171 seconds
Sum is 36
Sum is 108
Sum is 216
Sum is 360
Sum is 540
Sum is 756
Sum is 1008
Sum is 1296
It took 0.0204243 seconds
Sum is 45
Sum is 135
Sum is 270
Sum is 450
Sum is 675
Sum is 945
Sum is 1260
Sum is 1620
Sum is 2025
It took 0.0182069 seconds
Sum is 55
Sum is 165
Sum is 330
Sum is 550
Sum is 825
Sum is 1155
Sum is 1540
Sum is 1980
Sum is 2475
Sum is 3025
It took 0.0529091 seconds
                Average time is 0.662572 sec

                Standard deviation of time is 0.418225
[9]
Sum is 1
It took 0.0016866 seconds
Sum is 3
Sum is 9
It took 0.0030084 seconds
Sum is 6
Sum is 18
Sum is 36
It took 0.0101996 seconds
Sum is 10
Sum is 30
Sum is 60
Sum is 100
It took 0.0064897 seconds
Sum is 15
Sum is 45
Sum is 90
Sum is 150
Sum is 225
It took 0.0097212 seconds
Sum is 21
Sum is 63
Sum is 126
Sum is 210
Sum is 315
Sum is 441
It took 0.0209323 seconds
Sum is 28
Sum is 84
Sum is 168
Sum is 280
Sum is 420
Sum is 588
Sum is 784
It took 0.0160152 seconds
Sum is 36
Sum is 108
Sum is 216
Sum is 360
Sum is 540
Sum is 756
Sum is 1008
Sum is 1296
It took 0.0222438 seconds
Sum is 45
Sum is 135
Sum is 270
Sum is 450
Sum is 675
Sum is 945
Sum is 1260
Sum is 1620
Sum is 2025
It took 0.0343058 seconds
Sum is 55
Sum is 165
Sum is 330
Sum is 550
Sum is 825
Sum is 1155
Sum is 1540
Sum is 1980
Sum is 2475
Sum is 3025
It took 0.0275976 seconds
                Average time is 0.677792 sec

                Standard deviation of time is 0.439108
[10]
Sum is 1
It took 0.0230636 seconds
Sum is 3
Sum is 9
It took 0.0036476 seconds
Sum is 6
Sum is 18
Sum is 36
It took 0.0060233 seconds
Sum is 10
Sum is 30
Sum is 60
Sum is 100
It took 0.0170016 seconds
Sum is 15
Sum is 45
Sum is 90
Sum is 150
Sum is 225
It took 0.0425007 seconds
Sum is 21
Sum is 63
Sum is 126
Sum is 210
Sum is 315
Sum is 441
It took 0.0172194 seconds
Sum is 28
Sum is 84
Sum is 168
Sum is 280
Sum is 420
Sum is 588
Sum is 784
It took 0.0255849 seconds
Sum is 36
Sum is 108
Sum is 216
Sum is 360
Sum is 540
Sum is 756
Sum is 1008
Sum is 1296
It took 0.0572108 seconds
Sum is 45
Sum is 135
Sum is 270
Sum is 450
Sum is 675
Sum is 945
Sum is 1260
Sum is 1620
Sum is 2025
It took 0.0844838 seconds
Sum is 55
Sum is 165
Sum is 330
Sum is 550
Sum is 825
Sum is 1155
Sum is 1540
Sum is 1980
Sum is 2475
Sum is 3025
It took 0.0321042 seconds
                Average time is 0.708676 sec

                Standard deviation of time is 0.459956
Topic archived. No new replies allowed.
Pages: 12