Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

c++ help Write a program that obtains the execution times for the following three code segments with the different n. code 1: for(int i =

c++ help

Write a program that obtains the execution times for the following three code segments with the different n.

code 1:

for(int i = 0; i <= n; i++)

{

x = i;

}

code 2:

for (int i = 0; i <= n; i++)

{

for (int j = 0; j <= n; j++)

{

x = i + j;

}

}

code 3:

for (int i = 0; i <= n; i++)

{

for (int j = 0; j <= n; j++)

{

for (int k = 0; k <= n; k++)

{

x = i + j + k;

}

}

}

Please record the execution time of the following tasks, Code 1 when n=100, n=1,000, n=5,000, n=10,000, n=100,000, n=1,000,000, n=2,000,000, and n=10,000,000 Code 2 when n=100, n=1,000, n=5,000, n=10,000, n=100,000, and n=200,000. Code 3 when n=10, n=50, n=100, n=500, and n = 1,000. You can fill a table like this:

n=10 | n=50 | n=100 | n=200| n=1000|

Code 1 | | | | |

Code 2 | | | | |

Code 3 | | | | | Plotting the execution time of Code 1, Code 2 and Code 3. You may want to execute Code1, Code2 and Code 3 with more n values to plot. You can use the following code template to obtain the execution time.

#include

#include // for time function

#include void get_time_code1(int n)

{

using clock = chrono::steady_clock;

clock::time_point start = clock::now();//Get start time

// perform the task Code 1 which you want to analyze the

// execution time

int x = 0;

for(int i = 0; i <= n; i++)

{

x = i;

}

clock::time_point end = clock::now(); //Get end time clock::duration time_span = end - start; double ns = double(time_span.count()) * clock::period::num /

clock::period::den;

cout << "Code 1 execution time for n = " << n

<< " is " << ns << " seconds" << endl; } You can put all functions in the file a1q10.cpp. Please submit the program a1q10.cpp, a script file a1q10result containing result, and the plot of execution time.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database And Expert Systems Applications 33rd International Conference Dexa 2022 Vienna Austria August 22 24 2022 Proceedings Part 1 Lncs 13426

Authors: Christine Strauss ,Alfredo Cuzzocrea ,Gabriele Kotsis ,A Min Tjoa ,Ismail Khalil

1st Edition

3031124227, 978-3031124228

More Books

Students also viewed these Databases questions

Question

7. Compare and contrast cable modem and DSL.

Answered: 1 week ago

Question

1. Identify three approaches to culture.

Answered: 1 week ago

Question

2. Define communication.

Answered: 1 week ago

Question

4. Describe how cultural values influence communication.

Answered: 1 week ago