Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 18th, 2012, 9:30 PM   #1
toadead
Newbie
 
Join Date: Jan 2012
Posts: 13
Rep Power: 0 toadead is on a distinguished road
issues of using 2D array

Guys, I am doing a homework assignment in Data Structure class. Basically I need to use a 2D array to simulate linked lists and then use it to construct the STACK with 6 functions: Construction, Empty, Push, Top, Pop, and Display. After finishing 6 functions, then create an empty stack (capacity=25) and then push 10 random value by Push() consecutively. After having 10 elements, run Display() to print out the STACK. Then perform 3 times of Push( ) of random value on valid positions, run Display( ) to print out the list every time you insert a new element. Then perform 3 times of Top() and Pop( ) on valid positions, again, run Display( ) to print our the list every time you delete an element. To visualize the requirement after creating the empty stack, the code should looks like:

Push(rand() % 1000) * 10
Display()
Push(rand() % 1000)
Display()
Push(rand() % 1000)
Display()
Push(rand() % 1000)
Display()
Top()
Pop()
Display
Top()
Pop()
Display
Top()
Pop()
Display
Attachment is the pics to understand the whole idea of using 2D to simulate linked list.
Here is my code, but it shows error when being compiled. I don't know what's wrong with it. Please help me to find the problem with it. Thank you very much.
c++ Syntax (Toggle Plain Text)
  1. //#include <iostream>
  2. using namespace std;
  3.  
  4. int myTop, index, nex = -1;
  5. int value = rand()%1000;
  6. int twoDimentionalArray[25][3], L[25];
  7.  
  8. void construction(int twodimention[25][3], int list[25])
  9. {
  10. myTop = -1;
  11. }
  12.  
  13. void empty()
  14. {
  15. if (myTop == -1)
  16. cout << "The stack is empty.";
  17. else
  18. cout << "The stack is not empty.";
  19. }
  20.  
  21. void push(int value)
  22. {
  23. if (myTop < 24)
  24. {
  25. myTop++;
  26. L[myTop] = value;
  27. index = rand()%25;
  28. L[index][0] = index;
  29. L[index][1] = value;
  30. L[index][2] = nex;
  31. nex = index;
  32. }
  33. else
  34. cout << "The stack is full.";
  35. }
  36.  
  37. void top()
  38. {
  39. if (myTop != -1)
  40. cout << "The top of stack value is: " << L[myTop] << endl;
  41. else
  42. cout << "The stack is empty.";
  43. cout << endl;
  44. }
  45.  
  46. void pop()
  47. {
  48. if (myTop != -1)
  49. {
  50. myTop --;
  51. L[index][1] = -1;
  52. L[index][2] = -1;
  53.  
  54. }
  55.  
  56. }
  57.  
  58. void display()
  59. {
  60. cout << twoDimentionalArray[25][3] << endl;
  61. cout << endl;
  62. }
  63.  
  64. int main()
  65. {
  66. construction(twoDimentionalArray[25][3], L[25]);
  67. for (int i=0; i < 10; i++)
  68. push(value);
  69. display();
  70. for (int j=0; j<3; j++)
  71. {
  72. push(value);
  73. display();
  74. }
  75. for (int k=0; k<3; k++)
  76. {
  77. top();
  78. pop();
  79. display();
  80. }
  81. system ("pause");
  82. }
Attached Files
File Type: doc New Microsoft Office Word Document.doc (28.1 KB, 3 views)
toadead is offline   Reply With Quote
Old Feb 18th, 2012, 9:52 PM   #2
S.I
CS | Math Student
 
S.I's Avatar
 
Join Date: Jan 2009
Location: Belgrade
Posts: 280
Rep Power: 6 S.I is on a distinguished road
Re: issues of using 2D array

1. Uncomment first line - Inclusion of iostream is necessary for cout...
2. You are using rand function which is found in stdlib.h - so include that too
3. L is declared as one dimensional array and you are using it as it was two dimensional
4. Investigate how are arrays passed to the functions!
Hint #1 : This is wrong!
c Syntax (Toggle Plain Text)
  1. construction(twoDimentionalArray[25][3], L[25]);
Hint #2 : Investigate more here http://www.cplusplus.com/doc/tutorial/arrays/

Because this is a homework assignment for now I will not go any further! Fix this, learn, investigate and if you have more errors report back and post updated code... Also it would be nice to post your compiler output if you get some ... Currently your compiler errors are :
g++ pfo.c 
pfo.c: In function ‘void push(int)’:
pfo.c:30: error: invalid types ‘int[int]’ for array subscript
pfo.c:31: error: invalid types ‘int[int]’ for array subscript
pfo.c:32: error: invalid types ‘int[int]’ for array subscript
pfo.c: In function ‘void pop()’:
pfo.c:53: error: invalid types ‘int[int]’ for array subscript
pfo.c:54: error: invalid types ‘int[int]’ for array subscript
pfo.c: In function ‘int main()’:
pfo.c:68: error: invalid conversion from ‘int’ to ‘int (*)[3]’
pfo.c:68: error:   initializing argument 1 of ‘void construction(int (*)[3], int*)’
pfo.c:68: error: invalid conversion from ‘int’ to ‘int*’
pfo.c:68: error:   initializing argument 2 of ‘void construction(int (*)[3], int*)’
mi08247@mi08247-Aspire-5734Z:~$
__________________
--
Artificial Intelligence and Automated Stupidity.
Mathematics is the language of nature.
S.I is offline   Reply With Quote
Old Feb 18th, 2012, 10:16 PM   #3
toadead
Newbie
 
Join Date: Jan 2012
Posts: 13
Rep Power: 0 toadead is on a distinguished road
Re: issues of using 2D array

Thank you very much. Now the updated code can run, but the commend prompt shows wrong output. This is the code:
c++ Syntax (Toggle Plain Text)
  1. //
  2. #include <iostream>
  3. #include <stdlib.h>
  4. using namespace std;
  5.  
  6. int myTop, index, nex = -1;
  7. int value = rand()%1000;
  8. int twoDimentionalArray[25][3], L[25];
  9.  
  10. void construction(int twodimention[25][3], int list[25])
  11. {
  12. myTop = -1;
  13. }
  14.  
  15. void empty()
  16. {
  17. if (myTop == -1)
  18. cout << "The stack is empty.";
  19. else
  20. cout << "The stack is not empty.";
  21. }
  22.  
  23. void push(int value)
  24. {
  25. if (myTop < 24)
  26. {
  27. myTop++;
  28. L[myTop] = value;
  29. index = rand()%25;
  30. twoDimentionalArray[index][0] = index;
  31. twoDimentionalArray[index][1] = value;
  32. twoDimentionalArray[index][2] = nex;
  33. nex = index;
  34. }
  35. else
  36. cout << "The stack is full.";
  37. }
  38.  
  39. void top()
  40. {
  41. if (myTop != -1)
  42. cout << "The top of stack value is: " << L[myTop] << endl;
  43. else
  44. cout << "The stack is empty.";
  45. cout << endl;
  46. }
  47.  
  48. void pop()
  49. {
  50. if (myTop != -1)
  51. {
  52. myTop --;
  53. twoDimentionalArray[index][1] = -1;
  54. twoDimentionalArray[index][2] = -1;
  55.  
  56. }
  57.  
  58. }
  59.  
  60. void display()
  61. {
  62. cout << twoDimentionalArray << endl;
  63. cout << endl;
  64. }
  65.  
  66. int main()
  67. {
  68. construction(twoDimentionalArray, L);
  69. for (int i=0; i < 10; i++)
  70. push(value);
  71. display();
  72. for (int j=0; j<3; j++)
  73. {
  74. push(value);
  75. display();
  76. }
  77. for (int k=0; k<3; k++)
  78. {
  79. top();
  80. pop();
  81. display();
  82. }
  83. system ("pause");
  84. }
toadead is offline   Reply With Quote
Old Feb 18th, 2012, 10:38 PM   #4
S.I
CS | Math Student
 
S.I's Avatar
 
Join Date: Jan 2009
Location: Belgrade
Posts: 280
Rep Power: 6 S.I is on a distinguished road
Re: issues of using 2D array

Common man look at the output of your program when run:
mi08247@mi08247-Aspire-5734Z:~$ ./a.out 
0x804a100

0x804a100

0x804a100

0x804a100

The top of stack value is: 383

0x804a100

The top of stack value is: 383

0x804a100

First of all I can't open that .doc document! I am a linux user and open office states it is corrupted and can't open it! Try better! Also my help was to get your code running you are the one behind the idea and considering that this is a homework assignment don't expect more help! The compiler issues - Yes we can help, the logic - only suggestions not the solution!
__________________
--
Artificial Intelligence and Automated Stupidity.
Mathematics is the language of nature.
S.I is offline   Reply With Quote
Old Feb 20th, 2012, 4:39 PM   #5
toadead
Newbie
 
Join Date: Jan 2012
Posts: 13
Rep Power: 0 toadead is on a distinguished road
Re: issues of using 2D array

Here is the new code that I wrote, but the result shows same things instead of giving random numbers. Please tell what is wrong with my code. Thank you
//#include <iostream>
using namespace std;

int myTop, index, nex = -1;
int twoDimentionalArray[25][3], L[25];

void construction(int twodimention[25][3], int list[25])
{
	myTop = -1;
	for(int p=0; p<25;p++)
		twoDimentionalArray[p][2] = -1;
}

void empty()
{
	if (myTop == -1)
		cout << "The stack is empty.";
	else
		cout << "The stack is not empty.";
}

void push(int value)
{
	if (myTop < 24)
	{	
		myTop++;
		L[myTop] = value;
		index = rand()%25;
		twoDimentionalArray[index][0] = index;
		twoDimentionalArray[index][1] = value;
		twoDimentionalArray[index][2] = nex;
		nex = index;
	}
	else
		cout << "The stack is full.";		
}

void top()
{
	if (myTop != -1)
		cout << "The top of stack value is: " << L[myTop];
	else
		cout << "The stack is empty.";
	cout << endl;
}

void pop()
{
	if (myTop != -1)
	{
		myTop --;
		twoDimentionalArray[index][2] = -1;
	}
		
}

void display()
{
	for (int row=0; row<25; row++)
	{
		for (int column=0; column<3; column++)
		{
			cout << twoDimentionalArray[row][column] << "\t";
			if (column == 2)
				cout << endl;
		}
	}
	cout << endl;
}

int main()
{
	construction(twoDimentionalArray, L);
	for (int i=0; i < 10; i++)
		push(rand() % 1000);
	display();
	for (int j=0; j<3; j++)
	{
		push(rand() % 1000);
		display();
	}
	for (int k=0; k<3; k++)
	{
		top();
		pop();
		display();
	}
	system ("pause");
}
toadead is offline   Reply With Quote
Old Feb 21st, 2012, 8:02 AM   #6
S.I
CS | Math Student
 
S.I's Avatar
 
Join Date: Jan 2009
Location: Belgrade
Posts: 280
Rep Power: 6 S.I is on a distinguished road
Re: issues of using 2D array

I can't stress enough the usage of simple cout or printf in debugging purposes!!!
If you just print out the index and value you will see the problem ...

c Syntax (Toggle Plain Text)
  1. void push(int value)
  2. {
  3. if (myTop < 24)
  4. {
  5. myTop++;
  6. L[myTop] = value;
  7. index = rand()%25;
  8.  
  9. //Printing the values
  10. cout <<"Debug- Index " << index <<endl;
  11. cout <<"Debug- Value " << value <<endl;
  12.  
  13. twoDimentionalArray[index][0] = index;
  14. twoDimentionalArray[index][1] = value;
  15. twoDimentionalArray[index][2] = nex;
  16. nex = index;
  17. }
  18. else
  19. cout << "The stack is full.";
  20. }

Now when run the program outputs:
mi08247@mi08247-Aspire-5734Z:~$ ./a.out
Debug- Index 11
Debug- Value 383
Debug- Index 2
Debug- Value 383
Debug- Index 15
Debug- Value 383
Debug- Index 18
Debug- Value 383
Debug- Index 10
Debug- Value 383
Debug- Index 11
Debug- Value 383
Debug- Index 17
Debug- Value 383
Debug- Index 24
Debug- Value 383
Debug- Index 21
Debug- Value 383
Debug- Index 12
Debug- Value 383
0x804a100

Debug- Index 2
Debug- Value 383
0x804a100

Debug- Index 15
Debug- Value 383
0x804a100

Debug- Index 9
Debug- Value 383
0x804a100

The top of stack value is: 383

0x804a100

The top of stack value is: 383

0x804a100

The top of stack value is: 383

0x804a100

So as you see you are constantly pushing the same value! Index is changing and Value is not ? Why ? ... Well because index is modified in the push function and value is declared and initialised as a global variable... so what do you need to do ???
__________________
--
Artificial Intelligence and Automated Stupidity.
Mathematics is the language of nature.
S.I is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
2D array question(s) jzou91 C 2 Mar 14th, 2011 8:34 PM
Need help searching 2D array jzou91 C 12 Mar 12th, 2011 8:13 AM
How to handle 2D array class? thanks. everid C++ 8 Oct 27th, 2009 4:27 PM
Looking diagonally in a 2D array for a string peaceofpi C 3 May 25th, 2009 9:43 PM
Problem with keeping contents of a 2D Array neslolz C++ 3 May 3rd, 2009 10:25 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:46 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC