Question

c++ help Write a program that obtains the execution times for the following three code segments...

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 <iostream>

#include <ctime> // for time function

#include <chrono>

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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hi i am doing your question hope it will help you if you find it helpful then kindly give a thumbs up and if you face any problem then do comment.i am sending you the code and screen shoot of code and output as well.

code:-

#include <iostream>
#include <ctime> // for time function
#include <chrono>

using namespace std;

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;
cout<<ns<<" | ";
}
void get_time_code2(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++)
{
for (int j = 0; j <= n; j++)
{
x = i + j;
}
}
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 2 execution time for n = " << n<< " is " << ns << " seconds" << endl;
cout<<ns<<" | ";
}
void get_time_code3(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++)
{
for (int j = 0; j <= n; j++)
{
for (int k = 0; k <= n; k++)
{
x = i + j + k;
}
}
}
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 3 execution time for n = " << n<< " is " << ns << " seconds" << endl;
cout<<ns<<" | ";
}
int main()
{
cout<<"code1 ";
get_time_code1(100);
get_time_code1(1000);
get_time_code1(5000);
get_time_code1(10000);
get_time_code1(100000);
get_time_code1(1000000);
get_time_code1(2000000);
get_time_code1(10000000);
cout<<endl<<"code2 ";
get_time_code2(100);
get_time_code2(1000);
get_time_code2(5000);
get_time_code2(10000);
get_time_code2(100000);
get_time_code2(200000);
cout<<endl<<"code3 ";
get_time_code3(10);
get_time_code3(50);
get_time_code3(100);
get_time_code3(500);
get_time_code3(1000);
  
  
}

code image:-

output:-

happy to help :)

Add a comment
Know the answer?
Add Answer to:
c++ help Write a program that obtains the execution times for the following three code segments...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Lab 10A Measure the program execution time One easy way to measure the program execution time is ...

    Lab 10A Measure the program execution time One easy way to measure the program execution time is encapsulate the useful timing functionality of C++ chrono library, This is illustrated inhttps://www.learncpp.com/cpp-tutorial/8-16-timing-your-code/ The example usingChronoTimer.cpp (Github/m10) is an example program using this chrono Timer class object to measure the Sorting on on an integer array of 10000 random integers based on the Bubble Sort vs. the C++ built-in Sort (an https://www.quora.com/Which-sorting-algorithm-does-STL-standard-template-library-use-in-c++. ) Please measure the performance of sorting the same array used...

  • Properly format the following code such that the scope of each code block is properly indented....

    Properly format the following code such that the scope of each code block is properly indented. #include #include using namespace std; void method2(int& mult){ mult *= 2; } void method1(int& count){ count += 2; } int main(){ chrono::time_point start; chrono::time_point end; int mult = 1, count = 0; int nums[] = {1,2,3,4,5,6,7}; int choice; cout << "Enter your choice (1 or 2): "; cin >> choice; const int maxReps = 31; switch(choice){ //method 1 case 1: start = chrono::system_clock::now(); for(int...

  • Hello, I want to check if my C++ code is correct and follows the requeriments described...

    Hello, I want to check if my C++ code is correct and follows the requeriments described thanks. Requeriments: Assignment Sorting Benchmark each of the sorting methods listed below. Insertion Sort Bubble Sort Selection Sort Heap Sort. Quick Sort. Merge Sort. Benchmark each of the above sorting methods for data sizes of 10000, 20000, 30000, 40000 and 50000. Display the results in a table as shown below. The table should have rows and columns. However, the rows and columns need not...

  • 1. (25 points) Assume each expression listed below represents the execution time of a program Express...

    1. (25 points) Assume each expression listed below represents the execution time of a program Express the order of magnitude for cach time using big O notation. a. T(n) = ns + 100n . log2 n + 5000 b. T(n) = 2" +n” + 7 d. T(n) 1+2+4+2 2 (75 points+5 extra credit) For cach of the code segments below, determine an equation for the worst-case computing time Tn) (expressed as a function of n, ie. 2n+4) and the order...

  • Need help adjusting this code in C. I'm not sure this one part of the code...

    Need help adjusting this code in C. I'm not sure this one part of the code is correct, it needs to generate n random addresses between 0 and (2^32)-1. So I set it to "address = rand() % 232;" but I'm not sure if that is correct. Can you please fix thanks. #include <stdio.h> #include <stdlib.h> #include <time.h> int main() { unsigned int address; unsigned int pg_num; unsigned int offset; unsigned int i; unsigned int n = 1000000; double time_taken;...

  • My following program has an array which holds 1000 random integers between 1-1000. Now I need...

    My following program has an array which holds 1000 random integers between 1-1000. Now I need help to create an array that holds 10,000 random integer between 1-1000 in my following program. The main goal of this program is time analysis by using bubble sort and binary search algorithms. Please do the following task; 1. Replace the 1000 random integers with 10,000 random integers After change please answer the following question 2. what will be happen, if an array holds...

  • I need a program in c++ the same as below code but I want it to...

    I need a program in c++ the same as below code but I want it to prompt the user to enter the number of elements after that I want it to ask the user to enter the array elements #include<algorithm> #include<stdio.h> #include<string.h> #include<iostream> using namespace std; int a[50]={2,5,4,3}; bool x[100]; int N=4;//number of elements    int k=10;//target sum int sum;//current target sum int cmp(const void *a,const void *b) { return *(int *)b-*(int *)a; } void backtrace(int n) { if(sum>k) return...

  • task1 code: #include <stdio.h> #include <sys/time.h> int main() { struct timeval start,end; //to store time of starting and ending of program gettimeofday(&start,0); printf("Time...

    task1 code: #include <stdio.h> #include <sys/time.h> int main() { struct timeval start,end; //to store time of starting and ending of program gettimeofday(&start,0); printf("Time in microsecond for every second: \n"); //output for(int i=0;i<100;i++) //outer loop to run 100 times { struct timeval loopstart,loopend; //to store time of starting and ending of loop gettimeofday(&loopstart,0); //measuring time at start of loop for(int j=0;j<1000000;j++); //outer loop to run 1000000 times gettimeofday(&loopend,0); //measuring time at end of loop //calculating time for one iteration long ms=(loopend.tv_sec-loopstart.tv_sec)*1000000...

  • How would I be able to get a Merge Sort to run in this code? MY...

    How would I be able to get a Merge Sort to run in this code? MY CODE: #include <iostream> #include <fstream> #include <stdlib.h> #include <stdio.h> #include <time.h> using namespace std; class mergersorter { private:    float array[1000] ;    int n = 1000;    int i=0; public:    void fillArray()    {        for(i=1;i<=n;i++)        {            array[i-1]= ( rand() % ( 1000) )+1;        }    }    void arrayout ()    {   ...

  • The following C code keeps returning a segmentation fault! Please debug so that it compiles. Also...

    The following C code keeps returning a segmentation fault! Please debug so that it compiles. Also please explain why the seg fault is happening. Thank you #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> // @Name loadMusicFile // @Brief Load the music database // 'size' is the size of the database. char** loadMusicFile(const char* fileName, int size){ FILE *myFile = fopen(fileName,"r"); // Allocate memory for each character-string pointer char** database = malloc(sizeof(char*)*size); unsigned int song=0; for(song =0; song < size;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT