GIDForums  

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 03-May-2008, 18:02
Lao-Tzu Lao-Tzu is offline
New Member
 
Join Date: May 2008
Posts: 1
Lao-Tzu is on a distinguished road

Multiply 6X6 Matrices using "infile" (DAT file)


Hey guys,
I'm stuck on a Matrix assigment at the moment. The assignment asks us to multiply 2 6x6 matrices together and show the result. Our inputs are supposed to come form a .DAT file.. Can you guys show me how I would incorporate the "infile" into my current project? My current currently asks for someone to input numbers... but my instructor wants us to get input from the .DAT files.. Below is my current code.



CPP / C++ / C Code:

#include <iostream>
using namespace std;

int main()

{

     int A[6][6];

     int B[6][6];

     int C[6][6];

     int X[6][6][6];

     int i,j,m;

     

     

     cout<<"==========Enter matrix A==========\n";

     for(i=0;i<6;i++)

     {     cout<<endl;

          for(j=0;j<6;j++)

          {     

               cout<<"A["<<i<<"]["<<j<<"]= ";

               cin>>A[i][j];

               C[i][j]=0;

          }

     }

     

     cout<<"\n==========Enter matrix B==========\n";

     for(i=0;i<6;i++)

     {     cout<<endl;

          for(j=0;j<6;j++)

          {     

               cout<<" B["<<i<<"]["<<j<<"]= ";

               cin>>B[i][j];

          }

     }

     

     

     for(i=0;i<6;i++)

     {     

          for(j=0;j<6;j++)

          {     

               for(m=0;m<6;m++)

               {

               X[i][j][m]=A[i][m]*B[m][j];

               C[i][j]+=X[i][j][m];

               }

          }

     }

 

     cout<<"\n==========matrix C==========\n";

     for(i=0;i<6;i++)

     {     cout<<endl;

          for(j=0;j<6;j++)

          {     

               cout<<" C["<<i<<"]["<<j<<"]= "<<C[i][j];

         

          }

     }

     cout<<endl<<endl;

 

return 0;

}  






  #2  
Old 04-May-2008, 08:25
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,623
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Multiply 6X6 Matrices using "infile" (DAT file)


Quote:
Originally Posted by Lao-Tzu
Can you guys show me how I would incorporate the "infile" into my current project?

1. Declare an ifstream and give it the name of your file.

2. Make sure it was opened correctly. Tell the user what happened. (Did it open it or not?)

3. Use the overloaded operator >> to read the values.

4. Each and every time, make sure that it was able to read. If it was not able to read everything, inform the user. For now, just bail out if it sees an error condition.

For example:
CPP / C++ / C Code:
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    int i, j;
    double x[3][3];
    char *inname = "test.dat";
    ifstream infile(inname);
    if (!infile) {
        cout << "Can't open file " << inname << " for reading." << endl;
        return 0;
    }
    cout << "Opened " << inname << " for reading." << endl;
    for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++) {
            infile >> x[i][j];
            if (!infile) {
                cout << "There was a problem reading x[" << i << "][" << j << "]" << endl;
                return 0;
            }
        }
    }
    for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++) {
            cout << "x[" << i << "][" << j << "] = " << x[i][j] << endl;
        }
    }
    return 0;
}

Now, try something like the following as the file "test.dat":
Code:
1 2 3 5.2 4.00001 22.22 7 8 9

Output:
Code:
Opened test.dat for reading. x[0][0] = 1 x[0][1] = 2 x[0][2] = 3 x[1][0] = 5.2 x[1][1] = 4.00001 x[1][2] = 22.22 x[2][0] = 7 x[2][1] = 8 x[2][2] = 9


Now go back to test.dat and put in an error. Maybe put a comma instead of a period in 4.00001:
Code:
1 2 3 5.2 4,00001 22.22 7 8 9

Output:
Code:
Opened test.dat for reading. There was a problem reading x[1][2]

Now, go back and rename your file "test.dat" to something else (so there is no file named "test.dat").

Output:
Code:
Can't open file test.dat for reading.


Regards,

Dave
 
 

Recent GIDBlogToyota - 2008 July Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 07:44
After execution - Error cannot locate /Skin File? WSCH CPP / C++ Forum 1 05-Mar-2005 20:03
Yet another CD burner problem: Lite-On LSC-24082K Erwin Computer Hardware Forum 1 22-May-2004 11:28
CD Buring Failed skanth2000 Computer Hardware Forum 1 15-Nov-2003 03:52

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 15:01.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.