# Thread: elemnts in array counter

1. ## elemnts in array counter

Need to count how many numbers in array.
wrote this, which is:
1. far from being nice...
2. I allways get *size1=*size2! cant see why!

is ther other function to fulfill this task?

Code:
```void count(int *array1, int *array2, int *size1, int *size2)
{
int i;
int cntr1, cntr2;
cntr1=cntr2=0;

for (i=0;i<N-1;i++)
{
if	(array1[i])
cntr1++;
if  (array2[i])
cntr2++;
}
*size1=cntr1;
*size2=cntr2;

}```

2. Well you are always going to get the same values, because the statement (array1[i]) or (array2[i]) is always going to be true, since that will always generate a nonzero value.

Therefore each time your loop is processed, each counter goes up one.

Try to fix that.

3. Your data set needs to be defined a little more clearly. As it stands, it will only work for arrays that that contain data such as:

{ 1, 2, 3, 4, 0 }
or
{ 1, 2, 0, 0, 0 }

where the number of elements is at least N-1 and the end of the data is marked by a bunch of 0s that extends from the end of the data to fill the rest of the array. That is of course unless "size" isn't really what I expect it to mean. Please be a little more descriptive about what you're actually looking for (and fix your indentation).

As a side note, you might want to consider making your function only count one array and then make the calling function call it twice, once for each array.

Originally Posted by Beast()
Well you are always going to get the same values, because the statement (array1[i]) or (array2[i]) is always going to be true, since that will always generate a nonzero value.
I don't understand how you came to this conclusion. Any 0s in the array will cause the counter not to increment.

4. Well,

I have written a few code examples before, every now and then, that can disect an array and check for non-zero elements without destroying the loop. A while loop does wonders in these types of situations.

- Stack Overflow

5. I don't understand how you came to this conclusion. Any 0s in the array will cause the counter not to increment.
I assume that he wants to count how many elements there are in each individual array, since he is not trying to summate the elements themselves and has a counter that increments by one at each pass.

I also assumed that he wouldn't have inputted a zero to be an actual number; him saying that both sizes always ended up being equal helped solidify this belief. A zero would obviously make the if statement false.

In reality, I'm not really sure as to what he wants to do, but it seems that he is making a logic error with his if statements since size1 is always equal to size2.

To the original poster, you should follow itsme's advice and call the function twice, once for each array.

6. sorry for late respond...
as prposed by Beast():
you should follow itsme's advice and call the function twice, once for each array.
in order to do so modified function:

Code:
```void count(int *array, int *size)
{
int i;
int cntr;
cntr=0;

for (i=0;i<N-1;i++)
{
if (array[i])
cntr++;

}
*size=cntr;
}```
not sure of how to write the main for it. thats what I did:

Code:
```void main()
{
int i;
int array1[N] = {2,3,4,5,6,7,8,9};
int array2[N] = {9,8,7,6};
int *size1, *size2;
count (array1, size1);
count (array2, size2);
}```
Problem, anyways, still persists, with no change. I get the same value for both pointers.

7. > void main()
tsk tsk

8. Originally Posted by ronenk
sorry for late respond...
as prposed by Beast():

in order to do so modified function:

Code:
```void count(int *array, int *size)
{
int i;
int cntr;
cntr=0;

for (i=0;i<N-1;i++)
{
if (array[i])
cntr++;

}
*size=cntr;
}```
not sure of how to write the main for it. thats what I did:

Code:
```void main()
{
int i;
int array1[N] = {2,3,4,5,6,7,8,9};
int array2[N] = {9,8,7,6};
int *size1, *size2;
count (array1, size1);
count (array2, size2);
}```
Problem, anyways, still persists, with no change. I get the same value for both pointers.

You are declaring size1 and size2 to be pointers to int, but there is nothing for them to point to.

So: change
Code:
`int *size1, *size2;`
to

Code:
`int size1, size2;`
Then change
Code:
```count (array1, size1);
count (array2, size2);```
to

Code:
```count (array1, &size1);
count (array2, &size2);```
(The snippets that you posted do not print anything out. I assume that your real program prints out size1 and size2 somewhere. If you run your program without the modifications I have suggested, and you put printf() in the main program after the calls to count(), what do you get?)

Regards,

Dave

9. Intuitively, If I come across a function called count() to which I pass something for it to count, I'd expect it to return the number instead of having it return nothing and having to pass it a pointer to the count.

So, I'd expect the function to work like:
int count(int *array);

void count(int *array, int *size);

10. > So, I'd expect the function to work like:
> int count(int *array);
You mean like strlen() does
But that only works if there is some prior agreed value which marks the end of the array.

11. Originally Posted by Salem
> So, I'd expect the function to work like:
> int count(int *array);
You mean like strlen() does
But that only works if there is some prior agreed value which marks the end of the array.
Of course, but void count(int *array, int *size) suffers the same restriction unless size contains the size of the array when you pass it to count() instead of being a placeholder that count() fills (which is the case for the OP).

But there are alternatives that come to mind. The function doesn't might not need to worry about the end of the array. Suppose you know for certain that every array you'll pass to the function has at least three 1 values in it. The function might loop through the array until it finds the third 1 value and then return the index of it.

12. Originally Posted by itsme86
Of course, but void count(int *array, int *size) suffers the same restriction unless size contains the size of the array when you pass it to count() instead of being a placeholder that count() fills (which is the case for the OP).

But there are alternatives that come to mind. The function doesn't might not need to worry about the end of the array. Suppose you know for certain that every array you'll pass to the function has at least three 1 values in it. The function might loop through the array until it finds the third 1 value and then return the index of it.

The function as of his last post is

Code:
```void count(int *array, int *size)
{
int i;
int cntr;
cntr=0;

for (i=0;i<N-1;i++)
{
if (array[i])
cntr++;

}
*size=cntr;
}```
This function counts the number of non-zero elements among the first N elements of the array, (where N is a global variable or #defined constant), and stores the result in *size.

If that's what he wanted: mission accomplished.

Is that a Good Thing to do? Is that a Good Thing to want to do? Is that a Useful Thing to do? Is there something that is More Useful to want to do? Should we vote me off the island?

Programming is easy, it's those damn decisions that give me gray hairs.

Dave

13. This function counts the number of non-zero elements
And thus still suffering the problem stated by itsme86

among the first N elements of the array, (where N is a global variable or #defined constant), and stores the result in *size.
So then... what's the point?

14. Originally Posted by sean_mackrory

So then... what's the point?

Originally Posted by Dave Evans
Is that a Good Thing to do? Is that a Good Thing to want to do? Is that a Useful Thing to do? Is there something that is More Useful to want to do?
I personally can't imagine a situation where I would be interested in a zero-terminated array of ints, for example, but it's not my problem.

Regards,

Dave

15. Dave Evans post:
So: change
Code:
int *size1, *size2;

to

Code:
int size1, size2;

Then change
Code:
count (array1, size1);
count (array2, size2);

to

Code:
count (array1, &size1);
count (array2, &size2);
Had solved my original question. Base on this result I take a decision on which of the two array should I work.

Now I'm ready to go one step further, some of you have pointed to: Doing same task of counting assigned elements in array, including those zeros which where assigned to array by "purpose", but not those who where there from array initializing. Hope I made my self clear enough, & more then that hope I ask the right question. Would be glad to get some feedback on that.