[go: up one dir, main page]

0% found this document useful (0 votes)
54 views6 pages

Files: For Multiple-Choice and Essay Questions

The document provides instructions for submitting assignments for an Introduction to Programming course. Students must submit multiple choice and essay questions in a PDF file named with their student ID. Programming questions require submitting source code files named with the student ID and including any necessary explanations in the PDF file. The document also provides several multiple choice, essay, and programming questions for students to complete as part of their assignment.

Uploaded by

Phi Bảo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views6 pages

Files: For Multiple-Choice and Essay Questions

The document provides instructions for submitting assignments for an Introduction to Programming course. Students must submit multiple choice and essay questions in a PDF file named with their student ID. Programming questions require submitting source code files named with the student ID and including any necessary explanations in the PDF file. The document also provides several multiple choice, essay, and programming questions for students to complete as part of their assignment.

Uploaded by

Phi Bảo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Class 21CLC6 – Term I/2021-2022

Course: CSC10001 – Introduction to Programming

Files

Prepare your submission


• For multiple-choice and essay questions: Present your answers to a PDF file and name the
file as <StudentID>.pdf, where StudentID is your student number.
The following guides you how to treat different types of questions
o Single-choice question: Each choice is indicated by the circle  You pick the most
correct choice and explain for every other choice (i.e., why you did not choose it).
o Multiple-choice question: Each choice is indicated by the square  You pick one or
many correct choices and explain for every other choice.
o Essay question: You give a comprehensive answer covering all aspects of the problem.
• For C/C++ programming questions: Present your C/C++ source codes. Do not use any
additional libraries/open sources without permission
o For each question, create a solution and name the solution as <StudentID>, where
StudentID. For example, 21120001, 21120001, etc. There is only one main function.
The code for each question in Part B is organized in a separate function, and these
functions must be called from the main function with appropriate arguments.
o Do not include the intermediate files generated by the Visual Studio compiler. Do not
include the execution files since they are easily affected by viruses.
o Some programming question requires a brief explanation. Present that explanation to
the above report file.
• Solutions will be provided after the submission deadlines.
• Students contact TA through Moodle forum or email (Ms. Nguyễn Ngọc Thảo: nnthao@fit...).

1
A. Multiple-choice questions and essay questions (10pts)

A.1. Which of the following statements about fscanf and scanf is TRUE?
 fscanf read from standard input while scanf specifies a stream from which to read
 fscanf specifies a stream from which to read while scanf read only from standard input
 fscanf and scanf has no difference in their functions
 fscanf and scanf can read from specified stream
Selina
A.2. Consider the text file, “abc.txt”, that contains a list of names, as shown
Hebe
aside. Each name follows immediately with a new line character. Ella
What is the output of the following code segment? Justify your answer. Jack
Assume that all required headers are included. Rose

char str1[20] = "Hello, boys";


char str2[100] = "God created the world in 7 days.";
FILE* fp = fopen("abc.txt", "r");
if (fp == NULL) {
cout << "Cant open!" << endl;
exit(1);
}
fgets(str1, 12, fp);
fgets(&str2[12], 10, fp);
cout << str2;
fclose(fp);
 God created Ella  God createdElla  God created Hebe
 God createdHebe  God created Hebeworld in 7 days.

A.3. Consider the text file, “abc.txt”, whose content is as follows.


True peace is not merely the absence of tension; it is the presence of justice.
Determine the output when the following code segment is executed. Assume that all
required headers are included.
int i;
char c;
FILE* fp = fopen("abc.txt", "r");
for (i = 0; i < 15; i++){
c = fgetc(fp);
if (i > 7)
putchar(c);
}
fclose(fp);
 Ace is n  Ce is no  Ace is  Ce is n  Ce is

A.4. What is the output of the following code segment? Justify your answer. Assume that all
required headers are included.

2
FILE* fp = fopen("abc.txt", "wt");
fputs(fp, "This is an apple");
long pos = ftell(fp);
fseek(fp, 9, SEEK_SET);
fputs(fp, " sam");
fclose(fp);
A.5. What is the output of the following code segment? Justify your answer. Assume that all
required headers are included.
/* Create a file containing two integers */
FILE *out_fp = fopen("abc.txt", "wt");
fputs("123 456", out_fp);
fclose(out_fp);
/* Attempt to read a character, then an integer, then a character
again, then an integer again, from the file just created. */
char character;
int integer;
FILE* in_fp = fopen("abc.txt", "rt");
fscanf(in_fp, "%c %d ", &character, &integer);
cout << "character: '" << character << "'\n";
cout << "integer: " << integer << "\n";
fscanf(in_fp, "%c %d ", &character, &integer);
cout << "character: '" << character << "'\n";
cout << "integer: " << integer << "\n";
fclose(in_fp);

A.6. Consider the text file, “abc.txt”, whose content is as follows.


