I am doing a c program but the sample input is not giving the sample output if I use the. I think my program is not calling the function. I guess I have declared it incorrectly. Or my logic is wrong. What I am getting is a zero as output. The question is given below.
Write a C function to find the kth occurrence of an integer n in a sequence of non-negative integers, and then call your function from main.
Your function should be according to the following declaration:
int find(int n, int k);
Input
You are given the input in two lines:
The first line contains a non-negative integer, say n, and a positive integer k, in that order. You have to find the kth occurrence of n in the sequence below.
The second line consists of a sequence of non-negative integers, terminated with a -1. The -1 is not part of the sequence.
Output
If n occurs k times in the sequence, then output the index of the kth occurrence of n in the sequence.
If n does not occur k times in the sequence, then output -1.
(For example, the second occurrence of the integer 3 in the sequence 1 1 3 2 3 -1 is at position 4 in the sequence. The first index in the sequence is 0.)
Input:
3 2
1 1 2 3 3 -1
Output:
4
Code:
#include<stdio.h>
int check(int a,int n ,int k ){
int f;
int value;
int counter=0;
counter++;
if (a==n)
{
f++;
}
if(f==k)
{
value= counter;
}
return value;
}
int main(void)
{
int n , k,a;
int tempo;
scanf("%d",&n);
scanf("%d",&k);
while(a!=-1)
{
scanf("%d",&a);
tempo=check(a,n,k);
}
printf("%d",tempo);
return 0;
}