Linear search from command line

Pages: 12
Hi all,

I am having difficulty in making a program for C++. My problem is below:

I need to write a program which reads and stores in an array 10 integers from the command line, and then prompts the user to enter a single integer from standard input. If the entered number occurs in the array, the program should print out the first index at which it occurs:

> ./a.out 4 39 5 3 0 34 9 99 66 44
Enter number: 39
Found at position 1

If the entered number does not appear in the array the program should print out the message "Not found":

> ./a.out 4 39 5 3 0 34 9 99 66 44
Enter number: 42
Not found

any help would be great.

Thanks in advance.
● Read the input in the array
● Read the other number
● Iterate values in the loop
1
2
3
for(int i=0; i<array_size; i++)
   if ( array[i]==number ) print i;
if (after_the_loop) print "Not found"; 
I think that making a function would help, if (array[i]==number) you print i and return from the function
Last edited on
am I on the right lines by doing this? If so, how could I complete it because this is the trouble i am having really, ending it

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 <cstdlib>
#include <cassert>
using namespace std;

int main(int argc, char* argv[])
{
  if( argc != 11 )
    {
      cerr << "Usage: n1 n2 n3 n4 n5 n6 n7 n8 n9 n10\n" ;
      exit(1) ;
    }

  //Store the user's choice of 10 numbers
  int number[10] ;
  for ( int i = 0; i < 10; i++ )
    {
      number[i] = atoi( argv[i+1] ) ;
      assert( 1 <= number[i] && number[i] <= 1000 ) ;
    }

  //User enters number to be searched
  for ( int i = 0; i < 10; i++ )
    {
      cout << "Enter Number: " << endl ;
    }



You could enter the search number from the command line, too.
Why that range restriction?
Do you mean the range restriction to 1000? I don't know why I put it there, it can obviously be infinite can't it, what should I do in place of this then? (as you can probably tell, i'm a complete novice at c++ but learning things as quick as possible and need some advice on how to get this to work, because if I can get help on this it enables me to understand it better).
Well, no. It certainly can't be infinite. Usually, although it changes from compiler to compiler, int has a modest range of [-2^31;2^31-1]
You should use types that make sense with the kind of operation you are trying to perform. In this case, all you are doing is a experiment with linear search, so using an int is enough. No value of int could break the algorithm, so you don't need to do any checks.
Ok well I do need help completing my code which is above. What would I need to do in order to let it complete it search and display what position the number is in and also if the number is not found?
I don't understand the loop at lines 24-27, did you mean:
1
2
3
int n;
cout << "Enter number: ";
cin >> n;
Well basically, once the user has entered 10 numbers into the command line after ./a.out, the user then presses enter and has to enter a number from the 10 numbers which were entered, then it searches for that number's position, so really, i'm having difficulty in knowing what to do after line 21 (because the 10 numbers have been stored in the array and now I need the user to enter one of the 10 numbers so that the program can search for it and print its position in the array).
http://en.wikipedia.org/wiki/Linear_search

EDIT: Wait. I just noticed something weird. Why are you entering the number 10 times?
Last edited on
It is an array isn't it.
But you have to search ONE number.
Please refer to my first post with my original question, it states there what is needed. This is really troublesome!
I need to write a program which reads and stores in an array 10 integers from the command line, and then prompts the user to enter a single integer from standard input. If the entered number occurs in the array, the program should print out the first index at which it occurs. [...] If the entered number does not appear in the array the program should print out the message "Not found"
It seems to me that you've already completed the 'hard' piece of the program.

Just prompt the user to enter an integer and then loop through the number array to look for that integer. If it is found save the index that found it and display it, otherwise tell em it's not found.

Sample below:
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
#include <iostream>
#include <cstdlib>
#include <cassert>
using namespace std;

int main(int argc, char* argv[])
{
	int n; // number that the user enters
	int i; // used to iterate through an array
	int number[10]; // array to store the 10 command line inputs
	int index = -1; // index at which the integer is found
	bool found = false; // flag that tells whether the search was successful
	
	//Check to see if the command line input was correct
	if( argc != 11 )
	{
		cerr << "Usage: n1 n2 n3 n4 n5 n6 n7 n8 n9 n10\n" ;
		exit(1) ;
	}
	
	//Store the 10 command line integers
	for ( i = 0; i < 10; i++ )
	{
		number[i] = atoi( argv[i+1] ) ;
		assert( 1 <= number[i] && number[i] <= 1000 ) ;
	}
	
	//User enters integer to be searched
	cout << "Enter Number: " << endl ;
	cin >> n;
	
	//Loop through the array and look for the integer
	for ( i = 0; i < 10; i++ )
	{
		if ( n == number[i] )
		{
			index = i;
			found = true;
			// no need to continue the for loop since you're only looking for the first instance found
			break; 
		}
	}
	
	//Display result
	if ( found )
		cout << "\nFound at index: " << index << endl;
	else
		cout << "\nNOT FOUND" << endl;

	return 0;
}


In this example you don't really need to use the flag "found". You can simply check if index is still equal to -1. I thought using the flag was more descriptive however.
Thanks, I will have a try and see if this works for me. (Well of course it should work as you are an expert!)
I wouldn't call me an expert. That might offend some people.

I compiled and ran the program above and it worked like your sample workflow you originally posted. Hopefully it's what you're looking for. Good luck.
... That was exaclty what I suggested in my 1st post ...
yes you did bazzy
I get a small error...

25: failed assertion `1 <= number[i] && number[i] <= 1000' Abort.

By the way my editor is emacs and my compiler is g++ (eg. g++ test.cc)

What do I do here?
How do I correct this?
Last edited on
Pages: 12