Question

#include <stdio.h> #pragma warning(disable : 4996) // needed in VS // CSE 240 Fall 2019 Homework...

#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 :)

Homework Answers

Answer #1

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:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
#include <stdio.h> #pragma warning(disable : 4996) // CSE 240 Fall 2016 Homework 2 Question 3 (25...
#include <stdio.h> #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: //...
Why am I receiving different outputs when I run this code on visual studio and gcc...
Why am I receiving different outputs when I run this code on visual studio and gcc (unix)?. The correct output is -52. #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)); } int x = -7, y = 3; int x_copy = x, y_copy...
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 extern char **environ;    5 void output(char *a[], char...
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 extern char **environ;    5 void output(char *a[], char *b[]) { 6 int c = atoi(a[0]); 7 for (int i = 0; i < c && b[i]; ++i) { 8 printf("%s", b[i]+2); 9 } 10 } 11 12 void main(int argc, char *argv[]) { 13      14 switch (argc) { 15 case 1: 16 for (int i = 0; environ[i]; ++i) {    17 printf("%s\n", environ[i]); 18 } 19 break; 20 default: 21 output(argv +...
Using the following code answer the next couple questions: #include<stdio.h> #include<stdlib.h> #include<string.h> /* Rewrite using a...
Using the following code answer the next couple questions: #include<stdio.h> #include<stdlib.h> #include<string.h> /* Rewrite using a pointer to char str[] */ void array_to_ptr () { int n=0, len; char str[ ] = "Hello World!"; len = strlen(str); for( n=0; n<len; n++) {     putc(str[n], stdout); } printf("\nlength = %d\n", n); } int contains (char * str, char c); int * makearray(int n); int main (void) { printf("Question #2 - array_to_ptr:\n"); array_to_ptr();   printf("\n------------------------------------\n\n"); printf("Question #3 - contains:\n"); printf("Test #1: "); if...
A program is already given to you.  There are five problems in this skeleton version of the...
A program is already given to you.  There are five problems in this skeleton version of the program, each is 10 points. All you got to do is complete the missing code in each function. What the function does is clearly stated in the name of the function.   // ASSIGNMENT ON FUNCTIONS #include <stdio.h> // Problem 1: Compile with gcc func_assignment.c -Wall // There are some warnings because there is a mismatch between // data type passed and defined. // Find...
Please answer the following C question: Read the following files called array-utils5A.c and array-utils5A.h. Build an...
Please answer the following C question: Read the following files called array-utils5A.c and array-utils5A.h. Build an executable with gcc -Wall -DUNIT_TESTS=1 array-utils5A.c The definitions for is_reverse_sorted and all_different are both defective. Rewrite the definitions so that they are correct. The definition for is_alternating is missing. Write a correct definition for that function, and add unit tests for it, using the unit tests for is_reverse_sorted and all_different as models. Please explain the logic errors present in in the definition of is_reverse_sorted...
For this assignment you will implement a simple calculator or interpreter that reads arithmetic expressions from...
For this assignment you will implement a simple calculator or interpreter that reads arithmetic expressions from a file. Specifically, you will implement the following function: /* * Reads one arithmetic "expression" at a time from a file stream, computes, then * returns the result. If there are additional expressions in the file, they are * read and computed by successive calls to “calculator”. * * “Expressions” are groups of operations (add, subtract, multiply, divide). Your * calculator will read and...
CSE 231                                         &n
CSE 231                                                                                                                        Fall 2019 Project #2 ## Language Should be Python. Assignment Specifications The program will compute and display information for a company which rents vehicles to its customers. For a specified customer, the program will compute and display the amount of money charged for that customer’s vehicle rental. The program should start by asking the user if he wants to continue. The answer is ‘Y’ for yes or ‘N’ for no. You can check the value with Boolean expressions such...
Please answer the following C question: Read the files vec5C.h, vec5C.c, and main5C.h. Build an executable...
Please answer the following C question: Read the files vec5C.h, vec5C.c, and main5C.h. Build an executable using gcc -Wall main5C.c vec5C.c Do this: Add function definitions to vec5C.c so that functions are available for dot product, sum, and cross product. Do not change the function prototypes in vec5C.h. Then add code to main to call these functions and display the dot product of u and v, the sum of u and v, and the cross product of u and v....
Question 1 Which statement is false about what Data Types defines Question 1 options: What values...
Question 1 Which statement is false about what Data Types defines Question 1 options: What values a variable cannot hold? How much memory will be reserved for the variable? What value a variable will hold? How the program will use the data type? Question 2 Using the structure below, which of the following statements about creating an array (size 20) of structures are not true? struct Employee{     string emp_id;     string emp_name;     string emp_sex; }; Question 2 options:...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT