Need help with this problem I exercise and new to C++. Using a new editor called
ID: 3781931 • Letter: N
Question
Need help with this problem I exercise and new to C++. Using a new editor called "CodeLite".Write code for a function that uses a loop to compute the sum off all integers from 1 to n. Do a time analysis, counting each basic operation(such as assignment and ++) as one operation. Also find the time difference when n increases from 100-100,000.
Need help with this problem I exercise and new to C++. Using a new editor called "CodeLite".
Write code for a function that uses a loop to compute the sum off all integers from 1 to n. Do a time analysis, counting each basic operation(such as assignment and ++) as one operation. Also find the time difference when n increases from 100-100,000.
Need help with this problem I exercise and new to C++. Using a new editor called "CodeLite".
Write code for a function that uses a loop to compute the sum off all integers from 1 to n. Do a time analysis, counting each basic operation(such as assignment and ++) as one operation. Also find the time difference when n increases from 100-100,000.
Write code for a function that uses a loop to compute the sum off all integers from 1 to n. Do a time analysis, counting each basic operation(such as assignment and ++) as one operation. Also find the time difference when n increases from 100-100,000.
Also find the time difference when n increases from 100-100,000.
Explanation / Answer
The code below computes summation from 1 to n and the time taken.
---------------------
#include<iostream>
#include<time.h>
using namespace std;
int main()
{
clock_t begin = clock();
long long int x;
cin>>x;
long long int sum=0;
for(int i=0;i<x;i++)
{
sum=sum+i;
}
cout<<sum<<endl;
clock_t end = clock();
double time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
cout<<time_spent;
}