#include <iostream>
#include <iomanip>
usingnamespace std;
double fill_Array(double [], int);
//int linearSearch(const int[], int, int);
int main()
{
//Constants
constint arraySize = 5;
double myArray[arraySize]={};
int n = 0;
int value = 0;
for (int i = 0; i < arraySize; ++i)
myArray[i] = 10;
//Requests user input
cout << "Please enter a number: \n";
fill_Array( myArray, arraySize);
}
double fill_Array(double a[], int sizeOfArray)
{
//Constants
int n = 0;
double value = 0;
//While loop that fills up the array.
while (n < sizeOfArray)
{
cin >> value;
if ((value >= 10) && (value <=100))
{
if(a[n] != value)
{
a[n] = value;
n++;
cout << "Adding to the array." << endl;
}
else
{
cout << "Not adding to the array." << endl;
}
}
//This loop fills in the array
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 < sizeOfArray; ++j)
cout<< setw(4) << a[j] << endl;
return 0;
}
I filled the array with the int 10 and I tested it. The array recognizes the value already stored in the array and if I tried to repeat 10, it tells me it is already there. My problem is when I enter another number. If I enter 11, it stores it in the array but it doesn't recognize it if I try to enter 11 again.
You are only checking one element in the array - the one that's about to be filled. You should be checking all the elements already filled in, not just the one about to be filled in.
You should go back and think again about how to check every element in the array for repeat numbers.
thanks for the advice. I went back to the book and I found a little search function that can help me. However, once I implemented the loop, I can't get out.
int linearSearch(constint[], int, int);
int main()
{
//Constants
constint arraySize = 5;
int myArray[arraySize]={};
int n = 0;
int value;
//Requests user input
cout << "Please enter a number: \n";
cin >> value;
int element = linearSearch(myArray, value, arraySize);
while( n < arraySize)
{
cin >> value;
int element = linearSearch(myArray, value, arraySize);
if(element != -1)
{
myArray[n] = value;
n++;
cout << "Adding to the array." << endl;
}
else
{
cout << "Not adding to the array." << endl;
}
}
}
int linearSearch(constint array[], int key, int arraySize)
{
if ((key>= 10) && (key <=100))
{
for (int j= 0; j < arraySize; j++)
if(array[j]!=key)
return key;
}
else
cout << "value out of range." << endl;
}
Any advice on how to work this problem? I can search the array, I can put a condition for the value of the numbers but I can't seem to get all together.
I re-wrote the program above. As of right now, I can read the first integer, check to see if it is in the array, and add it if it is not. However, I can only do this for the first integer that I enter. After the first one, the function does not work.
Can any of you look at this code and point me in the right direction?
#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;
}