Thread: Numbers in an array

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    5

    Numbers in an array

    I need to be able to count the number of occurrances of the numbers 1->9 in the array 'a' and output them in the form

    printf("\n %d occurrs : %d times\n",num,occurrances)

    What do i need to do with this code so it does this?

    Code:
    int count_num(int a[], int range, int num)
    /* checks array a for number of occurrances of value */
    {
       int i, count=0;
       for (i=0; i<range; i++)
       {
    	 if (a[i] == num)
    	 {
    		++count; /* it was found */
    	 }
       }
       return(count);
    }

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    It seems to me that count_num() is a function counts how many times the number num occurs in the array a when the range is from 0 to range. The function returns the number of occurrances.

    Pass your array, the range and the number to search for to the function and store the return value in your variable occurrances.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Asked and answered here
    If you can't be bothered to read, not my problem
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. To Find Largest Consecutive Sum Of Numbers In 1D Array
    By chottachatri in forum C Programming
    Replies: 22
    Last Post: 07-10-2011, 01:43 PM
  2. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  3. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM
  4. getting numbers in an array
    By ct26torr in forum C Programming
    Replies: 6
    Last Post: 03-04-2003, 10:31 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM