Question

For different input n, i.e., n = 1, n = 10, n = 20, write down...

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;
}

Homework Answers

Answer #1

function 1:

In function 1 the value of the parameter 'n' is not used in computing the value of the counter. Therefore, no matter what value of 'n' we pass to function 1 the value of counter will be the same.

Here a loop runs 12 times and for each iteration, the value of count is incremented by 1. As the count variable starts from 0 so, the final value of the counter will be printed as 11 when we call function 1.

function 2:

In function 2 there is a nested loop and for each loop runs 'n' times. For each iteration of the outer loop the inner loop runs 'n' times and the value of the count variable is incremented accordingly.

For each iteration of the outer loop, the value of count is incremented by 'n'.

For the first iteration of the outer loop, the value of count will be incremented by 'n-1' as it starts from 0.

For the rest of the iterations, the value of count will be incremented by 'n'.So the final value of the counter will be (n-1) + n (n-1 times.. i.e for the rest of the iterations).

Therefore,

counter final value = (n-1) + (n-1)*n

or,

(n-1)*(n+1)

function 3:

In function 3 there is also a nested loop. For each iteration of the outer loop the inner loop runs 'x' times and the value of the count variable is incremented accordingly.

For each iteration of the outer loop, the value of count is incremented by 'x'.

For the first iteration of the outer loop, the value of count will be incremented by 0 as it starts from x=0.

When x=1:

the inner loop runs 0 times.

When x=2:

the inner loop runs 1 time.

.

.

.

.

When x=n:

the inner loop runs n-1 times.

Therefore,

counter final value = 1 + 2 + 3 + ........... + n-1 - 1= n(n-1)/2 - 1. ( -1 because value of count starts from 0)

NOTE: When n=1 no value of counter will be printed as the inner loop will never run.

Hence:

n=1

function 1:

final counter value = 11

function 2:

final counter value = (n-1)*(n+1) = (1-1)*(1+1) = 0

function 3:

final counter value = n(n-1)/2 - 1 = 1(1-1)/2 - 1 = 0

but here no value of the counter will be printed.

n=10

function 1:

final counter value = 11

function 2:

final counter value = (n-1)*(n+1) = (10-1)*(10+1) = 99

function 3:

final counter value = n(n-1)/2 - 1 = 10(10-1)/2 - 1 = 44

n=20

function 1:

final counter value = 11

function 2:

final counter value = (n-1)*(n+1) = (20-1)*(20+1) = 399

function 3:

final counter value = n(n-1)/2 - 1 = 20(20-1)/2 - 1 = 189

