An Introduction to Programming Through C++ Week 8 Answers

Q1. What will get printed due to the line labelled A above?

a. The letter ‘a’
b. The letter ‘b’
c. The letter ‘c’
d. It is not possible to predict what will be printed.

Answer: C) The letter ‘c’


Q2. What will get printed due to the line labelled B above?

a. The letter ‘a’
b. The letter ‘b’
c. The letter ‘c’
d. It is not possible to predict what will be printed.

Answer: D) It is not possible to predict what will be printed.


Q3. In the same code, suppose the user typed “abcdefghijkl”. Which of the following are true?

a. C++ will change the array size so that what the user types will be accommodated.
b. What the user types will potentially be stored in regions of memory not allocated for the variable buffer.
c. There is a different way in C++ to read data into char arrays so that no other variables get affected.

Answer: B) What the user types will potentially be stored in regions of memory not allocated for the variable buffer.


Q4. What will the value of argc be?

Answer: 4


Q5. What will the value of argv[2][2] be? Write without placing quotes etc.

Answer: u


Q6. Suppose the binary search function BSearch is called using an array of size 128. How many calls will there be including the initial?

Answer: 8

Q7. What will be the size of the array U declared in it?

Answer: 2


Q8. After mergesort has been called on U, V, what will V[0] equal?

Answer: 1


Q9. Select the correct statements among those given below. Assume that the sequence considered for sorting contains distinct elements.

a. During the execution of mergesort, the smallest element will always be compared with the second smallest.

b. During the execution of mergesort, the smallest element will always be compared with the third smallest.

c. During the execution of selectionsort, the smallest element will always be compared with the second smallest.

d. During the execution of selectionsort, the smallest element will always be compared with the third smallest.

Answer: A) During the execution of mergesort, the smallest element will always be compared with the second smallest.

Q10. Consider the merge function called to merge two sorted sequences of lengths m, n respectively. Suppose m < n. What is the minimum number of comparisons (between the elements of the sorted sequence)that the merge function must perform, no matter what the values of the elements?

a. m

b. n

c. m+n

d. None of the above

Answer: A) m

Q11. What is L?

Answer: 16


Q12. What will the first line print?

a. Nothing

b. 1

c. 4

d. 7

e. 9

f. 1 4 7 9

Answer: A) Nothing


Q13. What will the last line print?

a. Nothing

b. 1

c. 4

d. 7

e. 9

f. 1 4 7 9

Answer: f ) 1 4 7 9

Programming Assignment Answers:-

Q1. Write a program which takes the following as input: an integer N, an integer A, a sequence of N words which should be names of countries, a sequence of A words each of which is an attribute of a country (e.g. population, area, GDP), followed by a sequence of N*A real numbers (use double) where the iA+j th number gives the value of the jth attribute of the ith country, where 0<=j<A, 0<=i<N, followed by a sequence of pairs of words. The first word in each pair must be the name of a country, and the second an attribute. The program must then print the respective attribute of the respective country (e.g. population of India, GDP of China).

Code:-

#include<vector>
int main()
{
    int n, m;
    cin >> n >> m;
    vector<string> v;
    vector<string> a;
    vector<double> b;
    vector<string> c;
    for (int i = 0; i < n; i++)
    {
        string s;
        cin >> s;
        v.push_back(s);
    }
    for (int i = 0; i < m; i++)
    {
        string s;
        cin >> s;
        a.push_back(s);
    }
    for (int i = 0; i < n * m; i++)
    {
        double t;
        cin >> t;
        b.push_back(t);
    }
    string s;
    int y = 0;
    while (cin >> s)
    {
        c.push_back(s);
        y++;
    }
    int A[10000], B[10000], e = -1, p = 0;
    for (int i = 0; i < y; i++)
    {
        for (int j = 0; j < n; j++)
        {
            if (c[i] == v[j])
                A[++e] = j * m;
        }

        for (int k = 0; k < m; k++)
        {
            if (c[i] == a[k])
                B[p++] = k + A[e];
        }
    }
    for (int i = 0; i < p; i++)
        cout << b[B[i]] << endl;

    return 0;
}

Q2. You are to write a function makeT that takes as input a set S of integers, and a target value T, and determines if any subset of the numbers in Sadds up to T. The function should be a modification of the function given in the quiz for printing all subsets of a set. The function should have thefollowing signature:

Code:-

bool makeT(int S[], bool selected[], int i, int n, int T)
{

    bool set[n + 1][T + 1];

    for (int i = 0; i <= n; i++)
        set[i][0] = true;

    for (int i = 1; i <= T; i++)
        set[0][i] = false;

    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= T; j++)
        {
            if (j < S[i - 1])
                set[i][j] = set[i - 1][j];
            if (j >= S[i - 1])
                set[i][j] = set[i - 1][j] || set[i - 1][j - S[i - 1]];
        }
    }

    return set[n][T];
}

The content uploaded on this website is for reference purposes only. Please do it yourself first.