Question

In: Computer Science

NEED IN C++ For the following programming problems, you need to time a section of code...

NEED IN C++

For the following programming problems, you need to time a section of code in C++. For example, the following statements time the execution of the function doSomething:

#include

clock_t start = clock();

doSomething();

clock_t finish = clock();

double overallTime = static_cast(finish - start)/ CLOCKS_PER_SEC;

  1. Consider the following two loops:

//Loop A

for(i = 1; i <= n; i++)

   for(j = 1; j <= 10000; j++)

    sum = sum + j;

//Loop B

for(i = 1; i <= n; i++)

   for(j = 1; j <= n; j++)

    sum = sum + j;

       

       What us the Big O of each loop? Design and implement an experiment to find a value of n for which Loop B is faster than Loop A.

  1. Repeat the previous project, but use the following for Loop B:

//Loop B

for(i = 1; i <= n; i++)

   for(j = 1; j <= n; j++)

       for(k = 1; k <= j; k++ )

           sum = sum + k;

  1. Write a C++ program that implements the following three algorithms and times for various values of n. The program should display a table of the run times of each algorithm for various values of n.

//Algorithm A          //Algorithm B       //Algorithm C

sum = 0;               sum = 0;            sum = n * (n + 1) / 2

for(i = 1 to n)        for(i = 1 to n)    

   sum = sum + 1      {

                       for(j = 1 to i)

                         sum = sum + i

                       }

Expected Outputs:

Sum

#1

#2

Input size n

Loop A

Loop B

Loop A

Loop B2

3

10

100

1000

100000

1000000

Running time (milliseconds)

#1

#2

Input size n

Loop A

Loop B

Loop A

Loop B2

3

10

100

1000

100000

1000000

Sum

#3

Input size n

Algorithm A

Algorithm B

Algorithm C

3

10

100

1000

100000

1000000

Running time (milliseconds)

#3

Input size n

Algorithm A

Algorithm B

Algorithm C

3

10

100

1000

100000

1000000

Solutions

Expert Solution

Please give thumbs up if you like it

1)SUM

=> Loop A Sum will be = N * (10000 * 10001)/2 = N * 25002500

=> Loop B Sum will be = N * (N* (N+1))/2

=> Loop B2 Sum will be = N * (N * (N +1)*(N+2))/6

substitute value of N in those formula and get sum value

Input Size N Loop A Loop B Loop B2
3 75007500 18 30
10 250025000 550 2200
100 2500250000 5500 22000
1000 25002500000 55000 220000
100000 2500250000000 5500000 22000000
1000000 25002500000000 55000000 220000000

2) Running Time

=>Loop A running time is =10000*N

=>Loop B running time is =N*N

=>Loop A running time is =N*N*(N+1) /2

substitute value of N in those formula and get running time value

Input Size N Loop A Loop B Loop B2
3 30000 9 18
10 100000 100 550
100 1000000 10000 5500
1000 10000000 1000000 55000
100000 1000000000 10000000000 5500000
1000000 10000000000 1000000000000 55000000

3) Sum

=> Algorithm A Sum value is = N

=> Algorithm B Sum value is = N * (N+1) * (2N + 1)/6

=> Algorithm C Sum value is = N*(N+1)/2

substitute value of N in those formula and get sum value

Input Size N Alogrithm A Algorithm B Algorithm C
3 3 14 6
10 10 385 55
100 100 338350 5050
1000 1000 333833500 500500
100000 100000 333338333350000 50000 * 100001
1000000 1000000 333333833333499970 500000 *1000001

4) Running Time

=> Algorithm A Running Time is = N

=> Algorithm B Running Time is = N * N

=> Algorithm C Running Time is = constant

substitute value of N in those formula and get running time value

Input Size N Algorithm A Algorithm B Algorithm C
3 3 9 constant
10 10 100 constant
100 100 10000 constant
1000 1000 1000000 constant
100000 100000 10000000000 constant
1000000 1000000 1000000000000 constant

Related Solutions

