Programming in Modern C++ | Week 1
Session: JULY-DEC 2023
Course Name: Programming in Modern C++
Course Link: Click Here
These are Nptel Programming in Modern C++ Assignment 1 Answers
Programming
Question 1
Consider the following program. Fill in the blanks as per the instructions given below:
• at LINE-1 with stack declaration,
• at LINE-2 to push values into stack,
• at LINE-3 with appropriate statement
such that it will satisfy the given test cases.
Solution:
stack<char> s; //LINE-1
for(int i = 0; i < strlen(str); i+=2)
s.push(str[i]); //LINE-2
int len = s.size();
for(int i = 0; i < len; i++) {
ch = s.top(); //LINE-3
These are Nptel Programming in Modern C++ Assignment 1 Answers
Question 2
Consider the following program. Fill in the blanks as per the instructions given below.
• at LINE-1 with appropriate header statement,
• at LINE-2 with appropriate statement to calculate Euclidean distance
such that it will satisfy the given test cases.
Solution:
#include <iostream>
#include <cmath>
using namespace std;
struct point{
int x, y;
};
double get_len(point p1, point p2){
return sqrt((p2.x - p1.x)*(p2.x - p1.x)+(p2.y - p1.y)*(p2.y - p1.y));
}
These are Nptel Programming in Modern C++ Assignment 1 Answers
Question 3
Consider the following program. Fill in the blanks as per the instructions given below:
• at LINE-1 with appropriate function header,
• at LINE-2 with appropriate return statement
such that it will satisfy the given test cases.
Solution:
#include <iostream>
#include <algorithm>
using namespace std;
bool max_str(string a, string b) {
return a > b;
}
These are Nptel Programming in Modern C++ Assignment 1 Answers
More Weeks of Programming in Modern C++: Click here
More Nptel Courses: Click here
Session: JULY-DEC 2022
Course Name: Programming in Modern C++
Course Link: Click Here
These are Nptel Programming in Modern C++ Assignment 1 Answers
Quiz
Q1) Consider the following program
What will be the output/error?
a) 1
b) 0
c) -1
d) Compilation Error : function is not defined
Answer: a) 1
Q2) Consider the following code segment
Fill in the blank at LINE-1 so that the program will print “not found”?
a) &data[0], &data[5], key
b) data, data+5, key
c) &data[0], &data[4], key
d) data+1, data+4, key
Answer: c, d
Q3) Consider the following code segment
What will be the output?
a) 10 20 30 40 50
b) 10 30 40 50 20
c) 50 10 30 40 20
d) 50 10 20 30 40
Answer: c) 50 10 30 40 20
These are Nptel Programming in Modern C++ Assignment 1 Answers
Q4) Consider the following code segment
What will be the output?
a) 5 10 15 20 25
b) 5 10 15 25 20
c) 20 10 15 25 5
d) 25 5 10 15 20
Answer: b) 5 10 15 25 20
Q5) Consider the following code segment
What will be the output?
a) A A A B C Z Z Z
b) A A B B C Z Z Z
c) A A A B C Z Z
d) A A A B C Z Z Z Z
Answer: a) A A A B C Z Z Z
Q6) Consider the following code segment
Choose the appropriate option to fill in the blank at LINE-1, such that the output of the code would be: C++ Program.
a) si += s2
b) strcat (s1, s2)
c) s1.append(s2)
d) si.insert (s2)
Answer: a, c
These are Nptel Programming in Modern C++ Assignment 1 Answers
Q7) Consider the following code segment
Fill in the blank at LINE-1 such that the output is
5 2 3 4 5
a) data + 4 – i
b) data + 5 – i
c) data + i – 4
d) data + i – 5
Answer: a) data + 4 – i
Q8) Consider the following code segment
What will be the output?
a) 1234987654
b) 123498765
c) 1234897654
d) 123459876
Answer: a) 1234987654
Q9) Consider the following code segment
Which statement/statements is/are correct?
a) STMT-1
b) STMT-2
c) STMT-3
d) STMT-4
Answer: b) STMT-2
These are Nptel Programming in Modern C++ Assignment 1 Answers
Assignment Questions
Questions 1
Consider the program below.
• Fill in the blank at LINE-1 to declare a stack variable s.
• Fill in the blank at LINE-2 to push value into stack.
• Fill in the blank at LINE-3 with appropriate statement.
The program must satisfy the given test cases.
Solution:
stack<char> s;//LINE-1
for(int i=0;i<strlen(str);i++)
s.push(str[i]); //LINE-2
for(int i=0;i<strlen(str)-1;i++){
ch = s.top(); //LINE-3
These are Nptel Programming in Modern C++ Assignment 1 Answers
Question 2
Consider the following program.
• Fill in the blank at LINE-1 with the appropriate if statement,
• Fill in the blank at LINE-2 and LINE-3 with the appropriate return statements
The program must satisfy the sample input and output.
Solution:
#include<iostream>
using namespace std;
bool Compare(string s1, string s2){
if(s1<s2) //LINE-1
return 0; //LINE-2
else
return 1; //LINE-3
These are Nptel Programming in Modern C++ Assignment 1 Answers
Question 3
Consider the program below.
• Fill in the blank at LINE-1 to include the appropriate header file to utilize abs(·) function.
• Fill in the blank at LINE-2 to compute the length between two points p1 and p2 as
|(p1.y − p2.y)| + |(p1.x − p2.x)|.
The program must satisfy the given test cases.
Solution:
#include <iostream>
#include <cmath> //LINE-1
using namespace std;
struct point{
int x, y;
};
double get_len(point p1, point p2){
return abs(p1.y - p2.y) + abs(p1.x - p2.x); //LINE-2
}
These are Nptel Programming in Modern C++ Assignment 1 Answers
More Solutions of Programming in Modern C++: Click Here
More NPTEL Solutions: https://progiez.com/answers/nptel/

This content is uploaded for study, general information, and reference purpose only.