-1

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;
         } 
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Atanu Mondal
  • 61
  • 1
  • 12
  • **Input** does not match the **Sample input**. – Eugene Sh. Aug 18 '17 at 16:51
  • 4
    Grab a rubber duck and explain each line of the code. https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ – jmoon Aug 18 '17 at 16:52
  • @Eugene I am using that sample input mentioned above not the output I am getting is 0 instead of 4 – Atanu Mondal Aug 18 '17 at 16:53
  • 1
    Well, I am reading "*You are given the input in two lines:*". Where are the two lines? I see only one. – Eugene Sh. Aug 18 '17 at 16:54
  • @EugeneSh. sorry i have now edited the test case please check it out – Atanu Mondal Aug 18 '17 at 16:58
  • 2
    https://stackoverflow.com/questions/2069367/how-to-debug-using-gdb – Yunnosch Aug 18 '17 at 16:58
  • 3
    `f++;` is operating on an *uninitialised variable*. Please enable compiler warnings. – Weather Vane Aug 18 '17 at 17:00
  • 2
    `while(a!=-1)` is UB on the first iteration, it reads `a` without initializing it. You should use a `do { } while ()` (or add a copy of the loop's body before the first iteration). Or initialize `a`. – Filipe Gonçalves Aug 18 '17 at 17:03
  • @WeatherVane ok I have initialized f =0 still I am getting this -1963941528 not 4 as output. – Atanu Mondal Aug 18 '17 at 17:04
  • In your `check` function, add some `printf` statements to print the value of each variables in numerous places. You might be surprised at what you see. – dbush Aug 18 '17 at 17:04
  • You are supposed to *output the index of the kth occurrence of n* but you are not keeping count of the index of each number in the "array". – Weather Vane Aug 18 '17 at 17:06
  • 1
    your function doesn't follow the specification - it's meant to have 2 arguments, but you've got 3 – Chris Turner Aug 18 '17 at 17:07
  • @WeatherVane i have to do this with out an array. – Atanu Mondal Aug 18 '17 at 17:09
  • The "array" is in quotes meaning the "array" of numbers you are presented with. You don't need a formal array as you only need to process one value at a time. The problem requires you to keep count of the position in that "array". – Weather Vane Aug 18 '17 at 17:12
  • 1
    Somewhere, somehow, arrays are involved. The examples shows an array of integers for its test as the "Second" line. I will say in your defense, the instructions are poor. I would expect the function to accept a 3rd parameter of the array itself, but oh well. Maybe you are supposed to work off a global variable for the second line. – Michael Dorgan Aug 18 '17 at 17:20
  • can any one please solve this? – Atanu Mondal Aug 18 '17 at 17:30

2 Answers2

4

Your check function has numerous problems:

int check(int a,int n ,int k ){

Your prototype does not match the one in the assignment - you're only supposed to take 2 arguments, neither of which is the sequence of values you're checking against. Somehow, someway, you are supposed to access that sequence from within the body of the function, either by referencing a global array (bad), or by reading the input sequence from within the body of the function (slightly less bad, and probably the intent of the exercise1).

  int f;           
   int value;      

auto variables are not implicitly initialized in a declaration - their initial value is indeterminate (it may be 0, it may be a trap representation, it may be a valid non-zero integer value). This will cause problems later.

   int counter=0;
    counter++;

I think I know what you're trying to go for here, and it won't work as written2 - counter only exists for the lifetime of the function. Each time you call check, a new instance of counter is created and initialized to 0. It won't remember the value stored in it from a previous call.

  if (a==n)
  {
    f++;

f isn't guaranteed to be 0 at this point (or any other specific value). It could be 0, or it could be any non-zero value, or even a trap representation (a bit pattern that does not correspond to a valid integer value).

  }
  if(f==k)
  {
    value= counter;
  }
 return value;
}

At this point, counter is only ever going to be 1 - you initialize it to 0 at function entry and immediately increment it, then you never touch it again. So value is only ever going to be indeterminate or 1.

So, how should you proceed from here and satisfy the requirements of the assignment?

The less bad option is to read the sequence from within the check (or find) function, although that's still pretty bad (again, I/O should be a separate operation, and we're assuming all input comes through stdin).

int find( int n, int k )
{
  int next; // next value in the sequence
  ... // additional items for index, number of matches, etc.

  while ( scanf( "%d", &next ) == 1 && next != -1 )
  {
     // update index, does next match n, is it the k'th match, etc.
  }
  ...
}

scanf is a poor tool for interactive input, but at this point is the simpler approach.


  1. Which, honestly, isn't any better than keeping a global array. I/O should be factored out from computation whenever possible, and if a function *is* required to read from an input stream, that stream should be specified as a parameter to the function - you shouldn't force your code to assume all input comes through stdin.
  2. counter would need to be declared static for it to retain its value from call to call.

John Bode
  • 119,563
  • 19
  • 122
  • 198
1

My solution is totally extension of what John Bode said above and as John Bode said, you are using more parameters than the preferred. You should stick to only 2 parameters. And as you have two parameters n(for search element) and K(k th occurrence) you cant pass an sequential array to that function, So you should start reading(scanning) the sequence inside the find().

As the program clearly says it terminates with -1. You can use this to end the loop in terminating the find function. Scan function returns true as long as it reads. even for -1 it returns true so you should use the value!=-1. And inside the loop you can use your logic of matching and finding the index number.

int find(int n, int k){
   int next;
   int match=0;
   int  index=0; //for parsing the sequence
   while( scanf("%d", &next) ==1 && next!=-1){
       if(next == n){
           match++;
           if(match==k)
              return index;
        }
        index++; //move the index
    }
    return -1;
 }
Dinesh
  • 7,569
  • 4
  • 11
  • 20