Question: #include #include #include #include using namespace std; int main() { const string FILE_NAME = numbers.txt; //Declaring variables fstream inFile; //isSorted will check if numbers are

#include #include #include #include using namespace std; int main() { const string FILE_NAME = "numbers.txt"; //Declaring variables fstream inFile; //isSorted will check if numbers are sorted or not bool isSorted = true; int count = 0, sum = 0; int numFile; //Opening file cout << "Opening file \"" << FILE_NAME << "\"..." << endl; inFile.open(FILE_NAME); if (!inFile.is_open()) { cout << "Could not open file \"" << FILE_NAME << "\"..." << endl; system("pause"); return EXIT_FAILURE; } //Displaying as per given requierment cout << "*** " << "Number Cruncher" << " ***" << endl; //First number is added to numFile inFile >> numFile; //prev will be used to check the numbers sorted or not //Initially we'll put First number in it int prev = numFile; //Loop will run Until it wont encounter -999 while (numFile != -999) { //Adding the numbers sum = sum + numFile; //Counting numbers count++; //Displaying numbers cout << numFile << setw(3); //AS we need to display 5 numbers in a row count will give us number we have displayed when //IF count%5==0 means it has reached it 5 or 10 or 15 so we need to go to next line if (count % 5 == 0) cout << endl; //IF prevoius number is greater than next number means numbers are not sorted is so we'll assign false to isSorted if (prev > numFile) isSorted = false; //when next iteration will start prev will have prevoius number once if condition is checked number is numFile will be added to prev //numFile will hold next value prev = numFile; inFile >> numFile; } //Displaying count and sum cout << " count:" << count << endl; cout << "Sum: " << sum << endl; //If isSorted is true then we will display true if (isSorted == true) { cout << "SORTED" << endl; } //Else we'll display Not Sorted else { cout << "NOT SORTED" << endl; } cout << "Closing file \"" << FILE_NAME << "\" ..." << endl << endl; //Closing File inFile.close(); system("pause"); return EXIT_SUCCESS; }

Write a complete C++ program in a project called 07_2_IsSorted that reads a number of integers from a text file named numbers.txt. The sentinel value is -999. The following data file has 12 values and a sentinel value of -999. 2 3 5 7 8 10 12 13 15 1 11 15 -999 Your program must print out to the screen each integer value (five values per line right justified in a field of 3). After outputting all values, output the number of integers read (excluding the sentinel value), the sum of all numbers, and SORTED if the values are in order from smallest to largest; otherwise, output NOT SORTED. HINT: Write and test the code to produce the Count, then the Sum, then the SORTED portion of the project.

*** Number Cruncher ***

2 3 5 7 8

10 12 13 15 1

11 15

Count: 12

Sum: 102

NOT SORTED

Testing: Test your input on these input files.

Test1.txt

1 2 3 4 5 6 7 8 -999

Test2.txt

2 3 5 7 8 10 12 13 15 1 11 15 -999

Test3.txt

-999

Test4.txt

1 -999

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!