Bool search

I'm having a heck of a time getting my last function to work. I need to ask the user to input an integer and search my array and if it exists in array I need to output true and if it's not in the array output false. nothing outputs as it sits... help

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
#include <iostream>
#include <fstream>


using namespace std;

void ReadFromFile(int arrayF[]);
void Print(int arrayA[]);
void Copy(int arrayA[], int arrayB[],int& num);
int Find80To100(int arrayA[]);
int FindDivisibleBy5(int arrayA[]);
void PrintIndexDivisibleBy5(int arrayA[]);
float FindAverage(int arrayA[],int& sum,float& avg);
int FindMin(int arrayA[],int min);
bool Search(int arrayA[],int x);

int main()
{
	int arrayA[10];
	int arrayB[10];
	int min=INT_MAX;
	int num=10;
	int sum = 0;
	float avg=0;
	int x=0;
	
	
	ReadFromFile(arrayA);
	Print(arrayA);
	Copy(arrayA, arrayB, num);
	Print(arrayB);
	Find80To100(arrayA);
	FindDivisibleBy5(arrayA);
	PrintIndexDivisibleBy5(arrayA);
	FindAverage(arrayA, sum, avg);
	FindMin(arrayA, min);
		
	cout << "Please enter an integer:";
	cin >> x;
	Search(arrayA,x);
	

	system("pause");
}
void ReadFromFile(int arrayA[])
{
	ifstream fin;
	fin.open("data.txt");
	for (int i = 0; i < 10; i++)	
		fin >> arrayA[i];			
}
void Copy(int arrayA[], int arrayB[],int& num)
{
	for (int i=0;i <= num; i++)
		arrayB[num-i-1] = arrayA[i];

}

void Print(int arrayA[])
{
	for (int i = 0; i < 10; i++)	
		cout << arrayA[i]<<"\t";
		cout << endl;	
}
int Find80To100(int arrayA[])
{
	for (int i = 0; i < 10; i++)
	if ((arrayA[i] >= 80) && (arrayA[i] <= 100))
	cout << arrayA[i]<< "\t";
	cout << "\n" <<endl;
	return 0;
}
int FindDivisibleBy5(int arrayA[])
{
	for (int i = 0; i < 10; i++)
	if ((arrayA[i] % 5) == 0)
		cout << arrayA[i] << "\t";
		cout << "\n" << endl;
	return 0;
}
void PrintIndexDivisibleBy5(int arrayA[])
{
	for (int i = 0; i < 10; i++)
	if ((arrayA[i] % 5) == 0)
		cout << i << "\t";
	    cout << "\n" << endl;		 
}
float FindAverage(int arrayA[],int& sum,float& avg)
{
	for (int i = 0; i < 10; i++)
		sum += arrayA[i];
	    avg = sum / 10;
	    cout << avg << "\n"<< endl;
		return 0;
}
int FindMin(int arrayA[],int min)
{
	for (int i = 0; i < 10; i++)
	if (arrayA[i] < min)
		min = arrayA[i];
	    cout << min <<"\n" << endl; 
	    return 0;
}
bool Search(int arrayA[], int x)
{	
	for (int i = 0; i < 10; i++)
	{
		if (x == arrayA[i])
			return true;

			return false;
	}	
}
closed account (2AoiNwbp)
in line 111 you return false before checking the whole array
Ok I took it out of the statement and after I input integer nothing happens I need to see either true/false?
I've updated a little still no dice...

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

#include <iostream>
#include <fstream>


using namespace std;

void ReadFromFile(int arrayF[]);
void Print(int arrayA[]);
void Copy(int arrayA[], int arrayB[],int& num);
int Find80To100(int arrayA[]);
int FindDivisibleBy5(int arrayA[]);
void PrintIndexDivisibleBy5(int arrayA[]);
float FindAverage(int arrayA[],int& sum,float& avg);
int FindMin(int arrayA[],int min);
bool Search(int arrayA[],int x);

int main()
{
	int arrayA[10];
	int arrayB[10];
	int min=INT_MAX;
	int num=10;
	int sum = 0;
	float avg=0;
	int x=0;
	
	
	ReadFromFile(arrayA);
	Print(arrayA);
	Copy(arrayA, arrayB, num);
	Print(arrayB);
	Find80To100(arrayA);
	FindDivisibleBy5(arrayA);
	PrintIndexDivisibleBy5(arrayA);
	FindAverage(arrayA, sum, avg);
	FindMin(arrayA, min);
	Search(arrayA,x);
	

	system("pause");
}
void ReadFromFile(int arrayA[])
{
	ifstream fin;
	fin.open("data.txt");
	for (int i = 0; i < 10; i++)	
		fin >> arrayA[i];			
}
void Copy(int arrayA[], int arrayB[],int& num)
{
	for (int i=0;i <= num; i++)
		arrayB[num-i-1] = arrayA[i];

}

void Print(int arrayA[])
{
	for (int i = 0; i < 10; i++)	
		cout << arrayA[i]<<"\t";
		cout << endl;	
}
int Find80To100(int arrayA[])
{
	for (int i = 0; i < 10; i++)
	if ((arrayA[i] >= 80) && (arrayA[i] <= 100))
	cout << arrayA[i]<< "\t";
	cout << "\n" <<endl;
	return 0;
}
int FindDivisibleBy5(int arrayA[])
{
	for (int i = 0; i < 10; i++)
	if ((arrayA[i] % 5) == 0)
		cout << arrayA[i] << "\t";
		cout << "\n" << endl;
	return 0;
}
void PrintIndexDivisibleBy5(int arrayA[])
{
	for (int i = 0; i < 10; i++)
	if ((arrayA[i] % 5) == 0)
		cout << i << "\t";
	    cout << "\n" << endl;		 
}
float FindAverage(int arrayA[],int& sum,float& avg)
{
	for (int i = 0; i < 10; i++)
		sum += arrayA[i];
	    avg = sum / 10;
	    cout << avg << "\n"<< endl;
		return 0;
}
int FindMin(int arrayA[],int min)
{
	for (int i = 0; i < 10; i++)
	if (arrayA[i] < min)
		min = arrayA[i];
	    cout << min <<"\n" << endl; 
	    return 0;
}
bool Search(int arrayA[], int x)
{	
	cout << "Please enter an integer:";
	cin >> x;
	for (int i = 0; i < 10; i++)
	{
		{
			if (x == arrayA[i]) 
			return true;
		}
		
	}	
	return false;
}
closed account (2AoiNwbp)
I think you should return false, but after closing right brace of the for loop, not before.
so

1
2
3
4
5
6
7
8
9
10
11
12
13
14
bool Search(int arrayA[], int x)
{	
	cout << "Please enter an integer:";
	cin >> x;
	for (int i = 0; i < 10; i++)
	{
		{
			if (arrayA[i]==x) 
			return true;
		}
		return false;
	}	
	
}
I get Please enter an integer and say I enter 26 it doesn't display true or false??
No. You had it correct in the previous post.

The { } on lines 108 and 111 are unneeded.
closed account (2AoiNwbp)
Why did you add this
1
2
	cout << "Please enter an integer:";
	cin >> x;

in your code?
ok this is what I have now it's still not doing anything...
1
2
3
4
5
6
7
8
9
10
11
12

bool Search(int arrayA[], int x)
{	
	cout << "Please enter an integer:";
	cin >> x;
	for (int i = 0; i < 10; i++)
		{
			if (arrayA[i]==x) 
			return true;
		}
		return false;	
}
because I have to ask the user to input an integer and I have to take that integer and serch my array? it's my last function
closed account (2AoiNwbp)
This is your function (without previous lines 4 and 5)
1
2
3
4
5
6
7
8
9
bool Search(int arrayA[], int x)
{	
	for (int i = 0; i < 10; i++)
	{
		if (arrayA[i]==x) 
		return true;
	}
	return false;	
}
Last edited on
closed account (2AoiNwbp)
because I have to ask the user to input an integer and I have to take that integer and serch my array? it's my last function

