Question
For each of the following six program fragments: b. Implement the code in the language of your choice, and give the running time for several
For each of the following six program fragments: b. Implement the code in the language of your choice, and give the running time for several values of N.
(1) sum = 0; for( i = 0; i < n; ++i )
++sum; (2) sum = 0;
for( i = 0; i < n; ++i ) for( j = 0; j < n; ++j )
++sum; (3) sum = 0;
for( i = 0; i < n; ++i ) for( j = 0; j < n * n; ++j )
++sum; (4) sum = 0;
for( i = 0; i < n; ++i ) for( j = 0; j < i; ++j )
++sum; (5) sum = 0;
for( i = 0; i < n; ++i ) for( j = 0; j < i * i; ++j )
for( k = 0; k < j; ++k ) ++sum;
(6) sum = 0; for( i = 1; i < n; ++i )
for( j = 1; j < i * i; ++j ) if( j % i == 0 )
for( k = 0; k < j; ++k ) ++sum;
/************************Below Is My Code that won't seem to work for the same type of bug******************************/
#include
#include
using namespace std;
//First Function
void function1(int n)
{
auto start = chrono::high_resolution_clock::now();
int sum=0;
for (int i=0; i ++sum; auto finish = chrono::high_resolution_clock::now(); cout<<" Type 1 took"; cout< cout<<"for " <<"n="< } //Function 2 void function2(int n) { auto start = chrono::high_resolution_clock::now(); int sum=0; for (int i=0; i for(int j=0; j ++sum; auto finish = chrono::high_resolution_clock::now(); cout<<" Type 2 took"; cout< cout<<"for " <<"n="< } //Function 3 void function3(int n) { auto start = chrono::high_resolution_clock::now(); int sum=0; for (int i=0; i for(int j=0; j ++sum; auto finish = chrono::high_resolution_clock::now(); cout<<" Type 3 took"; cout< cout<<"for " <<"n="< } //Function 4 void function4(int n) { auto start = chrono::high_resolution_clock::now(); int sum=0; for (int i=0; i for(int j=0; j ++sum; auto finish = chrono::high_resolution_clock::now(); cout<<" Type 4 took"; cout< cout<<"for " <<"n="< } //Function 5 void function5(int n) { auto start = chrono::high_resolution_clock::now(); int sum=0; for (int i=0; i for(int j=0; j for(k=0;k ++sum; auto finish = chrono::high_resolution_clock::now(); cout<<" Type 5 took"; cout< cout<<"for " <<"n="< } //Function 6 void function6(int n) { auto start = chrono::high_resolution_clock::now(); int sum=0; for (int i=1; i for(int j=1; j if(j%i==0) for(k=0;k ++sum; auto finish = chrono::high_resolution_clock::now(); cout<<" Type 6 took"; cout< cout<<"for " <<"n="< } int main() { int N=125; function1(N); function2(N); function3(N); function4(N); function5(N); function6(N); system("PAUSE"); return 0; } /****(Feel free to edit or start new)//Output should be like following:*********/ Sample Output: Type 1 took 1244 ns for n=125, Total Iterations:125 Type 2 took 30480 ns for n=125, Total Iterations:15625 Type 3 took 4092355 ns for n=125, Total Iterations:1953125 Type 4 took 14928 ns for n=125, Total Iterations:7750 Type 5 took 5266447877 ns for n=125, Total Iterations:-1304240746 Type 6 took 53588774 ns for n=125, Total Iterations:29709625
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started