Advertisement
axyd

Lab 8, part 2

May 11th, 2016
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.26 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. using namespace std;
  4.  
  5. const int ROWS = 3;
  6. const int COLS = 4;
  7.  
  8. void getTotal(int[ROWS][COLS], int &);
  9. void getAverage(int[ROWS][COLS], double&);
  10. void getRowTotal(int[ROWS][COLS], int, int&);
  11. void getColumnTotal(int[ROWS][COLS], int, int&);
  12. void getHighestInRow(int[ROWS][COLS], int, int&);
  13. void getLowestInRow(int[ROWS][COLS], int, int&);
  14. void tblImg(int[ROWS][COLS]); //values table
  15. void err(); //fix cin errors
  16.  
  17. int main(){
  18.     char again = 'y';
  19.     while (again == 'y' || again == 'Y'){
  20.         int arrayx[ROWS][COLS] = { { 31, 41, 11, 91 }, { 62, 32, 72, 12 }, { 93, 73, 33, 23 } },
  21.             ttl = 0, trow = 0, tcol = 0, hrow = 0, lrow = 0, usrRow = 0, usrCol = 0;
  22.         double avg = 0;
  23.  
  24.         tblImg(arrayx); //display table to user
  25.  
  26.         cout << "Enter a Row (1-3): ";
  27.         cin >> usrRow;
  28.         while (cin.fail() || usrRow < 1 || usrRow > 3){ //fix letter and negative
  29.             err();
  30.             cout << "Enter a Row (1-3): ";
  31.             cin >> usrRow;
  32.         }
  33.  
  34.         cout << "Enter a Column (1-4): ";
  35.         cin >> usrCol;
  36.         while (cin.fail() || usrCol < 1 || usrCol > 4){ //fix letter and negative
  37.             err();
  38.             cout << "Enter a Column (1-4): ";
  39.             cin >> usrCol;
  40.         }
  41.  
  42.         getTotal(arrayx, ttl);
  43.         getAverage(arrayx, avg);
  44.         getRowTotal(arrayx, usrRow , trow);
  45.         getColumnTotal(arrayx, usrCol, tcol);
  46.         getHighestInRow(arrayx, usrRow, hrow);
  47.         getLowestInRow(arrayx, usrRow, lrow);
  48.         cout << "Total: " << ttl << endl
  49.             << "Average: " << setprecision(2) << fixed << avg << flush << endl
  50.             << "Row total: " << trow << endl
  51.             << "Column total: " << tcol << endl
  52.             << "Highest number in row is: " << hrow << endl
  53.             << "Lowest number in row is: " << lrow << endl;
  54.  
  55.         cout << "\n\t\t\tEnter y or Y to repeat: ";
  56.         cin >> again;
  57.     }
  58. }
  59. void getTotal(int ttlArray[ROWS][COLS], int& ttlOut){
  60.     for (int ir = 0; ir < ROWS; ir++){
  61.         for (int ic = 0; ic < COLS; ic++)
  62.             ttlOut += ttlArray[ir][ic];
  63.     }
  64. }
  65. void getAverage(int avgArray[ROWS][COLS], double& avgOut){
  66.     for (int ir = 0; ir < ROWS; ir++){
  67.         for (int ic = 0; ic < COLS; ic++)
  68.             avgOut += avgArray[ir][ic]; //sum
  69.     }
  70.     avgOut /= (ROWS*COLS); //avg
  71. }
  72. void getRowTotal(int rowArray[ROWS][COLS], int row, int& ttlRow){
  73.     for (int ic = 0; ic < COLS; ic++)
  74.         ttlRow += rowArray[row-1][ic];
  75. }
  76. void getColumnTotal(int colArray[ROWS][COLS], int col, int& ttlCol){
  77.     for (int ir = 0; ir < ROWS; ir++)
  78.         ttlCol += colArray[ir][col-1];
  79. }
  80. void getHighestInRow(int rowHigh[ROWS][COLS], int row, int& rowHighOut){
  81.     for (int ic = 0; ic < COLS; ic++){
  82.         if (rowHigh[row-1][ic] >= rowHighOut) //value greater than output, replace
  83.             rowHighOut = rowHigh[row-1][ic];
  84.     }
  85. }
  86. void getLowestInRow(int rowLow[ROWS][COLS], int row, int& rowLowOut){
  87.     rowLowOut = rowLow[row-1][0]; //initialize
  88.     for (int ic = 0; ic < COLS; ic++){
  89.         if (rowLow[row-1][ic] <= rowLowOut) //output greater than value, replace
  90.             rowLowOut = rowLow[row-1][ic];
  91.     }
  92. }
  93. void tblImg(int xRay[ROWS][COLS]){
  94.     cout << "\n       ||C1||C2||C3||C4||\n"
  95.         << "       ------------------\n";
  96.     for (int ir = 0; ir < ROWS; ir++){
  97.         cout << "(Row " << ir + 1 << ")||";
  98.         for (int ic = 0; ic < COLS; ic++)
  99.             cout << setw(2) << setfill('0') << xRay[ir][ic] << "||";
  100.         cout << endl;
  101.     }
  102.     cout << endl;
  103. }
  104. void err(){
  105.     cout << "\t\t/!\\ ERROR /!\\\n";   
  106.     cin.clear();
  107.     fflush(stdin);
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement