# Made Easy Test Series : Programming Test4

1 vote
221 views

Consider the following C code:

int getNextGap(int gap){
gap=(gap*10)/13;
if(gap<1)return 1;
return gap;
}
void mystery(int a[ ],int n){
int gap=n;
bool red=true;
while(gap!=1||red==true){
gap=getNextGap(gap);
red=false;
for(int i=0;i<(n-gap);i++){
if(a[i]>a[i+gap]){
swap(a[i],a[i+gap]);
red=true;
}
}
}
}

The array $A=\left \{ 9,4,-1,3,5,7,99,-33,104 \right \}$ passed in the above function , and n is number of array elements, then what output  will print at last?

I mostly stuck how bool function working here. Plz help me out, how program executing:(

edited
1
A={9,4,-1,3,5,7,99,-33,104}

n=9

gap=9

red=TRUE

Program enters loop when either gap!=1 or red is TRUE

1) gap=6, A={9,-33,-1,3,5,7,99,4,104}, red =TRUE

2) gap=4, A={5,-33,-1,3,9,7,99,4,104}, red =TRUE

3) gap=3, A={3,-33,-1,5,4,7,99,9,104}, red =TRUE

4) gap=2, A={-1,-33,3,5,4,7,99,9,104}, red =TRUE

5) gap=1, A={-33,-1,3,4,5,7,9,99,104}, red =TRUE

6) Since red=TRUE loop continues, gap=1, A={-33,-1,3,4,5,7,9,99,104}, red =FALSE since no swaps

LOOP TERMINATES
0

@sakharam

1st swapping for 3 and 9

right?

I mean 1st time in for loop

for(int i=0;i<(9-6);i++){

if(a[0]>a[3]){

//so 1st swapping should be in 3 and 9

Where am I missing?

1
for(int i=0;i<(9-6);i++){

if(a[0]>a[0+gap]) i.e.  if(a[0]>a[0+6]) i.e. if(a[0]>a[6])

So we check 9 and 99
0
oh , yes , bad mistake :(

let me chk once more
0
yes, thank u very much :)

is it not a complicated program?
0
If one figures out what each part of program is doing it becomes easy
I felt https://gateoverflow.in/311611/made-easy-test-series-programming-test4-function-call complicated because of the time required to solve it.
0

Yes Sorting is going IF we figure it out two three scan  i dont know why they make such easy concept into complicated (time consuming wasting)

1
Only correct dry run is necessary
0
@akshat sharmaThe program doesn't sort an array.

Example 5,4,3,2,1,0,10,9,8
0

@sakharam

In that example , it also gives sorted sequence

but i=1 loop execute one more time.

Isnot it??

If one figures out what each part of program is doing it becomes easy

which part in this program u divide??

mostly in scanning loop part there are chances of mistake

right??

1
Yes you are right

I forgot that it was OR condition in while loop
1
Here I would say two parts, the function and the statements inside if branch. Also the condition in while loop
0
There is no bool datatype in C then how the code will execute?

## Related questions

1 vote
1
229 views
Consider the following function int fun(int a[ ],int l, int target){ int i=0,j=0,sum=0,count=0; while(j<l){ if(sum<target){ sum=sum+a[j]; j++; } else if(sum>target){ sum=sum-a[i]; i++; } else{ count++; sum=sum-a[i]; i++; } } if(sum==target) ... be return value of function call $fun\left ( a,16,8 \right )=$_______________ Given ans $6,$ but I got $4.$ Which one correct?? Any shortcut to evaluate??
Consider the following $C$ implementation which when given $3$ numbers a,b,c as input, find the maximum of $3$ numbers $a,b,c.$ int kickstart(int a,int b,int c) { if(B1) return a; if(a>=b) return B2; return kickstart(c,a,b); } How the boxes filled up correctly? $I)B1:a\geq b$ ... $IV)B1:a\geq b$ && $a\geq c, B2:kickstart\left ( b,c,a \right );$ Is it $I) and II)$ or $I) and IV)$
Consider the following function $foo()$ void foo(int n){ if(n<=0) printf("Bye"); else{ printf("Hi"); foo(n-3); printf("Hi"); foo(n-1); } } Let $P(n)$ represent recurrence relation, indicating number of time print statement executed. What will best recurrence for ... $n=0$ The options are confusing to me. Can someone explain the options well. Moreover , what will be constant added $1$ or $2?$