# NPTEL An Introduction To Programming Through C++ Assignment 7 Answers 2022

Are you looking for some help in nptel an introduction to programming through C++ assignment 7 answers? So, in this article, we have provided you hint of both quiz and programming questions.

## NPTEL An Introduction To Programming Through C++ Assignment 7 Answers (Week 7)

Q1. Consider the line of code

``int x = {10, 20, 30, 40};``

What is the value of x?

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?

Q3. Consider the following program fragment:

``````int a;
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?

Note: Most C++ programs will print true as 1 and false as 0. So in the above, think of true and false as meaning 1 and 0.

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. The program outputs true if elements of a are in decreasing order, i.e. a[i] > a[i+1] for i=0..8

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.

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

Q6. What should blank2 be?

Q7. What should blank3 be?

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?

Consider the following code fragment:

``````int q={25, 26, 27, 28};
cout << q << endl;
cout << &q << endl;
cout << q << endl;
q = 6;
q = 1600;``````

Assume q is stored at address 1500. Also assume that each int is 4 bytes wide, so q will be stored at 1504, q 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.

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

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

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. Line 5 contains a valid C++ statement.

Disclaimer: These answers are provided only for the purpose to help students to take references. This website does not claim any surety of 100% correct answers. So, this website urges you to complete your assignment yourself.

Also Available:

Geometric Sequence and Calculation Method

An Introduction to Programming through C++ Week 8 NPTEL Assignment Solutions