Thread: Reading a file into a two dimensional array!!

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    44

    Question 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");

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    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!!!
    Last edited by 7stud; 04-18-2006 at 10:40 PM.

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    44
    thats fine just give me a solution when you get some time thanks

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    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.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    44
    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

  6. #6
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Yes, dynamically allocate memory from the heap manually and reinvent the wheel. And spend nights up fixing segfaults.



    Or learn how to use vectors.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  7. #7
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 02-02-2009, 07:27 AM
  2. Replies: 9
    Last Post: 12-08-2008, 10:27 AM
  3. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM