function to read in data and create dynamic array of pointers

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2008
Posts: 10
Reputation: M^2 is an unknown quantity at this point 
Solved Threads: 0
M^2 M^2 is offline Offline
Newbie Poster

function to read in data and create dynamic array of pointers

 
0
  #1
Apr 13th, 2009
suppose the input file is...
5(number of cells)
1 2 -1 4
0 2 3 4
1 0 3 -1
2 -1 3 1
2 0 1 -1
how will i Write a function to read data from an input file to create a dynamic array of pointers to cells, create the required number of cells one by one, store their addresses at the corresponding index in the array. Then fill the data (given in the input file) in each cell.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,346
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1460
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: function to read in data and create dynamic array of pointers

 
0
  #2
Apr 13th, 2009
It would be helpful to know what those numbers mean.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 10
Reputation: M^2 is an unknown quantity at this point 
Solved Threads: 0
M^2 M^2 is offline Offline
Newbie Poster

Re: function to read in data and create dynamic array of pointers

 
0
  #3
Apr 13th, 2009
these are actually doors os a maze..
The four numbers indicate the cell numbers of the cells being led to through the respective door. -1 indicates absence of a door. For example, line number 2 is about cell number 0 and says that door 1 leads to cell 1, door 2 to cell 2, door 3 is absent, and door 4 leads to cell number 4.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,346
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1460
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: function to read in data and create dynamic array of pointers

 
0
  #4
Apr 13th, 2009
If the first number is a cell number why are there duplicates ?

>>and says that door 1 leads to cell 1
There is no 1 on line 2. 0 2 3 4
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 10
Reputation: M^2 is an unknown quantity at this point 
Solved Threads: 0
M^2 M^2 is offline Offline
Newbie Poster

Re: function to read in data and create dynamic array of pointers

 
0
  #5
Apr 13th, 2009
its just an input format.......
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,346
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1460
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: function to read in data and create dynamic array of pointers

 
0
  #6
Apr 13th, 2009
So are you saying you just made up those numbers? You need to post some actual rows out of a real file.

The way I understand it, the first number is a cell number, second through fourth numbers are door numbers (three doors) and the value of the numbers indicate the cell number that the doors open to. If that is true then a cell is a square with only three doors, or only two doors when one of the door numbers is -1.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 10
Reputation: M^2 is an unknown quantity at this point 
Solved Threads: 0
M^2 M^2 is offline Offline
Newbie Poster

Re: function to read in data and create dynamic array of pointers

 
0
  #7
Apr 13th, 2009
one more thing
whats the diffrence between
fin.get
and fin.getline??
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,346
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1460
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: function to read in data and create dynamic array of pointers

 
0
  #8
Apr 13th, 2009
getline() read an entire text line into the buffer. get() just read unformatted data, such as binary or text data.

There are two versions of getline() -- one for character arrays and the other for std::string.
  1. std::string line;
  2. char ln[255];
  3. // character arrays
  4. cin.getline( ln, sizeof(ln) );
  5. //
  6. // std::string
  7. getline( cin, line );
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 10
Reputation: M^2 is an unknown quantity at this point 
Solved Threads: 0
M^2 M^2 is offline Offline
Newbie Poster

Re: function to read in data and create dynamic array of pointers

 
0
  #9
Apr 13th, 2009
thanks
  1. #include<iostream>
  2. #include<fstream>
  3. using namespace std;
  4.  
  5. struct cell
  6. {
  7. int *a[0];
  8. int *b[1];
  9. int *c[2];
  10. int *d[3];
  11. bool visited;
  12. int *back;
  13. };
  14. int main()
  15. {
  16. int array[80];
  17. ifstream fin;
  18. ofstream fout;
  19. fin.open("input.txt");
  20. fout.open("output.txt");
  21. int num;
  22. while(!fin.eof())
  23. {
  24. fin>>num;
  25. for(int i=0; i<num; i++)
  26. fin.getline(array,80);
  27. }
  28. cout<<array;
  29. return 0;
  30. }
i have tried this...
but *a[0]; is an error...
i cant understand this whole problem
Last edited by Ancient Dragon; Apr 13th, 2009 at 8:55 pm. Reason: add code tags
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 10
Reputation: M^2 is an unknown quantity at this point 
Solved Threads: 0
M^2 M^2 is offline Offline
Newbie Poster

Re: function to read in data and create dynamic array of pointers

 
0
  #10
Apr 13th, 2009
am trying to read in the number of cells
and then read in line by line
and then i have to create dynamic array...
using function.....
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC