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
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?
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).
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.
#include <iostream>
#include <cstdlib>
#include <cassert>
usingnamespace 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.