Skip navigation

This solution is for the book I am currently reading. All of my notes and solutions are available at Google Code.

Here is my work for problem 2.30 (my answer differs in form, but I believe is still correct, from the answer in the answer section:

Write a function with the following prototype:

/* Determine whether arguments can be added without overflow */
int tadd_ok(int x, int y);

This function should return 1 if arguments x and y can be added without causing
overflow

Answer:

int tadd_ok(int x, int y) {

  int sum;

  sum = x + y;
  if (sum >= 0 && x < 0 && y < 0) {
    return 0;
  }
  if (sum  0 && y > 0) {
    return 0;
  }
  return 1;

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: