Problem with an array

I am writing a program that is supposed to take in integers and add them to an array only if they are not repeat numbers.

I created the following code:

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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82

#include <iostream>
#include <iomanip>

using namespace std;


double fill_Array(double [], int);
//int linearSearch(const int[], int, int);

int main()
{
	//Constants
	const int 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.

Any advice if welcome.
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.
Last edited on
Moschops,

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.


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
52
53
54
55
56
57
58
59
60
int linearSearch(const int[], int, int);

int main()
{
	//Constants
	const int 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(const int 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.
Last edited on
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?

Thanks....

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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79

#include <iostream>
#include <iomanip>

using namespace std;


//double fill_Array(double [], int);
int linearSearch(int[], int, int);

int main()
{
	//Constants
	const int 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;
			else
				return -1;
}


Topic archived. No new replies allowed.