• Source
    1. #include <iostream>
    2. #include <fstream>
    3. #include <math.h>
    4. using namespace std;
    5. const int cols = 6;
    6. void getClosest(int rows, int columns, double arr[][cols]){
    7. // initialize difference between element and 50
    8. int diff = fabs(50 - arr[0][0]);
    9. // variables to store row number and column number
    10. int colNo =0, rowNo = 0;
    11. // get the element with minimum difference
    12. for(int i=0; i<rows; i++){
    13. for(int j=0; j<columns; j++){
    14. if(fabs(50-arr[i][j]) < diff){
    15. // update distance
    16. diff = fabs(50-arr[i][j]);
    17. // update row and column numbers
    18. rowNo = i;
    19. colNo = j;
    20. }
    21. }
    22. }
    23. // display result
    24. cout << "The element closest to 50 is : " << arr[rowNo][colNo] << " at row " << rowNo << " and column " << colNo << endl;
    25. }
    26. int main()
    27. {
    28. double sum = 0;
    29. int lowest, lowestR, highest, highestC;
    30. const int rows = 50;
    31. const int columns = 6;
    32. double File[rows][columns];
    33. fstream inFile;
    34. inFile.open("scores.txt");
    35. // read data from file into array
    36. for (int i = 0; i < rows; i++) {
    37. for (int k = 0; k < columns; k++) {
    38. inFile >> File[i][k];
    39. }
    40. }
    41. // get the column with highest sum
    42. // variable to store highest sum of column
    43. highest = 0;
    44. // variable to store column number with highest sum
    45. highestC = 0;
    46. for(int i=0; i<columns; i++){
    47. // initialize column sum to 0
    48. sum = 0;
    49. for(int j=0; j<rows; j++){
    50. // add the current value to sum
    51. sum = sum + File[j][i];
    52. }
    53. // check for larger sum
    54. if(sum > highest){
    55. // update column value and highest sum
    56. highest = sum;
    57. highestC = i;
    58. }
    59. }
    60. // display highest sum and column number with highest sum
    61. cout << "The column with highest sum is: " << highestC << ", and the value of sum is: " << highest << endl;
    62. // get the row with lowest total
    63. // initialize lowest sum
    64. lowest = 0;
    65. // initialize row with lowest sum
    66. lowestR = 0;
    67. // get sum of each row
    68. for(int i=0; i<rows; i++){
    69. // initialize row sum to 0
    70. sum = 0;
    71. for(int j=0; j<columns; j++){
    72. sum = sum + File[i][j];
    73. }
    74. // initialize lowest sum to sum of first row
    75. if(i == 0){
    76. lowest = sum;
    77. lowestR = 0;
    78. }
    79. // check if lower sum is found
    80. if(sum < lowest){
    81. // update lowest sum
    82. lowest = sum;
    83. lowestR = i;
    84. }
    85. }
    86. // display result
    87. cout << "The row with lowest sum is: " << lowestR << ", and the value of sum is: " << lowest << endl;
    88. // call closest function
    89. getClosest(rows, columns, File);
    90. }