Join Regular Classroom : Visit ClassroomTech

Problem 11:
The function/method printFibonacci accepts an integer num, representing a number. The function/method printFibonacci prints first num numbers of fibonacci series.
For example,given input 5, the function should print the string “01123”(without quotes). It compiles successfully but fails to give the desirable result for some test cases. Debug the code.

void printFibonacci(int num)
{
long num1=0;
long num2=1;
for(int i=1;i<num;++i)
{
printf("%ld",num1);
long sum=num1+num2;
num2=sum;
num1=num2;
}
}

Solution in C

void printFibonacci(int num)
{
long num1=0; long num2=1;
for(int i=1;i<=num;++i)
{
printf(“%ld “,num1); long sum=num1+num2;
num1=num2; num2=sum;
}
}

Problem 12:
The function/method selectionSortArray performs an in-place selection sort on the given input list which will be sorted in ascending order. It accepts two arguments-len,an integer representing the length of input list and arr, alist of integers representing the input list respectively. It compiles successfully but fails to get the desired result .

void selectionSortArray(int len,int *arr)
{
int x=0,y=0;
for(x=0;x<len;x++)
{
int index_of_min=x;
for(y=x;y<len;y++)
{
if(arr[index_of_min]>arr[x])
{
index_of_min=y;
}
}
int temp=arr[x];
arr[x]=arr[index_of_min];
arr[index_of_min]=temp;
}
}

Solution in C

void selectionSortArray(int len,int *arr)
{
int x=0,y=0;
for(x=0;x<len;x++)
{
int index_of_min=x;
for(y=x;y<len;y++)
{
if(arr[index_of_min]>arr[y])
{
index_of_min=y;
}
}
int temp=arr[x];
arr[x]=arr[index_of_min];
arr[index_of_min]=temp;
}
for(x=0;x<len;x++)
{
printf(“%d “,arr[x]);
}
}

Probelm 13:
The function/method descendingSortArray performs an in-place sort on the given input list which will be sorted in descending order. It accepts two argument-len of an array and array of elements. It compiles succeszsfully but fails to get the desirable output.

void descendingSortArray(int len, int *arr)
{
int small,pos, i,j,temp;
for(i=0;i<=len-1;i++)
{
for(j=i;j<len;j++)
{
temp=0;
if(arr[i]>arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
}

Solution in C

void descendingSortArray(int len, int *arr)
{
int small, i,j,temp;
for(i=0;i<=len-1;i++)
{
for(j=i+1;j<len;j++)
{
temp=0;
if(arr[i]<arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
for(i=0;i<=len-1;i++)
{
printf(“%d “,arr[i]);
}
}

problem 14:
The function/method patternPrint accepts an argument num,an integer. The function/method patternPrint prints num lines in the following pattern. For example, num=4, the pattern should be
1
11
111
1111
It compiles successfully but fails to print desirable result.

void patternPrint(int num)
{
int print=1,i,j;
for(i=0;i<num;i++)
{
for(j=0;j<=i;j++);
{
printf("%d", print);
}
printf("n");
}
}

Solution in C

void patternPrint(int num)
{
int print=1,i,j;
for(i=0;i<num;i++)
{
for(j=0;j<=i;j++)
{
printf(“%d”, print);
}
printf(“n”);
}
}

Problem 15:
Lisa always forgets her birthday which is on the 5th of july. So,develop a function/method which will be helpful to remember her birthday. It return an integer “1” if it is her birthday else return 0. It accepts two argument-month of her birthday, day of her birthday. It compiles successfully but fails to return the desirable output.

int checkBirthday(char *month,int day)
{
if(strcmp(month,"July"))||(day==5))
return 1;
else
return 0;
}

Solution in C

int checkBirthday(char *month,int day)
{
if(strcmp(month,”July”))&&(day==5))
return 1;
else
return 0;
}

Problem 16:
The function/method sameElementCount returns an integer representing the number of elements of the input list which are even numbers and equal to the elementto its right. For eg, if the input list is [4 [4 4 1 8 4 1 1 2 2] then the function should return the output 3 as it has three similar groups i.e., (4,4), (4,4),(2,2). It compiles successfully but fails to return desirable output.

int sameElementCount(int size,int inputList)
{
int i,count=0;
for(i=0;i<size;i++)
{
if((inputList[i]%2==0)&&(inputList[i]==inputList[i++])
{
count++;
}
}
return count;
}

Solution in C

int sameElementCount(int size,int *arr)
{
int i,count=0;
for(i=0;i<size-1;i++)
{
if((inputList[i]%2==0) && inputList[i]==inputList[i+1])
{
count++;
}
}
return count;
}

Problem 17:
The function/method manchester: for each element in the input array arr, a counter is incremented if the bit arr[i] is same as arr[i-1]. Then the element of the list is 0, otherwise 1. For the first bit in the input array, assume its previous bit to be 0. For example if arr is {0,1,0,0,1,1,1,0}, then it should print {0,1,1,0,1,0,0,1}. It accepts two arguments-size and arr-list of integers. Each element represents a bit 0 or 1. It compiles successfully but fails to print the desirable result. Fix the code.

void manchester(int len,int *arr)
{
int res[len];
res[0]=arr[0];
for(int i=1;i<len;i++)
{
res[i]=(arr[i]==arr[i-1]);
}
for(int i=0;i<len;i++)
{
printf("%d",res[i]);
}
}

Solution in C

void manchester(int len,int *arr)
{
int res[len];
res[0]=arr[0];
for(int i=1;i<len;i++)
{
res[i]=!(arr[i]==arr[i-1]);
}
for(int i=0;i<len;i++)
{
printf(“%d”,res[i]);
}
}

problem 18:
The function/method patternPrint accepts an argument num,an integer. The function/method patternPrint prints num lines in the following pattern. For example, num=4, the pattern should be
11
1111
111111
11111111
It compiles successfully but fails to print desirable result.

void drawPrintPattern(int num)
{
int i,j,print=1;
for(i=1;i<=num;i++)
{
for(j=1;j<=2*i;j++);
{
printf("%d",print);
}
printf("n");
}
}

Solution in C

void drawPrintPattern(int num)
{
int i,j,print=1;
for(i=1;i<=num;i++)
{
for(j=1;j<=2*i;j++)
{
printf(“%d”,print);
}
printf(“n”);
}
}

Problem 19:
The function/method countOccurence return integer representing the count of occurence of given value in input list. It accepts three arguments-len,value,arr list. It compiles successfully but fails to return the desirable result.

int countOccurence(int len, int value,int *arr)
{
int i=0,count=0;
while(i<len)
{
if(arr[i]==value)
{
count+=1;
}
}
return count;
}

Solution in C

int countOccurence(int len, int value,int *arr)
{
int i=0,count=0;
while(i<len)
{
if(arr[i]==value)
{
count+=1;
} i++;
}
return count;
}

Complete Code
Problem 20:
You are given predefined structure Time containing hour,minute and second as members. A collection of functions/methods for performing some common operations on times is also available. You must use these functions/methods to calculate and return the difference. It accepts two arguments time1 and time 2 representing two times and is supposed to return an integer represents the difference in the number of seconds. You must complete the code so that it passes all the test cases.

Note: The following structure is used to represent the time and is already implemented in the default code.

typedef struct
{
int hour, int minute, int second;
}Time;
int Time_compareTo(const Time* tim1,const Time* time2)
{
/* Return 1, if time1>time2 Return -1 if time1<time2
or, return 0, if time1==time2 This can be called as
*if time1 and time2 are two times then
*Time_compareTo(time1,time2);*/
}
void Time_addSecond(Time * time)
{
/* Add one second in the time;
}
int difference_in_times(Time *time1,Time * time2)
{
//write code
}

Solution in C

#include<stdio.h>
typedef struct
{
int hour;
int minute;
int second;
}Time;
int Time_compareTo(const Time*,const Time*);
int difference_in_times(Time*,Time*);
int t1s,t2s; int main()
{
Time t1,t2;
t1.hour=11;
t1.minute=43;
t1.second=50;
t2.hour=9;
t2.minute=27;
t2.second=57;
t1s=t1.hour*60*60+t1.minute*60+t1.second;
t2s=t2.hour*60*60+t2.minute*60+t2.second;
printf(“difference of times duration:%d”,difference_in_times(&t1,&t2));
}
int Time_compareTo(const Time* time1,const Time* time2)
{
if(t1s>t2s)
return 1;
else
return -1;
return 0;
}
int difference_in_times(Time* time1,Time* time2)
{
if(Time_compareTo(time1,time2)==1)
return t1s-t2s;
else
return t2s-t1s;
return 0;
}