// This program creates a two-dimensional array initialized with test data using any data type.
//
// The program should have the following functions:
//
// getTotal - return the total of all the values in the array
//
// getAverage - return the average of all the values in the array
//
// getRowTotal - This function should accept a two-dimensional array as its first argument and an integer as its second argument.
// The second argument should be the subscript of a row in the array. The function should return the total of the values in the specified row.
//
// getColumnTotal - This function should accept a two-dimensional array as its first argument and an integer as its second argument.
// The second argument should be the subscript of a column in the array. The function should return the total of the values in the specified column.
//
// getHighestInRow - This function should accept a two-dimensional array as its first argument and an integer as its second argument.
// The second argument should be the subscript of a row in the array. The function should return the highest value in the specified row of the array.
//
// getLowestInRow - This function should accept a two-dimensional array as its first argument and an integer as its second argument.
// The second argument should be the subscript of a row in the array. The function should return the lowest value in the specified row of the array
//
//
//Program from Starting Out With C++ from Control Structures through Objects (Ninth Edition) by Tony Gaddis(2018) pg. 461, Problem #21.
//
// Programmed by Devin Dahlberg, CIS 251 Student
// March 4, 2024
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const int ROWS = 2, COLS = 5;
// Calculates the total of all data in the file
int getTotal(int array[5][5]) {
int total = 0;
for (int i = 0; i < ROWS; ++i) {
for (int j = 0; j < COLS; ++j) {
total += array[i][j];
}
}
cout << "The total of the first and second row is: " << total << endl;
cout << endl;
return total;
}
// Calculates the average of all data in the file.
double getAverage(int array[5][5]) {
double total = 0;
double avg = 0;
for (int i = 0; i < ROWS; ++i) {
for (int j = 0; j < COLS; ++j) {
total += array[i][j];
}
}
avg = total / (ROWS * COLS);
cout << "The average of the first and second row is: " << avg << endl;
cout << endl;
return avg;
}
// Calculates the total for each row individually.
int getRowTotal(int array[5][5]) {
int total = 0;
for (int i = 0; i < ROWS; ++i) {
total = 0;
for (int j = 0; j < COLS; ++j) {
total += array[i][j];
}
cout << "Row " << i + 1 << " has a sum of " << total << endl;
}
cout << endl;
return total;
}
//Calculates the total for each column individually.
int getColumnTotal(int array[5][5]) {
int total = 0;
for (int j = 0; j < COLS; ++j) {
total = 0;
for (int i = 0; i < ROWS; ++i) {
total += array[i][j];
}
cout << "Column " << j + 1 << " has a sum of " << total << endl;
}
cout << endl;
return total;
}
// Gets the Highest Number in Each Row
int getHighestInRow(int array[5][5]) {
int max = array[0][0];
for (int i = 0; i < ROWS; ++i) {
int max = array[i][0];
for (int j = 0; j < COLS; ++j) {
if (max < array[i][j]) {
max = array[i][j];
}
}
cout << "The highest number in row " << i + 1 << " is " << max << endl;
}
cout << endl;
return max;
}
// Gets the Lowest Number in Each Row
int getLowestInRow(int array[5][5]) {
int min = array[0][0];
for (int i = 0; i < ROWS; ++i) {
int min = array[i][0];
for (int j = 1; j < COLS; ++j) {
if (min > array[i][j]) {
min = array[i][j];
}
}
cout << "The lowest number in row " << i + 1 << " is " << min << endl;
}
cout << endl;
return min;
}
int main() {
int array[ROWS][COLS] = { 0 };
string inFileName = "Program7.txt";
ifstream inFile;
inFile.open(inFileName.c_str());
if (inFile.is_open()) {
for (int i = 0; i < ROWS; ++i) {
for (int j = 0; j < COLS; ++j) {
inFile >> array[i][j];
}
}
for (int i = 0; i < ROWS; ++i) {
for (int j = 0; j < COLS; ++j) {
cout << array[i][j] << " ";
}
cout << endl;
}
cout << " " << endl;
inFile.close();
}
getTotal(array);
getAverage(array);
getRowTotal(array);
getColumnTotal(array);
getHighestInRow(array);
getLowestInRow(array);
return 0;
}