Hello everyone,
I am a newbie to C++ and programing. I am in need of help in a lab:
Write a program to calculate and print the result of the following series operations for the given values of x:
The series should be calculated using floats and doubles for the values for x that are specified.
This is what I have so far and do not understand how to output using ctime to calculate how long the loop takes and display the result and time required to calculate the loop.
#include <iostream>
#include <ctime>
usingnamespace std;
int main(void)
{
clock_t starttime, end;
double secs;
double fsumfloat=0;
long i;
starttime = clock();
for (i = 1; i <= 10000000L; i++)
{
fsumfloat += (1.0 / i);
}
end = clock();
secs = ((double)(end-starttime)) / CLOCKS_PER_SEC;
cout << secs << endl<<fsumfloat<<endl<<i<<endl;
}
A few things to notice:
1. starttime has to be before the loop
2. convert to double the difference between starttime and end
3. CLOCKS_PER_SEC implies that your answer will be in seconds, not miliseconds