Dynamic 2d array not passing?

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

Join Date: Apr 2008
Posts: 27
Reputation: DevC++4.9.9.2 is an unknown quantity at this point 
Solved Threads: 1
DevC++4.9.9.2 DevC++4.9.9.2 is offline Offline
Light Poster

Dynamic 2d array not passing?

 
0
  #1
Nov 2nd, 2008
Hey, im trying to print what is stored in my 2d dynmic array from a file in my print function. However whenever i try to access information in the array, it crashes the program. maybe the array is not being passed back some how, im not sure, any information would be helpful.

In the function getData(), i can output the array.

Attached is all the program files with a test file "data.txt"

Thank you in advance

  1. void getData(int**,int&,int&);
  2. void print(int**,int,int);
  3.  
  4. int main()
  5. {
  6. int **array;
  7. int row, column;
  8. getData(array,row,column);
  9.  
  10. print(array,row,column);
  11.  
  12. system("PAUSE");
  13. return 0;
  14. }
  15.  
  16. void print(int **array, int row, int column)
  17. {
  18. for(int y=0; y < row; y++)
  19. {
  20. cout << endl;
  21. for(int x=0; x < column; x++)
  22. {
  23. cout << array[y][x] << " ";
  24. }
  25. }
  26. cout << endl;
  27. }
  28.  
  29. void getData(int **array, int &row,int &column)
  30. {
  31. string fileName;
  32. ifstream data;
  33. cout << "Enter File Name\n";
  34. getline(cin, fileName);
  35. data.open(fileName.c_str());
  36. if (!data.is_open())
  37. cout << "Error opening file\n";
  38. else
  39. {
  40. data >> row >> column;
  41. array = new int*[column]; //Allocates memory for the array
  42. for (int q = 0; q < column; ++q)
  43. array[q] = new int[row];
  44. for(int y=0; y < column; y++)
  45. {
  46. for(int x=0; x < row; x++)
  47. {
  48. data >> array[y][x];
  49. }
  50. }
  51. }
  52. data.close();
  53. }
Attached Files
File Type: cpp Maze.cpp (1.6 KB, 0 views)
File Type: h stack.h (564 Bytes, 0 views)
File Type: txt data.txt (49 Bytes, 1 views)
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 36
Reputation: freudian_slip is an unknown quantity at this point 
Solved Threads: 4
freudian_slip's Avatar
freudian_slip freudian_slip is offline Offline
Light Poster

Re: Dynamic 2d array not passing?

 
0
  #2
Nov 2nd, 2008
When you are passing row and col to get Data, you are derefenrencing it. But they are of type int. And what does " >> " operator do? I've never seen it used out side of cin >> .


[edit] er nvm i didn't read your code right.
Last edited by freudian_slip; Nov 2nd, 2008 at 8:34 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 27
Reputation: DevC++4.9.9.2 is an unknown quantity at this point 
Solved Threads: 1
DevC++4.9.9.2 DevC++4.9.9.2 is offline Offline
Light Poster

Re: Dynamic 2d array not passing?

 
0
  #3
Nov 2nd, 2008
how am i derefrencing it?
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,677
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: Dynamic 2d array not passing?

 
0
  #4
Nov 2nd, 2008
Your getData( ) function is being passed the int ** BY VALUE. It can allocate memory to that pointer variable, but that allocation only exists within the scope of the function. The int ** in main( ) is still not allocated any memory.

So, to make life interesting, you must pass the int** by reference, like:
  1. void getData( int** & ,int&,int&);

Also, in getData( ), you are using the row and column variables backwards. Do the allocations and reading loops with row as the outer loop, column as the inner loop, as you do in your display function.
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
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