Thread: Pointer + Array + Function Problem~~

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    16

    Pointer + Array + Function Problem~~

    Code:
    Hi... i just start to learnt about pointer + function with array... this my toturial question.. now i try to do it..?
    Write a function countEvent(int *, int) which receives an integer array and its size, and returns the number of even numbers in the array.
    
    
    #include<stdio.h>
    int countEvent(int *ptr, int size)
    {
    int i, even_cnt = 0;
    for(i = 0; i<=size; i++)
    {
    if(*(ptr+1)%2 == 0)
    even_cnt++;
    }
    return even_cnt;
    
    }
    int main ()
    {
    int array[10] = {1,2,3,4,5,6,7,8,9,10};
    int even, count = 10;
    
    even = countEvent(&array,count);
    
    printf("%d\n", even);
    
    return 0;
    }
    
    this is my program... it should be giving me answer 5 rather than 11; but i get the output is 11?? what happen with my program..
    
    thank you very much~~

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Code:
    for(i = 0; i<=size; i++)
    What is the value of size and how many times will you go through the loop?

    Code:
    if(*(ptr+1)%2 == 0)
    What is that line doing? Does ptr ever change? Where's the use of your loop control variable, i?

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    OK... a few things....

    1) Learn to indent your code and leave a blank line or 2 between code sections to make it more readable ....
    Code:
    int countEvent(int *ptr, int size)
    {
        int i, even_cnt = 0;
        for(i = 0; i<=size; i++)
          {
            if(*(ptr+1)%2 == 0)
               even_cnt++;
          }
        return even_cnt;
    }
    2) You are not moving your pointer along the array *(ptr+1) always points to the same element, perhaps you wanted *(ptr + i) in that spot.

    3) You are overrunning your array's bounds... in C arrays start at 0 and go to size-1, thus an array of 10 elements has valid indexes of 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9 .... so your loops have to stop one short of the size... for (1 = 0; i < size; i++)

    4) Although you may not have learned about bitwise operations yet, a much easier way of checking for odd/even integers is to and (&) it with 1 as in y = x & 1; For even numbers y = 0, for odd numbers y = 1.

    5) When calling your function from main you do not need the & (address of) operator infront of the array name.

    6) The correct form of main() is int main (void).

  4. #4
    Registered User
    Join Date
    Jul 2011
    Posts
    16
    Quote Originally Posted by CommonTater View Post
    OK... a few things....

    1) Learn to indent your code and leave a blank line or 2 between code sections to make it more readable ....
    Code:
    int countEvent(int *ptr, int size)
    {
        int i, even_cnt = 0;
        for(i = 0; i<=size; i++)
          {
            if(*(ptr+1)%2 == 0)
               even_cnt++;
          }
        return even_cnt;
    }
    2) You are not moving your pointer along the array *(ptr+1) always points to the same element, perhaps you wanted *(ptr + i) in that spot.

    3) You are overrunning your array's bounds... in C arrays start at 0 and go to size-1, thus an array of 10 elements has valid indexes of 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9 .... so your loops have to stop one short of the size... for (1 = 0; i < size; i++)

    4) Although you may not have learned about bitwise operations yet, a much easier way of checking for odd/even integers is to and (&) it with 1 as in y = x & 1; For even numbers y = 0, for odd numbers y = 1.

    5) When calling your function from main you do not need the & (address of) operator infront of the array name.

    6) The correct form of main() is int main (void).
    it seem i got lots of mistake in C languages le.. thanks for your advice... i'll be work hard to get 'A' in the subject... thank you!

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Vinsento View Post
    it seem i got lots of mistake in C languages le.. thanks for your advice... i'll be work hard to get 'A' in the subject... thank you!
    We learn from our mistakes...

    Conversely... if we never discover our mistakes we will keep right on making them.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 11-05-2006, 02:57 PM
  2. Function array and pointer
    By tzpb8 in forum C Programming
    Replies: 14
    Last Post: 07-31-2006, 08:22 PM
  3. pointer to array as parameter to function
    By Jurgen in forum C++ Programming
    Replies: 1
    Last Post: 05-20-2004, 02:51 PM
  4. pointer to an array in a function
    By js_badboy in forum C Programming
    Replies: 5
    Last Post: 09-13-2003, 11:09 AM
  5. Array of pointer function
    By BigAl in forum C Programming
    Replies: 4
    Last Post: 11-14-2001, 01:19 PM