C program to count the total number of negative elements in an array – In this article, we will explain the multiple means to count the total number of negative elements in an array.
Suitable examples and sample programs have also been added so that you can understand the whole thing very clearly. The compiler has also been added with which you can execute it yourself.
The means used in this distinct piece are as follows:
- Using Standard Method
- Using Function
- Using Recursion
As we all know, in an array, a bunch of elements are placed in any particular order, be it positive or negative elements.
Arrays are a means of showcasing the stored information in a specific pattern.
As you can see in the photo uploaded above, firstly, you have to enter the size of the array.
In this case, the size of the array is mentioned as 5.
Later on, the elements are also added accordingly.
The elements for this particular array are 9, -1, 0, -2, -5.
As we can see, there are three negative elements.
Those are -1, -2, -5.
Hence, the number or the count of the negative numbers in the array = 3.
Thus, the methods used to do the same in C programming are as follows:
Using Standard Method
- c=0, Read the size of the array and store that value into the variable n.
2) Read the entered array elements using scanf() predefined function and store the elements into the array using for loop for(i=0;i<n;i++).
3) Compare each element of the array with zero as a[i]<0,
a) If any element is less than zero then increase the count value c by 1.Repeat until all iterations of for loop for(i=0; i<n;i++).
4) Print the count value c which is the count of negative numbers in the array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
#include <stdio.h> #include <conio.h> int main() { int a[1000],i,n,c=0; printf("Enter size of the array : "); scanf("%d", &n); printf("Enter elements in array : "); for(i=0; i<n; i++) { scanf("%d", &a[i]); } for(i=0; i<n; i++) { if(a[i] < 0) c++; } printf(" count of negative numbers in array: %d",c); return 0; } |
Output:
1 2 3 4 5 6 7 |
Enter size of the array: 5 Enter elements in array: 9 -1 0 -2 -5 count of negative numbers in array: 3 |
Using Function
- The function countnegativenumbers() is the user-defined function which return the number of negative numbers in the array to the main() function.
2) The main() function calls the countnegativenumbers() function by passing array,array size as arguments.
3) The function countnegativenumbers() compares each element of the array with zero as a[i]<0. If any element is less than zero then it increases the count c value by 1. After all the iterations of “for” loop, it returns the c value to the main() function.
4) The function main() prints the count value c which represent the total negative numbers in an array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
#include <stdio.h> #include <conio.h> int countnegativenumbers(int a[],int n) { int i,c=0; for(i=0; i<n; i++) { if(a[i] < 0) { c++; } } return c; } int main() { int a[1000],i,n; printf("Enter size of the array : "); scanf("%d", &n); printf("Enter elements in array : "); for(i=0; i<n; i++) { scanf("%d", &a[i]); } printf(" count of negative numbers in array: %d",countnegativenumbers(a,n)); } |
Output:
1 2 3 4 5 6 7 8 |
output: Enter size of the array: 5 Enter elements in array: -1 -5 0 -4 -7 count of negative numbers in array: 4 |
Using Recursion
- The main() calls the countnegativenumbers() function by passing array, size of the array,i=0 as arguments.
2) The function countnegativenumbers() checks the condition i<n. If this condition is true then
a) Itcompare the each array element with zero as a[i]<0. If it is true then c value increased by 1.
b) This function calls itself by increasing i value by 1 until the condition i<n becomes false. Here the function countnegativenumbers() is the recursive function.
This function returns c value to main(), main() function prints the count value.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
#include <stdio.h> #include <conio.h> int countnegativenumbers(int a[],int n,int i) { static int c=0; if(i<n) { if(a[i] < 0) { c++; } countnegativenumbers(a,n,++i); } return c; } int main() { int a[1000],i,n; printf("Enter size of the array : "); scanf("%d", &n); printf("Enter elements in array : "); for(i=0; i<n; i++) { scanf("%d", &a[i]); } printf(" count of negative numbers in array: %d",countnegativenumbers(a,n,0)); } |
1 2 3 4 5 6 7 |
Enter size of the array: 5 Enter elements in array: -1 -2 -3 -4 -5 count of negative numbers in array: 5 |