Array counting instances of negative and numbers that go beyond array size

Aug 31, 2015 at 2:46am
Hello i have a problem with array counting instances of negative and numbers that go beyond array size. I made an array to count the number of instances of only numbers that are not beyond the size limit of 30. Been smashing my head for a few hours. Anything help is much appreciated!

currently my code only runs from 1-30 inputs.
Last edited on Aug 31, 2015 at 4:09am
Aug 31, 2015 at 3:25am
// bubble sort algorithm
for( int i = 0; i <= input; i++)

it should be < instead of <= (but this will not cause an error)

// extracting number of instances for each element in array
for( int i = 0; i <= input; i++)

it should be < instead of <=

int count[30]
count[arr[i]]++;

this wont work unless all the input numbers are smaller than 30

// printing array of elements with number of instances
for( int i = 0; i < input; i++)

it should be: i<input-1

Your main problem is the size of the 'count' array. It must be as large as the greatest number that will be input by the user, plus 1.
Last edited on Aug 31, 2015 at 3:27am
Aug 31, 2015 at 3:25am
currently my code only runs from 1-30 inputs.


sorry more my fault than yours. What do you want your code to do that it is not?
Aug 31, 2015 at 3:29am
Perhaps you should do it without the 'count' array. Just set int cnt=0, and then go through array 'arr' to count equal elements, increasing cnt by 1 on each iteration. WHen you find an element not equal to the previous element, print out cnt and reset it back to 0.

Also, it is better to use std::vector than c-style arrays.
Last edited on Aug 31, 2015 at 3:36am
Aug 31, 2015 at 3:30am
@Kevin C

how would i count instances of negative numbers as well?

@Bdanielz

i want it to count the instances of negative numbers and the greatest number of the user input and print them.
Aug 31, 2015 at 3:34am
The solution with cnt will work for negative numbers, too. I have edited the previous answer, so reread it please.
Last edited on Aug 31, 2015 at 3:35am
Aug 31, 2015 at 3:42am
@Kevin C
thank you for your input and will try to work on it and will get back to u
Aug 31, 2015 at 4:09am
That inner loop is unnecessary. Just one loop. And you should be doinng the last part (printing out and count=0) only when arr[i]!=arr[i+1].

- And: in for loop, i < input-1
- and dont forget count++ when you remove the inner loop.
- use:
cout << arr[i] << "repeats: " << count << endl;

Edit: and I left out another thing for the end... you can figure it out
Last edited on Aug 31, 2015 at 4:10am
Aug 31, 2015 at 4:11am
Have you tried using a debugger? It will be easier. Are you using an IDE and which one?
Aug 31, 2015 at 4:25am
.
Last edited on Aug 31, 2015 at 4:40am
Aug 31, 2015 at 4:32am
Nah, your solution is still not good enough. And you have made it too complicated.

Try to figure out this one:

1
2
3
4
5
6
7
8
9
	// extracting number of instances for each element in array
	for( int i = 0; i < input; i++)
	{
		count++;
		if(i==input-1 || arr[i] != arr[i+1]) {
			cout << left << setw(10) << arr[i] << left << setw(10) << count << endl;
			count =0;
			}
	}
Last edited on Aug 31, 2015 at 4:35am
Aug 31, 2015 at 4:40am
@Kevin C

yours works too and much more simpler O.O and thank you for the help. my head feels like its gonna explode.
Topic archived. No new replies allowed.