Note: I need a code and other requirement Note: programming language is c++ If you need...
Note: I need a code and other requirement Note: programming language is c++ If you need more information, please clarify what information you want. consider solving the equation sin(x) - e^(-x) = 0 write a computer program to solve the given equation using: 1- bisection method 2- fixed-point method 3- newton's intervals: {0,1},{1,2},{2,3},{3,4},{4,5},{5,6},{6,7},{7,8},{8,9},{9,10} choose accuracy E = 10^(-5) Make sure you document your program Requirement : 1- Mathematical justification 2- algorithem description 3- code (program) with documentation 4-output: roots ,...
C++ Simple Programming Assignment you need to build off of the code below: #include using namespace...
C++ Simple Programming Assignment you need to build off of the code below: #include using namespace std; // class definition class Fraction {         // two data members         // one representing numerator         int numerator;         // other, representing denominator         int denominator;         public:                 void set (int numerator, int denominator);                 Fraction addedTo (Fraction f);                 Fraction subtract (Fraction f);                 Fraction multipliedBy (Fraction f);                 Fraction dividedBy (Fraction f);                 bool isEqualTo (Fraction f);                 void print (); }; void Fraction :: set (int numerator, int denominator) {         this...
C# programming. Comment/Explain the below code line by line. I am having a hard time following...
C# programming. Comment/Explain the below code line by line. I am having a hard time following it. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Nth_prime {     class Program     {         public static bool isPrime(int number)         {             int counter = 0;             for (int j = 2; j < number; j++)             {                 if (number % j == 0)                 {                     counter = 1;                     break;                 }             }             if (counter == 0)             {                 return true;             }             else             {                 return false;             }         }...
In this programming assignment, you will write C code that performs recursion. For the purpose of...
In this programming assignment, you will write C code that performs recursion. For the purpose of this assignment, you will keep all functions in a single source file main.c. Your main job is to write a recursive function that generates and prints all possible password combinations using characters in an array. In your main() function you will first parse the command line arguments. You can assume that the arguments will always be provided in the correct format. Remember that the...
Code in C Instructions For this programming assignment you are going to implement a simulation of...
Code in C Instructions For this programming assignment you are going to implement a simulation of Dijkstra’s solution to the Dining Philosophers problem using threads, locks, and condition variables. Dijkstra’s Solution Edsgar Dijkstra’s original solution to the Dining Philosophers problem used semaphores, but it can be adapted to use similar mechanisms: • Each philosopher is in one of three states: THINKING, HUNGRY, or EATING. • Every philosopher starts out in the THINKING state. • When a philosopher is ready to...
For the following two questions you need to provide a code in C 1. Build a...
For the following two questions you need to provide a code in C 1. Build a double linked list with 5 in the first node following the instructions: Insert a node with 3 at the top of the list Insert a node with 10 at the bottom of the list Insert a node with 7 between nodes with 5 and 10 2. Start deleting nodes from the list in the following order; Delete the node with 7 Delete the node...
The assignment: C++ program or Java You need to use the following programming constructs/data structures on...
The assignment: C++ program or Java You need to use the following programming constructs/data structures on this assignment. 1. A structure named student which contains:   a. int ID; student id; b. last name; // as either array of char or a string c. double GPA;    2. An input file containing the information of at least 10 students. 3. An array of struct: read the student information from the input file into the array. 4. A stack: you can use the...
Programming in C (not C++) ## Requirements Only need to edit the challenge.c You have one...
Programming in C (not C++) ## Requirements Only need to edit the challenge.c You have one function to implement: void fork_exec(char** argv): This takes in an array of strings representing arguments. The first argument is the filename of an executable (which will be given as a relative filepath, such as "./a") The remaining terms would be arguments for said executable. The array is null terminated You need to fork your process. The child needs to call exec (rather, a variant...
Need C++ code for following with a document describing the code as well: Write a program...
Need C++ code for following with a document describing the code as well: Write a program to implement the game of Tic Tac Toe Four. The game is played on a 4 × 4 chessboard, within 2 players. The player who is playing "X" always goes first. Players alternate placing Xs and Os on the board until either one player has four in a row, horizontally, vertically, or diagonally; or all 16 squares are filled(which is a draw). The program...
write pseudocode for the following problems not c code Pseudocode only Write a C program to...
write pseudocode for the following problems not c code Pseudocode only Write a C program to print all natural numbers from 1 to n. - using while loop Write a C program to print all natural numbers in reverse (from n to 1). - using while loop Write a C program to print all alphabets from a to z. - using while loop Write a C program to print all even numbers between 1 to 100. - using while loop...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT