Question

In: Computer Science

Run the time complexity.cpp, which you can find at directory Code/Chapter 1. For different input n,...

Run the time complexity.cpp, which you can find at directory Code/Chapter 1. For different input n, i.e., n = 1, n = 10, n = 20, write down the final value of counter for function1, function2, and function3. Explain the counter result through math calculation.

#include <iostream>  

using namespace std;

void function1(int n){
    int count = 0;
    for(int x = 0; x < 12; x++){
        cout<<"counter:"<< count++<<endl;
    }
}

void function2(int n){
    int count = 0;
    for(int x = 0; x < n; x++){
        cout<<"--- x="<<x<<"------"<<endl;
        for(int i =0; i < n; i++){
            cout<<"counter:"<< count++<<endl;
        }
    }
}

void function3(int n){
    int count = 0;
    for(int x = 0; x < n; x++){
        cout<<"--- x ="<<x<<"------"<<endl;
        for(int i = 0; i < x; i++){
            cout<<"counter:"<< count++<<endl;
        }
    }
}


int main()
{   
    int input = 10;
    cout<<"********function 1*********"<<endl;
    function1(input);
    cout<<"********function 2*********"<<endl;
    function2(input);
    cout<<"********function 3*********"<<endl;
    function3(input);

    return 0;
}

Solutions

Expert Solution

OUTPUT:

********function 1*********
counter:0
counter:1
counter:2
counter:3
counter:4
counter:5
counter:6
counter:7
counter:8
counter:9
counter:10
counter:11

Explanation:

count value assigned to 0. In for loop, the condition is x<12 and is initialized to 0 .so the counter is printed 12 times. count++ is a post increment operator it will count after the current count value printed
********function 2*********
--- x=0------
counter:0
counter:1
counter:2
counter:3
counter:4
counter:5
counter:6
counter:7
counter:8
counter:9

Explanation:

The first for loop variable x initialized to 0 and the condition x<n .inside the loop there is another  loop  has variable i initialized to 0  and the condition i<n the loops prints count value upto n.


--- x=1------
counter:10
counter:11
counter:12
counter:13
counter:14
counter:15
counter:16
counter:17
counter:18
counter:19

Explanation:

The first for loop variable x initialized to 1 and the condition x<n .inside the loop there is another  loop  has variable i initialized to 0  and the condition i<n. the loops prints count value upto n.

--- x=2------
counter:20
counter:21
counter:22
counter:23
counter:24
counter:25
counter:26
counter:27
counter:28
counter:29

Explanation:

The first for loop variable x initialized to 2 and the condition x<n .inside the loop there is another  loop  has variable i initialized to 0  and the condition i<n. the loops prints count value upto n.

--- x=3------
counter:30
counter:31
counter:32
counter:33
counter:34
counter:35
counter:36
counter:37
counter:38
counter:39

Explanation:

The first for loop variable x initialized to 3 and the condition x<n .inside the loop there is another  loop  has variable i initialized to 0  and the condition i<n. the loops prints count value upto n.

--- x=4------
counter:40
counter:41
counter:42
counter:43
counter:44
counter:45
counter:46
counter:47
counter:48
counter:49

Explanation:

The first for loop variable x initialized to 4 and the condition x<n .inside the loop there is another  loop  has variable i initialized to 0  and the condition i<n. the loops prints count value upto n.

--- x=5------
counter:50
counter:51
counter:52
counter:53
counter:54
counter:55
counter:56
counter:57
counter:58
counter:59

Explanation:

The first for loop variable x initialized to 5  and the condition x<n .inside the loop there is another  loop  has variable i initialized to 0  and the condition i<n. the loops prints count value upto n.

--- x=6------
counter:60
counter:61
counter:62
counter:63
counter:64
counter:65
counter:66
counter:67
counter:68
counter:69

Explanation:

The first for loop variable x initialized to 6 and the condition x<n .inside the loop there is another  loop  has variable i initialized to 0  and the condition i<n. the loops prints count value upto n.

