Start New Discussion within our Software Development Community

help plz...I have to create a program that multiplies matrices...those elements in the array must come from a file or reads a file...I have constructed these codes but this program lets the user type their number, not read from a file..I have these code so far...

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


int main ()
 {
   int a[3][5];
   int b[5][4];
   int answer[3][4];

   int i,j,k;   
   int temp; 
   fstream InFile;
   InFile.open("readfile.txt", ios::in);

  /*type in values for first array */

   cout << "Enter values for array A." << endl;
   for (i = 1; i <= 2; i++)
     { cout << "Row number " << i << endl;
       for (j = 1; j <= 4; j++)
          { cout << "  Element number " << j << " : ";
            cin >> a[i][j];
          }
     }

  /*type in values for second array */

   cout << "Enter values for array B." << endl;
   for (i = 1; i <= 4; i++)
     { cout << "Row number " << i << endl;
       for (j = 1; j <= 3; j++)
          { cout << "  Element number " << j << " : ";
            cin >> b[i][j];
          }
     }

  /*calculate answers in answer array */

   for (i = 1; i <= 2; i++)
      for (j = 1; j <= 3; j++)
         {
           temp = 0;   /* Build up answer here */
           for (k = 1; k <= 4; k++)
              temp += a[i][k] * b[k][j];
           answer[i][j] = temp;
         }

   /*display the answers */

   for (i = 1; i <= 2; i++)
     {
       for (j = 1; j <= 3; j++)
          cout << answer[i][j] << " ";
       cout << endl;
     }
    system("PAUSE");
   return EXIT_SUCCESS; 

 }

You can try out using "fscanf" to read your matrix elements from files, or if you prefer going purely the C++ way I suggest looking at "ifstream" class

This article has been dead for over six months. Start a new discussion instead.