Help for a true beginner?

Jan 30, 2015 at 2:55pm
Hi! I'm new to the forums and C++ in itself, but I am taking a class for it on the side and I just cannot seem to grasp this one linear search program. I feel like I am missing something basic but any help would be greatly appreciated! Thanks all!

S0 here is the assignment:
Hardcode the following integer array: {7, 3, 32, 2, 55, 34, 6, 13,29,22, 11, 9, 1, 5,42,39, 8} into your program. Display this array to the user. Then ask the user to input an integer to search for. Your program should then search the array for the integer the user entered and output its array position (i.e., its array index). Your output should look similar to the following:

List = 7, 3, 32, 2, 55, 34, 6, 13, 29, 22, 11, 9, 1, 5, 42, 39, 8
Enter an integer in the list to search for: 55
Item found at index [4]
Press any key to continue

And here is what I have. As you can see, I cannot seem to figure out how to search for the integer or display it back to the user.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  #include <iostream>
using namespace std;

int main()
{
	int value = 0;
	int position = 0;

	int array[17] = {7, 3, 32, 2, 55, 34, 6, 13, 29, 22, 11, 9, 1, 5, 42, 39, 8};

	cout << "List = 7, 3, 32, 2, 55, 34, 6, 13, 29, 22, 11, 9, 1, 5, 42, 39, 8" << endl;

	cout << "Enter an integer in the list to search for: ";
	cin >> value;
}
Last edited on Jan 30, 2015 at 2:55pm
Jan 30, 2015 at 3:06pm
Do you know how to use loops? You can loop though the array and for each element you can use an if statement to check if it is equal to the value entered by the user.
Jan 30, 2015 at 3:12pm
Sadly I do not, I will read about them and see what I can learn, thanks! :)
Topic archived. No new replies allowed.