Functions Objectives
In this Tutorial, you will practice:
• writing modular C programs that consist of functions
Section A: Sum of Two Numbers
The following program computes the sum of two numbers:
#include <stdio.h>
float sum(float, float);
void disp(float);
int main(void) {
float num1, num2, res;
printf("Please enter first number: ");
scanf("%f", &num1);
printf("Please enter second number: ");
scanf("%f", &num2);
res = sum(num1, num2);
disp(res);
return 0;
}
float sum(float numx, float numy) {
float result;
result = numx + numy;
return result;
}
void disp(float val) {
printf("The sum is %f", val);
}
(a) Compile and run the program.
(b) Rewrite the program such that the program displays the value of the digit of the sum to the left of the decimal point.
Section B: Calculating the value of sine using Taylor Series
The function sin(x) can be approximated by summing the first n terms of the infinite series sin(x) = x - (x^3/3!) + (x^5/5!) - (x^7/7!) + ... where x is expressed in radians.
Write a C program that can be used to answer the following question: For a value of x = 0.1, how many terms do you need in the power series so that the result from the series equals the result from the sin(x) library function up to the 6th decimal place.
Write a function double fact(int n) that returns the factorial value of an integer as a double. There is a standard C function double pow(double x, double y) that returns the result of x to the power y as a double type. You will need to include math.h to use this function (#include <math.h>).
Note: You may need to refer to chapter 5 of your textbook to do this question.
Section A1: Explain and correct all errors in the program.
Section A2: Write the program that displays the value of the digit of the sum to the left of the decimal point. Also record the output of the program.
Section B: Write a flow chart for the complete program, including one for the fact function.
Section B: Write down the complete program and also record the output of the program.