Please complete the following three short functions utilizing C format: 1. Write
ID: 3780021 • Letter: P
Question
Please complete the following three short functions utilizing C format:
1. Write the definition of a function printLarger, which has two int parameters and returns nothing. The function prints the larger value of the two parameters to standard output on a single line by itself.
NOTE: In this context, the word "parameter" means "argument."
2: Write the definition of a function powerTo, which receives two parameters . The first is a double and the second is an int . The function returns a double .
If the second parameter is negative, the function returns 0. Otherwise it returns the value of the first parameter raised to the power of the second.
Instructor Notes: Note that double is a data type very similar to float. You should use it exactly the same way that you would use float. NOTE: In this context, the word "parameter" means "argument."
3: Write the definition of a function, isReverse, whose first two parameters are arrays of integers of equal size, and whose third parameter is an integer indicating the size of each array . The function returns true if and only if one array is the reverse of the other. ("Reverse" here means same elements but in reverse order.)
Instructor Notes: NOTE: In this context, the word "parameter" means "argument."
Explanation / Answer
1. Function Definition to printLarger
#include<stdio.h>
#include<conio.h>
void printLarger(int,int);
main()
{
int n1,n2;
clrscr();
printf("Enter first number:");
scanf("%d",&n1);
printf(" Enter second number:");
scanf("%d",&n2);
printLarger(n1,n2);
getch();
}
void printLarger(int n1,int n2)
{
/* Calculating the Largest Number */
n1>n2?printf("%d",n1):printf("%d",n2);
}
2. Function Definition for powerTo
#include<stdio.h>
#include<conio.h>
double powerTo(double,int);
void main()
{
double base,power;
int exp;
clrscr();
printf("Enter base value:");
scanf("%lf",&base);
printf(" Enter exponent value:");
scanf("%d",&exp);
power=powerTo(base,exp);
printf(" Power = %.1lf",power);
getch();
}
double powerTo(double base,int exp)
{
int i;
double pow=1;
if(exp>=0)
{
for(i=1;i<=exp;i++) /* Loop to calculate the base ^ exponent */
{
pow=pow*base;
}
return pow;
}
else
return 0.0;
}
3.Function definition for isReverse
#include<stdio.h>
#include<conio.h>
char* isReverse(int[],int[],int);
main()
{
int a[100],b[100],len,i;
char *p;
clrscr();
printf("Enter array size:");
scanf("%d",&len);
printf(" Enter elements into first array:");
for(i=0;i<len;i++)
scanf("%d",&a[i]);
printf(" Enter elements into second array:");
for(i=0;i<len;i++)
scanf("%d",&b[i]);
p=isReverse(a,b,len);
printf("%s",p);
getch();
}
char* isReverse(int a[],int b[],int len)
{
int i,j,flag=0;
/* Code to compare second array is reverse of first array */
for(i=0,j=len-1;i<=j;i++,j--)
{
if(a[i]!=b[j])
{
flag=1;
break;
}
}
if(flag==0)
return "True";
else
return "False";
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.