An Introduction To Programming Through C++
IIT Bombay.
Week 7- QUIZ ASSIGNMENT:
1)Suppose at the beginning of my main program I have the following declarations.
double vx[50],vy[50],z;
Given the above declarations, which of the following statement(s) contain an error?
a)vx[10] = vy[15];
b)vx = vy;
c)z = vy[50];
d)z[0] = vx[10];
-----------------------------------------------------------------------------------------------------------------------
Consider the following program fragment.
int a[5];
for (int i=0; i<5; i++) cin >> a[i];
int s = 0;
for (int i=0; i<4; i++)
s = s + a[i];
if (s == a[4]) cout <<1;
else cout <<0;
2)What does the above program print for the input 10 20 -30 5 5?
Ans: 1
3)What does the above program print for the input 10 20 30 5 5?
Ans: 0
-----------------------------------------------------------------------------------------------------------------------
4)Which statements are true regarding the above program?
a)If the input consists of an increasing sequence of positive integers the program will always print 1.
b)If the input consists of an increasing sequence of positive integers the program will sometimes print 1 and sometime 0.
c)If the input consists of a decreasing sequence of positive integers the program will always print 0.
d)If the input consists of a decreasing sequence of positive integers the program will sometimes print 1 and sometime 0.
-----------------------------------------------------------------------------------------------------------------------
5)In the lectures, we saw a program that reads roll numbers and corresponding marks, and then subsequently prints marks given a roll number. The code below does the same thing, but without having the bool variable 'found' which was present in the code given in the lecture. Also note that the variable i is declared differently, see comment.
int main(){
int rollno[100]; double marks[100];
for(int i=0; i<100; i++) cin >> rollno[i] >> marks[i];
while(true){
int r; cin >> r; // read in query roll number
if(r == -1) break;
int i; // i declared outside loop
for(i=99; i>=0; i--){
if(rollno[i] == r){
cout << marks[i] << endl;
break;
}
}
if(i == ___) cout << "Roll number not found.\n";
}
}
What value should be in the blank so that the code works as described
above?
Ans: 100
-----------------------------------------------------------------------------------------------------------------------
6)Suppose I use the program given in the lectures to multiply a degree 5 polynomial by a degree 10 polynomial. Note that a degree n polynomial has n+1 coefficients. Then the number of multiplications (between polynomial coefficients) performed is?
Ans: 66
Suppose I am given an array x[20]. The following code is meant to determine if there exist distinct i,j,k such that x[i]+x[j]+x[k]=0. It is expected to print True (which is printed as 1 on some computers) if such a triple exists, and False (which is printed as 0 on some computers) otherwise.
bool sum0found = false
for(int i=19; i>=0; i--)
for(int j=j1; j>=0; j--)
for(int k=k1; k>=0; k--)
if (x[i] + x[j] + x[k] == 0){
sum0found = true;
break;
}
cout << sum0found;
7)Which of the following values should I use for j1 and k1? Note that I want the program to be correct and also not do unnecessary work.
a)Use j1=19, k1=19
b)Use j1=i, k1=j
c)Use j1=i+1, k1=j+1
d)Use j1=i-1,k1=j-1
8)Suppose all the elements of x are positive except for x[19]. Then I can modify one of the loop bounds in the above code - so that the program gives the correct answer but the number of times the condition x[i]+x[j]+x[k] ==0 is checked, reduces considerably. How many times is the condition checked in the worst case after the modification?
Ans:
-----------------------------------------------------------------------------------------------------------------------
Consider the following code fragment.
int q[4]={25,26,27,28};
Assume q[0] is stored at address 2000. Also assume that each int is 4 bytes wide, so q[1] will be stored at 2004, q[2] at 2008 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.
For each of the following statements, give what is printed. For the questions below, give your answer as a decimal number. If you think the question cannot be answered, respond by writing -1.
9)cout << q;
Ans: 2000
10)cout << q[1];
Ans: 26
11)cout << &q[3];
Ans: 12
-----------------------------------------------------------------------------------------------------------------------
The function below is supposed to print the sum of the array passed to it.The main program calls the function to get the sum of the elements of the array A.
int amax(int A[], int n){ // precondition n > 0
if(n==1) return blank1;
return blank2 + amax(A,n-1);
}
int main(){
int A[5]={25,61,0,3,7};
cout << amax(blank3,blank4)<<endl;
}
The following questions ask you about the values to fill in the blanks.You are expected to give simplest possible values, i.e. if your answer is an expression that can be simplified, give the simplified answer, do not put any spaces in your answer. Also do not use '&' or '*' (pointer 'dereferencing') in your answer. Using subscripts ([]) is fine.
12)What value you can fill for blank1?
a)A[0]
b)A[n]
c)5
d)0
13)What value you can fill for blank2?
Ans: A[n-1]
14)What value you can fill for blank3?
Ans: A
15)What value you can fill for blank4?
Ans: 5
WEEK-7 :PROGRAMMING ASSIGNMENT 1 AND 2
<<CLICK HERE FOR WEEK7:PROGRAMMING -01>>
<<CLICK HERE FOR WEEK 7: PROGRAMMING-02>> FOLLOW OUR WEBSITE FROM THE BUTTON PROVIDED TO THE BOTTOM OF PAGE TO GET SOON ANSWERS OF PROGRAMS.
Disclaimer: Here you can find all nptel assignment solutions related to CS stream.These may help you for your assignment.The answers are only for verification.You cannot copy these directly from the post(code of conduct of NPTEL).These is not 100% correct solutions.
No comments:
Post a Comment
If you have any problems related to solutions or any concept please let me know.