elemnts in array counter

This is a discussion on elemnts in array counter within the C Programming forums, part of the General Programming Boards category; Need to count how many numbers in array. wrote this, which is: 1. far from being nice... 2. I allways ...

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    123

    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. #2
    Registered User
    Join Date
    Jun 2004
    Posts
    93
    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. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    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.

    Quote 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.
    Last edited by itsme86; 08-19-2004 at 11:41 AM.
    If you understand what you're doing, you're not learning anything.

  4. #4
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477
    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
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  5. #5
    Registered User
    Join Date
    Jun 2004
    Posts
    93
    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. #6
    Registered User
    Join Date
    Jun 2004
    Posts
    123
    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. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    > void main()
    tsk tsk
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote 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
    Last edited by Dave Evans; 08-20-2004 at 08:09 AM.

  9. #9
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    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);

    instead of:
    void count(int *array, int *size);
    If you understand what you're doing, you're not learning anything.

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    > 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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  11. #11
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    Quote 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.
    If you understand what you're doing, you're not learning anything.

  12. #12
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote 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. #13
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    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. #14
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by sean_mackrory

    So then... what's the point?

    I already asked that:

    Quote 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
    Last edited by Dave Evans; 08-20-2004 at 01:10 PM.

  15. #15
    Registered User
    Join Date
    Jun 2004
    Posts
    123
    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.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 11-25-2008, 12:50 AM
  2. array of pointers/pointer arithmetic
    By tlpog in forum C Programming
    Replies: 18
    Last Post: 11-09-2008, 06:14 PM
  3. 1-D array
    By jack999 in forum C++ Programming
    Replies: 24
    Last Post: 05-12-2006, 07:01 PM
  4. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-11-2004, 11:15 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21