Created
April 8, 2014 19:05
-
-
Save dillmo/10172408 to your computer and use it in GitHub Desktop.
Introduction to C++ Test 2 Lab solution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Introduction to C++ Test 2 Lab solution by Dillon Morse | |
* ------------------------------------------------------- | |
* a) Write a program that will create a file that will contain | |
* the data values: | |
* 4 -9 7 | |
* 1 -1 8 | |
* 7 4 5 | |
* 9 0 -3 | |
* when your program completes. | |
* | |
* b) Now, reopen your data and find the largest value stored. | |
*/ | |
#include <fstream> | |
#include <iostream> | |
#include <cstring> | |
using namespace std; | |
void out(); | |
int in(); | |
int main() { | |
string state; | |
int output; | |
ifstream check_ran; | |
// See if the code already ran | |
check_ran.open( "ran.txt" ); | |
if( strcmp(state.c_str(), "true") ) { | |
output = in(); | |
cout << output << '\n'; | |
} else out(); | |
} | |
void out() { | |
ofstream file; | |
// Verify that the code already ran | |
file.open( "ran.txt" ); | |
file << "true\n"; | |
file.close(); | |
file.open( "out.txt" ); | |
file << "4 -9 7\n" << "1 -1 8\n" << "7 4 5\n" << "9 0 -3\n"; | |
} | |
int in() { | |
int largest = 0; | |
int num; | |
ifstream file; | |
file.open( "out.txt" ); | |
while( file >> num ) { | |
if( num > largest ) largest = num; | |
} | |
return largest; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment