bsc

Comprehensive codebase and cou...
Log | Files | Refs | Activity | README | LICENSE

root / semester_1 / tuition-c / P060.c

P060.c (1108B)


      1 /* write a C program to count the occurrences of a given element in an array using a user-defined
      2 function. Create a function int count_occurrences(int arr[], int n, int target) that countts how
      3 many times target appears in the array. */
      4 
      5 #include <stdio.h>
      6 
      7 int count_occurrences(int[], int, int);
      8 void inputarr(int[], int);
      9 
     10 int main()
     11 {
     12     int n, arr[20], target, found;
     13     printf("Enter the n (Max: 20): ");
     14     scanf("%d", &n);
     15     inputarr(arr, n);
     16     printf("Enter the target: ");
     17     scanf("%d", &target);
     18     found = count_occurrences(arr, n, target);
     19     if (found)
     20     {
     21         printf("\n%d found %d times.", target, found);
     22     }
     23     else
     24     {
     25         printf("\n%d is not found.", target);
     26     }
     27     return 0;
     28 }
     29 
     30 void inputarr(int arr[], int n)
     31 {
     32     int i;
     33     for (i = 0; i < n; i++)
     34     {
     35         printf("Enter element %d: ", i + 1);
     36         scanf("%d", &arr[i]);
     37     }
     38 }
     39 
     40 int count_occurrences(int arr[], int n, int target)
     41 {
     42     int count = 0;
     43     int i;
     44     for (i = 0; i < n; i++)
     45     {
     46         if (arr[i] == target)
     47         {
     48             count++;
     49         }
     50     }
     51     return count;
     52 }
© notamitgamer • Site Built: 2026-09-05 01:53:16 UTC • git-mirror commit: c170d72 [view raw info]
Originally created with stagit • modified by notamitgamer
Forked from github.com/notamitgamer/git-mirror