views:

99

answers:

2

Here is my simple multidimensional array program. The first error occurs where I declare the function addmatrices and then a second one where it is implemented. I am also getting an undefined variable error for bsize.

What am I doing incorrectly?

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

//Function declarations

void constmultiply (double matrixA[][4], int asize, double matrixC[][4], int bsize, double multiplier);
//Pre: The address of the output file, the matrix to be multiplied by the constant, the matrix in which
// the resultant values will be stored and the multiplier are passed in.
//Post: The matrix is multiplied by the multiplier and the results are displayed on screen and written to the
// output file.

int addmatrices (double matrixA[][4], int asize, double matrixB[]4], int bsize, double matrixC[][4], int csize);
//Pre: The addresses of three matrices are passed in
//Post: The values in each of the two matrices are added together and put into a third matrix

//Error Codes
int INPUT_FILE_FAIL = 1;
int UNEQUAL_MATRIX_SIZE = 2;

//Constants
const double multiplier = 2.5;
const int rsize = 4;
const int csize = 4;

//Main Driver
int main()
{
    //Declare the two matrices
    double matrix1 [rsize][csize];
    double matrix2 [rsize][csize];
    double matrix3 [rsize][csize];

    //Variables
    double temp;
    string filename;

    //Declare filestream object
    ifstream infile;

    //Ask the user for the name of the input file
    cout << "Please enter the name of the input file: ";
    cin >> filename;

    //Open the filestream object
    infile.open(filename.c_str());

    //Verify that the input file opened correctly
    if (infile.fail())
    {
 cout << "Input file failed to open" <<endl;
 exit(INPUT_FILE_FAIL);
    }

    //Begin reading in data from the first matrix
    for (int i = 0; i <= 3; i++)//i = row
    {
 for (int j = 0; j <= 3; j++)// j = column
 {
     infile >> temp;
     matrix1[i][j] = temp;
 }
    }

    //Begin reading in data from the second matrix
    for (int k = 0; k <= 3; k++)// k = row
    {
 for (int l = 0; l <= 3; l++)// l = column
 {
     infile >> temp;
     matrix2[k][l] = temp;
 }
    }

    //Notify user
    cout    << "Input file open, reading matrices...Done!" << endl
     << "Read in 2 matrices..."<< endl; 

    //Output the values read in for Matrix 1
    for (int i = 0; i <= 3; i++)
    {
 for (int j = 0; j <= 3; j ++)
 {
     cout << setprecision(1)  << matrix1[i][j] << setw(8);
 }
 cout << "\n";
    }

    cout << setw(40)<< setfill('-') << "-" << endl ;

    //Output the values read in for Matrix 2
    for (int i = 0; i <= 3; i++)
    {
 for (int j = 0; j <= 3; j ++)
 {
     cout << setfill(' ') << setprecision(2) << matrix2[i][j] << setw(8);
 }
 cout << "\n";
    }
    cout << setw(40)<< setfill('-') << "-" << endl ;


    //Multiply matrix 1 by the multiplier value
    constmultiply (matrix1, rsize, matrix3, rsize, multiplier);

    //Output matrix 3 values to screen
    for (int i = 0; i <= 3; i++)
    {
 for (int j = 0; j <= 3; j ++)
 {
     cout << setfill(' ') << setprecision(2) << matrix3[i][j] << setw(8);
 }
 cout << "\n";
    }
    cout << setw(40)<< setfill('-') << "-" << endl ;

 //   //Add matrix1 and matrix2
 //   addmatrices (matrix1, 4, matrix2, 4, matrix3, 4);

 //   //Finished adding. Now output matrix 3 values to screen
 //   for (int i = 0; i <= 3; i++)
 //   {
 //for (int j = 0; j <= 3; j ++)
 //{
 //    cout << setfill(' ') << setprecision(2) << matrix3[i][j] << setw(8);
 //}
 //cout << "\n";
 //   }
 //   cout << setw(40)<< setfill('-') << "-" << endl ;

    //Close the input file
    infile.close();

    return 0;
}

//Function implementation
void constmultiply (double matrixA[][4], int asize, double matrixC[][4], int bsize, double multiplier)
{
    //Loop through each row and multiply the value at that location with the multiplier
    for (int i = 0; i < asize; i++)
    {
 for (int j = 0; j < 4; j++)
 {
     matrixC[i][j] = matrixA[i][j] * multiplier;
 }
    }
}

int addmatrices (double matrixA[][4], int asize, double matrixB[]4], int bsize, double matrixC[][4], int csize)
{
    //Remember that you can only add two matrices that have the same shape - i.e. They need to have an equal
    //number of rows and columns. Let's add some error checking for that:
    if(asize != bsize)
    {
 cout << "You are attempting to add two matrices that are not equal in shape. Program terminating!"
  << endl;
 return exit(UNEQUAL_MATRIX_SIZE);
    }

    //Confirmed that the matrices are of equal size, so begin adding elements
    for (int i = 0; i < asize; i++)
    {
 for (int j = 0; j < bsize; j++)
 {
     matrixC[i][j] = matrixA[i][j] + matrixB[i][j];
 }
    }

}
+3  A: 
double matrixB[]4],  // missing "["
Potatoswatter
arrggh. Many thanks decompiled and potatoswatter. I can't believe I looked it over so many times and didn't catch it!!!!
noobzilla
+1  A: 

There should not be return on this line:

return exit(UNEQUAL_MATRIX_SIZE);
SurvivalMachine
Also, `throw std::logic_error` (from `<stdexcept>`) is better C++.
Potatoswatter
Hi Guys,The above syntax error now being resolved, why is the above return statement incorrect? - I do see that I get a C2440 error - 'return' cannot convert from 'void' to 'int' on account of that line.Thanks for your time.
noobzilla
exit() is a function that returns void, not int. You called it in a statement that requires int.
SurvivalMachine