I am not getting desired output for a C program -


i doing c program sample input not giving sample output if use the. think program not calling function. guess have declared incorrectly. or logic wrong. getting 0 output. question given below.

write c function find kth occurrence of integer n in sequence of non-negative integers, , call function main.

your function should according following declaration:

int find(int n, int k); 

input

you given input in 2 lines:

the first line contains non-negative integer, n, , positive integer k, in order. have find kth occurrence of n in sequence below.

the second line consists of sequence of non-negative integers, terminated -1. -1 not part of sequence.

output

if n occurs k times in sequence, output index of kth occurrence of n in sequence.

if n not occur k times in sequence, output -1.

(for example, second occurrence of integer 3 in sequence 1 1 3 2 3 -1 @ position 4 in sequence. first index in sequence 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 not match 1 in assignment - you're supposed take 2 arguments, neither of sequence of values you're checking against. somehow, someway, supposed access sequence within body of function, either referencing global array (bad), or reading input sequence within body of function (slightly less bad, , intent of exercise1).

  int f;               int value;       

auto variables not implicitly initialized in declaration - initial value indeterminate (it may 0, may trap representation, may valid non-zero integer value). cause problems later.

   int counter=0;     counter++; 

i think know you're trying go here, , won't work written2 - counter exists lifetime of function. each time call check, new instance of counter created , initialized 0. won't remember value stored in previous call.

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

f isn't guaranteed 0 @ point (or other specific value). could 0, or non-zero value, or trap representation (a bit pattern not correspond valid integer value).

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

at point, counter ever going 1 - initialize 0 @ function entry , increment it, never touch again. value ever going indeterminate or 1.

so, how should proceed here and satisfy requirements of assignment?

the less bad option read sequence within check (or find) function, although that's still pretty bad (again, i/o should separate operation, , we're assuming input comes through stdin).

int find( int n, int k ) {   int next; // next value in sequence   ... // additional items index, number of matches, etc.    while ( scanf( "%d", &next ) == 1 && next != -1 )   {      // update index, next match n, k'th match, etc.   }   ... } 

scanf poor tool interactive input, @ point simpler approach.


  1. which, honestly, isn't better keeping global array. i/o should factored out computation whenever possible, , if function *is* required read input stream, stream should specified parameter function - shouldn't force code assume input comes through stdin.
  2. counter need declared static retain value call call.


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -