An Introduction to Programming Through C++ | NPTEL 2022 | Week 7 Answers

This set of MCQ(multiple choice questions) focuses on the An Introduction to Programming Through C++ NPTEL 2022 Week 7 Answers.

Course layout

Answers COMING SOON! Kindly Wait!

NOTE: You can check your answer immediately by clicking show answer button. An Introduction to Programming Through C++ NPTEL 2022 Week 7 Assignment Answers” contains 12 questions.

Now, start attempting the quiz.

An Introduction to Programming Through C++ NPTEL 2022 Week 7 Assignment Answers

Q1. Consider the line of code
int x[4] = {10, 20, 30, 40};
What is the value of x[3]?

Answer: 40

Q2. Suppose students can obtain any (integer) mark from 0 to 100. I would like a histogram giving the number of students getting marks in the range 0-4, 5-9, 10-14, … 95-99, 100. What size array do I need to store the histogram?

Answer: 21

Q3. Consider the following program fragment:

int a[10];
for(int i=0; i<10; i++) cin >> a[i];
bool s=true;
for(int i=0; i<9; i++)
     if(a[i] < a[i+1]) s = false;
cout << s;
Which of the following are true?

a) The program outputs true if elements of a are in increasing order, i.e. a[i] < a[i+1] for i=0..8
b) The program outputs true if elements of a are in decreasing order, i.e. a[i] > a[i+1] for i=0..8
c) The program outputs true if elements of a are in non-increasing order, i.e. a[i] >= a[i+1] for i=0..8
d) The program always outputs true if elements of a are in non-decreasing order, i.e. a[i] <= a[i+1] for i=0..8

Answer: b)

Q4. Consider the following function which is supposed to return true if all the numbers in an array A are even and false otherwise:

bool allEven(int a[], int n){
   int i=0;
   for(i=0; i<n; i++)
        if(a[i] %2 != 0) break;
   if(i == blank) return true;
   else return false;
}
What should be in place of blank? Answer without any quotes or spaces.

Answer: n

Suppose we wish to know if 3 numbers in a set S add up to a certain target value T. The program below is meant to determine this, by going over all triples of numbers in S. It should check all triples, but not check
any triple more than once.
int main(){
   int n; cin >> n;
int S[n]; for(int i=0; i<n; i++) cin >> S[i];
int T; cin >> T;
bool addsToT = false;
for(int i=0; i<blank1; i++)
   for(int j=0; j<blank2; j++)
      for(int k=0; k<blank3; k++)
          if(S[i] + S[j] + S[k] == T) addsToT = true;
cout << addsToT << endl;
}

Give the simplest answers for the questions below. Do not use quotes or unnecessary spaces.

Q5. What should blank1 be?

Answer: n

An Introduction to Programming Through C++ NPTEL Week 7 Assignment Answers

Q6. What should blank2 be?

Answer: i

Q7. What should blank3 be?

Answer: j

An Introduction to Programming Through C++ NPTEL Week 7 Assignment Answers

Q8. Suppose that we wanted the program to print whether T is obtained by taking the sum of 3 numbers chosen from S with replacement, i.e. the same number can be used several times. The code above will work, but the blanks will need to be filled possibly differently. What should blank2 be for this case?

Answer: n

Consider the following code fragment.
int q[4]={25, 26, 27, 28};
cout << q[0] << endl;
cout << &q[2] << endl;
cout << q << endl;
q[0] = 6;
q = 1600;

Assume q[0] is stored at address 1500. Also assume that each int is 4 bytes wide, so q[1] will be stored at 1504, q[2] at 1508 and so on. Note that normally when you print an address, it will get printed in
hexadecimal; but just for the exercises below assume that addresses are also printed in decimal.

Q9. State what value is printed by the statement in line 2.

Answer: 25

Q10. State what value is printed by the statement in line 3.

Answer: 1508

An Introduction to Programming Through C++ NPTEL week 7 Assignment Solutions

Q11. State what value is printed by the statement in line 4.

Answer: 1500

Q12. Which of the following statements are true?

a) Line 5 contains a valid C++ statement.
b) Line 6 contains a valid C++ statement.

Answer: a)

An Introduction to Programming Through C++ NPTEL Week 7 Programming Assignment Answers

Programming Assignment 7.1

Write a program that keeps track of the money in your wallet. As an example, suppose only notes of denominations 2000, 500, 200, 100, 50,20, 10 are in use. And suppose you have respectively 2, 5, 8, 4, 1, 1, 1 notes of the denominations. Thus you have a total of 4000 + 2500 + 1600+ 400 + 50 + 20 + 10 = 8580 rupees.

CODE:

