-2

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:

  1. In another programming language.
  2. 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?

  • Use a `std::map` to [make a histogram](https://stackoverflow.com/q/21682716/4342498). Then just output each key from the map that has an odd value. – NathanOliver Sep 01 '22 at 13:17
  • 3
    `sizeof(name)` is the byte size of the array, not the number of elements. `int` is larger than 1 byte so you access your array out of bounds. – François Andrieux Sep 01 '22 at 13:18
  • `std::unordered_map` alternatively... – Aconcagua Sep 01 '22 at 13:19
  • What is the topic of the chapter in your C++ textbook that this practice problem is from? This will help in pointing you at the correct approach that you're expected to learn from this chapter. There are several different possible C++ topics that this kind of a problem is designed to cover, so we need to know exactly what you're supposed to learn from doing this practice problem, in order not to waste your time with something irrelevant. – Sam Varshavchik Sep 01 '22 at 13:23
  • 1
    This is solvable in maybe 5 lines of code using `std::map`. You don't need any of these confusing loops with all sorts of counters going on, boolean flag variables, etc. – PaulMcKenzie Sep 01 '22 at 13:24
  • `sizeof(count)` is not what you expect, you want `std::size(count)`. – Jarod42 Sep 01 '22 at 13:30
  • Sam Varshavchik - this was not taken from an excercise book. I found it as a challenge posted on the internet. – SomeoneWhoLikesToCode Sep 01 '22 at 13:31
  • `int name[32]{}; int count[32]{};` would initialize arrays with 0. – Jarod42 Sep 01 '22 at 13:31
  • 1
    @SomeoneWhoLikesToCode Learn C++ from [a good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list), *not* from online challenges, competitive programming sites, YouTube videos and similar crap. And expect to spend, at least, a few years learning. – Jesper Juhl Sep 01 '22 at 13:34
  • 2
    There's a popular myth about a secret shortcut to becoming an elite C++ guru: throw away your C++ textbooks; instead do random coding puzzles that, otherwise, have no inherent learning value, they don't teach anything except bad programming habits. This myth comes from a bunch of clickbait web sites that promise to turn anyone into an instant C++ uberhacker if only they solve their puzzles. Everyone eventually realizes how useless these coding puzzles are. But they've already sunk a massive amount of time writing one coding puzzle after another. And they have nothing to show for it. – Sam Varshavchik Sep 01 '22 at 13:36
  • @SomeoneWhoLikesToCode *Please note that I couldn't find a solution in similar questions because they are either: In another programming language.* -- I bet that if you looked at those solutions in those other languages, they use something similar to a map. Also, those sites you are going to assume you know the C++ language well-enough to know things like `std::map` or `std::unordered_map` (as well as other containers and algorithm functions), or at least you know about them and can research how to use them. They are not sites that are designed to teach C++. – PaulMcKenzie Sep 01 '22 at 14:01

1 Answers1

3

I normally try to do a mini code review, but given that there isn't a specific principle being learned, I'm free to just throw out your code completely, which I will.

What I'm going to use is the std::unordered_map. It stores data in key/value pairs. The keys will be the numbers in the array, and the values will be the count. So we're literally going to store a histogram of the array, and then filter it. What's nice about this is that it makes the counting trivial, and the keys and values are intrinsically linked to each other (no maintaining two arrays separately).

Here's an example. I've hard-coded the array just for ease of demonstration.

#include <array>
#include <iostream>
#include <unordered_map>

int main() {
  std::array<int, 9> arr{0, 3, 0, 4, 4, 9, 4, 1, 3};
  std::unordered_map<int, int> intFreq;

  // This loop does the counting
  for (auto i : arr) {
    ++intFreq[i];
  }

  // When using a range-based for loop on a std::unordered_map you
  // get std::pair objects
  for (auto p : intFreq) {
    if (p.second & 1) {  // Check for odd using bitwise AND
      std::cout << p.first << ": " << p.second << " time"
                << (p.second == 1 ? "\n" : "s\n");
    }
  }
}

Output:

❯ ./a.out 
1: 1 time
9: 1 time
4: 3 times

The counting works because when using operator[]() on a std::unordered_map, the key will be added if it doesn't already exist. This can be a gotcha if you didn't know about the behavior. In our case we are taking advantage of it.

As was said in the comments, your approach to learning programming and C++ is not feasible. I have yet to find a good and competent programming principles resource online, and the same is also true for C++ as a language. Competition sites like hackerrank, leetcode, etc., do NOT teach anything except bad habits. They can be fun enough to mess around with after you gain experience with a language.

sweenish
  • 4,793
  • 3
  • 12
  • 23