C Tutorial
This c program is used to read the list of integer array elements and finds the particular array element count in the array.
The occurrence of particular element logic is implemented using for loop and if statement.
numbers is the integer array variable, i is for loop iteration, n for array limit or size, search is integer variable to hold which element to be searched and count is to hold the occurrences value.
#include<stdio.h> void main() { int numbers[10], i, n, search,count=0; printf("Enter number of elements:\n"); scanf("%d", &n); /* Reading array elements */ printf("\nInput %d values: ", n); for(i=0; i<n; i++) { scanf("%d", &numbers[i]); } printf("\nEnter element to find: "); scanf("%d",&search); for(i=0;i<n;i++) { if(numbers[i]==search) { count = count + 1; } } printf("\nElement %d is available in array count: %d", search, count); }
Output:
]$ cc matching_element_count.c $ ./a.out Enter number of elements: 5 Input 5 values: 23 56 23 45 23 Enter element to find: 23 Element 23 is available in array count: 3 $ ./a.out Enter number of elements: 5 Input 5 values: 23 56 23 45 23 Enter element to find: 56 Element 56 is available in array count: 1
This c program is used to find the number of occurrence count and index location for particular array element in array.
numbers is to hold input array of integers, occurrence is to indicate the search element is present in particular index or not.
#include<stdio.h> void main() { int numbers[10], i, n, search,count=0, occurrence[10]; printf("Enter number of elements:\n"); scanf("%d", &n); /* Reading array elements */ printf("\nInput %d values: ", n); for(i=0; i<n; i++) { scanf("%d", &numbers[i]); occurrence[i] = 0; } printf("\nEnter element to find: "); scanf("%d",&search); for(i=0;i<n;i++) { if(numbers[i]==search) { occurrence[i] = 1; count = count + 1; } } printf("\nElement %d is available in array count: %d", search, count); printf("\nOccurrence index's: "); for(i=0;i<n;i++) { if(occurrence[i]==1) { printf("%d\t",i); } } }
Output:
$ cc matching_element_count.c $ ./a.out Enter number of elements: 5 Input 5 values: 34 53 34 23 34 Enter element to find: 34 Element 34 is available in array count: 3 Occurrence index's: 0 2 4 $ ./a.out Enter number of elements: 5 Input 5 values: 34 53 34 23 34 Enter element to find: 53 Element 53 is available in array count: 1 Occurrence index's: 1
C Tutorial
Privacy Policy | Copyright2020 - All Rights Reserved. | Contact us | Report website issues in Github | Facebook page | Google+ page