Question 1

Write a program that opens a file named "data.dat" in append mode. The program runs until the user chooses to exit by entering 'e' or E at the keyboard. During each run, the program takes as input from the user

  • a class number and
  • the number of students in that class

and adds a record of this information on a new line at the end of the file. The data will be formatted on a given line in the file as follows:

ECE71 35

Notice that, the "ECE71" and 35 are separated by a tab. A new line should be added at the end of each added line.

Your program should check if the file failed to open. If the file failed to open, the program prints a message to inform the user about it, and exits.

File content after program run (data.dat):

ece1 38
ece71 34
ece85 27

Sample run: see image.

Question 2

Write a program that takes its input from a file of numbers of type double and outputs the average of the numbers in the file to the screen. The file contains nothing but numbers of type double separated by blanks and/or line breaks. Name the input file "infile.txt". Note that, you should not assume any specific number of decimal values in the input file. Your program should keep reading decimal values from the input file until it reaches the end of file. Your program needs to keep track of the total number of decimal values read, and their accumulated sum, in order to be able to calculate the average value.

Sample run: see image.

Question 3

Write a program that gives and takes advice on program writing. The program starts by writing a piece of advice to the screen and asking the user to type in a different piece of advice. The program then ends. The next person to run the program receives the advice given by the person who last ran the program. The advice is kept in a file, and the contents of the file change after each run of the program. You can use your editor to enter the initial piece of advice in the file so that the first person who runs the program receives some advice. Allow the user to type in advice of any length so that it can be any number of lines long. The user is told to end his or her advice by pressing the Return key two times. Your program can then test to see that it has reached the end of the input by checking to see when it reads two consecutive occurrences of the character '\n'. Name the input file as "advice.txt".

Sample run: see image.

Question 4

Write a program that merges the numbers in two files and writes all the numbers into a third file. Your program takes input from two different files and writes its output to a third file. Each input file contains a list of numbers of type int in sorted order from the smallest to the largest. After the program is run, the output file will contain all the numbers in the two input files in one longer list in sorted order from smallest to largest. Your program should define a function to merge the input files into the output file. The function is called with the two input-file streams and the output-file stream as three arguments (all file streams are passed by reference).

Sample run: see image.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.