I have been working on this array for about two weeks now and I can't seem to get it right. The idea of the array is to take a number between 10 and 100 and add it to the array if the number is not already in the array. I have a function that search the array and it is suppose to compare the numbers. However, it only works on the first number. I hope someone can look at it and give me some pointers.
#include <iostream>
#include <iomanip>
usingnamespace std;
//double fill_Array(double [], int);
int linearSearch(int[], int, int);
int main()
{
//Constants
constint arraySize = 5;
int myArray[arraySize]={};
int n = 0;
int value;
// for (int i = 0; i < arraySize; ++i)
// myArray[i] = 10;
//Requests user input
cout << "Please enter a number: \n";
//cin >> value;
//int element = linearSearch(myArray, value, arraySize);
while( n < arraySize)
{
cin >> value;
if ((value >= 10) && (value <=100))
{
value = linearSearch(myArray, value, arraySize);
if(value != -1)
{
myArray[n] = value;
n++;
cout << "Adding to the array." << endl;
}
else
{
cout << "Not adding to the array." << endl;
}
}
else
cout << "value out of range." << endl;
}
cout << "The user entered the numbers in this order:" << endl;
//This loop prints the inside of the array.
for (int j=0; j < arraySize; ++j)
cout<< setw(4) << myArray[j] << endl;
}
int linearSearch(int array[], int key, int arraySize)
{
for (int j= 0; j <= arraySize; j++)
if(array[j]!=key)
return key;
elsereturn -1;
}
Note: if you think it doesn't work because the array is empty, I already tried filling the array with the int = 10 and still does the same. It will recognize the first number once it is added but nothing else.
The logic in your function is flawed. It will always return when j=0 (1st iteration). The whole if-else is within the loop.
You want to go through the array checking if(array[j] == key). If this is true then return -1.
If the whole loop runs and no return occurs then key is not in the array. return key; after the loop in that case. There's no need for an else in the function.
When you said that I don't need the else statement, were you talking about the else statement in the linerSearch function or the one inside the while loop?
You mentioned that the if-else statement is within the loop. If I take the if statement out of the loop, how do I enter new values that match the criteria of being equal or greater than 10 and equal or less than 100?
I'm pretty green when it comes to programming myself so don't hate me if I'm wrong but I think
the reason your for loop isn't working is probably because it will only check your first number and will only return for this number. The reason for this, is that if your if-sentence is false it will return 0 and thus break the loop. instead move your bracket so that your "return 0" is outside the loop.
Like this:
int linearSearch(int array[], int key, int arraySize)
{
for (int j= 0; j <= arraySize; j++){ //<--- If this for loop fails for every number up to arraySize
if(array[j]!=key) // It will break the loop and return -1
return key;
}
return -1;
}
I hope you are able to read my norwenglish and find this somewhat useful :)
I think fun2code was talking about the loop in the linearSearch() function.
As written, the first time around the for-loop both the if and the else clauses will return from the function, preventing it from searching the whole array.
You need to remove the else clause, so that the loop only returns if you find a match. Then after the loop's finished, just return -1 to indicate a non-match.
Actually, from your use of the linearSearch() function in your main code, it looks like you're expecting a -1 to be returned if the value is found, and 'value' if it's not found. Your linearSearch() code doesn't work that way, so you need to rethink that function anyway.
Yes, I was referring to the if-else within the linearSearch() function. Sorry about the confusion there.
All of your code in the main() function looks fine. It's the linearSearch() which is keeping the program from working.
Do my hints about how to rewrite the function make sense?