1. Write a preprocessor directive to accomplish each of the following: A) Define
ID: 3763540 • Letter: 1
Question
1. Write a preprocessor directive to accomplish each of the following:
A) Define the symbolic constant TRUE to have the value 1.
B) Define the symbolic constant FALSE to have the value 0.
C) If symbolic constant YES is defined, undefine it and redefine it as 1.
D) Define a macro, AREA, that takes a single argument and computes the area of a square.
E) Define macro SUM that will sum two numbers.
F) Define macro MIN2 that will determine the smallest of two numeric values.
2. Write a macro that computes the remainder when n is divided by 4.
3. Let DOUBLE be the following macro:
#define DOUBLE (x) 2*x
a) What is the value of DOUBLE (1+2)?
b) What is the value of 4/DOUBLE (2)?
c) Fix the definition of DOUBLE.
4. For each of the following macros, give an example that illustrates a problem with the macro and show how to fix it.
a) #define AVG (x, y) (x + y) / 2
b) #define AREA (x, y) (x) * (y)
Explanation / Answer
1
A) #define TRUE 1
B) #define FALSE 1
C)
#if defined(YES)
#undef YES 1
#define YES 1
#endif
D)
Macro AREA that takes a single argument and reutrns the area of sqaure
#define AREA(x) x*x
E)
Macro SUM that takes two arguments and returns the sum of two numbers
#define SUM(x,y) x+y
F)
Macro MIN2 that takes two numbers and return the minimum of two
#define MIN2(x, y) (x < y ? x :y)
-----------------------------------------------------------------------
2
Macro that give remainder divided by 4
#define REMAINDER(x) x%4;
-----------------------------------------------------------------------
3
a)
#define DOUBLE (x) 2*x
For this macro, DOUBLE (1+2)
macro DOUBLE(1) is called then it return 2*1
and result is added to 2 . So 2+2 =4
Then call macro DOUBLE(1+2) returns 4
c)
Fix the definition of DOUBLE macro
#define DOUBLE(x) (x+x)
------------------------------------------------------------
4)
a) #define AVG (x, y) (x + y) / 2
example of AVG macro
AVG(1,2) returns the (1+2)/2=1.5
b) #define AREA (x, y) (x) * (y)
AREA(1,2) returns the (1)*(2)=2
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.