#define polyMacro(a, b) (a*a + 8*a + 3*a*b - b*b)
//change this part so it will get same value with polyFunc
//change this part so it will get same value with polyFunc
//change this part so it will get same value with polyFunc
int polyFunc(int a, int b)
{
return (a * a + 8 * a + 3 * a * b - b * b);
}
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));//please change the macro so polyMacro will have the same value with polyFunc
int main()
{
int x = -7, y = 3;//give value
printf("Part 2:\n\n");//print
part2(x, y);
while (1); // needed to keep console open in VS
return 0;
}
Asked
Active
Viewed 84 times
-2

Steve Summit
- 45,437
- 7
- 70
- 103

Yuelin
- 1
-
What do you see when you run your program? – shadowspawn Sep 07 '19 at 03:28
-
There’s a `}` missing from `part2()`; what else is missing? – Jonathan Leffler Sep 07 '19 at 03:39
-
Hi! Please read [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). Your question is just a block of code; try including some explanation on what you tried, how, why, what the expected output is.... give the most detail you can to help others help you. Otherwise, no one will take the time needed to understand what you need. – 89f3a1c Sep 07 '19 at 03:40
-
Note [Why are these constructs using pre and post increment undefined behaviour?](https://stackoverflow.com/questions/949433/why-are-these-constructs-using-pre-and-post-increment-undefined-behavior) – Jonathan Leffler Sep 07 '19 at 05:46
1 Answers
1
You can modify your macro part like this :
#define polyMacro(a, b) ({ typeof(a) a1= a;\
typeof(b) b1= b;\
a1*a1 + 8*a1 + 3*a1*b1 - b1*b1;\
})
By doing so, your macro increments or decrements the integer and saves it in a1 and b1 respectively. Operations are now done on a1 and b1.
Instead of:
--x_copy*--x_copy + 8*--x_copy + 3*--x_copy*--y_copy - --y_copy*--y_copy
you get
int a1= --x_copy;
int b1= --y_copy;
a1*a1 + 8*a1 + 3*a1*b1 - b1*b1;

arvind
- 275
- 2
- 11