linear search

closed account (4jzvC542)
Here is given task in my textbook

write a program to find out whether data entered by user is present in
one dimensional array of following 10 elements, using linear search.

          1, 5, 21, 16, 49, 35, 87, 63, 90, 6


on this i made following program :

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
#include<iostream>
#include<conio.h>

using namespace std;

int main()
{
	
	int a[10], n, x, i, flag = 0;
	

	int a[i] = {1, 2, 3, 4, 5, 6, 7 ,8, 9. 10};


	cout << "\nEnter Element to search:";
	cin >> x;
	for (i = 0;i < n;++i)
	{
		if (a[i] == x)
		{
			flag = 1;
			break;
		}
	}
	if (flag)
		cout << "\nElement is Found at position " << i + 1;
	else
		cout << "\nElement not found";
	return 0;
}



but this code doesn't work and i get errors
I am using MinGW GCC compiler of Quincy 2005
can someone tell me where i am wrong
You are defining two arrays with the same name

int a[10], n, x, i, flag = 0;


int a[i] = {1, 2, 3, 4, 5, 6, 7 ,8, 9. 10};

one of which has 10 elements and the other has undefined number of elements because variable i 1) is not a const expression and 2) was not initialized.
closed account (4jzvC542)
@vlad from moscow

ya i forgot that..........

so i removed it and now my code is :

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

#include<iostream>
#include<conio.h>

using namespace std;

int main()
{
	
	int n, x, i, flag = 0;
	

	int a[10] = {1, 2, 3, 4, 5, 6, 7 ,8, 9. 10};//or int a[i] ={10 elements};


	cout << "\nEnter Element to search:";
	cin >> x;
	for (i = 0;i < n;i++)
	{
		if (a[i] == x)
		{
			flag = 1;
			break;
		}
	}
	if (flag)
		cout << "\nElement is Found at position " << i + 1;
	else
		cout << "\nElement not found";
	return 0;
}


however it still shows errors
i would paste errors here but my ide Quincy 2005 does not let me copy

some of them are:

"type conservation << before token
invalid constructor and destructor expected
extra ;"

i want to know is my linear search algorithm ok would it work?
please help
parjanya
You forgot to replace that period with a comma.... On line 13.
It is the common approach to return an index or a pointer to an element beyond the array if the target element was not found. Also you should not declare variables before their usage. So I would rewrite your code the following way


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

using namespace std;

int main()
{
	const int N = 10;
	int a[N] = { 1, 5, 21, 16, 49, 35, 87, 63, 90, 6 };


	cout << "\nEnter Element to search: ";

	int x = 0;
	cin >> x;

	int i = 0;
	while ( ( i < N ) && !( a[i] == x )  ) ++i;

	if ( i != N )
		cout << "\nElement is Found at position " << i + 1;
	else
		cout << "\nElement not found";

	return 0;
}



There is standard algorithm std::find that does all the work in you program. With its using the program could be rewritten as

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
#include<iostream>
#include <algorithm>
#include <iterator>

using namespace std;

int main()
{
	const int N = 10;
	int a[N] = { 1, 5, 21, 16, 49, 35, 87, 63, 90, 6 };


	cout << "\nEnter Element to search: ";

	int x = 0;
	cin >> x;

	int *p = find( a, a + N, x );
 
	if ( p != a + N )
		cout << "\nElement is Found at position " << distance( a, p ) + 1;
	else
		cout << "\nElement not found";

	return 0;
}

Last edited on
closed account (4jzvC542)
@Daleth
yes.... it was a really stupid error and compilation was sucessfull and program ran fine

thaks a lot


however there is a new problem that i really can't understand

the thing is that if i enter any number to be searched in given list
it shows fine that "That element found at respected position..........

however

when i enter a nuber that is not in list say 55000,

it should say element not found instead all the time it says
element found at position 11,
and this is true for all numbers that are not present

is something wrong with loop
please help
parjanya
closed account (4jzvC542)
@vlad from moscow

words kind describe joy i am feeling............. thanks a lot friend
really thanks

about the program with header files
<algorithm>
<iterator>

i dont really know these headers but will be hell bent on learning them
thanks you very much

yours in debt,
Parjanya
Topic archived. No new replies allowed.