73.3k views
5 votes
The following program is suppose to use the sin() function in the math library and write out an input's absolute value over an interval. So for example if sine(0.6) is 0.564 then its absolute value is the same (0.564). But if sine(2.4) is -0.675 then its absolute value is 0.675. #include #include /* has sin(), abs(), and fabs() */ int main(void) { double interval; int i; for(i = 0; i <30; i++) { interval = i/10.0; printf("sin( %lf ) = %lf \t", interval, abs(sin(interval))); } printf("\\+++++++\\"); return 0; }

2 Answers

2 votes

Final answer:

The given code contains a mistake in the usage of the abs() function. To compute the absolute value of a floating-point number, the fabs() function should be used instead.

Step-by-step explanation:

The given program is supposed to use the sin() function from the math library to compute the absolute value of sine for different intervals. However, the program contains a mistake in the usage of the abs() function. The abs() function is used for integers, but sin() returns a floating-point value. To compute the absolute value of a floating-point number, the fabs() function should be used instead.

Here is the corrected code:

#include <stdio.h>
#include <math.h>
int main(void) {
double interval;
int i;
for(i = 0; i < 30; i++) {
interval = i/10.0;
printf("sin(%lf) = %lf\\", interval, fabs(sin(interval)));
}
printf("\\+++++++");
return 0;
}

With this correction, the program will correctly compute and print the absolute value of sine for the given intervals.

User Cengizkrbck
by
3.2k points
10 votes

Answer:

#include <stdio.h>

#include <math.h> /* has sin(), abs(), and fabs() */

int main(void) {

double interval;

int i;

for(i = 0; i <30; i++) {

interval = i/10.0;

printf("sin( %.1lf ) = %.3lf \t", interval, abs(sin(interval)));

}

printf("\\+++++++\\");

return 0;

}

Step-by-step explanation:

The C source code defines a void main program that outputs the absolute value of the sine() function of numbers 0.1 to 3.0.

User Inigo Flores
by
3.5k points