简体   繁体   中英

Matrix subtraction program c++

I'm very new to programming, and I have a project where I need to take in two 6-by-2 matrices from a separate data file containing the life expectancy of black and white males/females in each decade from 1950 to 2000, then subtract the matrices to form a third 6-by-2 matrix containing the difference between the life expectancy of males and females of the same race in each decade. We didn't cover multidimensional arrays in class, and I'm a bit confused by this. I get a lot of errors saying undeclared identifier. I also know there has to be a better way to get the arrays from the data file, but I'm not sure how. Sorry if this is a stupid question.

#include <iostream>
#include <fstream>

using namespace std;

void getMatrixFemaleW();
void getMatrixFemaleB();
void getMatrixMaleW();
void getMatrixMaleB();
void matrix_diff();

int main()
{
   float matrixFemale[6][2];
   getMatrixFemaleW;
   getMatrixFemaleB;
   float matrixMale [6][2];
   getMatrixMaleW;
   getMatrixMaleB;
   float matrixDifference[6][2];
   matrix_diff;
   for (int x=0; x<6; x++)
   {
      for (int y=0; y<2; y++)
      {
         cout << matrixFemale[x][y] << " ";
      }
      cout << endl;
   }
}

void getMatrixFemaleW()
{
   ifstream inputFile;
   inputFile.open("MatrixFemaleW.txt");
   int count = 0;
   while (count < 6 && inputFile >> matrixFemale[count][0])
      count++;

}

void getMatrixFemaleB()
{
   ifstream inputFile;
   inputFile.open("MatrixFemaleB.txt");
   int count = 0;
   while (count < 6 && inputFile >> matrixFemale[count][1])
      count++;

}

void getMatrixMaleW()
{
   ifstream inputFile;
   inputFile.open("MatrixMaleW.txt");
   int count = 0;
   while (count < 6 && inputFile >> matrixMale[count][0])
      count++;


void getMatrixMaleB()
{
   ifstream inputFile;
   inputFile.open("MatrixMaleB.txt");
   int count = 0;
   while (count < 6 && inputFile >> matrixMale[count][1])
      count++;

}
void matrix_diff()
{
   for (i=0; i<6; i++)
   {
      matrixDifference[i][0] = matrixFemale [i][0] - matrixMale[i][0];  
   }
   for (i=0 i<6; i++)
   {
      matrixDifference [i][1] = matrixFemale [i][1] - matrixMale [i][1];
   }

}

first these lines should be

getMatrixFemaleW();
getMatrixFemaleB();
getMatrixMaleW();
getMatrixMaleB();
matrix_diff();

because they are fuctions not variables you need to call them not declear them then if you wanna reach a variable from outher function it should be global variable or a pointer that means

float matrixFemale[6][2];
float matrixMale [6][2];
float matrixDifference[6][2];

make these variables global variable or pointers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM