Programming in Modern C++ Week 9 Assignment Answers
Are you looking for the NPTEL Programming in Modern C++ Week 9 Assignment Answers? You’ve come to the right place! This resource provides comprehensive solutions to all the questions from the Week 8 assignment, helping you navigate through the complexities of modern C++ programming.
Table of Contents
Programming in Modern C++ Week 9 Assignment Answers (Jan-Apr 2025)
Course Link: Click Here
1) Consider the following program.
#include<cstdio>
using namespace std;
int main(){
int a = 0x067, b = 067, c = 67;
char d = 67; //LINE-1
printf("’d %d %d %c", a, b, c, d);
return 0;
}
What will be the output /error?
a) 67 67 67 67
b) 67 67 67 C
c) 103 55 67 C
d) Compiler error at LINE-1: type cast from int -> char is invalid
2) Match the appropriate descriptions about the fseek function calls.
Here, fp is a file pointer.
| Function call | Description |
|---|---|
| 1. fseek(fp, 10, SEEK_SET) | A. Move the file pointer to the end of the file |
| 2. fseek(fp, -10, SEEK_CUR) | B. Move the file pointer forward from the beginning of the file by 10 positions |
| 3. fseek(fp, 0, SEEK_END) | C. Move the file pointer backwards from the current position in the file by 10 positions |
| 4. fseek(fp, -10, SEEK_END) | D. Move the file pointer backwards from the end of the file by 10 positions |
a) 1-A, 2-D, 3-C, 4-B
b) 1-A, 2-C, 3-D, 4-B
c) 1-B, 2-A, 3-B, 4-D
d) 1-B, 2-C, 3-A, 4-D
3) Consider the following code segment.
#include<cstdio>
using namespace std;
int main(){
FILE *infp, *outfp;
if((infp = fopen("myfile.txt", "r")) == NULL)
return -1;
if((outfp = fopen("procfile.txt", "w")) == NULL)
return 2;
int c;
while((c = fgetc(infp)) != EOF)
if(c == ' ' || c == '\n'); //LINE-1
else
fputc(c, outfp);
fclose(infp);
fclose(outfp);
return 0;
}
Choose the correct option regarding the program.
a) It makes the exact copy of the contents from the file myfile.txt to the file procfile.txt.
b) It makes the exact copy of the contents from the file procfile.txt to the file myfile.txt.
c) It makes the exact copy of the contents from the file myfile.txt to the file procfile.txt without spaces and newlines.
d) It generates a compiler error at LINE-1 since the ; is placed at the wrong position.
Programming in Modern C++ Week 9 Assignment Answers
4) Consider the following code segment.
#include <iostream>
#include <iomanip>
int main() {
std::cout.setf(std::ios::showpoint);
std::cout << std::setfill('0') << std::setw(10) << 11.0;
return 0;
}
What will be the output?
a) 00000011.0
b) 0000000011
c) 00011.0000
d) 0000000011.00
5) Consider the file myfile.txt has a single line as follows:
“pointer to the array where the read objects are stored”
Consider the following code segment:
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ifstream myfile("myfile.txt");
char c;
int i = 0;
if (myfile.is_open()) {
while (!myfile.eof()) {
______ //LINE-1
if (c == ' ')
i++;
}
myfile.close();
cout << i;
}
else
cout << "Unable to open file";
}
Identify the appropriate option to fill in the blank at LINE-1 such that the program prints the number of words in the file myfile.txt if the file exists. Otherwise, prints "Unable to open file". Here, each word is separated by a single space.
a) getline(myfile, c)
b) c = myfile.get()
c) myfile >> c
d) myfile.get(c)
Programming in Modern C++ Week 9 Assignment Answers
6) Consider the following code segment.
#include<iostream>
using namespace std;
template<class Itr, class T>
void MinMax(Itr first, Itr last, T& max, T& min) { // LINE-1
max = *first;
min = *first;
while (first != last) {
if (*first > max)
max = *first;
else if (*first < min)
min = *first;
++first;
}
}
int main() {
int min = 0, max = 0;
int iArr[] = {5, 6, 7, 1, 2, 9, 3, 4};
MinMax(iArr, iArr + sizeof(iArr) / sizeof(*iArr), max, min);
cout << min << ", " << max;
return 0;
}
Fill in the blank at LINE-1 such that the program will print 1, 9.
a) Itr first, Itr last, T max, T min
b) Itr first, Itr last, T& max, T& min
c) T first, T last, Itr& max, Itr& min
d) T first, T last, T& max, T& min
Programming in Modern C++ Week 9 Assignment Answers
7) Consider the code segment below.
#include <iostream>
#include <list>
#include <numeric>
#include <functional>
using namespace std;
double compute(list<int> li) {
double result = accumulate(li.begin(), li.end(), 1, multiplies<int>()); //LINE-1
return result / 2;
}
int main() {
int arr[] = {10, 20, 30, 40};
list<int> li(arr, arr + sizeof(arr) / sizeof(*arr));
cout << compute(li) << endl;
return 0;
}
Identify the appropriate option such that it multiplies the elements of list li and then divides it by 2.
a) li.begin(), li.end(), 1, multiplies<double>()
b) li.begin(), li.end(), 0.5, multiplies<double>()
c) li.begin(), li.end(), 0.5, multiplies<int>()
d) li.begin(), li.end(), 0, multiplies<int>()
Programming in Modern C++ Week 9 Assignment Answers
Programming in Modern C++ Week 9 Assignment Answers
For answers to others Nptel courses, please refer to this link: NPTEL Assignment