All Exams Test series for 1 year @ ₹349 only
Question

#include <stdio.h>

int foo(int S[], int size){
    if(size == 0) return 0;
    if(size == 1) return 1;
    if(S[0] != S[1]) return 1 + foo(S + 1, size - 1);
    return foo(S + 1, size - 1);
}

int main(){
    int A[] = {0, 1, 2, 2, 2, 0, 0, 1, 1};
    printf("%d", foo(A, 9));
    return 0;
}

The value printed by the given C program is _______ . (Answer in integer)

To determine the value printed by the C program, we need to understand the function foo(int S[], int size). This recursive function counts the number of unique transitions in a contiguous sequence of the array. Let's break it down step by step:

  1. The base condition checks if size is 0, returning 0 in that case. This signifies an empty array segment.
  2. If size is 1, it returns 1 because a single element is considered unique in its own scope.
  3. If the first two elements of the current array segment S are different (S[0] != S[1]), it counts this as a unique transition and continues to the rest of the array segment by calling foo(S + 1, size - 1), adding 1 for the transition.
  4. If the first two elements are the same (S[0] == S[1]), it simply skips to the next element by calling foo(S + 1, size - 1) without adding anything.

Next, apply this logic to the array:

  1. Array A[] = {0, 1, 2, 2, 2, 0, 0, 1, 1}, and size is 9.
  2. Compare sequential elements:
    • {0, 1} transition → count = 1.
    • {1, 2} transition → count = 2.
    • Next elements {2, 2}, no transition.
    • Next elements {2, 2}, no transition.
    • Next elements {2, 0} transition → count = 3.
    • Next elements {0, 0}, no transition.
    • {0, 1} transition → count = 4.
    • {1, 1}, no transition.
  3. Total unique transitions: 4.

The value printed by the program is 4. The expected range is 5, 5, which doesn't match our solution 4; confirming an initial range misinterpretation or oversight where the problem was anticipated to fit within.

IndexValueTransition
00-
111 / transition
222 / transition
32No transition
42No transition
503 / transition
60No transition
714 / transition
81No transition
Was this answer helpful?

Important Questions from Function Recursion

  1. The process of removing recursion involves replacing recursive function calls with:

  2. Consider the recursive functions represented by the following code segment:
    int bar(int n){
    if (n == 1) return 0;
    else return 1 + bar(n/2);
    }
    int foo(int n){
    if (n == 1) return 1;
    else return 1 + foo(bar(n));
    }

    The smallest positive integer n for which foo(n) returns 5 is ______. (answer in integer)
    Note: Ignore syntax errors (if any) in the function.
  3. Consider the following ANSI-C function.

    int func(int start, int end){
    int length=end+1-start;
    if((length < 1)||(start < 0)||(end < 0)){ return(0); }
    if(length%3==0){
    return(func(start+1, end));
    } else if(length%3==1){
    return(1+func(start, end-1));
    } else {
    return(func(start+2, end));
    }
    }


    The maximum possible value that can be returned from this function is ____________. (answer in integer)

    Note: Ignore syntax errors (if any) in the function.
  4. What is the value returned by the function f given below when n = 100 ?
    int f (int n)
    { if (n == 0) then return n;
    else
    return n + f(n-2);
    }
  5. What is the output of the given C language code snippet? 

    #include<stdio.h>

     void f(int n)

    {

     if(n==0)

     return; 

    printf("%d ",n); 

    f(n-1); 

    int main()

    f(3); 

    return 0; 

    }

Need Expert Advice?

Start Your Preparation with Prepp Mobile App

Download the app from Google Play & App Store
Download the app from Google Play & App Store
Prepp Mobile App