#include <iostream>
#include <fstream>
#include <math.h>
using namespace std;
const int cols = 6;
void getClosest(int rows, int columns, double arr[][cols]){
// initialize difference between element and 50
int diff = fabs(50 - arr[0][0]);
// variables to store row number and column number
int colNo =0, rowNo = 0;
// get the element with minimum difference
for(int i=0; i<rows; i++){
for(int j=0; j<columns; j++){
if(fabs(50-arr[i][j]) < diff){
// update distance
diff = fabs(50-arr[i][j]);
// update row and column numbers
rowNo = i;
colNo = j;
}
}
}
// display result
cout << "The element closest to 50 is : " << arr[rowNo][colNo] << " at row " << rowNo << " and column " << colNo << endl;
}
int main()
{
double sum = 0;
int lowest, lowestR, highest, highestC;
const int rows = 50;
const int columns = 6;
double File[rows][columns];
fstream inFile;
inFile.open("scores.txt");
// read data from file into array
for (int i = 0; i < rows; i++) {
for (int k = 0; k < columns; k++) {
inFile >> File[i][k];
}
}
// get the column with highest sum
// variable to store highest sum of column
highest = 0;
// variable to store column number with highest sum
highestC = 0;
for(int i=0; i<columns; i++){
// initialize column sum to 0
sum = 0;
for(int j=0; j<rows; j++){
// add the current value to sum
sum = sum + File[j][i];
}
// check for larger sum
if(sum > highest){
// update column value and highest sum
highest = sum;
highestC = i;
}
}
// display highest sum and column number with highest sum
cout << "The column with highest sum is: " << highestC << ", and the value of sum is: " << highest << endl;
// get the row with lowest total
// initialize lowest sum
lowest = 0;
// initialize row with lowest sum
lowestR = 0;
// get sum of each row
for(int i=0; i<rows; i++){
// initialize row sum to 0
sum = 0;
for(int j=0; j<columns; j++){
sum = sum + File[i][j];
}
// initialize lowest sum to sum of first row
if(i == 0){
lowest = sum;
lowestR = 0;
}
// check if lower sum is found
if(sum < lowest){
// update lowest sum
lowest = sum;
lowestR = i;
}
}
// display result
cout << "The row with lowest sum is: " << lowestR << ", and the value of sum is: " << lowest << endl;
// call closest function
getClosest(rows, columns, File);
}