Ask Question

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.

+1
Answers (1)
  1. 4 January, 12:41
    0
    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
    {

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

    {

    count++; / / increment of count by 1

    }

    }

    Explanation:

    Following are the code in c language

    #include / / header file

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

    {

    int k, count=0; / / variable declaration

    for (k=0; k
    {

    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
    {

    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
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “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 ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers