In this assignment you will continue to practice what you learned last class:
- Getting input from the user using
scanf
- Formatting output that is being printed to the screen using
printf
You will also gain more practice with using (calling) functions.
Furthermore, you will be introduced to a new set of library functions called math.h
As part of this assigment you will create a program in a file called math.c
. When you
complete the assigment you will submit math.c
by uploading it to Slack. If you do not
finish today, you have until the end of class (6:45pm) on Thursday, 10/12/2017 to turn it in.
Note this doesn't necessarily mean you will have time to work on it in class on 10/12. If don't
finish today, you should try and finish it before Thursday.
Copy and paste the code below into a new file and save it. Give it the name math.c
.
Upload this file to the UMBC Linux server, compile it and run it.
If, when you compile this program, you get an error about an undefined reference,
you need to add an extra option to your gcc
command. The whole command looks like gcc math.c -lm
.
#include <stdio.h>
#include <math.h>
int main()
{
/**
* Part 1
*/
printf("****** Begin part 1\n");
// your code goes here
printf("****** End part 1\n\n");
/**
* Part 2
*/
printf("****** Begin part 2\n");
// your code goes here
printf("****** End part 2\n\n");
/**
* Part 3
*/
printf("****** Begin part 3\n");
// your code goes here
printf("****** End part 3\n\n");
/**
* Part 4
*/
printf("****** Begin part 4\n");
// your code goes here
printf("****** End part 4\n\n");
/**
* Part 5
*/
printf("****** Begin part 5\n");
// your code goes here
printf("****** End part 5\n\n");
return 0;
}
The library math.h
includes several functions for doing mathematical
operations. The first one you'll be using is sqrt
which calculates
the sqaure root of a number.
First - a little bit of terminology review. A function signature is the part of the definition that defines the return type, the function's name and its arguments. You've written a number of function signatures already in this class - it's the part of the definition that looks something like:
double sqrt(double x)
That is the function signature for the sqrt
function. Just to review:
sqrt
is the name, the double
to the left of the name is the return type
and the arguments are between the parenthesis. In this case you can see that
the function sqrt
has one argument of type double
.
The function signature contains nearly all of the information you need to use the function in your program. It's also helpful you have a brief description of how the function works. Something like:
/**
* Returns the square root of x.
*/
double sqrt(double x)
You won't be writing function definitions/signatures in this assignment. However,
I will be telling you the function signature and a description for several functions
from math.h
and you will need to figure out how to use them based on that. I'll show
you how to do the first one.
Given the above signature for sqrt
use it by first prompting the user to enter a number.
Then print the square root of that number. When you print the result also print the original
number the user entered. Print both numbers to three decimal places. The output when you
run the program should look something like:
****** Begin part 1
Enter a value: 25
The square root of 25.000 is 5.000
****** End part 1
Insert your code in the section of math.c
for
Part 1, just below the line that says // your code goes here
. To be clear, that's this part:
/**
* Part 1
*/
printf("****** Begin part 1\n");
// your code goes here
printf("****** End part 1\n\n");
First, let's add the code to get a number from the user. Recall that to do that, we need to
define a variable of the appropriate type and use scanf
to assign it a value from the user.
If we look at the function definition we see that to use it, we need an argument of type double
(sqrt
has one argument called x of type double
). Adding the code to declare the variable,
prompt the user, and assign the value using scanf
, results in our program looking like:
/**
* Part 1
*/
printf("****** Begin part 1\n");
// your code goes here
double squaredValue;
printf("Enter a value: ");
scanf("%lf", &squaredValue);
printf("****** End part 1\n\n");
Compile and run the program to make sure it prompts for a value.
Next, we add the code to print the square root of squaredValue
. Looking at the function signature again, note that the return value of sqrt
is double
, so
we'll need a variable of type double
to hold it. We can declare the variable and assign it
the result all on one line like this: double squareRootOfValue = sqrt(squaredValue);
.
Together with a call to printf
to print the result the whole thing looks like:
/**
* Part 1
*/
printf("****** Begin part 1\n");
// your code goes here
double squaredValue;
printf("Enter a value: ");
scanf("%lf", &squaredValue);
double squareRootOfValue = sqrt(squaredValue);
printf("The square root of %.3lf is %.3lf\n", squaredValue, squareRootOfValue);
printf("****** End part 1\n\n");
Complete the rest of the assignment. When you are done, upload math.c
to me on Slack.
The next function you'll use is a function called floor. It's signature looks like:
// Returns the largest whole number value less than or equal to x. E.g. if
// x = 2.4, floor returns 2.0.
double floor(double x)
Prompt the user to enter a number. Then print the largest whole number value less than that number. When you print the result, also print the value the user input. Print both numbers to 2 decimal places. The output should look something like:
****** Begin part 2
Enter a value: 12.3
The floor of 12.30 is 12.00
****** End part 2
Next you'll use the pow
function to raise one number to the power of another. The
signature for pow
looks like:
// Returns x raised to the power of y
pow(double x, double y)
Prompt the user to enter one number, then another. Then print the result of the first number raised to the power of the second number. When you print the result, also print the two numbers the user entered. Print all numbers to 1 decimal place. The output should look like:
****** Begin part 3
Enter a value: 2
Enter another value: 4
2.0 raised to the power of 4.0 is 16.0
****** End part 3
The signature for fabs
is:
// Returns the absolute value of x
double fabs(double x)
Prompt the user to enter a number. Print the absolute value of that number. When you print the result, also print the number the user enters. Print all numbers to 2 decimal places. The output should look like:
****** Begin part 4
Enter a value: -4.0
The absolute value of -4.00 is 4.00
****** End part 4
The signature of fmod
is:
// Returns the remainder of x divided by y
double fmod(double x, double y)
Prompt the user to enter a number followed by a second number. Use fmod
to calculate and
print the remainder of dividing the first number entered by the second. When you print
the result, also print the two numbers entered by the user. Print all numbers to 4 decimal
places. The output should look like:
****** Begin part 5
Enter a value: 12
Enter another value: 5
The remainder when dividing 12.0000 by 5.0000 is 2.0000
****** End part 5