--- x=7------
counter:70
counter:71
counter:72
counter:73
counter:74
counter:75
counter:76
counter:77
counter:78
counter:79

Explanation:

The first for loop variable x initialized to 7 and the condition x<n .inside the loop there is another  loop  has variable i initialized to 0  and the condition i<n. the loops prints count value upto n.

--- x=8------
counter:80
counter:81
counter:82
counter:83
counter:84
counter:85
counter:86
counter:87
counter:88
counter:89

Explanation:

The first for loop variable x initialized to 8 and the condition x<n .inside the loop there is another  loop  has variable i initialized to 0  and the condition i<n. the loops prints count value upto n.

--- x=9------
counter:90
counter:91
counter:92
counter:93
counter:94
counter:95
counter:96
counter:97
counter:98
counter:99

Explanation:

The first for loop variable x initialized to 9 and the condition x<n .inside the loop there is another  loop  has variable i initialized to 0  and the condition i<n. the loops prints count value upto n.

********function 3*********
--- x =0------

Explanation:

The first for loop variable x initialized to 0 and the condition x<n .inside the loop there is another  loop  has variable i initialized to 0  and the condition i<x(0<0)  the condition FALSE and the inside loop will never executed.

--- x =1------
counter:0

Explanation:

The first for loop variable x initialized to 1 and the condition x<n .inside the loop there is another  loop  has variable i initialized to 0  and the condition i<x(0<1) the condition TRUE and the inside loop will executes upto x.
--- x =2------
counter:1
counter:2

Explanation:

The first for loop variable x initialized to 2 and the condition x<n .inside the loop there is another  loop  has variable i initialized to 0  and the condition i<x(0<2) the condition TRUE and the inside loop will executes upto x.

--- x =3------
counter:3
counter:4
counter:5

Explanation:

The first for loop variable x initialized to 3 and the condition x<n .inside the loop there is another  loop  has variable i initialized to 0  and the condition i<x(0<3) the condition TRUE and the inside loop will executes upto x.

--- x =4------
counter:6
counter:7
counter:8
counter:9

Explanation:

The first for loop variable x initialized to 4 and the condition x<n .inside the loop there is another  loop  has variable i initialized to 0  and the condition i<x(0<4) the condition TRUE and the inside loop will executes upto x.

--- x =5------
counter:10
counter:11
counter:12
counter:13
counter:14

Explanation:

The first for loop variable x initialized to 5 and the condition x<n .inside the loop there is another  loop  has variable i initialized to 0  and the condition i<x(0<5) the condition TRUE and the inside loop will executes upto x.

--- x =6------
counter:15
counter:16
counter:17
counter:18
counter:19
counter:20

Explanation:

The first for loop variable x initialized to 6 and the condition x<n .inside the loop there is another  loop  has variable i initialized to 0  and the condition i<x(0<6) the condition TRUE and the inside loop will executes upto x.

--- x =7------
counter:21
counter:22
counter:23
counter:24
counter:25
counter:26
counter:27

Explanation:

The first for loop variable x initialized to 7 and the condition x<n .inside the loop there is another  loop  has variable i initialized to 0  and the condition i<x(0<7) the condition TRUE and the inside loop will executes upto x.

--- x =8------
counter:28
counter:29
counter:30
counter:31
counter:32
counter:33
counter:34
counter:35

Explanation:

The first for loop variable x initialized to 8 and the condition x<n .inside the loop there is another  loop  has variable i initialized to 0  and the condition i<x(0<8) the condition TRUE and the inside loop will executes upto x.

--- x =9------
counter:36
counter:37
counter:38
counter:39
counter:40
counter:41
counter:42
counter:43
counter:44

Explanation:

The first for loop variable x initialized to 9 and the condition x<n .inside the loop there is another  loop  has variable i initialized to 0  and the condition i<x(0<9) the condition TRUE and the inside loop will executes upto x and finally the loop terminates.


Related Solutions

how do i find a peak in time complexity of O(log(n)) in a list? input: a...
how do i find a peak in time complexity of O(log(n)) in a list? input: a list of numbers or integers output: a possible local peak in the list for an example: calling function [2,3,8,9,5] would return 3 im thinking about using binary search but it would require a sorted list.
1) Which of the following is/are true? - C++ programs can run on different platforms, like...
1) Which of the following is/are true? - C++ programs can run on different platforms, like Windows and macOS, without being recompiled. - The source code for a program can include code from the C++ standard library. - The machine language that results from compiling a program is easy for a human to read. - All of the above 2) What feature of an IDE can help you identify and fix syntax errors? -the code completion feature -the automatic compilation...
please write the java code so it can run on jGRASP Thanks! CODE 1 1 /**...
please write the java code so it can run on jGRASP Thanks! CODE 1 1 /** 2 * SameArray2.java 3 * @author Sherri Vaseashta4 * @version1 5 * @see 6 */ 7 import java.util.Scanner;8 public class SameArray29{ 10 public static void main(String[] args) 11 { 12 int[] array1 = {2, 4, 6, 8, 10}; 13 int[] array2 = new int[5]; //initializing array2 14 15 //copies the content of array1 and array2 16 for (int arrayCounter = 0; arrayCounter < 5;...
A Mystery Algorithm Input: An integer n ≥ 1 Output: ?? Find P such that 2...
A Mystery Algorithm Input: An integer n ≥ 1 Output: ?? Find P such that 2 P is the largest power of two less than or equal to n. Create a 1-dimensional table with P +1 columns. The leftmost entry is the Pth column and the rightmost entry is the 0th column. Repeat until P < 0 If 2 P ≤ n then put 1 into column P set n := n − 2 P Else put 0 into column...
Objectives To learn to code, compile, and run a program using file input and an output...
Objectives To learn to code, compile, and run a program using file input and an output file. Assignment Plan and code a program utilizing one file for input and one file for output to solve the following problem: Write a program to determine the highest number, the lowest number, their total, and the average of each line of numbers in a file. A file contains 7 numbers per line. How many lines a file contains is unknown. Note Label all...
A Mystery Algorithm Input: An integer n ≥ 1 Output: ?? Find P such that 2^p...
A Mystery Algorithm Input: An integer n ≥ 1 Output: ?? Find P such that 2^p is the largest power of two less than or equal to n. Create a 1-dimensional table with P +1 columns. The leftmost entry is the Pth column and the rightmost entry is the 0th column. Repeat until P < 0 If 2^p≤n then put 1 into column P set n := n - 2^p Else put 0 into column P End if Subtract 1...
There are logical and run time errors in the code, as well as compilation errors. **...
There are logical and run time errors in the code, as well as compilation errors. ** Your objective is get the code to run, fix any bugs in the code and ** place validation needed to prevent any memory issues. Each function has a ** description of what it does next to its prototype. ** When you find a problem, fix it in the code and note it in this header comment below. ** Please note each problem you find...
in C please! Write a code that asks user for input N and calculates the sum...
in C please! Write a code that asks user for input N and calculates the sum of the first N odd integers. There should be a "pure" method that calculates the sum separately that Main method should call when printing the output.
please write the java code so it can run on jGRASP Thanks! 1 /** 2 *...
please write the java code so it can run on jGRASP Thanks! 1 /** 2 * PassArray 3 * @Sherri Vaseashta 4 * @Version 1 5 * @see 6 */ 7 import java.util.Scanner; 8 9 /** 10 This program demonstrates passing an array 11 as an argument to a method 12 */13 14 public class PassArray 15 { 16 public static void main(String[] args) 17 { 18 19 final int ARRAY_SIZE = 4; //Size of the array 20 // Create...
Java Chapter 12.29 (Rename files) suppose you have a lot of files in a directory named...
Java Chapter 12.29 (Rename files) suppose you have a lot of files in a directory named Exercisei_j, where i and j are digits. Write a program that pads a 0 before j if j is a single digit. For example, a file named Exercise2_1 in a directory will be renamed to Exercise2_01. In Java, when you pass the symbol * from the command line, it refers to all files in the directory (see Supplement III.V). Use the following command to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT