Question

#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:
// 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\n\n", (++x), result);
        
        // Why did this error occur? Please provide the answer in your own words below following "Explanation: "
        printf("Explanation: __________________________________________________________________\n\n\n"); // (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\nmacro_2(x, y) = %d\n\n", 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) = __ \n"); // (5 points)
        printf("In GCC: the result of function_1(x, y) = __ and macro_2(x, y) = __ \n\n"); // (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: __________________________________________________________________\n\n"); //  (5 points)
}

// Do not edit any of the following code  
int main()
{
        int x = 4, y = 5;

        printf("Part 1:\n\n");
        part1(x);
        printf("Part 2:\n\n");
        part2(x, y);

        return 0;
}

Please help me to answer part 1 and part 2

Homework Answers

Answer #1

For the moment, I am uploading answer for part 1 only. I don't have VS but I am installing it. As soon as I installed it. I will edit my answer for part 2.

#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:
// 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\n\n", (++x), result);
  
// Why did this error occur? Please provide the answer in your own words below following "Explanation: "
printf("Explanation: We know that macro put the variable as it is in the equation and then calculate the result; when it pass (++m) to macro, it passes (++4) not 5; so while calculating the macro,it will check the condition either(++4 which become)>0 which evaluates to true and value of expression will be (++5)i.e., 6. In order to get 5, we should pass m++ in place of ++m.\n\n\n"); // (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\nmacro_2(x, y) = %d\n\n", 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) = __ \n"); // (5 points)
printf("In GCC: the result of function_1(x, y) = -273 and macro_2(x, y) = -549 \n\n"); // (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: __________________________________________________________________\n\n"); // (5 points)
}

// Do not edit any of the following code
int main()
{
int x = 4, y = 5;

printf("Part 1:\n\n");
part1(x);
printf("Part 2:\n\n");
part2(x, y);

return 0;
}

Hope it helps. For the query related to part 1, please refer back to me.

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
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 +...
This is C programming assignment. The objective of this homework is to give you practice using...
This is C programming assignment. The objective of this homework is to give you practice using make files to compose an executable file from a set of source files and adding additional functions to an existing set of code. This assignment will give you an appreciation for the ease with which well designed software can be extended. For this assignment, you will use both the static and dynamic assignment versions of the matrix software. Using each version, do the following:...
We see that this computes the product of two matrices. Add a new kernel code, called...
We see that this computes the product of two matrices. Add a new kernel code, called sum, to compute the sum of the two matrices. #include <stdio.h> #include <math.h> #include <sys/time.h> #define TILE_WIDTH 2 #define WIDTH 6 // Kernel function execute by the device (GPU) __global__ void product (float *d_a, float *d_b, float *d_c, const int n) {    int col = blockIdx.x * blockDim.x + threadIdx.x ;    int row = blockIdx.y * blockDim.y + threadIdx.y ;    float...
C CODE PLZ! All instructions are in sections of code #include <stdio.h> /* TODO: Define 3...
C CODE PLZ! All instructions are in sections of code #include <stdio.h> /* TODO: Define 3 functions input, gcd and lcm in such a way that the main function below compiles correctly and has the correct behavior. The input function prompts the user to enter a non-negative integer. If the user enters a negative integer, the function prints a "sorry" statement and prompts the user again. It keeps on prompting until the user enters a non-negative number. The input function...
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...
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:...
Miscellaneous C knowledge. (Put all answers in the same q5.txt file, they’re all short.) (a) [2...
Miscellaneous C knowledge. (Put all answers in the same q5.txt file, they’re all short.) (a) [2 marks] On an old 16-bit computer system and its C compiler,sizeof(double)=8andsizeof(int)=2. Given the two type definitions below, what weresizeof(s)andsizeof(lingling)on that system? Assume that ins, there is no gap between the twofields. typedef struct s { double r[5];int a[5]; } s; typedef union lingling { double r[5];int a[5]; } lingling; (b) [2 marks] Given the following declarations, two questions: What is the type ofq? Whatis...
7. For the parametric curve x(t) = 2 − 5 cos(t), y(t) = 1 + 3...
7. For the parametric curve x(t) = 2 − 5 cos(t), y(t) = 1 + 3 sin(t), t ∈ [0, 2π) Part a: (2 points) Give an equation relating x and y that represents the curve. Part b: (4 points) Find the slope of the tangent line to the curve when t = π 6 . Part c: (4 points) State the points (x, y) where the tangent line is horizontal
IN C++ - most of this is done it's just missing the bolded part... Write a...
IN C++ - most of this is done it's just missing the bolded part... Write a program that creates a class hierarchy for simple geometry. Start with a Point class to hold x and y values of a point. Overload the << operator to print point values, and the + and – operators to add and subtract point coordinates (Hint: keep x and y separate in the calculation). Create a pure abstract base class Shape, which will form the basis...
QUESTION 1 For the following recursive function, find f(5): int f(int n) { if (n ==...
QUESTION 1 For the following recursive function, find f(5): int f(int n) { if (n == 0)    return 0; else    return n * f(n - 1); } A. 120 B. 60 C. 1 D. 0 10 points    QUESTION 2 Which of the following statements could describe the general (recursive) case of a recursive algorithm? In the following recursive function, which line(s) represent the general (recursive) case? void PrintIt(int n ) // line 1 { // line 2...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT