# Reading a file into a two dimensional array!!

• 04-18-2006
Dilmerv
Reading a file into a two dimensional array!!
:) Hello guys,
I'm trying to read a minimum value from a file, but the file contain two minimum values that are the same. Here's a example

file

32 345 23 122
21 23 233 239
132 252 21 231
673 141 45 211

How could I specify that the minimum value is located at column 1 and the other is located in column 3

This is what I have so far

Code:

``` double power [NROWS] [NCOLS], min;         string Days[7] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};         int weeks [10] = { 1,2,3,4,5,6,7,8,9,10};         int set_day, set_week;         cout << "--[ Minimum Daily Power Output] --" << endl;         ifstream inFile;         inFile.open("power1.dat");                 if (inFile.fail())         {                 cout << "Error opening The file " << endl;         }         else         {                 for ( int i = 0 ; i <= NROWS -1; i++)                 {                         for ( int j = 0; j <= NCOLS-1; j++)                         {                                 inFile >> power [i] [j];                         }                        }         }                for (int i=0; i<=NROWS -1; i++)         {                 for ( int j = 0; j <=NCOLS -1 ; j++)                 {                         if        ( i == 0 && j == 0)                         min = power[i][j];                         else                         {                                 if ( power[i][j] <= min )                                 {                                         min = power[i][j];                                         set_day = j;                                         set_week = i;                                 }                         }                 }                }                 cout << "\n\n Minimum Output is = " << min <<" megawatts" << endl;         cout << " Occurred on = " << Days[set_day] << " of week "<< weeks[set_week]<< endl;         system("pause");```
• 04-18-2006
7stud
Hi!!!!

Your request looked so urgent, I wanted to let you know I am reading your post!!!! I haven't had time to finish your post!!! But when I do, I'll see if I have a solution!!! Oh, heck!! I give up!! I just don't think there's enough time!!!
• 04-18-2006
Dilmerv
thats fine just give me a solution when you get some time thanks :)
• 04-19-2006
jafet
You'll need to find a way to store multiple values, maybe using std::vector. push_back() each new minimum position into the vector. If a new minimum element is found, clear() the vector. When doen, return the entire vector and print it's contents individually.
• 04-19-2006
Dilmerv
that sounds as a good Idea but I don't know how to use vectors, however is there another easier way to do that ?
Thank you
• 04-20-2006
jafet
Yes, dynamically allocate memory from the heap manually and reinvent the wheel. And spend nights up fixing segfaults.

Or learn how to use vectors.
• 04-20-2006
Bench82
Quote:

Originally Posted by Dilmerv
that sounds as a good Idea but I don't know how to use vectors, however is there another easier way to do that ?
Thank you

Using vectors is the easier way... Take the time to learn how to use STL containers before looking for the alternative.