SOURCE FILE: sumChecker.cpp



/* 

      This program reads an array of numbers from a file named "square" and
      computes the sums of the rows and columns, and the sums of the diagonal
      entries.  It writes the array of numbers to the screen, and then writes
      the sums.
*/

#include <iostream>
#include <fstream>
using namespace std ;

const int NUM_ROWS = 6 ;
const int NUM_COLS = 6 ;

void readSquare(int array[][NUM_COLS]) ;
void computeSums (const int array[][NUM_COLS]) ;

int main (void)
{
   int squareArr [NUM_ROWS][NUM_COLS] ;
   readSquare (squareArr) ;
   computeSums (squareArr) ;

   return 0;
}

/* 
    This function reads an array of integers from a file named "square" and
    copies the values into an array (here nammed "array") of the same
    dimensions (currently set at 6 X 6).  The function writes the values to
    the screen immediately after reading them from the file.
*/
void readSquare(int array[][NUM_COLS])
{
   ifstream infile ("square") ;
   
   for (int rowNum = 0; rowNum < NUM_ROWS; rowNum++)
   {
      for (int colNum = 0; colNum < NUM_COLS; colNum++)
      {
           infile >> array[rowNum][colNum] ;
           cout << '\t' << array[rowNum][colNum];
      }
      cout << endl ;
   }
}

/*
     This function inputs an array (called "array").  It is assumed that the
     number of columns in the array is equal to the number of columns.  This
     function computes the sums of the numbers in each row, each column and
     also each 'diagonal'.  One diagonal consists of all the entries that have
     equal indices.  The other diagonal consists of all entries such that the
     sum of the indices is equal to NUM_ROWS-1.  This function writes the value
     of each sum to the screen - with an appropriate label identifying which
     row, column, or diagonal.
*/
void computeSums (const int array[][NUM_COLS]) 
{
   int sum ;
   int rowNum, colNum ;

   cout << endl ;
       /* This section of code sums the rows */
   for (rowNum = 0; rowNum < NUM_ROWS; rowNum++)
   {
      sum = 0 ;
      for (colNum = 0; colNum < NUM_COLS; colNum++)
      {
         sum = sum + array[rowNum][colNum] ;
      }
      cout << "Row sum number " << rowNum << " is " << sum << endl ;
   }
   cout << endl ;
       /* This section of code sums the columns */
   for (colNum = 0; colNum < NUM_COLS; colNum++)
   {
      sum = 0 ;
      for (rowNum = 0; rowNum < NUM_ROWS; rowNum++)
      {
         sum = sum + array[rowNum][colNum] ;
      }
      cout << "Column sum number " << colNum << " is " << sum << endl ;
   }
   cout << endl ;
    
       /* This section of code sums the 
           upper-left-to-lower-right diagonal. */
   sum = 0 ;
   for (rowNum = 0; rowNum < NUM_ROWS; rowNum++)
   {
      sum = sum + array[rowNum][rowNum] ;
   }
   cout << "The main diagonal sum is " << sum << endl ;

       /* This section of code sums the 
           lower-left-to-upper-right diagonal. */
   sum = 0 ;
   for (rowNum = NUM_ROWS-1; rowNum >= 0; rowNum--)
   {
      sum = sum + array[rowNum][NUM_ROWS-1-rowNum] ;
   }
   cout << "The lower-left-to-upper-right diagonal sum is " << sum << endl ;
   cout << endl ;
}