Know the answer?
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
Similar Questions
Analyze the following program and write down the output. # include <iostream> using namespace std;    void...
Analyze the following program and write down the output. # include <iostream> using namespace std;    void modifyArray( int [ ], int );    void modifyElement ( int );      int main( ) {   const int arraySize = 8;   int a[arraySize] = { 2, -2, 10, -3, -1 ,0, 10, -5 };      modifyArray ( a, arraySize);      for ( int i =0; i < arraySize; i++)               cout << a[i] << ‘  ’;         modifyElement ( a[4] );          for ( int i =0; i <...
1) Create a flowchart for the program below and Trace the program below with input of...
1) Create a flowchart for the program below and Trace the program below with input of 3 and then again with input of 5. #include <iostream> using namespace std; int Fall(int x, int m) { int Xm = 1, i=x; while(i>=x-m+1) { Xm = Xm*i; i=i-1; } return Xm; } int Delta(int x, int m) { int ans = 0; ans = Fall(x+1,m); ans = ans - Fall(x,m); return ans; } void main() { int x = 0, m =...
Analyze the following programs and write down the output of the program. Please print every character...
Analyze the following programs and write down the output of the program. Please print every character (including whitespace character) clearly!       # include <iostream> using namespace std;   int fun( int a ) {       int b = a * 2;       return b;   }   int main()   {       int y = 5;       cout << fun(y) << endl;       cout << fun(-- y) << endl;       cout << fun(y--) << endl;       cout << y <<endl;           return 0;   }
Write up to three lines to explain the logic used behind those codes.        1) #include <iostream>...
Write up to three lines to explain the logic used behind those codes.        1) #include <iostream> #include <string> #include <fstream> #include <vector> #include <sstream> using namespace std; int main() {         ifstream infile("worldpop.txt");         vector<pair<string, int>> population_directory;         string line;         while(getline(infile, line)){                 if(line.size()>0){                         stringstream ss(line);                         string country;                         int population;                         ss>>country;                         ss>>population;                         population_directory.push_back(make_pair(country, population));                 }         }         cout<<"Task 1"<<endl;         cout<<"Names of countries with population>=1000,000,000"<<endl;         for(int i=0;i<population_directory.size();i++){                 if(population_directory[i].second>=1000000000){                        ...
Please write variables and program plan (pseudocode) of the C++ programming below: #include <iostream> #include <cmath>...
Please write variables and program plan (pseudocode) of the C++ programming below: #include <iostream> #include <cmath> using namespace std; void divisors(int num); int main () {    char repeat;    int num;       while (repeat !='n')    {        cout << "Enter a number: ";        cin >> num;        divisors(num);        cout << "Continue? (y or n): ";        cin >> repeat;    }    return 0; } void divisors(int num) {   ...
can someone edit my c++ code where it will output to a file. I am currently...
can someone edit my c++ code where it will output to a file. I am currently using xcode. #include <iostream> #include <cctype> #include <cstring> #include <fstream> using namespace std; bool inputNum(int [],int&,istream&); void multiply(int[],int,int[],int,int[],int&); void print(int[],int,int,int); int main() {ifstream input; int num1[35],num2[35],len1,len2,num3[60],len3=10,i; input.open("multiplyV2.txt"); //open file if(input.fail()) //is it ok? { cout<<"file did not open please check it\n"; system("pause"); return 1; }    while(inputNum(num1,len1,input)) {inputNum(num2,len2,input); multiply(num1,len1,num2,len2,num3,len3); print(num1,len1,len3,1); print(num2,len2,len3,2); for(i=0;i<len3;i++) cout<<"-"; cout<<endl; print(num3,len3,len3,1); //cout<<len1<<" "<<len2<<" "<<len3<<endl; cout<<endl;    } system("pause"); } void...
Write a program that reads a string and outputs the number of lowercase vowels in the...
Write a program that reads a string and outputs the number of lowercase vowels in the string. Your program must contain a function with a parameter of a char variable that returns an int. The function will return a 1 if the char being passed in is a lowercase vowel, and a 0 for any other character. The output for your main program should be: There are XXXX lowercase vowels in string yyyyyyyyyyyyyyyyyyyyyy Where XXXX is the count of lowercase...
Implement the body of the swap function using the indirection operator // finish this program so...
Implement the body of the swap function using the indirection operator // finish this program so that the values of // x and y are swapped in main(). #include <iostream> using namespace std; void swap(int* first, int* second) { // IMPLEMENT THIS FUNCTION // YOUR CODE GOES HERE } int main() { int x = 9; int y = 2; swap(&x, &y); cout << "x: " << x << endl; cout << "y: " << y << endl; // if...
C++ please Write code to implement the Karatsuba multiplication algorithm in the file linked in Assignment...
C++ please Write code to implement the Karatsuba multiplication algorithm in the file linked in Assignment 2 (karatsuba.cpp) in Canvas (please do not rename file or use cout/cin statements in your solution). As a reminder, the algorithm uses recursion to produce the results, so make sure you implement it as a recursive function. Please develop your code in small The test program (karatsuba_test.cpp) is also given. PLEASE DO NOT MODIFY THE TEST FILE. KARATSUBA.CPP /* Karatsuba multiplication */ #include <iostream>...
C++ Write a recursive function that reverses the given input string. No loops allowed, only use...
C++ Write a recursive function that reverses the given input string. No loops allowed, only use recursive functions. Do not add more or change the parameters to the original function. Do not change the main program. I had asked this before but the solution I was given did not work. #include #include using namespace std; void reverse(string &str) { /*Code needed*/ } int main() {    string name = "sammy";    reverse(name);    cout << name << endl; //should display...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT