Page 1 of 1

Programming Exercise (50 points) 1. Macros are available in most high-level programming languages. The body of a macro i

Posted: Sun Jul 03, 2022 11:23 am
by answerhappygod
Programming Exercise (50 points)1. Macros are available in most high-level programming languages.The body of a macro issimply used to replace a macro-call during the preprocessing stage.A macro introduces a"true inline" function that is normally more efficient than an"out-line" function. However,macros suffer from side-effects: unwanted, or unexpectedmodifications to variables.Macros should be used cautiously. The main purpose of the followingprograms is todemonstrate the differences between a function and a macro. Otherpurposes includedemonstrating the differences between different programmingenvironments, andlearning different ways of writing comments, formatted input andoutput, variabledeclaration and initialization, unary operation ++, macrodefinitions/calls, functiondefinitions/calls, if-then-else and loop structures, etc.Start by running each of the functions below and understand theirfunctionality. The codewill need to manually be copied/typed into your editor from thisdocument. You can useeither GNU gcc under Unix or Visual Studio to implement the code inthis question.
int subf(int a, int b) { return a - b; }int cubef(int a) { return a * a * a; }int minf(int a, int b) { if (a <= b) { return a; } else { returnb; } }int oddf(int a) { if (a % 2 == 1) { return 1; } else { return 0; }}
1.1 Write four macros to re-implement the given four functions.Name them: subm, cubem,minm, and oddm, respectively. [12 points]1.2 Make a C file hw02q1.c that has the four functions and fourmacros defined in previousquestion. Write a main() function to test these functions andmacros. Use the followingtest cases in the main() to call your functions and macros in thisorder and observe theresults: [8 points]
a = 3, b = 6;subf(a, b);subm(a, b);subf(a++, b--);a = 3; b = 6; // reset a,b valuessubm(a++, b--);a = 3; b = 6;cubef(a);cubem(a);cubef(--a);a = 3; b = 6;cubem(--a);a = 3; b = 6;minf(a, b);minm(a, b);minf(--a, --b);a = 3; b = 6;minm(--a, --b);a = 2; b = 6;oddf(a);oddm(a);oddf(a++);a = 2; b = 6;oddm(a++);You must insert print statements to print answer of every functioncall and macro above,so that the expected output looks like the following:Your output should have actual answers, not zeros! Take ascreenshot of the output. Usingan image editor (such as Paint) mark the lines in color where thefunction-macro pair givesdifferent results, like oddf(a++) and oddm(a++) in figure above.Submit in a PDF file calledhw02q1.pdf.
CSE240 – Introduction to Programming Language 4 | PageHomework 02For question parts 1.1 and 1.2, submit your program as hw02q1.cfile and the screenshotfile as hw02q1.pdf.
Screenshot of problem attached below:
Programming Exercise 50 Points 1 Macros Are Available In Most High Level Programming Languages The Body Of A Macro I 1
Programming Exercise 50 Points 1 Macros Are Available In Most High Level Programming Languages The Body Of A Macro I 1 (155.65 KiB) Viewed 19 times
Programming Exercise (50 points) Macros are available in most high-level programming languages. The body of a macro is simply used to replace a macro-call during the preprocessing stage. A macro introduces a "true inline" function that is normally more efficient than an "out-line" function. However, macros suffer from side-effects: unwanted, or unexpected modifications to variables. Macros should be used cautiously. The main purpose of the following programs is to demonstrate the differences between a function and a macro. Other purposes include demonstrating the differences between different programming environments, and learning different ways of writing comments, formatted input and output, variable declaration and initialization, unary operation ++, macro definitions/calls, function definitions/calls, if-then-else and loop structures, etc. 1. Start by running each of the functions below and understand their functionality. The code will need to manually be copied/typed into your editor from this document. You can use either GNU gcc under Unix or Visual Studio to implement the code in this question. int subf(int a, int b) { return a b; } int cubef(int a) { } return a* a * a; int minf(int a, int b) { if (a <= b) { } else { } return a; } int oddf(int a) { } else { return b; if (a % 2 == 1) { return 1; } return 0;
1.1 Write four macros to re-implement the given four functions. Name them: subm, cubem, minm, and oddm, respectively. [12 points] 1.2 Make a C file hw02q1.c that has the four functions and four macros defined in previous question. Write a main() function to test these functions and macros. Use the following test cases in the main() to call your functions and macros in this order and observe the results: [8 points] 2 | Page CSE240 - Introduction to Programming Language Homework 02 a = 3, b = 6; subf(a, b); subm (a, b); subf(a++, b--); a = 3; b = 6; subm(a++, b--); a = 3; b = 6; cubef(a); cubem(a); cubef(--a); a = 3; b = 6; cubem(--a); a = 3; b = 6; minf(a, b); inm(a, b); minf(a, b); a = 3; b = 6; minm(-a, -b); a = 2; b = 6; oddf (a); oddm (a); oddf // reset a,b values.
minf(--a, -b); a = 3; b = 6; minm(-a, -b); a = 2; b = 6; oddf (a); oddm(a); oddf(a++); a = 2; b = 6; oddm (a++); You must insert print statements to print answer of every function call and macro above, so that the expected output looks like the following: subf(a, b) = 0 subm(a, b) = 0 subf(a++, b--) = 0 subm(a++, b--) = 0 cubef(a) = 0 cubem(a) = 0 cubef(--a) = 0 cubem(-a) = 0 minf(a, b) = 0 minm(a, b) = 0 minf(--a, -b) = 0 minm(--a, -b) = 0 oddf(a) = 0 oddm(a) = 0 oddf(a++) = 0 oddm(a++) = 0 Your output should have actual answers, not zeros! Take a screenshot of the output. Using an image editor (such as Paint) mark the lines in color where the function-macro pair gives different results, like oddf(a++) and oddm(a++) in figure above. Submit in a PDF file called hw02q1.pdf.