I'm working on a program that searches for numbers that appear an odd amount of times in an array and how many times they appeared.
Example:
INPUT:
array = [0 , 3 , 0 , 4 , 4 , 9 , 4 , 1 , 3]
OUPUT:
oddNumbers = [4 , 9 , 1]
oddCounter = [3 , 1 , 1]
Please note that I couldn't find a solution in similar questions because they are either:
- In another programming language.
- Searching for only one number that appears an odd number of times, while my program is required to find all of them and how many times they show up. Those solutions simply don't apply in my case.
For this, I'm using two static arrays that contains the "name" and "count" of each number; as well as a static array that is filled with numbers through input. The current code returns no errors.
I'm having a hard time figuring out where the problem lies. Somehow, the numbers in the "name" array are stored on indexes which should be impossible. On top of that, the "count" array seems completely broken despite all the indexes being initialized. [Check log at the bottom for details!]
Here is the script:
int main() {
int name[32];
int count[32];
for (int i = 0 ; i < 32 ; i++){
count[i] = 0;
}
int numberArray[64];
int size , temp = 0;
bool control = true;
cout << "Input size of number array: ";
cin >> size;
cout << endl << endl;
for (int i = 0 ; i < size ; i++) {
cout << "Input number of index " << i << " into the array: ";
cin >> numberArray[i];
}
cout << endl << endl;
for (int i = 0 ; i < size ; i++) {
cout << "Main loop; index: " << i << endl;
for (int j = 0 ; j < sizeof(name) ; j++) {
if (numberArray[i] == name[j]) {
count[j]++;
control = false;
cout << "Number " << name[j] << " detected at slot " << j << "! There are " << count[j] << " of this number counted so far!";
break;
}
temp = j;
}
if (control) {
name[temp] = numberArray[i];
count[temp]++;
cout << "New number, " << name[temp] << " detected at slot " << temp << "! There is now at least" << count[temp] << " of this number!";
}
control = true;
cout << endl << endl;
}
for (int i = 0; i < sizeof(count); i++) {
if (count[i] % 2 != 0) {
cout << "Number " << name[i] << " appears an odd number of times. It appears " << count[i] << " time(s)." << endl;
} else {
cout << "Number " << name[i] << " doesn't appear an odd number of times. It appears " << count[i] << " times." << endl;
}
}
return 0;
}
Here is the direct log from an attempt:
Input size of number array: 11
Input number of index 0 into the array: 1
Input number of index 1 into the array: 1
Input number of index 2 into the array: 2
Input number of index 3 into the array: -2
Input number of index 4 into the array: 5
Input number of index 5 into the array: 2
Input number of index 6 into the array: 4
Input number of index 7 into the array: 4
Input number of index 8 into the array: -1
Input number of index 9 into the array: -2
Input number of index 10 into the array: 5
Main loop; index: 0
Number 1 detected at slot 88! There are -858993459 of this number counted so far!
Main loop; index: 1
Number 1 detected at slot 88! There are -858993458 of this number counted so far!
Main loop; index: 2
Number 2 detected at slot 90! There are -858993459 of this number counted so far!
Main loop; index: 3
Number -2 detected at slot 91! There are -858993459 of this number counted so far!
Main loop; index: 4
Number 5 detected at slot 92! There are -858993459 of this number counted so far!
Main loop; index: 5
Number 2 detected at slot 90! There are -858993458 of this number counted so far!
Main loop; index: 6
Number 4 detected at slot 94! There are -858993459 of this number counted so far!
Main loop; index: 7
Number 4 detected at slot 94! There are -858993458 of this number counted so far!
Main loop; index: 8
Number -1 detected at slot 96! There are -858993459 of this number counted so far!
Main loop; index: 9
Number -2 detected at slot 91! There are -858993458 of this number counted so far!
Main loop; index: 10
Number 5 detected at slot 92! There are -858993458 of this number counted so far!
Number -858993460 doesn't appear an odd number of times. It appears 0 times.
Number -858993460 doesn't appear an odd number of times. It appears 0 times.
Number -858993460 doesn't appear an odd number of times. It appears 0 times.
Number -858993460 doesn't appear an odd number of times. It appears 0 times.
Number -858993460 doesn't appear an odd number of times. It appears 0 times.
Number -858993460 doesn't appear an odd number of times. It appears 0 times.
Number -858993460 doesn't appear an odd number of times. It appears 0 times.
Number -858993460 doesn't appear an odd number of times. It appears 0 times.
Number -858993460 doesn't appear an odd number of times. It appears 0 times.
Number -858993460 doesn't appear an odd number of times. It appears 0 times.
Number -858993460 doesn't appear an odd number of times. It appears 0 times.
Number -858993460 doesn't appear an odd number of times. It appears 0 times.
Number -858993460 doesn't appear an odd number of times. It appears 0 times.
Number -858993460 doesn't appear an odd number of times. It appears 0 times.
Number -858993460 doesn't appear an odd number of times. It appears 0 times.
Number -858993460 doesn't appear an odd number of times. It appears 0 times.
Number -858993460 doesn't appear an odd number of times. It appears 0 times.
Number -858993460 doesn't appear an odd number of times. It appears 0 times.
Number -858993460 doesn't appear an odd number of times. It appears 0 times.
Number -858993460 doesn't appear an odd number of times. It appears 0 times.
Number -858993460 doesn't appear an odd number of times. It appears 0 times.
Number -858993460 doesn't appear an odd number of times. It appears 0 times.
Number -858993460 doesn't appear an odd number of times. It appears 0 times.
Number -858993460 doesn't appear an odd number of times. It appears 0 times.
Number -858993460 doesn't appear an odd number of times. It appears 0 times.
Number -858993460 doesn't appear an odd number of times. It appears 0 times.
Number -858993460 doesn't appear an odd number of times. It appears 0 times.
Number -858993460 doesn't appear an odd number of times. It appears 0 times.
Number -858993460 doesn't appear an odd number of times. It appears 0 times.
Number -858993460 doesn't appear an odd number of times. It appears 0 times.
Number -858993460 doesn't appear an odd number of times. It appears 0 times.
Number -858993460 doesn't appear an odd number of times. It appears 0 times.
Number -858993460 doesn't appear an odd number of times. It appears -858993460 times.
Number -858993460 doesn't appear an odd number of times. It appears -858993460 times.
Number -858993460 doesn't appear an odd number of times. It appears -858993460 times.
Number -858993460 doesn't appear an odd number of times. It appears -858993460 times.
Number -858993460 doesn't appear an odd number of times. It appears -858993460 times.
Number -858993460 doesn't appear an odd number of times. It appears 32 times.
Number -858993460 doesn't appear an odd number of times. It appears -858993460 times.
Number -858993460 doesn't appear an odd number of times. It appears -858993460 times.
Number 0 doesn't appear an odd number of times. It appears -858993460 times.
Number 0 doesn't appear an odd number of times. It appears -858993460 times.
Number 0 doesn't appear an odd number of times. It appears -858993460 times.
Number 0 doesn't appear an odd number of times. It appears -858993460 times.
Number 0 doesn't appear an odd number of times. It appears -858993460 times.
Number 0 doesn't appear an odd number of times. It appears -858993460 times.
Number 0 doesn't appear an odd number of times. It appears -858993460 times.
Number 0 doesn't appear an odd number of times. It appears -858993460 times.
Number 0 appears an odd number of times. It appears 1 time(s).
Number 0 appears an odd number of times. It appears 1 time(s).
Number 0 doesn't appear an odd number of times. It appears 2 times.
Number 0 doesn't appear an odd number of times. It appears -2 times.
Number 0 appears an odd number of times. It appears 5 time(s).
Number 0 doesn't appear an odd number of times. It appears 2 times.
Number 0 doesn't appear an odd number of times. It appears 4 times.
Number 0 doesn't appear an odd number of times. It appears 4 times.
Number 0 appears an odd number of times. It appears -1 time(s).
Number 0 doesn't appear an odd number of times. It appears -2 times.
Number 0 appears an odd number of times. It appears 5 time(s).
Number 0 doesn't appear an odd number of times. It appears -858993460 times.
Number 0 doesn't appear an odd number of times. It appears -858993460 times.
Number 0 doesn't appear an odd number of times. It appears -858993460 times.
Number 0 doesn't appear an odd number of times. It appears -858993460 times.
Number 0 doesn't appear an odd number of times. It appears -858993460 times.
Number 0 doesn't appear an odd number of times. It appears -858993460 times.
Number 0 doesn't appear an odd number of times. It appears -858993460 times.
Number 0 doesn't appear an odd number of times. It appears -858993460 times.
Number 0 doesn't appear an odd number of times. It appears -858993460 times.
Number 0 doesn't appear an odd number of times. It appears -858993460 times.
Number 0 doesn't appear an odd number of times. It appears -858993460 times.
Number 0 doesn't appear an odd number of times. It appears -858993460 times.
Number 0 doesn't appear an odd number of times. It appears -858993460 times.
Number -858993460 doesn't appear an odd number of times. It appears -858993460 times.
Number -858993460 doesn't appear an odd number of times. It appears -858993460 times.
Number -858993460 doesn't appear an odd number of times. It appears -858993460 times.
Number -858993460 doesn't appear an odd number of times. It appears -858993460 times.
Number -858993460 doesn't appear an odd number of times. It appears -858993460 times.
Number 32 doesn't appear an odd number of times. It appears -858993460 times.
Number -858993460 doesn't appear an odd number of times. It appears -858993460 times.
Number -858993460 doesn't appear an odd number of times. It appears -858993460 times.
Number -858993460 doesn't appear an odd number of times. It appears -858993460 times.
Number -858993460 doesn't appear an odd number of times. It appears -858993460 times.
Number -858993460 doesn't appear an odd number of times. It appears -858993460 times.
Number -858993460 doesn't appear an odd number of times. It appears -858993460 times.
Number -858993460 doesn't appear an odd number of times. It appears -858993460 times.
Number -858993460 doesn't appear an odd number of times. It appears -858993460 times.
Number -858993460 doesn't appear an odd number of times. It appears -858993460 times.
Number -858993460 doesn't appear an odd number of times. It appears -858993460 times.
Number 1 doesn't appear an odd number of times. It appears -858993458 times.
Number 1 doesn't appear an odd number of times. It appears -858993460 times.
Number 2 doesn't appear an odd number of times. It appears -858993458 times.
Number -2 doesn't appear an odd number of times. It appears -858993458 times.
Number 5 doesn't appear an odd number of times. It appears -858993458 times.
Number 2 doesn't appear an odd number of times. It appears -858993460 times.
Number 4 doesn't appear an odd number of times. It appears -858993458 times.
Number 4 doesn't appear an odd number of times. It appears -858993460 times.
Number -1 appears an odd number of times. It appears -858993459 time(s).
Number -2 doesn't appear an odd number of times. It appears -858993460 times.
Number 5 doesn't appear an odd number of times. It appears -858993460 times.
Number -858993460 doesn't appear an odd number of times. It appears -858993460 times.
Number -858993460 doesn't appear an odd number of times. It appears -858993460 times.
Number -858993460 doesn't appear an odd number of times. It appears -858993460 times.
Number -858993460 doesn't appear an odd number of times. It appears -858993460 times.
Number -858993460 doesn't appear an odd number of times. It appears -858993460 times.
Number -858993460 doesn't appear an odd number of times. It appears -858993460 times.
Number -858993460 doesn't appear an odd number of times. It appears -858993460 times.
Number -858993460 doesn't appear an odd number of times. It appears -858993460 times.
Number -858993460 doesn't appear an odd number of times. It appears -858993460 times.
Number -858993460 doesn't appear an odd number of times. It appears -858993460 times.
Number -858993460 doesn't appear an odd number of times. It appears -858993460 times.
Number -858993460 doesn't appear an odd number of times. It appears -858993460 times.
Number -858993460 doesn't appear an odd number of times. It appears -858993460 times.
Number -858993460 doesn't appear an odd number of times. It appears -858993460 times.
Number -858993460 doesn't appear an odd number of times. It appears -858993460 times.
Number -858993460 doesn't appear an odd number of times. It appears -858993460 times.
Number -858993460 doesn't appear an odd number of times. It appears -858993460 times.
Number -858993460 doesn't appear an odd number of times. It appears -858993460 times.
Number -858993460 appears an odd number of times. It appears 11 time(s).
Number -858993460 doesn't appear an odd number of times. It appears -858993460 times.
Number -858993460 doesn't appear an odd number of times. It appears -858993460 times.
Number -858993460 doesn't appear an odd number of times. It appears -858993460 times.
Number -858993460 doesn't appear an odd number of times. It appears -858993460 times.
Number -858993460 doesn't appear an odd number of times. It appears -858993460 times.
Number -858993460 doesn't appear an odd number of times. It appears -858993460 times.
Number -858993460 doesn't appear an odd number of times. It appears -858993460 times.
Number -858993460 appears an odd number of times. It appears 91 time(s).
Number -858993460 doesn't appear an odd number of times. It appears -858993460 times.
Number -858993460 doesn't appear an odd number of times. It appears -858993460 times.
Is there any way to fix this or is there another solution?