Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C language assignment Makefiles: For both questions you will need to use a Makefile. You should be creating a standard Makefile with targets for all

C language assignment

Makefiles: For both questions you will need to use a Makefile. You should be creating a standard Makefile with targets for "all" and "clean". You should also create object files for each of the modules, and they should be set up as proper dependencies so that they are only compiled when needed. Make sure to "cat" the makefile when creating the script, and to include the makefile when you are submitting your code.

Question 1:

For this queston, I want you to write a simple math library. The library should include four functions - add(), subtract(), multiply() and divide(). All of the functions should take in a pair of ints, and all should returns ints except divide(), which should return a double. The divide function should also check for division by zero and handle that error gracefully so that the program returns an error message to the user but the program continues to run. Write a menu that takes in a number to determine which function to call, take in the pair of numbers to pass to the function, and then calls the appropriate function and prints the result. Please note that all of these math functions should be in a file called myMath.c which has a corresponding myMath.h file that you include in your main program. You should test the functions from your main program where you #include "myMath.h".

Question 2:

Extend your myMath module such that it includes a new function that calculates the hypotenuse (c) of a right-angle triangle given the other two sides of the triangle (a and b). An example of the Pythagorean theorem is given below.

image text in transcribed

The function for calculating c should take the form:

double pythag( int a, int b )

This function will accept two integers and treat them as the two sides (a, b) of a right-angle triangle and return the length (c) of the hypotenuse. To do so you will want to use the square root and maybe the power functions from the math.h library. The calculation for c is as follows:

image text in transcribed

Note that to use the functions in math.h you will need to include the compiler flag -lm at the end of your compile statement, so you will need to modify your makefile appropriately.

Conditional compilation: To make this code more interesting, I want you to add an option via conditional compilation where, if debugging is turned on, then instead of using the pythag function, your main function uses macros to calculate the value for the hypotenuse (c) instead. To achieve this, you need to write macros that take parameters for ADD, SQRT, and POW. Build your code with conditional compilation so that when the -DDEBUG flag is used, then the three macros are used instead of the pythag function. Modify your makefile so that it has a debug target that turns on the -DDEBUG flag. When the -DDEBUG flag is turned on, the program should print out "In debugging mode..." as a part of the menu options.

Run and test the code.

Make sure that you keep the code for Assignments 4-1 and 4-2 separate.

hypotenuse C 9.43 a 5 b. 8 c=va? +62

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Programming The Perl DBI Database Programming With Perl

Authors: Tim Bunce, Alligator Descartes

1st Edition

1565926994, 978-1565926998

More Books

Students also viewed these Databases questions

Question

How do Excel Pivot Tables handle data from non OLAP databases?

Answered: 1 week ago