I am in a beginning C++ class in college and I need some help with a project. The project is to design a C++ program that takes a user enter sequence of non-negative numbers and then output the number and output how many times each number is used. I am having trouble getting the count of each number and having it displayed. Any help is appreciated. Here's the code:
In line 37 I was trying to set a variable equal to the amount of array values entered to by the user so I could just use the partially filled array in the following functions.
The reason why kfmfe04 asked about it is because that variable will be destroyed after the function completes. Either declare that variable (num) outside of the function or pass a pointer to that variable as function parameter.
However, you don't need it because you have "SIZE" which will have the same value.
Sorry my dumb mistake. I meant to assign the value of num to limit and it should be limit = num and since limit is a call by reference parameter the value will be carried over. Thanks for helping me out... just a stupid mistake I feel a little embarrassed right now.
Now when I want to display only one of an array value and the count it sometimes prints out multiple. Here is a sample run of the program:
Please enter a sequence of non-zero integers (up to 50)
and this program will tell you the count of each different integer used
Enter a negative number to end sequence.
12 12 1 4 1 4 45 45 1 3 7 -1
Number Count
12 2
1 2
4 2
1 2
4 2
45 2
45 2
1 2
3 2
7 2
I thought the if loop and the changing of the array parameters to a negative number would prevent this.
The reason why you are outputting duplicates is because your array "a[]" that you are iterating through contains duplicates of the same number. I recommend creating a struct with int value and count variables. Whenever a user enters a number that number is either put into a struct and inserted into a vector or if it exists in the vector its count is increased. Then all you need to do is ouput the contents of the vector and their respective counts.
For a new value, before printing, check to see if it is in set<unsigned>.
If it's there, don't print.
If it's not there, print and put the value in the set.
Of course, if you wanted to redo the entire algorithm, you can get away with just using an stl map<unsigned,unsigned> and do away with your array and counting completely.
I just coded that up - using your formatting, it comes out to 47 lines and output looks like this:
Please enter a sequence of non-zero integers (up to 50)
and this program will tell you the count of each different integer used
Enter a negative number to end sequence.
12 12 1 4 1 4 45 45 1 3 7 -1
Number Count
1 3
3 1
4 2
7 1
12 2
45 2
What I was trying to do starting the numcount at 1 is assume that if the number comes up there will be at least one of it and what I was trying to do with the a[index] *= -1 is if it finds a duplicate change that to a negative number so when it comes to 62 it wouldn't pass the boolean expression but it seems to only be running the numcount one so it only gets a value of 2 and so I was thinking there must be a way to make it run for the entire array so every number if found will turn into a negative number and not be printed on the screen.
Once you modify a[index] *= -1, you cannot count that value any more!!! In other words, the check on Line 47 will always return false, subsequently. This explains why none of your results show greater than 2.
In calc(), a[] is an input for your for() loop. By modifying a[] inside with a[index] *= -1, it also acts as an output. Be extremely careful about doing that.
In 99% of the cases, I would avoid doing that inside a loop - either use the array as input or as output. Otherwise, you could end up with serious debugging headaches.
You could do it inside calc(), using a second loop, but even that is dangerous, from a developer's point of view.
Why? When you see a function called calc(), would you expect it to change the values of something you pass it? In other words, if you call calc() multiple times, would you expect it to do the same thing?
You have to ask yourself this because once you write code and put it away for a while before playing with it again, you don't want any "surprises" (aka bugs) in your code.
For a "hack", you could do it, but be aware of what you are doing - it's not really a good habit, if you are serious about programming.
I see what your saying. Would a better alternative be to have a function that saves the number count in a separate array and then another function that deletes the duplicates in the first array and then output both arrays?
I recommend using the strategy that I suggested by creating a struct, converting the input into that struct, then either inserting or incrementing a vector containing that struct. This way it is neat, logic safe and you will not have problems with memory.