NPTEL | Week 0: Assignment 3 Question 1 Introduction to Programming in C (14 Sep- 06 Nov) 2020.



        Introduction to Programming in C       



Assignment 3_Question 1:


Write a C function to find the kth occurrence of an odd integer 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_odd(int k);

Input
You are given the input in two lines:

The first line contains a positive integer k. 
In the second line, you will be given a sequence of numbers. 

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 there are  k odd numbers in the sequence, then output the  kth
occurrence of odd in the sequence. Otherwise, output  -1.


Sample Input
2
1 1 3 2 3 4 1 -1

Sample Output
1

PROGRAM:

#include<stdio.h>

 int find_odd(int k);
   


int main()
{
  int k,r=0;
  scanf("%d",&k);
 
  
  r=find_odd(k);
  
  if(r==0)
    printf("-1");
  else
  printf("%d",r);
  

  return 0;
  
}

 int find_odd(int k)
  {
    int a[100],b[100],i=-1,n=0,l=0;
    do
  {
    i++;
    scanf("%d",&a[i]);
 
  }while(a[i]!=-1);
  
  for(int j=0;a[j]!=-1;j++)
  {
      b[j]=a[j];
      
    n++;
  }
    for(int i=0;i<n;i++)
    {
      if((b[i]%2)!=0)
      {
        l++;
        if(l==k)
          return b[i];
      }
    }
   return 0;
    
  }
FOLLOW OUR WEBSITE FROM THE BUTTON PROVIDED TO THE BOTTOM OF PAGE TO GET SOON ANSWERS OF PROGRAMS.
Disclaimer: Here you can find all nptel assignment solutions related to CS stream.These may help you for your assignment.The answers are only for verification.You cannot copy these directly from the post(code of conduct of NPTEL).These is not 100%  correct solutions.

No comments:

Post a Comment

If you have any problems related to solutions or any concept please let me know.

Copyright (c) 2020 Custom Programs All Right Reserved

Pages