Help: Finding the missing number in the array.

I'm trying to write a program where in you input 9 numbers from 1-10, then it determines the missing number. Here's my code. It has a lot of errors. Help me do the right structure. Maybe after that, I'll improve it by setting conditions like no repetition/ 0< number <10. Just help me to fix this first:

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>
using namespace std;
 
int main()
{
    int d[]={1,2,3,4,5,6,1,7,9, 10};
    int i=0,j=0,c=0,missing=0;
 
for (i = 0; i < 9;i++)
{
    cout << "input: ";
    cin >> d[i];
    c = 0;
    for (j = 0;j<10; j++)
    {
        if(i == d[j])
        {
            c = i;
        }
    }
    if (i==d[j])
    {
        missing = j;
 
    }
}
    cout << "missing is " << missing << endl;
    return 0;
}
 
 


I'm so new to C++. It always prints out the number 10 even though I typed it already. I really have no idea how to determine the missing one.
Last edited on
When declaring you for-loop, you said i<9, but it should be i<10 if you want to input 10 numbers.
Sorry, i forgot you are inputing 9 numbers. My bad.
Try this

#include <iostream>
using namespace std;

int main()
{
int d[]={1,2,3,4,5,6,7,8, 9,10};

int a[9];

int i, j, missing;
bool bMissing;

for (i = 0; i < 9;i++)
{
cout << "input: ";
cin >> a[i];
}

for (i = 0; i < 10; i++)
{
bMissing = true;

for (j = 0; j< 9; j++)
{
if (d[i] == a[j])
{
bMissing = false;
break;
}
}
if (bMissing)
{
missing = d[i];
}
}

cout << "missing is " << missing << endl;
return 0;
}
First, you need a way to determine whether a number has been entered by the user or not. I would suggest an array of integral type with all the elements initialized to zero:

int digit_count[10] = {} ;

Now, the convention will be to for digit_count[x-1] to refer to the number of times the user has entered the digit x. So, for 1, digit_count[0] is the element that holds the count.

So, for each number the user enters, increment the corresponding element in the array. (You may also wish to validate the user's input so you aren't accessing outside of the digit_count array.)

When you're done with the input, you can then iterate through the array. Any element that is equal to zero is 'missing'.
Here's another way to do it... a little more readable... also will detect multiple numbers left out (like if you leave out 2 and 3 or something)...

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
#include <iostream>

using namespace std;

int main()
{
	int counter = 0;
	int userNumbers[9]; //This is index 0-8 (9 numbers)

	//Loop 9 times to accept input
	while (counter < 9)
	{
		//Accept input
		cout << "Input: ";
		int aNumber;
		cin >> aNumber;

		//Record this number
		userNumbers[counter] = aNumber;
		++counter;
	}

	//Now that we have all the user numbers,
	//check to see what's missing
	
	//Store numbers to check against
	bool checkNumbers[11]; //1-10 will represent actual numbers (we'll ignore 0)

	int missingNumber;

	//Loop through our userNumbers list
	for (int i=0; i<9;++i) {
		//Loop through our checKNumbers list
		for (int j=1; j<11; ++j) {
			//If this userNumber = this checkNumber, we found a number,
			//so make the checkNumber boolean value = TRUE
			if (userNumbers[i] == j) { checkNumbers[j] = true; }
		}
	}

	//We're done with the loops, so whatever checkNumber[?] doesn't equal true we didn't find

	//Loop through the checkNumbers
	for (int i=1; i<11; ++i)
		if (checkNumbers[i] != true) { cout << "YOU LEFT OUT: " << i << "\n\n"; }

	system("pause");
	return 0;
}
Last edited on
This is the final code I came up with and it has worked for me:

#include <iostream>
using namespace std;
int main()
{
int a[] = {1,2,3,4,5,6,7,8,9,10};
int usera[9];
for(int i=0; i<9; i++)
{
cin>>usera[i];
}
int count = 0;
int final;

for(int i=0; i<9; i++)
{
if(a[count]==usera[i])
{
count++;
}
else {

final = a[count];
for(int b=0; b<count+1; b++)
{
if(a[count]==usera[b])
{
count++;
}
}

}

}

cout<<final<<endl;
system("pause");
}
Topic archived. No new replies allowed.