diff options
| author | Tyler Taormina <[email protected]> | 2021-11-23 22:53:45 -0800 |
|---|---|---|
| committer | Tyler Taormina <[email protected]> | 2021-11-23 22:53:45 -0800 |
| commit | bc522c36755525fe7189f5314beef859c3c93350 (patch) | |
| tree | 2b217c3c45f1153e0d358bcaa6cba0a5e8142f65 | |
| parent | Debug exercise complete. (diff) | |
| download | cst115-lab8-till-t-bc522c36755525fe7189f5314beef859c3c93350.tar.xz cst115-lab8-till-t-bc522c36755525fe7189f5314beef859c3c93350.zip | |
Update
| -rw-r--r-- | 11.14_exercise/num3.cpp (renamed from num3.cpp) | 0 | ||||
| -rw-r--r-- | debug_exercise/debug_report.txt | 11 | ||||
| -rw-r--r-- | lab8.txt | 216 |
3 files changed, 222 insertions, 5 deletions
diff --git a/num3.cpp b/11.14_exercise/num3.cpp index 139597f..139597f 100644 --- a/num3.cpp +++ b/11.14_exercise/num3.cpp diff --git a/debug_exercise/debug_report.txt b/debug_exercise/debug_report.txt index e69de29..ba57069 100644 --- a/debug_exercise/debug_report.txt +++ b/debug_exercise/debug_report.txt @@ -0,0 +1,11 @@ + Here is the Output File +tyler 27 +taury 26 +ollie 1 +ryan 25 +elayna 15 +mom 40 + + + ** Total Records: 6 ** + The End @@ -17,6 +17,7 @@ p 317 Submit: Statements for 5, Judgements for 6 5. a.) .is_open() + b.) int num_data = 0; @@ -36,10 +37,14 @@ Submit: Statements for 5, Judgements for 6 c.) for the above ^^ - input.close() - - + input.close(); +6. + a. false + b. true + c. false + d. true + e. true ================================================================================================ 13b @@ -54,8 +59,103 @@ Submit: code & run CODE: +// Tyler Taormina +// CST 116 +// Nov 2021 +// Lab 8 Exercises + + +#include <iostream> +using namespace std; + +void findMedian(float arr[], int); +float GetValues (float values[]); +#define MAX 20 + + +int main () +{ + int num_values; + float values [MAX] = {0}; + num_values = GetValues(values); + + cout << "Here is our list..." << endl; + for (int i = 0; i < num_values; i++) + cout << values[i] << " "; + cout << endl; + + cout << "===================================================" << endl; + + findMedian(values, num_values); + return 0; +} + + +float GetValues (float values[]) +{ + int num_values = 0; + char cont = 'n'; + + do + { + cout << "Enter a number: "; + cin >> values[num_values++]; + + cout << "Enter another values (y/n)? "; + cin >> cont; + } while (toupper (cont) == 'Y' && + num_values < MAX); + return num_values; +} + + +void findMedian(float arr[], int num_val) +{ + float x; + + if ((num_val % 2) == 0) + { + x = (arr[num_val/2] + arr[(num_val/2) - 1])/2; + + cout << "Even number Array. We will average the two middle most numbers to determine the median." << endl; + cout << "The median is: " << x << endl; + } + else + { + cout << "Odd number Array. We will find the number in the middle of the list which determines the median." << endl; + cout << "The median is: " << arr[num_val/2] << endl; + } + +} + RUN: +Enter a number: 1 +Enter another values (y/n)? y +Enter a number: 2 +Enter another values (y/n)? y +Enter a number: 3 +Enter another values (y/n)? y +Enter a number: 4 +Enter another values (y/n)? n +Here is our list... +1 2 3 4 +=================================================== +Even number Array. We will average the two middle most numbers to determine the median. +The median is: 2.5 +till-t@Air:~/code/cst115-lab8-till-t|master⚡ ⇒ ./a.out +Enter a number: 1 +Enter another values (y/n)? y +Enter a number: 2 +Enter another values (y/n)? y +Enter a number: 3 +Enter another values (y/n)? n +Here is our list... +1 2 3 +=================================================== +Odd number Array. We will find the number in the middle of the list which determines the median. +The median is: 2 + ================================================================================================ 13c @@ -68,8 +168,115 @@ pp 333-336 Submit: code & runs CODE: +#include <iostream> +#include <fstream> // For the files!!!! +#include <iomanip> // For manipulators & formatting options +using std::cin; +using std::cout; +using std::endl; +using std::setw; +using std::ios; +using std::resetiosflags; +using std::setiosflags; + +using std::ifstream; +using std::ofstream; + +const int EMPLOYEES = 20; +const int MAX = 21; + +int ReadData ( ifstream &inFile, ofstream &outFile, char name[][MAX], int age[] ); +void WriteOutputFile ( ofstream &outFile, char name[][MAX], int age[], + int counter ); +void PrintTotalsAndSummary ( ofstream &out, int totalRecords ); + +int main ( ) +{ + char name[EMPLOYEES][MAX]; + int age[EMPLOYEES]; + int record_counter ( 0 ); + + ifstream inFile; + + // Notice how this automatically opens the file + ofstream outFile ( "debug_report.txt" ); + + inFile.open ( "debug.txt"); + + if ( inFile.is_open ( ) ) + { + record_counter = ReadData ( inFile, outFile, name, age ); + inFile.close ( ); + + if ( outFile.is_open ( ) ) + { + WriteOutputFile ( outFile, name, age, record_counter ); + PrintTotalsAndSummary ( outFile, record_counter ); + outFile.close ( ); + } + else + { + cout << "Trouble Opening File: outFile"; + cout << "\n\n\t\t ** About to EXIT NOW! ** "; + } + } + else + { + cout << "Trouble Opening File: inFile"; + cout << "\n\n\t\t ** About to EXIT NOW! ** "; + } + return 0; +} +int ReadData ( ifstream & inFile, ofstream & outFile, char name[][MAX], int age[] ) +{ + int counter = 0; + inFile >> name[counter] >> age[counter]; // Priming Read -RUN: + while ( !inFile.eof ( ) ) + { + cout << setiosflags ( ios::left ) << setw ( 25 ) + << name[counter] << resetiosflags ( ios::left ) + << setw ( 4 ) << age[counter] << endl; + counter++; + inFile >> name[counter] >> age[counter]; + } + + return counter; +} +void WriteOutputFile ( ofstream &outFile, char name[][MAX], int age[], int counter ) +{ + outFile << " Here is the Output File" << endl; + for ( int r = 0; r < counter; r++ ) + { + outFile << setiosflags ( ios::left ) << setw ( 25 ) + << name[r] << setw ( 4 ) + << resetiosflags ( ios::left ) << age[r] + << endl; + } +} +void PrintTotalsAndSummary ( ofstream &outFile, int totalRecords ) +{ + // To screen + cout << "\n\n\t** Total Records: " << totalRecords << " **\n" + << "\t\t The End \n"; + + // To file + outFile << "\n\n\t** Total Records: " << totalRecords << " **\n" + << "\t\t The End \n"; +} + +RUN: ***output file is included in debug exercise folder in github*** + +tyler 27 +taury 26 +ollie 1 +ryan 25 +elayna 15 +mom 40 + + + ** Total Records: 6 ** + The End ================================================================================================ @@ -81,7 +288,6 @@ pp. 336-337 Submit: code & run -34 pts CODE: |