Advertisement
Guest User

Untitled

a guest
Feb 19th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.27 KB | None | 0 0
  1. #include <stdio.h>
  2. #pragma warning(disable : 4996)
  3.  
  4. // CSE 240 Fall 2016 Homework 2 Question 3 (25 points)
  5. // Note: You may notice some warnings for variables when you compile in GCC, that is okay.
  6.  
  7. #define macro_1(x) ((x > 0) ? (x -1) : 0)
  8. #define macro_2(a, b) (3*a - 3*b*b + 4*a * b - a*b * 10)
  9.  
  10. int function_1(int a, int b) {
  11. return (3*a - 3*b*b + 4*a * b - a*b * 10);
  12. }
  13.  
  14. // Part 1:
  15. // It appears that the result of macro_1 should be 5, why is the result 6? Correct the error. (5 points)
  16. void part1(int x) {
  17.  
  18. int m = x, result;
  19.  
  20. result = macro_1(++m);
  21.  
  22. printf("macro_1(%d) = %d\n\n", (++x), result);
  23.  
  24. // Why did this error occur? Please provide the answer in your own words below following "Explanation: "
  25. printf("Initially x = 4. The macro is preprocessed. x++ is incremented twice while running this code. The first time \n"
  26. "was while comparing whether the value is greater than 0. Since it was, x++ is the result for return.\n"
  27. "This comes from the fact that this is preprocessed and whe are sending a ++ in to compare. I fixed this by adding a --\n"
  28. "to the return value\n\n\n"); // (5 points)
  29. }
  30.  
  31.  
  32. // Part 2:
  33. // 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).
  34. void part2(int x, int y) {
  35. int i, j, s, t;
  36.  
  37. s = i = x; // initialize variables with value from x
  38. t = j = y; // initialize variables with value from y
  39.  
  40. printf("function_1(x, y) = %d\nmacro_2(x, y) = %d\n\n", function_1(++i, ++j), macro_2(++s, ++t));
  41.  
  42. // Replace the 4 '__' spaces below with the actual output observed when running the code in VS and GCC.
  43. printf("In VS : the result of function_1(x, y) = __ and macro_2(x, y) = __ \n"); // (5 points)
  44. printf("In GCC: the result of function_1(x, y) = __ and macro_2(x, y) = __ \n\n"); // (5 points)
  45.  
  46. // Explain why Visual Studio and GCC programming environments could possibly produce a different value for the same program and for the same input.
  47. printf("Explanation: __________________________________________________________________\n\n"); // (5 points)
  48. }
  49.  
  50. // Do not edit any of the following code
  51. int main()
  52. {
  53. int x = 4, y = 5;
  54.  
  55. printf("Part 1:\n\n");
  56. part1(x);
  57. printf("Part 2:\n\n");
  58. part2(x, y);
  59.  
  60. return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement