Web & Social Media Analytics Previous Year Question Paper.pdf
Write a recursive function power (x, n), which returns x to the nth p.docx
1. Write a recursive function power (x, n), which returns x to the
nth power. You can assume n >= when n is 1, return x;
otherwise, recursively, power (x, n) = x * power (x, n - 1).)
Write a recursive function print Number to print a number, digit
by digit, separated by spaces, forwards. Write a recursive
function six Count that returns a count of the number of digits
that are 6. Write a recursive function two Part. The function
returns the largest power of 2 that divides a positive integer
parameter. Write a recursive function third Digit. The function
has an integer parameter and returns the third digit in its
parameter. If the parameter has just one or two digits, the
function returns 0 because there is no third digit.
Solution
4)
/**
The C program that deomonstrates the method
findMax that takes an array ofpointer and returns
the maximum in the array and print to console.
*/
//pointer.c
#include<stdio.h>
#include<conio.h>
//function prototype
2. int findMax(int *vals,int numls);
int main()
{
//intialize an array of integer values
int arr[]={5,6,12,8,9};
//get address of arr to pointer,ptr variable
int*ptr=arr;
//call findMax
printf("%d",findMax(arr,5));
getch();
return 0;
}
/**
The method findMax takes a pointer variable and size
and finds the maximum value in the array and returns
the maximum value to main
*/
int findMax(int *vals,int numels)
{
int i,max=*vals++;
for(i=1;i<numels;i++)
if(max<*vals)
max=*vals++;
return (max);
3. }
--------------------------------------------------------------------------
----------------------------------
5)
//Reason for incorrect results of findMax
int findMax(int *vals,int numels)
{
int i,max=*vals++;
for(i=1;i<numels;i++)
//In if condition,
//The comparision between max and *vals , but if the
condition is true
//then max is assigned with next address element of the
vals since *vals++ is post increment
//but actually ,max is to be set with the element with
which it is compared but assigned with next
//address element.That is the logical error.To correct it,
remove post increment in if condition
//and after assigning the *vals, then set max=*vals++;
if(max<*vals++)
max=*vals;
return (max);
}