#include <stdio.h>
#pragma warning(disable : 4996) // needed in VS
// CSE 240 Fall 2019 Homework 2 Question 3 (25 points)
// Note: You may notice some warnings when you compile in GCC or VS, that is okay.
#define isNegative(x) ((x < 0) ? x : 0)
#define polyMacro(a, b) (a*a + 8*a + 3*a*b - b*b)
int polyFunc(int a, int b) {
return (a*a + 8*a + 3*a*b - b*b);
}
// Part 1:
// We want to pass the value of -8 to isNegative and expect the result of
isNegative to be -8, why is the result -9? Correct the error (5 points)
void part1(int x) {
int result;
result = isNegative(--x);
printf("isNegative(-8) = %d \n\n", result);
// Why did this error occur? Please provide the answer in your own words
below.
printf("Explanation: _______\n\n\n"); // (2.5 points)
}
// Part 2:
// We want to pass decremented values of x and y to the macro and function to
compare their outputs in VS and GCC.
// Run this program in Visual Studio(VS) and then again in GCC. Fill the blanks
below with the output values for polyFunc and polyMacro.
// Then correct/edit this function so that polyFunc and polyMacro produce same
correct output of -52. // (5 points)
//
void part2(int x, int y) {
int x_copy = x, y_copy = y;
printf(" polyFunc(x, y) = %d \n polyMacro(x, y) = %d \n\n", polyFunc(--x, --
y), polyMacro(--x_copy, --y_copy));
// Replace the 4 blank spaces below with the actual output observed when
running the code in VS and GCC.
// The blanks should have the answers of unedited program. Keep the answers
in blanks as they were, after editing the program.
printf("In VS : the result of polyFunc = __ and polyMacro = __ \n");
// (5 points)
printf("In GCC: the result of polyFunc = __ and polyMacro = __ \n\n");
// (5 points)
// Explain in a short sentence why VS and GCC could possibly produce a
different value for the same program and for the same input.
printf("Explanation: _____\n\n"); // (2.5 points)
}
// Do not edit main()
int main()
{
int x = -7, y = 3;
printf("Part 1:\n\n");
part1(x);
printf("Part 2:\n\n");
part2(x, y);
while (1); // needed to keep console open in VS
return 0;
}
Hi any help would be super appreciated! this is using C on visual studio :)
The complete program with explanation is:
#include <stdio.h>
#pragma warning(disable : 4996) // needed in VS
// CSE 240 Fall 2019 Homework 2 Question 3 (25 points)
// Note: You may notice some warnings when you compile in GCC or
VS, that is okay.
#define isNegative(x) ((x < 0) ? x : 0)
#define polyMacro(a, b) (a*a + 8*a + 3*a*b - b*b)
int polyFunc(int a, int b) {
return (a*a + 8 * a + 3 * a*b - b*b);
}
// Part 1:
// We want to pass the value of -8 to isNegative and expect the
result of
// isNegative to be - 8, why is the result - 9 ? Correct the
error(5 points)
void part1(int x) {
int result;
//result = isNegative(--x);
--x;
result = isNegative(x);
printf("isNegative(-8) = %d \n\n", result);
// Why did this error occur? Please provide the answer in your
own words
// below.
printf("Explanation:\n"
"Macro is a pre-processor statement which directs pre-processor to
"
"perform search in the code and replace the found macro with the
"
"defined expression at all places. In this case code
statement\n"
"\tresult = isNegative(--x);\n"
"will get replaced with\n"
"\tresult = ((x < 0) ? x : 0)\n"
"Next thing preprocessor does is: it replaces the occurence of
"
"argument in the given expression with the value passed to it as
"
"it is. Means above statement becomes:\n"
"\tresult = ((--x < 0) ? --x : 0)\n"
"Now on when running the program the above statement will get
"
"evaluated. We have passed -7 to part1 function call. So x value is
-7."
" We are passing --x to the macro. --x is not evaluated before
passing"
" it to the macro as macro just replace it in macro expression as
it is."
" Our preprocessed statement is:\n"
"\tresult = ((--x < 0) ? --x : 0)\n"
"First the condition will be evaluated. After evaluation of (--x
< 0),"
" x becomes --8. Now as the condition is true, the ternary
operator"
" will evaluate first expression and return the result. Means, --x
"
"will get evaluated. After evaluation x becomes -9.\n\n"
"To get the correct value the pass the pre-decremented value to
the"
" isNegative macro. The solution statements would be:\n"
"\t--x;\n"
"\tresult = isNegative(x);\n\n\n"); // (2.5 points)
}
// Part 2:
// We want to pass decremented values of x and y to the macro
and function to
// compare their outputs in VS and GCC.
// Run this program in Visual Studio(VS) and then again in GCC.
Fill the blanks
// below with the output values for polyFunc and polyMacro.
// Then correct/edit this function so that polyFunc and polyMacro
produce same
//correct output of - 52. // (5 points)
void part2(int x, int y) {
int x_copy = x, y_copy = y;
printf(" polyFunc(x, y) = %d \n polyMacro(x, y) = %d \n\n", polyFunc(--x, --y), polyMacro(--x_copy, --y_copy));
// Replace the 4 blank spaces below with the actual output
observed when
// running the code in VS and GCC.
// The blanks should have the answers of unedited program. Keep the
answers
// in blanks as they were, after editing the program.
printf("In VS : the result of polyFunc = -52 and polyMacro = 33 \n");
// (5 points)
printf("In GCC: the result of polyFunc = -52 and polyMacro = -65 \n\n");
// (5 points)
// Explain in a short sentence why VS and GCC could possibly
produce a
// different value for the same program and for the same input.
printf("Explanation:\n"
"The difference is in the output of polyMacro only.\n"
"As explained in part1 about macro replacement, the
statement\n"
"\tpolyMacro(--x_copy, --y_copy)\n"
"will get converted to following preprocessed code
statement\n"
"\t(--x_copy*--x_copy + 8*--x_copy + 3*--x_copy*--y_copy -
--y_copy*--y_copy)"
"VS processing:\n"
"At run time --x_copy got evaluated 4 times so the final value of
x_copy "
"would be -11: In first evaluation it becomes -8, in second -9, in
third "
"-10 and in last -11. Now all the occurences of x_copy get replaced
with "
"the final value. Similarly --ycopy got evaluated 3 times. Starting
value "
"is 3, the final value is 0: In first evaluation t becomes 2, in
second 1 "
"and in last 0. So our expression becomes\n"
"\t-11*-11 + 8*-11 + 3*-11*0 - 0*0\n"
"\t= 121 - 88 + 0 - 0\n"
"\t= 33\n\n"
"GCC processing:\n"
"GCC works bit different while performing evaluation.\n"
"It breaks the expression in short expressions. Our
expression:\n"
"\t(--x_copy*--x_copy + 8*--x_copy + 3*--x_copy*--y_copy -
--y_copy*--y_copy)"
"get divided in four expressions:\n"
"\t1. --x_copy*--x_copy\n"
"\t2. 8*--x_copy\n"
"\t3. 3*--x_copy*--y_copy\n"
"\t4. --y_copy*--y_copy\n"
"They got evaluated separately.\n"
"\tx_copy after first expression is -9 and expression value =
81\n"
"\tx_copy after second expression is -10 and expression vale =
-80\n"
"\tx_copy after third expression is -11, y_copy is 2, expression
value = -66\n"
"\ty_copy after last expression is 0, the expression value =
0\n"
"So the final expression becomes:\n"
"\t= 81 + -80 + -66 - 0\n"
"\t= -65\n"
"One cannot the chnage the evaluation order of the compiler.
"
"So they have to change there code to get the expected
value."
"There are always undefined behavior with the pre and post ++ or --
operator in expression."
"So if same value is required on both system one should avoid using
macro and use functions instead."
"\n\n"); // (2.5 points)
}
// Do not edit main()
int main()
{
int x = -7, y = 3;
printf("Part 1:\n\n");
part1(x);
printf("Part 2:\n\n");
part2(x, y);
while (1); // needed to keep console open in VS
return 0;
}
VS Output:
Get Answers For Free
Most questions answered within 1 hours.