114k views
3 votes
write a function that counts the number of times the value of y occurs in the first n values in array x. y is an integer variable, x is an array of integers, and n is an integer variable. the name of the function must be count. the parameters must appear in the order of y, x, n.

User Pan Yan
by
4.9k points

1 Answer

1 vote

Answer:

Following are the function of count:

void count(int y,int x[],int n) // function definition of count

{

int k,count=0; // variable declaration

for(k=0;k<n;++k) // iterating over the loop

{

if(x[k]==y) //check the conndition number of times the value of y occurs

{

count++; // increment of count by 1

}

}

Step-by-step explanation:

Following are the code in c language

#include <stdio.h> // header file

void count(int y,int x[],int n) // function definition of count

{

int k,count=0; // variable declaration

for(k=0;k<n;++k) // iterating over the loop

{

if(x[k]==y) //check the conndition number of times the value of y occurs

{

count++; // increment of count by 1

}

}

printf(" the number of times the value of y occurs :%d",count); // display count value

}

int main() // main method

{

int x[100],y,n,k; // variable declarartion

printf(" Enter the number of terms n :");

scanf("%d",&n); // input the terms

printf(" Enter the array x :");

for(k=0;k<n;++k) // input array x

{

scanf("%d",&x[k]);

}

printf("Enter the value of y:");

scanf("%d",&y);// input value y by user

count(y,x,n); // calling function

return 0;

}

In the given program we declared an array x ,variable y and n respectively Input the array x ,y,n by user after that we call the function count .In the count function we iterate the loop from o position to array length-1 and check the number of times the value of y occurs by using if statement i.e if(x[k]==y) if the condition of if block is true then we increment the count variable Otherwise not .Finally display the count variable which describe the number of count.

Output

Enter the number of terms n :5

1

2

2

56

5

Enter the value of y:2

the number of times the value of y occurs :2

Enter the number of terms n :5

1

2

3

56

5

Enter the value of y:26

the number of times the value of y occurs :0

User Vodokan
by
5.1k points