main_program {
  
  int n, m, i;
  int a[10], d[m], sum = 0, rup, max = 0;
  char com;
  cin>>n;
  for(i=0; i<n; i++) {
    cin>>a[i];
  }
  m = a[i-1];
  for(i=1; i<=m; i++) {
    d[i] = 0;
  }
  for(i=0; i<n; i++) {
    cin>>d[a[i]];
    sum = sum + a[i] * d[a[i]];
  }
  while(1) {
    cin>>com;
    switch(com) {
      case 'P': cout<<sum<<endl;
                break;
      case 'S': cin>>rup;
                if(d[rup]>0) {
                    d[rup]--;
                sum = sum - rup;
                }
                break;
      case 'E': exit(1);
    }
  }
  return 0;
}

Programming Assignment 7.2

Write a function that takes an array of integers as an argument (in the usual manner, so two arguments including the length), and returns the mode of the elements in the array, i.e. an integer that occurs the maximum number of times. If there are several integers that occur the maximum number of times, then you should return that integer whose first appearance in the array is at the largest index. So for example, if the array contains 9, 8, 3, 4, 2, 4, 2, 9, 7, 4, 9, then there are 2 integers, 4 and 9 that appear the maximum number of times, i.e. 3 times. The first appearance of 9 is at index 0, while the first appearance of 4 is at index 3. Thus the function should return 4.

CODE:

int mode(int arr[], int n) {
  
  int max;
  max = arr[0];
  int count[n];
  int c=1, val=arr[0];
  
  for(int i=0; i<n; i++) {
    if(max < arr[i])
      max = arr[i];
  }
  for(int i=0; i<n; i++) 
    count[arr[i]]=1;
  
  for(int i=0; i<n-1; i++) {
    for(int j=i+1; j<n; j++) {
      if(arr[i]==arr[j]) {
        count[arr[i]]++;
        if(c <= count[arr[i]]) {
          c = count[arr[i]];
          val = arr[i];
        }
      }
    }
  }
  if(c == 1) 
    val = arr[n-1];
  return val;
}
🏆🏆🏆 Thank You Pratyush Kumar for correction.👏

<< Prev- An Introduction to Programming Through C++ Week 6 Solutions

>> Next- An Introduction to Programming Through C++ Week 8 Solutions


DISCLAIMER: Use these answers only for the reference purpose. Quizermania doesn't claim these answers to be 100% correct. So, make sure you submit your assignments on the basis of your knowledge.

For discussion about any question, join the below comment section. And get the solution of your query. Also, try to share your thoughts about the topics covered in this particular quiz.

6 thoughts on “An Introduction to Programming Through C++ | NPTEL 2022 | Week 7 Answers”

    1. int mode (int arr[], int n)
      {
      int i, j, max, count[max], c=1, val;
      max=val=arr[0];

      for (i=0;i<n;i++)
      {
      if(max<arr[i])
      max=arr[i];
      }
      for (i=0;i<n; i++)
      {
      count[arr[i]]=1;
      }
      for (i=0;i<n-1; i++)
      {
      for(j=i+1;j<n; j++)
      {
      if(arr[i]==arr[j])
      {
      count [arr[i]]++;
      if(c<=count[arr[i]])
      {
      c=count[arr[i]];
      val=arr[i];
      }
      }
      }
      }

      if(c==1)
      {
      val=arr[n-1];
      }
      return val;
      }

    2. int mode(int arr[],int n){

      int i,j,max,count[max],c=1,val;
      max=val=arr[0];
      for(i=0;i<n;i++){
      if(max<arr[i]){
      max=arr[i];
      }
      }

      for(i=0;i<n;i++){
      count[arr[i]]=1;
      }
      for(i=0;i<n-1;i++){
      for(j=i+1;j<n;j++){
      if(arr[i]==arr[j]){
      count[arr[i]]++;
      if(c<=count[arr[i]]){
      c=count[arr[i]];
      val=arr[i];
      }
      }
      }
      }
      if(c==1){
      val=arr[n-1];
      }
      return val;
      }

  1. Programming Assignment 7.2
    int mode (int arr[], int n)
    {
    int i, j, max, count[max], c=1, val;
    max=val=arr[0];

    for (i=0;i<n;i++)
    {
    if(max<arr[i])
    max=arr[i];
    }
    for (i=0;i<n; i++)
    {
    count[arr[i]]=1;
    }
    for (i=0;i<n-1; i++)
    {
    for(j=i+1;j<n; j++)
    {
    if(arr[i]==arr[j])
    {
    count [arr[i]]++;
    if(c<=count[arr[i]])
    {
    c=count[arr[i]];
    val=arr[i];
    }
    }
    }
    }

    if(c==1)
    {
    val=arr[n-1];
    }
    return val;
    }

Leave a Comment

Your email address will not be published. Required fields are marked *