Skip to content

Instantly share code, notes, and snippets.

@dillmo
Created April 25, 2014 18:55
Show Gist options
  • Save dillmo/11299507 to your computer and use it in GitHub Desktop.
Save dillmo/11299507 to your computer and use it in GitHub Desktop.
Introduction to C++ Homework Problem Set 5
/* Introduction to C++ Homework Problem Set 5 Project by Dillon Morse
* ------------------------------------------------------------------
* Create a sequential file that contains 34 rows and 34 columns. Fill the
* array with random numbers between -20 and 80. Your main code will ask the
* user to enter a number N between -20 and 80. Yoiu will open the file and
* search the two dimension array you read in for the number N. If found,
* display the number of times it was found. Next, pass your two dimension
* array to a functoin called diagonal. The function will ask the user if they
* want the sum of the main diagonal values to a function called diagonal. The
* function will ask the user if they want the sum of the main diagonal values
* or the sum of the off diagonal values.
*/
#include <iostream>
#include <fstream>
#include <ctime>
#include <cstdlib>
using namespace std;
void create_file();
int get_input( int& );
int search_file( int, int** );
int** derive_array();
int diagonal( int**, int );
int main(int argc, char const* argv[])
{
int response_1;
int pass = 1;
srand( time(0) );
create_file();
cout << search_file( get_input( pass ), derive_array() ) << endl;
cout << diagonal( derive_array(), get_input( pass ) ) << endl;
return 0;
}
void create_file() {
ofstream file_out ( "file.txt" );
if ( file_out.is_open() ) {
for ( int j = 0; j < 34; j++ ) {
for ( int k = 0; k < 34; k++ ){
file_out << rand() % 101 - 20 << endl;
}
}
}
}
int get_input( int& pass ) {
int input;
enum diagonal {
main,
off
};
switch ( pass ) {
case 1:
cout << "Enter a number between -20 and 80" << endl;
break;
case 2:
cout <<
"Would you like to sum the main diagonal or the off diagonal?"
<< endl
<< "Enter 'main' or 'off'"
<< endl;
break;
}
cin >> input;
pass++;
return input;
}
int** derive_array() {
ifstream file_in ( "file.txt" );
string line;
int pos_1 = 0;
int pos_2 = 0;
// Allocate memory for a 2D array arr
int** arr = new int* [34];
for ( int i = 0; i < 34; i++ ) {
arr[i] = new int[34];
}
if ( file_in.is_open() ) {
while ( getline ( file_in, line ) ) {
if ( pos_2 == 34 ) {
pos_1++;
pos_2 = 0;
}
arr[pos_1][pos_2] = atoi( line.c_str() );
pos_2++;
}
}
return arr;
}
int search_file ( int input, int** arr ) {
int times_found = 0;
for ( int j = 0; j < 34; j++ ) {
for ( int k = 0; k < 34; k++ ) {
if ( arr[j][k] == input ) times_found++;
}
}
// Unallocate memory for arr
for ( int i = 0; i < 34; i++ ) {
delete[] arr[i];
}
delete[] arr;
return times_found;
}
int diagonal ( int** arr, int input ) {
int sum = 0;
enum diagonal {
main,
off
};
switch (input) {
case main:
for ( int i = 0; i < 34; i++ ) {
sum += arr[i][i];
}
break;
case off:
for ( int i = 34; i < 0; i-- ) {
sum += arr[i][i];
}
break;
}
// Unallocate memory for arr
for ( int i = 0; i < 34; i++ ) {
delete[] arr[i];
}
delete[] arr;
return sum;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment