Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a C++ program named midterm.cpp. When you're done, upload the midterm.cpp file and press the Submit Quiz button to complete this part of the

Write a C++ program named midterm.cpp. When you're done, upload the midterm.cpp file and press the Submit Quiz button to complete this part of the exam.

Write a program to retrieve and process data stored in a text file. The first line of the file contains the number of data items stored in the remaining part of the file. The data items are four-digit integers separated by the newline character.

The program must be organized in the following order.

1. Write the program description

2. Write #include directives

3. Write using namespace statement

4. Write function prototypes

5. Write the main function

6. Write function definitions

In the main function, you must write code in the following order:

1. Define a const string variable for the file name and initialize it with "random.txt". Use the const whenever the file name is used in your code.

2. Define an ifstream object for reading from the file.

3. If the file cannot be opened, print the message that says,

"File random.txt cannot be opened."

This checking code must be there.

4. If the file is opened, get the count of data items by reading the number from the first line of the file.

5. If the count has been obtained, dynamically allocate an array using the count obtained in step #4 as the size and using a C++ 11 unique_ptr pointer.

6. Read the integers from the file into the array.

7. If the reads are successful, sort the array in ascending order. You must call the selectionSort function. The source of the function is provided here. SelectionSort.cppimage text in transcribed Copy that into the function definitions section in your code. The sort function only accepts the address of an array and the size of that array. You can obtain the address of the array owned by the unique_ptr by calling its member function get().

8. Report. If the numbers have been read successfully, print the count of the numbers read from the file and up to 20 numbers in the sorted list starting from the smallest. If a number occurs more than once, print the occurrence count on the side. Otherwise, print an error message that says, "The file does not contain valid data."

9. If the file has been opened, close the file.

Download

The file random.txt must be downloaded from random.txt image text in transcribed. Copy that file to the location where it can be read from your program. When testing using Visual Studio, copy the file to the same location as the source file of your program. Note that the file was created on Mac and the newlines may appear to be missing when you read it using a text editor. For Xcode users, you need to build your code first and use Show in Finder menu action to find the location of the product and copy the input file there. Selection sort code can be downloaded from SelectionSort.cppimage text in transcribed and copied into the function definitions section in your code.

Grading Criteria

The grading criteria are the same as the labs. For one thing, make sure the lines are not too wide. If a statement is too long, split it into multiple lines and use proper indentations. You should try to limit the line width to about 80 characters for the ease of reading. Among other things, the grading will look at specific aspects such as the structure (every major step is clearly seen), documentation/comments, indentations, identifier naming of variables and constants, usage of named constants, prohibited usage of break/continue statements, prohibited return from the middle of a function, etc.

-----

Output Format

Except for nnnn which is to be replaced in with a count discovered by your code, the output should look as follows .

There are nnnn numbers in the file. First 20 numbers after sort: 1000 1002 (2) 1004 1007 1012 1016 1018 1019 1027 1032 1033 1035 1043 1049 1058 1063 1065 1068 (

Transcribed image text

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Describe how and why clinicians classify psychological disorders.

Answered: 1 week ago