If I saw an Aardvark, I would scream!
Determine the output when the following code segment is executed. Assume that all
required headers are included.
FILE* fp = fopen("abc.txt", "rt");
char c;
int i = 0;
while ((c = fgetc(fp))!= EOF) {
if (tolower(c) == 'a')
i++;
}
cout << i;
fclose(fp);

A.7. Why should a program close a file when it has finished using it?

A.8. What are the differences between the two classes, FILE and fstream?

A.9. What are the C functions used to read or write a file in in Binary Mode? Give an example to
demonstrate those functions.

A.10. Differentiate the following pairs of functions, puts vs. fputs and gets vs. fgets.

3
B. Programming questions (10pts)

The input values are guaranteed to be valid. Note that you need to create a solution with a single
main function. The code for each question is organized in a separate function, and these functions
must be called from the main function with appropriate arguments.

B.1. Write a function that does the following steps


1 Create a new text file and write a sequence of integers from 1 to 100 to the file. Each
number is on a separate line.
2 Read the newly created file and display prime numbers only

Expected output:

The text file Display to screen

1 2 3 5 7 11 13 17 19 23 29 31 37 41
43 47 53 59 61 67 71 73 79 83 89 97
2

100

B.2. Write a function that does the following steps


1 Create a new text file and write a sequence of 10 integers of arbitrary values. Two
adjacent numbers are separated by a single space.
2 Read the newly created file and sum the numbers at prime indices (start from 0) only.
3 Display the sum.

Expected output:

The text file Display to screen

10 9 8 7 6 5 4 3 2 1 23

B.3. The file grades.txt includes lines of text, each of which contains the student’s ID, name (a
single word) and score, as demonstrated below.
1 student1 3.5
2 student2 0.6
3 student3 4.0
4 student4 2.2
. . .
Write a function to input a student ID, search in given file the student with the specified
student ID, and then print out his/her name and GPA.
4
B.4. Consider two text files, one of them contains a sequence of 10 odd positive integers, and the
other file contains a sequence of 10 even positive integers. In both files, two adjacent
numbers are separated by a single space. Write a function to merge the numbers in these
two files such that they come in an alternative fashion and then write the resulting content
to a new text file

Test data:

FileA FileB

9 5 7 3 1 11 15 13 17 19 8 4 6 12 2 10 14 18 16 20

Expected output (in a text file):

9 8 5 4 7 6 3 12 1 2 11 10 15 14 13 18 17 16 19 20

B.5. This question is the reverse version of the above question. Consider a text file that contains a
sequence of 20 positive integers such that the odd and even numbers come in an alternate
fashion. Write a function to split the numbers to two text files such that the first file contains
odd numbers only and the second file contains even numbers only. The numbers must
appear in the same order as they do in the original file.

Test data:

9 8 5 4 7 6 3 12 1 2 11 10 15 14 13 18 17 16 19 20

Expected output (in a text file):

FileA FileB

9 5 7 3 1 11 15 13 17 19 8 4 6 12 2 10 14 18 16 20

B.6. Assume that the file abc.txt includes several lines of text. Write a function to receive a non-
negative integer, which is smaller than the number of lines, insert your name to the line
whose index (starts from 0) is the specified integer, and write the resulting content to a new
text file.

Test data:
Input a non-negative integer: 2

abc.txt
Dear March - Come in -
How glad I am -
I hoped for you before

Expected output (in a text file):


5
Output text file
Dear March - Come in -
How glad I am –
Nguyen Van A
I hoped for you before

B.7. Consider a text file that contains a single string, “happy new year”. Write a function that
uses ftell and fseek to modify the string such that the new content is “happy
birthday”. Note that you are not allowed to overwrite the whole string.

B.8. Consider the following two text files, FileA and FileB.
i never saw a purple cow the forest is the town of trees
i never hope to see one where they live quite at their ease
but i can tell you, anyhow with their neighbors at their side
i would rather see than be one just as we in cities wide

Write a function to merge these two files into a single file such that their lines come in an
alternative fashion, that is, the first line of FileA goes first and it is followed by the first line
of FileB, and then the second line of FileA and FileB, respectively, and so on.

B.9. Consider a text file that contains letters only. Write a function to flip the letter case of every
letter read from the given file and write the resulting content to a new text file.

Test data (in a text file):


i lOVe yOU
Expected output (in a text file):
I LovE You

B.10. Consider a text file that contains letters only. Write a function to encrypt the given file,
using your own algorithm, and store the encrypted content to the file MyEncryption.enc.
After that, decrypt the previously encrypted file and store the decrypted content to the file
MyDecryption.dec.

Expected output:

Original text file MyEncryption.enc MyDecryption.dec

hello world HELLO WORLD hello world

Encryption algorithm: character – 32  Decryption algorithm: character + 32

You might also like