简体   繁体   中英

I am not getting desired output for a C program

I am doing ac 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.

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. 一个C函数,以查找非负整数序列中第n个整数n的第k个出现,然后从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;
         } 

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 exercise 1 ).

  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 written 2 - 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.

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;
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM