Linear Search in array

Hye guys! I need your help in this code. It's not working.

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
  P#include <iostream>
using namespace std;
int main ()
{
	int x,arr[x],key;
	cout << "Enter the number of elements in array" <<endl;
    cin >> x;
    cout  << "Enter the elements in array" <<endl;
	for (int i=0 ;i <x; i++)
	{
	cin >> arr[i];
    }
	cout << "Enter element to find" <<endl;
	cin >> key;
	for (int j=0;j<x;j++)
	{
	
	if (arr[j]==key)
	{
		  cout << "found value at index" << j <<endl;
    }
		else 

	  cout  << "value not found" <<endl;

}

}ut the code you need help with here.
not working

Does not tell enough to us. What do you mean by it?

* Compilation fails with error?
* Program crashes?
* Program produces unexpected result?

Can you answer these?
1
2
3
4
5
6
int main() {
  int foo;
  // what is the value of foo now?
  int bar [foo];
  // how many elements does the array bar have?
}

The compiler warns:
warning: 'foo' is used uninitialized in this function [-Wuninitialized]


The compiler has to generate instructions that allocate (stack) memory for the array. It has to know how much, already during compilation. What is the value that you tell to compiler?
Hello sallu110,

Along with what keskiverto has said. What error messages did you receive?

Your problem starts with the fact that "x" is uninitialized. Do you have any idea what value it holds? You may want to find out to help understand why "arr[x]" would not likely work. Then there is the point that you are using "x" before you give it a value. Then most of the time "arr[x]" is not allowed with the newer compilers. There are some older compilers that do allow this, but I would not get use to counting on it.

A "std::vector would be a better choice as it does not need a size to start with.

I offer this suggestion to fix your code as one possibility:
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
#include <iostream>

//using namespace std;  // <--- Best not to use.

int main()
{
	constexpr size_t MAXARRSIZE{ 20 };

	int numEllements{}, arr[MAXARRSIZE], key{};

	std::cout << "Enter the number of elements in array (max 20): ";
	std::cin >> numEllements;

	std::cout << "Enter the elements in array:" << std::endl;

	for (int i = 0; i < numEllements; i++)
	{
		std::cout << "\n Element " << i + 1 << " : ";
		std::cin >> arr[i];
	}

	std::cout << "\n Enter element to find: ";
	std::cin >> key;

	for (int j = 0; j < numEllements; j++)
	{

		if (arr[j] == key)
		{
			std::cout << " found value at index " << j << std::endl;
		}
		else

			std::cout << "  value not found" << std::endl;

	}
	// <--- Used mostly for testing in Debug mode. Removed if compiled for release.
	// <--- Used to keep the console window open in Visual Studio Debug mode.
	// The next line may not be needid. If you have to press enter to see the prompt it is not needed.
	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
	std::cout << "\n\n Press Enter to continue";
	std::cin.get();

	return 0;

}


I changed the output a little to produce this:

Enter the number of elements in array (max 20): 4
Enter the elements in array:

 Element 1 : 10

 Element 2 : 20

 Element 3 : 30

 Element 4 : 40

 Enter element to find: 30
  value not found
  value not found
 found value at index 2
  value not found


 Press Enter to continue


If you have learned about vectors let me know and I will come up with something different.

Hope that helps,

Andy
Never trust the user. Validate input somehow.
1
2
3
4
5
std::cout << "Enter the number of elements in array (max " << MAXARRSIZE << "): ";
std::cin >> numEllements;
// clamp to acceptable range:
if ( numEllements < 1 ) numEllements = 1;
else if ( MAXARRSIZE < numEllements ) numEllements = MAXARRSIZE;


PS. The user could type non-numbers. That is a different input error that requires handling too.
Topic archived. No new replies allowed.