Sunday, July 12, 2009

Visual studio C++ help reading in arrays!!!?

i need help. for my computer science class i need to read in an array from a file. the file has 10 rows and 15 columns. the first line of the file has two different numbers, the first one is the number of rows in the file and the second number is the number of columns in the file. so the first line of the file is "10 15" and then the next line starts the array w/ the 10 rows and 15 columns





the only numbers in it are 0's and 1's. but we need to call them in with a void function called 'getfromfile' and then use another function called 'display' to output it on the screen, but change the 0's and 1's to -'s and *'s





the real problem i have is just how to set up inside the 'getfromfile' function. i dont know what i should put to try and collect all the data for the array, i dont know what i should do





if anyone has any idea please help me!

Visual studio C++ help reading in arrays!!!?
Give me a few minutes and I'll have something for ya...





Okay, here is something...I don't have time to get the multi-dim code right (its been a while) but the file i/o should help.





#include %26lt;iostream.h%26gt;


#include %26lt;fstream.h%26gt;


#define maxcols 256


//multidim dynamic arrays in c++ are a pain.





int getfromfile(void);


int getfromfile(void){


int rows;


int cols;


int x=0;int y=0;


ifstream myfile;


int *array[maxcols];





myfile.open ("filename", ios::in);


myfile%26gt;%26gt;rows;


myfile%26gt;%26gt;cols;


array=new int[maxcols][rows];





while (myfile %26gt;%26gt; array[x][y]){


y++;


if (y==cols) {y=0;x++;}


}


for(x=0;x%26lt;rows;x++){


for(y=0;y%26lt;cols;y++){


cout%26lt;%26lt;array[x][y];





}


cout%26lt;%26lt;endl;


}


return 0;


}


int main(void){





getfromfile();





}

spring flowers

No comments:

Post a Comment