(C PROGRAMING PLEASE) 10. Give the function header(prototypes) for each of the f
ID: 3850571 • Letter: #
Question
(C PROGRAMING PLEASE)
10. Give the function header(prototypes) for each of the following functions.
a. Function hypotenuse that takes two double-precision floating-point arguments, side1 and side2, and returns a double-precision floating-point result.
b. Function smallest that takes three integers, x, y, z, and returns an integer.
c. Function instructions that does not receive any arguments and does not return a value. [Note: Such functions are commonly used to display instructions to a user.]
d. Function intToFloat that takes an integer argument, number, and returns a floating- point result.
11. Find the error in each of the following program segments and correct the error.
a. #define SIZE 100;
b. int b[10] = { 0 }, i;
for (i = 0; i <= 10; ++i) {
b[i] = 1;
c. #define VALUE = 120;
d. int a[2][2] = { { 1, 2 }, { 3, 4 } }; a[1, 1] = 5;
12. Find the error (if any) in the following code and specify how to correct it or predict the output along with brief explanation:
a. void product(void) {
int a, b, c, result;
printf("%s", "Enter three integers: ")
scanf("%d%d%d", &a, &b, &c);
result = a * b * c;
printf("Result is %d", result);
return result; }
13. Compare Iteration with Recursion.
Explanation / Answer
10)
A function prototype is
return_type function_name(argument_type argument_name,argument_type argument_name .........);
argument_name is optional.
a)the function name is hypotenuse. The double-precision floating point is nothing but double number that it is it has 16 significant bits.So the Prototype of the function is as follows
double hypotenuse(double side1, double side2);
b)when a function returns nothing its return type should be void.
void smallest(int x,int y,int z);
c)since it will not take any argument and it will not return any value the prototype is
void instructions (void);
d)
float intToFloat( int number);
11)
a) #define SIZE 100;
#define is preprocessor directive
syntax: #define Identifier value
It makes the compiler to replace every occurance of Identifier with the value given.
Preprocessor directives should not be terminated by using semicolon
So the line should be as
correct: #define SIZE 100
b)int b[10] = { 0 }, i;
for (i = 0; i <= 10; ++i) {
b[i] = 1;
I dont think there is any error except for missing of } after for i.e
int b[10] = { 0 }, i;
for (i = 0; i <= 10; ++i) {
b[i] = 1;
}
c)#define VALUE = 120;
#define is preprocessor directive
syntax: #define Identifier value
so there should not be '=' sign
correct: #define VALUE = 120
d)int a[2][2] = { { 1, 2 }, { 3, 4 } }; a[1, 1] = 5;
one dimensional arrays should be declared array_name[size].so,
Correct: int a[2][2] = { { 1, 2 }, { 3, 4 } }; a[1]= 5;
12)
void product(void) {
int a, b, c, result;
printf("%s", "Enter three integers: ")
scanf("%d%d%d", &a, &b, &c);
result = a * b * c;
printf("Result is %d", result);
return result; }
Error:
1)Here the function declartion says that the function will not return any value by giving void in place of return type.
But the function has return statement which is illegal.
2) the printf statement should be terminated by semicolon, and
printf("%s", "Enter three integers: ")
is not terminated by ';' hence causing syntax error.
corrected program:
void product(void) {
int a, b, c, result;
printf("%s", "Enter three integers: ");
scanf("%d%d%d", &a, &b, &c);
result = a * b * c;
printf("Result is %d", result);
}
or
int product(void) {
int a, b, c, result;
printf("%s", "Enter three integers: ");
scanf("%d%d%d", &a, &b, &c);
result = a * b * c;
printf("Result is %d", result);
return result; }
--->when this function is called in main it produces output as
Enter three integers: 10 12 5
Result is 600
13)
Eg: all for loops,while loops and do while loops
Using itreration to find factorial of n
int fact=1;
for( i=1;i<=n;i++)
fact=fact*i;
Eg:
int Factorial(int x)
{
int fact=1;
if(n!=0)
fact=fact*factorial(x-1);
return fact;
}
Here function factorial is recursive as it is called within itself.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.