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:
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'.
#include <iostream>
usingnamespace 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;
}
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;