Read Multidimesional Array from Data Files

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • farizaz
    New Member
    • Jan 2008
    • 6

    Read Multidimesional Array from Data Files

    Hi!

    I'm new here also just learned the program. I'm trying to read 2-dimensional data from data files. It seems to display weird numbers. I think I don't know how to read the data files. Hopefully i can learn from someone.
  • scruggsy
    New Member
    • Mar 2007
    • 147

    #2
    Originally posted by farizaz
    Hi!

    I'm new here also just learned the program. I'm trying to read 2-dimensional data from data files. It seems to display weird numbers. I think I don't know how to read the data files. Hopefully i can learn from someone.
    Post the code that's not working. Someone will tell you what's wrong with it.

    Comment

    • farizaz
      New Member
      • Jan 2008
      • 6

      #3
      The code is given below:


      int main()
      {
      // ifstream constructor opens the file
      ifstream inClientFile( "example.tx t", ios::in);

      // exit program if ifstream could not open file
      if ( !inClientFile)
      {
      cerr << "File could not be opened" << endl;
      exit(1);
      } // end if

      double itemsets[6][5];
      int row = 6;
      int column = 5;

      cout << left << setw(5) << "Itemsets" << endl;

      // display each record in file

      // while(inClientF ile >> itemsets[row][column])
      for (int i=0; i<row; i++)
      {
      cout << itemsets[i][0];
      for (int j=0; j<column; j++)
      {
      inClientFile >> itemsets[row][column];
      }
      }
      outputLine(item sets);

      return 0;
      } // end main

      // display single record from file
      void outputLine( double itemsets[6][5])
      {
      int row = 6;
      int column = 5;

      for (int i=0; i<row; i++)
      {
      cout << i;
      cout << itemsets[i][0];
      for (int j=0; j<column; j++)
      {
      cout << j;
      // cout << left << setw(10) << itemsets[][j];
      if ((j+1)%5 == 0)
      cout << endl;
      }
      }
      } // end function outputLine

      Comment

      • Savage
        Recognized Expert Top Contributor
        • Feb 2007
        • 1759

        #4
        If i and j are counters why do you put row and columns as the matrix indexes?

        [CODE=cpp]inClientFile >> itemsets[row][column];[/CODE]

        Comment

        • farizaz
          New Member
          • Jan 2008
          • 6

          #5
          Originally posted by Savage
          If i and j are counters why do you put row and columns as the matrix indexes?

          [CODE=cpp]inClientFile >> itemsets[row][column];[/CODE]

          Thank you very much!

          Comment

          Working...