Code:
//---------------------------------------------------------------------------------------
// Preprocessor directives...
#include<iostream>
#include<fstream>
#include<cstring>
#include<cstdlib>
using namespace std;
//---------------------------------------------------------------------------------------
// Global constants...
#define INPUT_FILE "input.txt"
#define OUTPUT_FILE "output.txt"
const int MAX = 66;
//---------------------------------------------------------------------------------------
// Functions declarations...
void read_file(ifstream&, ofstream&, int&, int&);
void calculate(ofstream&, int[], int, int[], int);
void total_length(ofstream&, int&, int&);
void compare_initial(ofstream&, int, int);
void display(ofstream&, int, int, int, int, int, int, int, int, int, int, int, int, int, int);
//---------------------------------------------------------------------------------------
// driver function...
int main()
{
ifstream fin(INPUT_FILE); // object for file input
ofstream fout(OUTPUT_FILE); // object for file output
int first_length = 0; // total length of the North/South road
int second_length = 0; // total length of the West/East road
if(fin.fail())
{
cout << "Can't open the file for input! Exiting! \n";
exit(0);
}
else
{
read_file(fin, fout, first_length, second_length);
// reads values from a text file...
}
fin.close();
fout.close();
return 0;
}
//---------------------------------------------------------------------------------------
// read_file(): reads in values from a file into an array of integers
//
// Parameters: ifstream object and ofstream object by reference
// Return Value: void
void read_file(ifstream& fin, ofstream& fout, int& first_length, int second_length)
{
char first_road[MAX];
char second_road[MAX];
int current_first = 0; // stands for initial level point of N/S road
int current_second = 0; // stands for initial level point of W/E road
int first_level[MAX]; // an array of 66 integer values...
int second_level[MAX]; //
int first_max = 1; // stand for indexes of arrays of integers...
int second_max = 1;
fin.getline(first_road, MAX); // reads in characters till the 'eol' symbol...
for(int i = 0; ((first_level[i] != -999) || (i < MAX)); i++)
{
fin >> first_level[i];
if(first_level[i] != -999)
{
current_first = (current_first + first_level[i]);
first_max++; // implementing counter for index of an array
first_length = (first_length + 100); // adds 100 feet to total length of
} // the first road...
}
fin.getline(second_road, MAX); // reads in characters till 'eol' symbol...
for(int j = 0; ((second_level[j] != -999) || (j < MAX)); j++)
{
fin >> second_level[j];
if(second_level[j] != -999)
{
current_second = (current_second + second_level[j]);
second_max++; // incrementing counter for index of an array
second_length = (second_length + 100); // adds 100 feet to total length of
} // of the second road...
}
total_length(fout, first_length, second_length);
calculate(fout, first_level, first_max, second_level, second_max);
compare_initial(fout, first_level[0], second_level[0]);
}
//---------------------------------------------------------------------------------------
// calculate(): will do various computations on the arrays such as: the largest and
// the smallest value; total number of highs and lows versus the initial
// elevation of the roads
//
// Parameters: ofstream object by reference, array of integers, integer, array of
// integers, and an integer
// Return Value: void
void calculate(ofstream& fout, int first_level[], int first_max, int second_level[],
int second_max)
{
int marker_first_highest, marker_first_lowest;
int marker_second_highest, marker_second_lowest;
int first_starter = first_level[0];
int second_starter = second_level[0];
int first_highest = first_level[0],
second_highest = second_level[0],
first_lowest = first_level[0],
second_lowest = second_level[0],
total_first_higher = 0,
total_second_higher = 0,
total_first_lower = 0,
total_second_lower = 0,
total_first_equal = 0,
total_second_equal = 0;
for (int i = 0; i < first_max; i++) // traversing the first array...
{
if(first_level[i] > first_highest)
{
first_highest = first_level[i];
marker_first_highest = i;
}
if(first_level[i] < first_lowest)
{
first_lowest = first_level[i];
marker_first_lowest = i;
}
if(first_level[i] > first_starter)
{
total_first_higher++; // incrementing a total of numbers larger than
// the starting level point...
}
else if(first_level[i] < first_starter)
{
total_first_lower++;
}
else if(first_level[i] == first_starter)
{
total_first_equal++;
}
}
for (int j = 0; j < second_max; j++) // traversing the second array...
{
if(second_level[j] > second_highest)
{
second_highest = second_level[j];
marker_second_highest = j;
}
if(second_level[j] < second_lowest)
{
second_lowest = second_level[j];
marker_second_lowest = j;
}
if(second_level[j] > second_starter)
{
total_second_higher++; // incrementing a total of numbers larger than
// the starting level point...
}
else if(second_level[j] < second_starter)
{
total_second_lower++;
}
else if(second_level[j] == second_starter)
{
total_second_equal++;
}
}
display(fout,
marker_first_highest, // calling a function that will display
marker_first_lowest, // all the necessary data required by
marker_second_highest, // by an engineer...
marker_second_lowest,
first_highest,
second_highest,
first_lowest,
second_lowest,
total_first_higher,
total_second_higher,
total_first_lower,
total_second_lower,
total_first_equal,
total_second_equal);
}
//---------------------------------------------------------------------------------------
// display(): write the results from the calculate() function into a file
//
// Parameters: ofstream object, 14 integers, avoid logical errors
// Return Value: void
void display(ofstream& fout,
int marker_first_highest,
int marker_first_lowest,
int marker_second_highest,
int marker_second_lowest,
int first_highest,
int second_highest,
int first_lowest,
int second_lowest,
int total_first_higher,
int total_second_higher,
int total_first_lower,
int total_second_lower,
int total_first_equal,
int total_second_equal)
{
fout << "The highest point of the road from North to South is: " << first_highest << endl
<< "That point's index marker is: "<< marker_first_highest << endl
<< "The lowest point of the road from North to South is: " << first_lowest << endl
<< "That point's index marker is: " << marker_first_lowest << "\n\n";
fout << "Total number of levels higher than the initial level of N/S road is: "
<< total_first_higher << endl
<< "Total number of levels lower than the initial level of N/S road is: "
<< total_first_lower << endl
<< "Total number of levels equal to the initial level of N/S road is: "
<< total_first_equal << "\n\n";
fout << "The highest point of the road from West to East is: " << second_highest << endl
<< "That point's index marker is: " << marker_second_highest << endl
<< "The lowest point of the road from West to East is: " << second_lowest << endl
<< "That point's index marker is: " << marker_second_lowest << "\n\n";
fout << "Total number of levels higher than the initial level of W/E road is: "
<< total_second_higher << endl
<< "Total number of levels lower than the initial level of W/E road is: "
<< total_second_lower << endl
<< "Total number of levels equal to the initial level of W/E road is: "
<< total_second_equal << "\n\n\n";
}
//---------------------------------------------------------------------------------------
// total_length(): writes the total length of each road into a file
//
// Parameters: ofstream object by reference, 2 integers
// Return Value: void
void total_length(ofstream& fout, int first_length, int second_length)
{
fout << "The total length of the road going from North to South is \n"
<< first_length << " feet long. \n\n";
fout << "The total length of the road going from West to East is \n"
<< second_length << " feet long. \n\n";
}
//---------------------------------------------------------------------------------------
// compare_initial(): compares initial values of the two roads and gives the highest
//
// Parameters: ofstream object by reference, 2 initial integers...
// Return Value: void
void compare_initial(ofstream& fout, int first, int second)
{
int difference = 0; // difference between initial elevations
if(first > second)
{
difference = (first - second);
fout << "The road going from North to South has a higher starting \n"
<< "point level, by " << difference << " units. \n\n";
}
else if(second > first)
{
difference = (second - first);
fout << "The road going from West to East has the higher starting \n"
<< "point level, by " << difference << " units. \n\n";
}
else
{
fout << "The starting point levels of both roads are equal. \n\n";
}
}
//---------------------------------------------------------------------------------------