Function Output Bool Errors

My functions testArray and searchArray only seem to have one possible output. The output of testArray is always "Duplicate elements present", and the output of searchArray is always 0. Each finction is supposed to have two possible outputs as you can see below. The output is supposed to depend on the value of a boolean variable that I have decalred in each function. I have included the code of each function as well as the the overall program below. Please help, as I am not sure how to fix this problem. Thank you.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void testArray(int numArray[], int & SIZE)
{
	assert(SIZE <= CAPACITY);

	bool duplicatePresent = false;
	
	do
	{
 		for (int i = 0; i < SIZE; i++)
		{
			for (int j = 0; j < SIZE; j++)
			{	
				if (i == j)
					duplicatePresent = true;
			}
		} 
	}
	while(duplicatePresent == false);

	if (duplicatePresent == true)
		cout << "Duplicate elements present." << endl;
	else
		cout << "No duplicate elements present." << endl;
}



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
int searchArray(int numArray[], int value, int & SIZE)
{
	assert(SIZE <= CAPACITY);

	bool valuePresent = false;
	int indexPosition = 0;	

	do
	{
		for (int i = 0; i < SIZE; i++)
		{
			if (numArray[i] == value)
				indexPosition = i;
				valuePresent = true;
		}
		
	}
	while (valuePresent == false);
	if (valuePresent = true)
		cout << indexPosition << endl;
	else
		cout << "-l" << endl;
		

}



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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
 

#include <iostream>
#include <assert.h>
using namespace std;

const int CAPACITY = 20;


/*
This function displays the array up
to the point at which it is filled.
@param. numArray[]: the array to be displayed
@param. SIZE: the number of elements contained in the array
precondition: SIZE <= CAPACITY
postcondition: the array is displayed
*/
void displayArray(int numArray[], int SIZE)
{
	assert(SIZE <= CAPACITY);

        cout << "the contents of your array are: " << endl;

        for (int i = 0; i < SIZE; i++)
        {
                cout << numArray[i] << " ";
        }
        cout << endl;
}

/*
This function tests the array to
see if it contains any duplicate
values and tells the user whether
they are.
@param. numArray[]: the array to be tested
@param. SIZE: the number of elements in the array
precondition: SIZE <= CAPACITY
postcondition: the program will tell the 
user that either there are duplicate values
or that there are not.
*/
void testArray(int numArray[], int & SIZE)
{
	assert(SIZE <= CAPACITY);

	bool duplicatePresent = false;
	
	do
	{
 		for (int i = 0; i < SIZE; i++)
		{
			for (int j = 0; j < SIZE; j++)
			{	
				if (i == j)
					duplicatePresent = true;
			}
		} 
	}
	while(duplicatePresent == false);

	if (duplicatePresent == true)
		cout << "Duplicate elements present." << endl;
	else
		cout << "No duplicate elements present." << endl;
}


/*
This function checks each element of
the array to see if any of them match 
a given input parameter called "value"
@param. numArray[]: the array to be searched
@param. value: the integer value to be searched for
@param. SIZE: the number of elements in the array
precondition: value is an integer value
precondition: SIZE <= CAPACITY
postcondition: the function either will output
the index of the desired value or "-l" to 
indicate that no such value is present. 
*/
int searchArray(int numArray[], int value, int & SIZE)
{
	assert(SIZE <= CAPACITY);

	bool valuePresent = false;
	int indexPosition = 0;	

	do
	{
		for (int i = 0; i < SIZE; i++)
		{
			if (numArray[i] == value)
				indexPosition = i;
				valuePresent = true;
		}
		
	}
	while (valuePresent == false);
	if (valuePresent = true)
		cout << indexPosition << endl;
	else
		cout << "-l" << endl;
		

}

/*
This function deletes an element at a desired
index from the array and replaces it with an 
element from another index in the array.
@param. numArray[]: the array from which an
element will be deleted
@param. SIZE: the number of elements in the array
@param. index: the desired index whose element
will be deleted
precondition: SIZE <= CAPACITY
postcondition: The desired element will be deleted
and the positions of the other elements will
be adjusted to accomodate.
*/
void deleteArrayElement(int numArray[], int & SIZE, int index)
{
	assert(SIZE <= CAPACITY);

	numArray[index] = numArray[SIZE - 1];
}

/*
This function inserts or appends a new 
element into the array.
@param. numArray[]: the array to which a new 
element will be added or appended
@param. SIZE: the number of elements in the array
@param. value: the desired value to be added
into the array as a new element
@param. index: the desired location at which
the new element is to be inserted
precondition: SIZE <= CAPACITY
precondition: index <= SIZE
postcondition: a new value will be added to the
array, and the positions of the other elements
may be adjusted to accomodate
*/
int insertValueIntoArray (int numArray[], int & SIZE, int value, int index)
{
	assert(SIZE <= CAPACITY);
	assert(index <= SIZE);

	numArray[index] = value;
}





int main()
{
	int numArray[CAPACITY]; //an int array with a given capacity
	int SIZE = 0; //the array is initially empty
	int counter = 0;
	int i = 0;

	cout << "Welcome to the program." << endl;
	cout << "Enter a sequence of integer values" << endl;
	cout << "seperated by space" << endl;
	cout << "and ended with -1" << endl;
	
	for (i = 0; i < CAPACITY; i++)
	{
		cin >> numArray[i];
		SIZE++;	
		if (numArray[i] == -1)
		{
			SIZE--;
			break;
		}
	}
	

	displayArray(numArray, SIZE);
	
	testArray(numArray, SIZE);

	searchArray(numArray, 50, SIZE);

	insertValueIntoArray(numArray, SIZE, 216, SIZE);
	displayArray(numArray, SIZE);

	insertValueIntoArray(numArray, SIZE, 90, 3);
	displayArray(numArray, SIZE);
	
	deleteArrayElement(numArray, SIZE, 0);
	deleteArrayElement(numArray, SIZE, 3);	

	displayArray(numArray, SIZE);


	return 0;
}

*sigh*

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void testArray(int numArray[], int & SIZE)
{
	assert(SIZE <= CAPACITY);

	bool duplicatePresent = false;
	
	do
	{
 		for (int i = 0; i < SIZE; i++)
		{
			for (int j = 0; j < SIZE; j++)
			{	
				if (i == j) // at the start of these loops, i = 0 and j = 0. That means i == j.
					duplicatePresent = true;
			}
		} 
	}
	while(duplicatePresent == false); // this will loop until duplicatePresent is true. IT WILL ONLY EXIT THE LOOP IF duplicatePresent IS TRUE.

	if (duplicatePresent == true)
		cout << "Duplicate elements present." << endl;
	else
		cout << "No duplicate elements present." << endl;
}
Topic archived. No new replies allowed.