In: Computer Science
Write a function with the following prototype: int add_ok (int s, unsigned x, unsigned y); This function should return 1 if arguments x and y can be added without causing overflow, otherwise return 0. If s is 0, x and y are unsigned numbers. If s is 1, x and y are treated as signed numbers. I tried this code but it doesn't work. Does anyone help me please?
#include int add_ok(int s, unsigned x, unsigned y) {
s=x+y;
if ((s<x) || (s<y)) {
return 0; }
return 1;
}
int main()
{
int nums = new int[(sizeof(int))];
unsigned x = 3357162450;
unsigned y = 40;
printf("%d\n",add_ok(s, x, y));
scanf("%d\n", &nums);
return 0;
}
#include <cstdio> int add_ok(int s, unsigned x, unsigned y) { double doubleSum = x + y; int intSum = x + y; //printf("%lf, %d", doubleSum, intSum); if (doubleSum == intSum) { return 0; } return 1; } int main() { int *nums = new int[(sizeof(int))]; unsigned x = 3357162450; unsigned y = 40; // set this 0 -> unsigned, 1 -> signed int s = 0; // if 1 -> overflow, 0 -> no-overflow printf("%d\n",add_ok(s, x, y)); scanf("%d\n", nums); return 0; }