Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

2) Read C_part1.pptx and/or text section 1.4.2 Macros are available in most high-level programming languages. The body of a macro is simply used to replace

2) Read C_part1.pptx and/or text section 1.4.2

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 the side-effect, unwanted, or unexpected modifications to variables.

Macros should be used cautiously. The main purpose of the following programs are 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 definition/call, function definition/call, if-then-else and loop structures, etc.

Observe each of the functions below and understand their functionality. 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) {

return a;

} else {

return b;

} }

int oddf(int a) {

if ( a % 2 == 0) {

return 0;

} else {

return 1;

} }

2.1 Writefourmacrostore-implementthegivenfourfunctions.Name them: subm, cubem, minm, and oddm, respectively. [8 points]

2.2 Writethemainfunctiontotestthefunctionsandmacros.Usethe following test cases in the main function to call your macros: [5 points]

 int a = 5, b = 7; subf(a, b); subm(a, b); subf(a++, b--); subm(a++, b--); cubef(a); cubem(a); cubef(--a); cubem(--a); minf(a, b); minm(a, b); minf(--a, --b); minm(--a, --b); oddf(a); oddm(a); oddf(a++); oddm(a++); 

Your main function must print the results of the test run.

For questions 2.1 and 2.2, submit your program labeled as hw02q2.c

2.3 Observeandcomparewiththeoutputsofthefunctionsandthe macros. Do the functions and macros generate the same results? Submit your short answer and the screenshots of the test run and label the file hw02q2.pdf [2 points]

Programming Portion:

3. You are given a file named hw02q3.c. All instructions are given in the form of comments in the file. You are to again run the file in both Visual Studio and in GCC and observe the outputs and make changes as requested. Please read all instructions very carefully, then complete and submit the updated file as hw02q3.c. [25 points]

THE FILE -

#include  #pragma warning(disable : 4996) // CSE 240 Fall 2016 Homework 2 Question 3 (25 points) // Note: You may notice some warnings for variables when you compile in GCC, that is okay. #define macro_1(x) ((x > 0) ? x : 0) #define macro_2(a, b) (3*a - 3*b*b + 4*a * b - a*b * 10) int function_1(int a, int b) { return (3*a - 3*b*b + 4*a * b - a*b * 10); } // Part 1: // It appears that the result of macro_1 should be 5, why is the result 6? Correct the error. (5 points) void part1(int x) { int m = x, result; result = macro_1(++m); printf("macro_1(%d) = %d ", (++x), result); // Why did this error occur? Please provide the answer in your own words below following "Explanation: " printf("Explanation: __________________________________________________________________ "); // (5 points) } // Part 2: // Run this program in Visual Studio and then again in GCC. Take note of the output values for function_1(x,y) and macro_2(x,y). void part2(int x, int y) { int i, j, s, t; s = i = x; // initialize variables with value from x t = j = y; // initialize variables with value from y printf("function_1(x, y) = %d macro_2(x, y) = %d ", function_1(++i, ++j), macro_2(++s, ++t)); // Replace the 4 '__' spaces below with the actual output observed when running the code in VS and GCC. printf("In VS : the result of function_1(x, y) = __ and macro_2(x, y) = __ "); // (5 points) printf("In GCC: the result of function_1(x, y) = __ and macro_2(x, y) = __ "); // (5 points) // Explain why Visual Studio and GCC programming environments could possibly produce a different value for the same program and for the same input. printf("Explanation: __________________________________________________________________ "); // (5 points) } // Do not edit any of the following code int main() { int x = 4, y = 5; printf("Part 1: "); part1(x); printf("Part 2: "); part2(x, y); return 0; } 

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

Professional SQL Server 2012 Internals And Troubleshooting

Authors: Christian Bolton, Justin Langford

1st Edition

1118177657, 9781118177655

More Books

Students also viewed these Databases questions

Question

3. How has Starbucks changed since its early days?

Answered: 1 week ago