But you already asked it in main, at lines 38 and 39. Why would you do that in the "search" function??
Last edited on
it's still not giving me true/false????????



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
#include <iostream>
#include <fstream>


using namespace std;

void ReadFromFile(int arrayF[]);
void Print(int arrayA[]);
void Copy(int arrayA[], int arrayB[],int& num);
int Find80To100(int arrayA[]);
int FindDivisibleBy5(int arrayA[]);
void PrintIndexDivisibleBy5(int arrayA[]);
float FindAverage(int arrayA[],int& sum,float& avg);
int FindMin(int arrayA[],int min);
bool Search(int arrayA[],int x);

int main()
{
	int arrayA[10];
	int arrayB[10];
	int min=INT_MAX;
	int num=10;
	int sum = 0;
	float avg=0;
	int x=0;
	
	
	ReadFromFile(arrayA);
	Print(arrayA);
	Copy(arrayA, arrayB, num);
	Print(arrayB);
	Find80To100(arrayA);
	FindDivisibleBy5(arrayA);
	PrintIndexDivisibleBy5(arrayA);
	FindAverage(arrayA, sum, avg);
	FindMin(arrayA, min);	
	cout << "Please enter an integer:";
	cin >> x;
	Search(arrayA, x);
	
	

	system("pause");
}
void ReadFromFile(int arrayA[])
{
	ifstream fin;
	fin.open("data.txt");
	for (int i = 0; i < 10; i++)	
		fin >> arrayA[i];			
}
void Copy(int arrayA[], int arrayB[],int& num)
{
	for (int i=0;i <= num; i++)
		arrayB[num-i-1] = arrayA[i];

}

void Print(int arrayA[])
{
	for (int i = 0; i < 10; i++)	
		cout << arrayA[i]<<"\t";
		cout << endl;	
}
int Find80To100(int arrayA[])
{
	for (int i = 0; i < 10; i++)
	if ((arrayA[i] >= 80) && (arrayA[i] <= 100))
	cout << arrayA[i]<< "\t";
	cout << "\n" <<endl;
	return 0;
}
int FindDivisibleBy5(int arrayA[])
{
	for (int i = 0; i < 10; i++)
	if ((arrayA[i] % 5) == 0)
		cout << arrayA[i] << "\t";
		cout << "\n" << endl;
	return 0;
}
void PrintIndexDivisibleBy5(int arrayA[])
{
	for (int i = 0; i < 10; i++)
	if ((arrayA[i] % 5) == 0)
		cout << i << "\t";
	    cout << "\n" << endl;		 
}
float FindAverage(int arrayA[],int& sum,float& avg)
{
	for (int i = 0; i < 10; i++)
		sum += arrayA[i];
	    avg = sum / 10;
	    cout << avg << "\n"<< endl;
		return 0;
}
int FindMin(int arrayA[],int min)
{
	for (int i = 0; i < 10; i++)
	if (arrayA[i] < min)
		min = arrayA[i];
	    cout << min <<"\n" << endl; 
	    return 0;
}
bool Search(int arrayA[], int x)
{	
	for (int i = 0; i < 10; i++)
		{
		if (arrayA[i] == x)
			return true;			
		}
	return false;	
}
closed account (2AoiNwbp)
So, what`s the output?
you don't have any output statements in main after calling search...
Last edited on
it prints all my other functions perfect it then asks me to input an integer I put 26 then it just says press any key to continue?
closed account (2AoiNwbp)
Ok, put the following code after cin << x;
1
2
3
4
if(Search(arrayA, x))
    cout << x << " is in the list" << endl;
else
    cout << x << " is not in the list" << endl;


regards,
Alejandro
Last edited on
Alejandro thank you sir!
closed account (2AoiNwbp)
you're welcome!
Topic archived. No new replies allowed.