By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
463,190 Members | 667 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 463,190 IT Pros & Developers. It's quick & easy.

code for reading a file then storing the value and matrix into the system..

P: 34
hi, well this is the file i have to read into the system...
Expand|Select|Wrap|Line Numbers
  1. <matrix>
  2.     rows = 2
  3.     cols = 2
  4.  
  5. 1 2
  6. 2 4
  7. </matrix>
  8.  
any ideas on how to do it?

below is my code..
and i checked that this doesnt work...
and apparantly someone told me that this is in c programmin style..
could ya help me design a method to read the file in a c++ programming way?
i have done my work.. and this is just exercise from the textbook i am learning c++ from.. unfortunately it doesnt have the solutions.. so i am lost on how to do it.. check my other postings to see that i have actually done my work and this is not homework.. so could someone help me please????

Expand|Select|Wrap|Line Numbers
  1. void Matrix::readMatrixFile(string FILENAME)
  2. {
  3.     const string filename(FILENAME);
  4.     ifstream file(filename.c_str());
  5.     string line;
  6.     if (getline(file, line) && line == "<matrix>")
  7.     {
  8.         if (file >> line /* "rows" */ && file >> line /* "=" */ && file >> row && getline(file,line) /* ; */)
  9.         {
  10.             if (file >> line /* "cols" */ && file >> line /* "=" */ && file >> col && getline(file,line) /* ; */)
  11.             {
  12.                 for (int r = 0; r < row; r++)
  13.                 {
  14.                     for (int c = 0; c < col; c++)
  15.                     {
  16.                         int value;
  17.                         if (file >> value)
  18.                         {
  19.                             m[r][c] = value;
  20.                         }
  21.                     }
  22.                     cout << endl;
  23.                 }
  24.             }
  25.         }
  26.     }
  27. }
  28.  
thanks
Aug 1 '08 #1
Share this Question
Share on Google+
5 Replies

boxfish
Expert 100+
P: 469
Hi,
Well, the code works fine for me, what goes wrong when you run it?
Aug 1 '08 #2

gpraghuram
Expert 100+
P: 1,275
You are reading a ; from the line rows = 2 but the file you have posted dosent have it...
Also i think the logic is complex....
Try reading the vine and tokenize it to get the data.

Raghu
Aug 1 '08 #3

weaknessforcats
Expert Mod 5K+
P: 9,197
You may be going to a lot of extra touble here. All arrays in C++ are one-dimensional. You might read this: http://bytes.com/forum/thread772412.html.
Aug 1 '08 #4

P: 34
You are reading a ; from the line rows = 2 but the file you have posted dosent have it...
Also i think the logic is complex....
Try reading the vine and tokenize it to get the data.

Raghu


how to tolkenize it to get the data?
could u show me the code that does it?
Aug 1 '08 #5

P: 34
Hi,
Well, the code works fine for me, what goes wrong when you run it?

well the program doesnt work for me when i run it...
could u take a look and tell me wats wrong?

header file...
Expand|Select|Wrap|Line Numbers
  1. #pragma once
  2. #include <iostream>
  3. #include <fstream>
  4. #include <string>
  5. #include "Matrix.h"
  6. using namespace std;
  7.  
  8. class Matrix
  9. {
  10. public:
  11.     Matrix(void);
  12.     ~Matrix(void);
  13.     void readMatrixFile(string FILENAME);
  14.     void addMatrix(Matrix m1, Matrix m3);
  15.     void displayMatrix();
  16.     void transposeMatrix();
  17.     void multiplyMatrices(Matrix m2, Matrix m3);
  18.  
  19. private:
  20.     int row;
  21.     int col;
  22.     /*m = new (double*)[10];
  23.     for(int i = 0; i < 10; i++)
  24.     {
  25.         m[i] = new double[10];
  26.     }
  27.     for(int j = 0; j < 10; j++)
  28.     {
  29.         delete[] m[i];
  30.     }
  31.     delete[] m;*/
  32.     double m[10][10];
  33. };
  34.  
matrix.cpp file
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include "Matrix.h"
  5. using namespace std;
  6.  
  7.  
  8. Matrix::Matrix(void)
  9. {
  10. }
  11.  
  12.  
  13. Matrix::~Matrix(void)
  14. {
  15. }
  16.  
  17.  
  18. void Matrix::readMatrixFile(string FILENAME)
  19. {
  20.     const string filename(FILENAME);
  21.     ifstream file(filename.c_str());
  22.     string line;
  23.     if (getline(file, line) && line == "<matrix>")
  24.     {
  25.         if (file >> line /* "rows" */ && file >> line /* "=" */ && file >> row && getline(file,line) /* ; */)
  26.         {
  27.             if (file >> line /* "cols" */ && file >> line /* "=" */ && file >> col && getline(file,line) /* ; */)
  28.             {
  29.                 for (int r = 0; r < row; r++)
  30.                 {
  31.                     for (int c = 0; c < col; c++)
  32.                     {
  33.                         int value;
  34.                         if (file >> value)
  35.                         {
  36.                             m[r][c] = value;
  37.                         }
  38.                     }
  39.                     cout << endl;
  40.                 }
  41.             }
  42.         }
  43.     }
  44. }
  45.  
  46.  
  47. void Matrix::addMatrix(Matrix m1, Matrix m3)
  48. {
  49.     for(int a = 0; a < row; a++)
  50.     {
  51.         for(int b = 0; b < col; b++)
  52.         {
  53.             m3.m[a][b]=(m1.m[a][b] + m[a][b]);
  54.         }
  55.     }
  56. }
  57.  
  58.  
  59. void Matrix::displayMatrix()
  60. {
  61.     for(int i = 0; i < row; i++)
  62.     {
  63.         cout << "| ";
  64.         for (int j = 0; j < col; j++)
  65.         {
  66.             cout << m[i][j] << " ";
  67.         }
  68.         cout << "|" << endl;
  69.     }
  70. }
  71.  
  72.  
  73. void Matrix::transposeMatrix()
  74. {
  75.     for(int k = 0; k < row; k++)
  76.     {
  77.         for(int j = 0; j < col; j++)
  78.         {
  79.             m[k][j] = m[j][k];
  80.             m[j][k] = m[k][j];
  81.         }
  82.     }
  83. }
  84.  
  85.  
  86. void Matrix::multiplyMatrices(Matrix m2, Matrix m3)
  87. {
  88.     if(col != m2.row)
  89.     {
  90.         cout << "These two matrices cannot be multiplied!!!" << endl;
  91.     }
  92.     else
  93.     {
  94.         for(int i = 0; i < row; i++)
  95.         {
  96.             for(int j = 0; j < m2.col; j++)
  97.             {
  98.                 m3.m[i][j] = 0;
  99.                 for(int k = 0; k < m2.row; k++)
  100.                 {
  101.                     m3.m[i][j] = m3.m[i][j] + m[i][k] * m2.m[k][j];
  102.                 }
  103.             }
  104.         }
  105.     }
  106. }
  107.  
cpp file containing main
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include "Matrix.h"
  5. using namespace std;
  6.  
  7.  
  8. int main(int argc, char* argv[])
  9. {
  10.     Matrix m1, m2 ,m3;
  11.     char theOption = '0';
  12.     char whichMatrixToDisplay;
  13.     string filename1, filename2;
  14.  
  15.     while(theOption != '5')
  16.     {
  17.         cout << "Please choose one of the following options from the menu below:" << endl;
  18.         cout << "---------------------------------------------------------------" << endl;
  19.         cout << "(1) Add two matrices" << endl;
  20.         cout << "(2) Multiply two matrices" << endl;
  21.         cout << "(3) Take transpose of a matrix" << endl;
  22.         cout << "(4) Display a matrix" << endl;
  23.         cout << "(5) Exit" << endl;
  24.         cout << "---------------------------------------------------------------" << endl;
  25.         cin >> theOption;
  26.  
  27.         switch(theOption)
  28.         {
  29.             case '1':
  30.                 cout << "You have chosen option 1!: Add two matices!" << endl;
  31.                 cout << "Please enter the two filenames that contain the two input matrices respectively!" << endl;
  32.                 cout << "Enter name of 1st file" << endl;
  33.                 cin >> filename1;
  34.                 cout << "Enter name of 2nd file" << endl;
  35.                 cin >> filename2;
  36.                 m1.readMatrixFile(filename1);
  37.                 m2.readMatrixFile(filename2);
  38.                 m2.addMatrix(m1, m3);
  39.                 cout << "The two matrices have been added together." << endl;
  40.                 cout << "Select (4): Display a matrix to view it." << endl << endl;
  41.                 break;
  42.  
  43.             case '2':
  44.                 cout << "You have chosen option 2!: Multiply two matices!" << endl;
  45.                 cout << "Please enter the two filenames that contain the two input matrices respectively!" << endl;
  46.                 cout << "Enter name of 1st file" << endl;
  47.                 cin >> filename1;
  48.                 cout << "Enter name of 2nd file" << endl;
  49.                 cin >> filename2;
  50.                 m1.readMatrixFile(filename1);
  51.                 m2.readMatrixFile(filename2);
  52.                 m1.multiplyMatrices(m2, m3);
  53.                 cout << "The two matrices have been multiplied with each other." << endl;
  54.                 cout << "Select (4): Display a matrix to view it." << endl << endl;
  55.                 break;
  56.  
  57.             case '3':
  58.                 cout << "You have chosen option 3!: Transpose a matrix!" << endl;
  59.                 cout << "Please enter the filename that contains the matrix to be transposed!" << endl;
  60.                 cout << "Enter name of file" << endl;
  61.                 cin >> filename1;
  62.                 m1.readMatrixFile(filename1);
  63.                 m1.transposeMatrix();
  64.                 cout << "The matrix has been transposed." << endl;
  65.                 cout << "Select (4): Display a matrix to view it." << endl << endl;
  66.                 break;
  67.  
  68.             case '4':
  69.                 cout << "You have chosen option 4!: Display a matix!" << endl;
  70.                 cout << "Please enter the option 1, 2 or 3 to display the respective matrices!" << endl;
  71.                 cin >> whichMatrixToDisplay;
  72.                 if(whichMatrixToDisplay == '1')
  73.                     m1.displayMatrix();
  74.                 else if(whichMatrixToDisplay == '2')
  75.                     m2.displayMatrix();
  76.                 else if(whichMatrixToDisplay == '3')
  77.                     m3.displayMatrix();
  78.                 else
  79.                 cout << endl;
  80.                 break;
  81.  
  82.             case '5':
  83.                 cout << "You have chosen the option to Quit. Goodbye!" << endl;
  84.                 break;
  85.  
  86.             default:
  87.                 cout << " Sorry the option chosen is invalid. Please enter a valid option!" << endl << endl;
  88.                 break;
  89.         }
  90.     }
  91.  
  92.     return 0;
  93. }
  94.  
Aug 1 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.