What am I doing wrong?

Hey guys! I'm trying to make a deque in C++. According to dry run, my code is right and the function must first enter 10 from the right of the array, then 60 and 70 towards the right. Then it should delete 70. After that, it should insert 40, then 80 and after that 20 towards the left. Then it should delete 20.

So the output of the array according to my calculation should be 60, 0, grabage_value, 0, 80, 40, 10.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <iostream>
#include <conio.h>

using namespace std;
		
int right_insert(int item, int deque[], int right, int left)
{
	if((right % 6) + 1 == left)
	{
		cout<<"Deque Overflow!\n";		
	}
			
	if(right == -1 && left == -1)
	{
		right = left = 6;
	}
			
	else if(right == 6)
	{
		right = 0;
	}
			
	else
	{
		right++;
	}
	
	deque[right] = item;
	return right;
	return left;	
}

int right_delete(int item, int deque[], int right, int left)
{
	if(right == -1 && left  == -1)
	{
		cout<<"Deque Underflow!\n";
	}
	
	deque[right] = 0;
	
	if(right == left)
	{
		right = left = -1;
	}
	
	else if(right == 6)
	{
		right = 0;
	}	
	
	else 
		right--;
	
	
	return right;
	return left;
}

int left_insert(int item, int deque[], int right, int left)
{
	if((right % 6) + 1 == left)
	{
		cout<<"Deque Overflow!\n";
	}
			
	if(right == -1 && left == -1)
	{
		right = left = 0;
	}
			
	else if(left == 0)
	{
		left = 6;
	}
			
	else
	{
		left--;
	}
	
	deque[left] = item;
	return right;
	return left;	
}

int left_delete(int item, int deque[], int right, int left)
{
	if(right == -1 && left == -1)
	{
		cout<<"Deque Underflow!\n";
	}
	
	deque[left] = 0;
	
	if(left == right)
	{
		right = left = 0;
	}
	
	else if(left == 0)
	{
		left = 6;
	}	
	
	else 
		left++;
		
	return right;
	return left;
}


int main()
{
	int item, deque[7], right = -1, left = -1;
	right_insert(10, deque, right, left);
	right_insert(60, deque, right, left);
	right_insert(70, deque, right, left);
	right_delete(item, deque, right, left);
	left_insert(10, deque, right, left);
	left_insert(60, deque, right, left);
	left_insert(70, deque, right, left);
	left_delete(item, deque, right, left);
	
	cout<<"DSIPLAY\n";
	for(int x = 0; x < 7; x++ )
	{
		cout<<deque[x]<<endl;
	}
	_getch();
	return 0;
}
Last edited on

What stands out to me is that your functions return a integer yet your main() function is not actually using it.

For example:

result = right_insert(10, deque, right, left);

whatever is returned from the function "right_insert" would be stored in "result"

Another thing which hit me was you are trying to use a uninitialised variable at line 120 and 124.


EDIT:

I've had a little more time to take a look more at your code.. your functions are set to return an integer yet you are also trying to return two (left and right).. basically what will happen is as soon as it reaches the first return it will exit returning "right" and totally ignoring left

I'll have a more detailed look when I return home.
Last edited on
Hmm... the output improved quite a bit. Thanks.

But it ain't perfect yet. Array is displayed as 60, 0, garbage, 0, garbage, 0, 10.

Any more pearls of wisdom anyone? Thanks a lot in advance! :)

EDIT: I deleted the one return statement in each function. Each function now returns only one integer. But the output is the same.
Last edited on

ok.. as I explained in my edited post you cant return multiple values from a function unless your returning something such as a structure. In your code it would only allow you to return one integer.

To return multiple values using a structure you could do something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
using namespace std;

// create a structure that we can use to
// either pass or return from a function.
typedef struct {
	int left;
	int right;
} myStruct;

// function declaration
myStruct Test(int right, int left);

int main()
{
	// declare a structure to hold our return values
	myStruct ret;

	ret = Test(10, 5);

	// test we have the correct values
	cout << "Left was: " << ret.left << endl;
	cout << "Right was: " << ret.right << endl;
	return 0;
}

myStruct Test(int right, int left)
{
	// declare a structure so we can populate
	// and return its value back to the caller.
	myStruct ret;

	// for simplicity I will just add something whatever
	// was passed to the function and return both values
	// back to the caller (main in this case)

	// adjust what was passed to me
	right += 22;
	left += 5;

	// populate my structure
	ret.left = left;
	ret.right = right;
	
	// return it.
	return ret;

}


So as it stands your original functions are only returning one value and not the left and right integers you intended.

Last edited on

ok, back home... here you go with the example i provided..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <iostream>
#include <conio.h>

using namespace std;

typedef struct {
	int left;
	int right;
} myStruct;


myStruct right_insert(int item, int deque[], int right, int left)
{
	myStruct ret;

	if ((right % 6) + 1 == left)
		cout << "Deque Overflow!\n";

	if (right == -1 && left == -1)
		right = left = 6;
	else if (right == 6)
		right = 0;
	else
		right++;

	deque[right] = item;
	
	ret.left = left;
	ret.right = right;

	return ret;

}

myStruct right_delete(int item, int deque[], int right, int left)
{

	myStruct ret;

	if (right == -1 && left == -1)
		cout << "Deque Underflow!\n";

	deque[right] = 0;

	if (right == left)
		right = left = -1;
	else if (right == 6)
		right = 0;
	else
		right--;

	ret.left = left;
	ret.right = right;

	return ret;

}

myStruct left_insert(int item, int deque[], int right, int left)
{

	myStruct ret;

	if ((right % 6) + 1 == left)
		cout << "Deque Overflow!\n";

	if (right == -1 && left == -1)
		right = left = 0;
	else if (left == 0)
		left = 6;
	else
		left--;

	deque[left] = item;

	ret.left = left;
	ret.right = right;

	return ret;

}

myStruct left_delete(int item, int deque[], int right, int left)
{

	myStruct ret;

	if (right == -1 && left == -1)
		cout << "Deque Underflow!\n";

	deque[left] = 0;

	if (left == right)
		right = left = 0;
	else if (left == 0)
		left = 6;
	else
		left++;

	ret.left = left;
	ret.right = right;

	return ret;

}


int main()
{
	myStruct ret;

	int item = 0, deque[7], right = -1, left = -1;

	ret = right_insert(10, deque, right, left);
	ret = right_insert(60, deque, ret.right, ret.left);
	ret = right_insert(70, deque, ret.right, ret.left);
	ret = right_delete(item, deque, ret.right, ret.left);
	ret = left_insert(40, deque, ret.right, ret.left);
	ret = left_insert(80, deque, ret.right, ret.left);
	ret = left_insert(20, deque, ret.right, ret.left);
	ret = left_delete(item, deque, ret.right, ret.left);			

	cout << "DSIPLAY\n\n";

	for (int x = 0; x < 7; x++)
		cout << deque[x] << endl;

	_getch();
	return 0;
}



Edit: Just for information purposes, the last call (left_delete) we don't need its return value since we are not relying on another call to use its left and right values. You could have left out the ret = but it does no harm to leave it there.

Last edited on
Topic archived. No new replies allowed.