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?
#include "experiment.h"
#include <chrono>
#include <iostream>
#include <thread>
usingnamespace std;
usingnamespace 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;
}
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;
}
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.
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?
#include <iostream>
usingnamespace std;
unsignedlonglong experiment( unsignedlonglong 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';
}
// Example program
#include <iostream>
int tri(int n)
{
return n*(n+1)/2;
}
int main()
{
constint m = 10;
for (int n = 0; n <= 10; n++)
{
std::cout << tri(m)*tri(n) << '\n';
}
}
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;
}