An Introduction to Programming Through C++ Week 12 Answers

                     

Due date: 2022-04-20, 23:59 IST.

  1. For answering this question you will need to see the program given in the announcement “The circuit simulator program” made on 2022-03-31. On line number 146, there is a call to a member function addNode. What is the number of the line starting from which this function is defined? If you look at the program you will see one addNode member function starts on line number 32 and another on line number 91. You have to choose the correct line number out of these two.

Answer: 91

2. For answering this question you will need to see the program given in the announcement “The circuit simulator program” made on 2022-03-31. The program contains a selectNode member function. When this function executes, the computer waits for the user to click. What value will this function return if the user clicks on the third button from the top?

Answer: 3

For answering this question you will need to see the program given in the announcement “The circuit simulator program” made on 2022-03-31.Suppose in a certain circuit node 5 is connected to nodes 6, 7, and 8 respectively using conductances of magnitude 12, 18 and 20. Suppose node 5 is not connected to any other nodes.

3. What will be the value of A[5][6]?

Answer: -12

4. What will be the value of A[5][5]?

Answer: 50

5. Consider the definition
map > citiesInCountry;

where citiesInCountry maps the country name to its set of cities.

What does citiesInCountry.count(“India”) return?
a) How many cities are there in India, as stored in citiesInCountry.
b) Whether any cities are stored for India in citiesInCountry, (0: none, 1: some).
c) This expression is invalid.

Answer: b
6. The following code is supposed to print the cities in India, as stored in
citiesInCountry.

for(auto c : citiesInCountry[“India”]) cout << blank <<‘ ‘;

What should blank be?

Answer: c

The following function uses the first order Euler method to find f(t1) given t0, t1, f0=f(t0), t, a function fdash (representing the derivative f’), and the number of steps from which the stepsize delta is to be calculated.

double euler1(double t0, double t1, double f0,
std::function fdash, int n){
double delta = (t1-t0)/n;
double f1 = f0;
double t = t0;
for(int i=0; i<n; i++){
f1 = f1 + delta * fdash(t);
t = t + delta;
}
return f1;
}

The second order Euler code is as follows.

double euler2(double t0, double t1, double f0,
std::function fdash, int n){
double delta = (t1-t0)/n;
double f1 = f0;
double t = t0;
for(int i=0; i<n; i++){
f1 = f1 + delta * fdash(t+delta/2);
t = t + delta;
}
return f1;
}

7. Suppose fdash is a constant function, i.e. the derivative is the same throughout. Suppose fdash(t) = 3 for all t, t0 = 0, t1= 1, f0=0 and n=4. These parameters are used to call euler1. Select the correct alternative(s) from the following by reasoning out or by manually executing the function. Ignore roundoff errors.

a) euler1 will return the exactly correct answer for this case.
b) euler1 will not return the exactly correct answer for this case.
c) euler2 will return the exactly correct answer for this case.
d) euler3 will not return the exactly correct answer for this case.

Answer: a,c

8. Suppose fdash is a linear function, say fdash(t) = 2t for all t. Suppose t0 = 0, t1= 1, f0=0 and n=4.
Select the correct alternative(s) from the following by reasoning out or by manually executing the function. Ignore roundoff errors.
a) euler1 will return the exactly correct answer for this case.
b) euler1 will not return the exactly correct answer for this case.
c) euler2 will return the exactly correct answer for this case.
d) euler2 will not return the exactly correct answer for this case.

Answer: b,d

Programming Assignment 12.1

Due on 2022-04-21, 23:59 IST

Given below is the main program in a program to print the past tense of a verb in the English language. If the verb ends in “e”, then it simply adds a “d” and returns the result as the past tense. If the verb ends in any other letter, it adds “ed” and returns the result. However, both these behaviours are superseded if the verb supplied to it is in an exception table that it maintains. The exception table also holds the past tense of each word in the table, and so that is returned instead. The program can also be told that a certain word x has a certain past tense y. If y can be obtained from x using the basic rules given above, the program does nothing. If y cannot be obtained from x using the basic rules, then x, y are added to the table. The table is implemented as a map from strings to strings. In addition, the program has a command which causes it to terminate. The main program is given below, and already entered in your submission. You only have to supply the code for the functions it calls. The code will be placed above the main program so that separate declarations are not needed.

Solution:


string pasttense(map<string,string> &m,string &verb)
{
  if(m.find(verb)!=m.end())
    return m[verb];
  char lc=verb[verb.length()-1];
  if(lc=='e')
    return verb+"d";
  return verb+"ed";
}
void add(map<string,string> &m,string &verb,string &pt)
{
  char lc=verb[verb.length()-1];
  string past_tense="";
  if(lc=='e')
    past_tense=verb+"d";
  else past_tense=verb+"ed";
  if(past_tense!=pt)
    m[verb]=pt;
}

Programming Assignment 12.2

Due on 2022-04-21, 23:59 IST

Suppose I have a box which contains some boxes, each of which contain more boxes and so on, for a total of n + 1 boxes. Suppose the boxes are numbered from 0 to n with the outermost box receiving the number 0. Now the configuration can be described by specifying numbers x_1, … , x_n,where x_i denotes the number of the box directly containing box i. For example, the configuration shown below is described by the sequence 0, 3,4, 0


You are to help write a program that takes the description of such a configuration, and numbers y, z and prints (a) how many boxes need to beopened to take out the contents of y, (b) how many boxes need to be opened to take out the contents of z, ( c ) how many boxes need to beopened to take out the contents of both y and z. For example, if (y,z) = (3,1) in the picture then our answers are 3, 2 and 4 respectively.We have written the main program for you, and you are to only write the two functions open1box and open2boxes. Note that our program does not always print all 3 numbers. Sometimes it prints just the first 2. This makes it easier for us to give you credit for getting the open1box function correct.

Solution:

int open1box(vector<int> &x, int y) {

  int out = 1;

  while (true) {
    out++;
    int temp = x[y];
    if (temp == 0) break;
    y = temp;
  }
  return out;
}

int open2boxes(vector<int> &x, int y, int z) {

  int len_x = open1box(x, y),dis = 0;
  int len_y = open1box(x, z);
  int a[len_x], b[len_y];

  a[0] = y; b[0] = z;
  for (int i = 1; i < len_x; i++){
    a[i] = x[y];
    y = a[i];
  }

  for (int j = 1; j < len_y; j++) {
    b[j] = x[z];
    z = b[j];
  }

  int arr[len_x+len_y];
  for (int i = 0; i < len_x+len_y; i++) {
    if (i < len_x) arr[i] = a[i];
    else arr[i] = b[i-len_x];
  }

  for (int i = 0; i < len_x+len_y; i++) {
    bool flag = 1;
    for (int j = 0; j < i; j++) {
      if (arr[i] == arr[j]) {flag = 0; break;} 
    }
    if (flag) dis++;
  }
  return dis;

}