Cycle for

Pages: 12
There is a code. It counts the time of function foo() with the help of measureTime(). It works but sum in foo() is counting only 1 times. So I have 3025 on console. It looks like:


[1]
Sum is 3025.    It took me 0.0025959 seconds
Sum is 3025.    It took me 0.0008156 seconds
Sum is 3025.    It took me 0.0007984 seconds
Sum is 3025.    It took me 0.0007991 seconds
Sum is 3025.    It took me 0.0005835 seconds
Sum is 3025.    It took me 0.0084796 seconds
Sum is 3025.    It took me 0.0008464 seconds
Sum is 3025.    It took me 0.0008067 seconds
Sum is 3025.    It took me 0.0008738 seconds
Sum is 3025.    It took me 0.0009699 seconds
Average time is 0.00175689 sec
[2]
Sum is 3025.    It took me 0.0007993 seconds
Sum is 3025.    It took me 0.0006137 seconds
Sum is 3025.    It took me 0.0005998 seconds
Sum is 3025.    It took me 0.0005916 seconds
Sum is 3025.    It took me 0.0078414 seconds
Sum is 3025.    It took me 0.000871 seconds
Sum is 3025.    It took me 0.0007911 seconds
Sum is 3025.    It took me 0.0007849 seconds
Sum is 3025.    It took me 0.0006541 seconds
Sum is 3025.    It took me 0.0005846 seconds
Average time is 0.00141315 sec
[3]
...
[10]


But I need:

[1]
Sum is 0.    It took me 0.0025959 seconds
Sum is 55.    It took me 0.0008156 seconds
Sum is 165.    It took me 0.0007984 seconds
Sum is 330.    It took me 0.0007991 seconds
Sum is 550.    It took me 0.0005835 seconds
Sum is 825.    It took me 0.0084796 seconds
Sum is 1155.    It took me 0.0008464 seconds
Sum is 1540.    It took me 0.0008067 seconds
Sum is 1980.    It took me 0.0008738 seconds
Sum is 2475.    It took me 0.0008738 seconds
Sum is 3025.    It took me 0.0009699 seconds
Average time is 0.00175689 sec
[2]
Sum is 0.    It took me 0.0025959 seconds
Sum is 55.    It took me 0.0008156 seconds
Sum is 165.    It took me 0.0007984 seconds
Sum is 330.    It took me 0.0007991 seconds
Sum is 550.    It took me 0.0005835 seconds
Sum is 825.    It took me 0.0084796 seconds
Sum is 1155.    It took me 0.0008464 seconds
Sum is 1540.    It took me 0.0008067 seconds
Sum is 1980.    It took me 0.0008738 seconds
Sum is 2475.    It took me 0.0008738 seconds
Sum is 3025.    It took me 0.0009699 seconds
Average time is 0.00141315 sec
[3]
...
[10]


As I understend I need to fix output in foo(). How I can do it?

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

using namespace std;
using namespace std::chrono;

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;
}

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 me " << time_span.count() << " seconds\n";
    
    return time_span.count();
}


statistics.cpp
1
2
3
4
5
6
7
8
9
10
11
#include "statistics.h"
#include "cmath"
#include <iostream>

double statistics::meanTime(double* t, size_t z){
    double x = 0;
    for (size_t i = 0; i < z; ++i)
        x += t[i];
    std::cout << "Average time is " << x / z << " sec" << std::endl;
    return x / 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 n{ 10 };
    int nn{ 10 };
    experiment ex;
    statistics stat;
    double* t = new double[n];

    for (size_t j = 1; j <= n; ++j) {
        std::cout << "[" << j << "]" << std::endl;
        for (size_t i = 0; i < n; ++i) {
             t[i] = ex.measureTime(nn);
        }
        stat.meanTime(t, n);
    }
    delete[] t;
}

Last edited on
1
2
3
4
5
6
7
8
double experiment::measureTime( int n ) {    
        foo( n );
}

int main() {
    int nn { 10 };
             t[i] = ex.measureTime( nn );
}

You call foo( 10 ) repeatedly. Same input, same output?
You call foo( 10 ) repeatedly. Same input, same output?


Can I do it so?

t[i] = ex.measureTime(i);
Last edited on
Yes, if i has the value that you want to calculate for.
Yes, if i has the value that you want to calculate for.


Thanks!
But if I need to increase by 1000 times. I mean that sigma counts from 1000 to 10000 with the step 1000

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


But then I have a mistake. Problem with array?
But if I need to increase by 1000 times


Increase what by 1000 times?

Note that n as passed to ::foo() varies from 0 to 10.
Increase what by 1000 times?


Probably, I siad wrong.

In my task It looks like: Take the range of input values n from 1000 to 10000, step equal to 1000, the number of repeated starts z = 10.
I thought that I need to increase i, j, n to 1000. But It is wrong.
"But It is wrong" -- how are you quantifying what is wrong or right?
What I guess is, that when you change nn to i (as discussed above), your sequence now looks like:
Sum is 0.      
Sum is 1.    
Sum is 9.      
Sum is 36.     
Sum is 100.   
Sum is 225.   
Sum is 441.   
Sum is 784. 
Sum is 1296. 
Sum is 2025.


But you need:
Sum is 0.    
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.


Are we understanding correctly?
Last edited on
Sum is 0.    
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 is right. But I need to take the range of input values n from 1000 to 10000, step equal to 1000, the number of repeated starts z = 10.
Okay, I still don't know what that means. Hopefully somebody else can help.

If your foo function is now
1
2
3
4
5
6
7
8
9
10
int foo(int n) {
    int s{ 0 };
    for (int i = 1000; i <= n; i += 1000) {
        for (int j = 1000; j <= n; j += 1000) {
            s += i * j;
        }
    }
    std::cout << "Sum is " << s << ".\t";
    return 0;
}
Then your loops are only going to activate if n >= 1000. So are you passing in appropriate values into the foo function?
Last edited on
Then your loops are only going to activate if n >= 1000. So are you passing in appropriate values into the foo function?


As I understend n should be 10000
As it stands, your "experiment" does this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
using namespace std;

unsigned long long experiment( unsigned long long n )
{
   return n * n * ( n + 1 ) * ( n + 1 ) / 4;
}

int main()
{
   for ( unsigned i = 0; i < 10; i++ ) cout << i << "  " << experiment( i ) << '\n';
   cout << '\n';
   for ( unsigned i = 1000; i < 10000; i += 1000 ) cout << i << "  " << experiment( i ) << '\n';
}


0  0
1  1
2  9
3  36
4  100
5  225
6  441
7  784
8  1296
9  2025

1000  250500250000
2000  4004001000000
3000  20263502250000
4000  64032004000000
5000  156312506250000
6000  324108009000000
7000  600421512250000
8000  1024256016000000
9000  1640614520250000


What sum is your "experiment" actually trying to compute?
FYI, this sequence
0
55   
165  
330  
550  
825  
1155 
1540 
1980 
2475 
3025

can be produced by the following code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// Example program
#include <iostream>

int tri(int n)
{
    return n*(n+1)/2;   
}

int main()
{
    const int m = 10;
    
    for (int n = 0; n <= 10; n++)
    {
        std::cout << tri(m)*tri(n) << '\n';
    }
}


Not sure if that helps.
What sum is your "experiment" actually trying to compute?


foo() should count it

ΣΣi*j
Last edited on
foo() should count it

ΣΣi*j


What are the LIMITS on i and j. (In Ganado's code, they are n and 10).
#include <iostream>
using namespace std;

unsigned long long experiment( unsigned long long n )
{
return n * ( n + 1 ) * 55 / 2;
}

int main()
{
for ( unsigned i = 0; i <= 10; i++ ) cout << i << " " << experiment( i ) << '\n';
cout << '\n';
for ( unsigned i = 1000; i <= 10000; i += 1000 ) cout << i << " " << experiment( i ) << '\n';
}


Shouldn't this code count this double sum?
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;
}


ΣΣi*j
Last edited on
Repeat my question: what are the intended limits on i and j?

In your code they are n and n.
In Ganado's code (which gives the sequence you claim to want) they are n and 10.
What are the LIMITS on i and j. (In Ganado's code, they are n and 10).


It should be from 1000 to 10000
You don't understand my question, alexandr2222.

WITHIN experiment, i.e. the two loops in that routine, what are the two upper limits on i and j?

At the moment you have coded them as n and n:
1
2
    for (int i = 0; i <= n; i += 1) {
        for (int j = 1; j <= n; j += 1


However, the only way to get the sequence you claim is to change one of the upper limits to 10 (say j <= 10)
Last edited on
what are the two upper limits on i and j?


As I understend It should be 10000

i'm afraid to be wrong, sorry
Last edited on
Pages: 12