Question

In: Computer Science

Write a function with the following prototype: int add_ok (int s, unsigned x, unsigned y); This...

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;
}

Solutions

Expert Solution


#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;
}


Related Solutions

Part 1.1 Write a function whose prototype is void exchange ( /*inout*/ int *p, /*inout*/ int...
Part 1.1 Write a function whose prototype is void exchange ( /*inout*/ int *p, /*inout*/ int *q ) that takes two pointers to integer variables and exchanges the values in these variables. Part 1.2 Write a function whose prototype is char lastChar ( /*in*/ const char *str ) that takes a nonempty C-String as parameter and returns the last character in the string. For example the call lastChar ("srjc") will return the character c. Part 1.3 Define an array of...
Reimpelment a function called bubble_sort that has the following prototype. bubble_sort(int *array, int size, pointer to...
Reimpelment a function called bubble_sort that has the following prototype. bubble_sort(int *array, int size, pointer to a function) Pre condition array - a pointer to a an array of size element. pointer to function - a pointer to a function that compares two values (depending on sorting in ascending order or descending order) Post condition Sort the array in ascending or descending based on the the pointer to a function. Call the function bubble_sort to sort the array in ascending...
Write a Haskell function combine :: Int -> Int -> Int -> Int with the following...
Write a Haskell function combine :: Int -> Int -> Int -> Int with the following behavior: • When x, y, and z all correspond to digit values (i.e., integers between 0 and 9, inclusive), combine x y z returns the integer given by the sequence of digits x y z. (That is, x is treated as the digit in the hundreds place, y is treated as the digit in the tens place, and z is treated as the digit...
Consider the following function: int counter( int n) { int s = 0;    for ( int...
Consider the following function: int counter( int n) { int s = 0;    for ( int i = 0; i < n; i++)             for ( int j = i; j > 0; j--)                        s = s + 1;   return s; } Part (a): How many times "s=s+1;" will be executed? Determine a precise count.  Show all your work. Part (b): What is the time complexity of the "counter" function in terms of Big-O notation? Justify and show all your work.
static int product(int x,int y){ if(x==0||y==0){//checking if x or y is 0 return 0;//if x or...
static int product(int x,int y){ if(x==0||y==0){//checking if x or y is 0 return 0;//if x or y is 0, then the return value and x*y will be zero. }else if(y<0&&x<0){ x=-x;//Changing the sign of x y=-y;//Changing the sign of y }else if(x>=1){ return (y+product(x-1,y)); } return (x+product(x,y-1)); } find the space complexity and the time complexity of the above algorithm.
Write the MIPS assembly version of this C code: int weird(char[] s, int x) { int...
Write the MIPS assembly version of this C code: int weird(char[] s, int x) { int i; for (i = 0; i < x; i++) { if (s[i] == ‘A’) { return power(10, i); } } return -1; } int power(int base, int i) { int j = 0; while (j < base) { base = base * base; j++; } return base; }
Given the method static int test(int x, int y, int z){ return(x=y+z); } Which of the...
Given the method static int test(int x, int y, int z){ return(x=y+z); } Which of the follwing is true: public static void main(String[] args) { A System.out.println(test ( 7, 14, 23)) ; B System.out.println(test ( test ( 7,9, 14) , 23 ); C System.out.println( test ( 14, 23 ) ; D System.out.println(test(1,2,4), 14, 23)) ;
int get_value(int array[], unsigned int index) { ????? } int main() { int data[] = {1,...
int get_value(int array[], unsigned int index) { ????? } int main() { int data[] = {1, 2, 3, 4}; get_value(data, 2) = 100; } Write ????? that returns the value in the array at index: Question Blank type your answer...
A consumes two goods, x and y. A ’s utility function is given by u(x, y)...
A consumes two goods, x and y. A ’s utility function is given by u(x, y) = x 1/2y 1/2 The price of x is p and the price of y is 1. A has an income of M. (a) Derive A ’s demand functions for x and y. (b) Suppose M = 72 and p falls from 9 to 4. Calculate the income and substitution effects of the price change. (c) Calculate the compensating variation of the price change....
Create a function called merge that takes two int vectors (x and y) as argument and...
Create a function called merge that takes two int vectors (x and y) as argument and returns an int vector (z) that is the result of merging the two vectors x and y. Here is an example to explain how the merge function works: If the vector x has the following values: 1 2 3 4 5, and the vector y has the following: 6 7 8 9 10 11 12, then the merging vector z will have: 1 6...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT