Showing posts with label Sams. Show all posts
Showing posts with label Sams. Show all posts

Tuesday, August 9, 2011

Chapter 4, exercise 10

Bug Buster: Fix the following program so that it runs correctly:


/* a program with problems... */
#include <stdio.h>
int x=1;
int main(void)
{
  if (x=1);
    printf(" x equals 1");
  otherwise
    printf(" x does not equal 1");
  return 0;
}

/* a program without problems... */
#include <stdio.h>
int x=1;
int main(void)
{
  if (x==1)
    printf("x equals 1\n");
  else
    printf("x does not equal 1\n");
  return 0;
}

Chapter 4, exercise 9

9.  Write an if statement that determines whether someone is legally an adult (age 21), but not a senior citizen (age 65).

if ( age>=21 && age<65 )

Chapter 4, exercise 8

8.  If x = 4, y = 6, and z = 2, determine whether each of the following evaluates to true or false.


a.  if( x == 4)

true

b.  if(x != y - z)

false

c.  if(z = 1)

true (note the use of the assignment operator, and not the equality operator)

d. if(y)

true

Chapter 4, exercise 7

7.  To what value do each of the following expression evaluate?


a.  (1 + 2 * 3)

7

b.  10 % 3 * 3 - (1 + 2)

0

c.  ((1 + 2) * 3)

9

d.  (5 == 5)

1


e.  (x = 5)

5

Chapter 4, exercise 6

6.  Rewrite the following nested if statements using a single if statement and logical operators.


if (x<1)
  if (x>10)
    statement;

if ( x<1 && x>10 )
  statement;

Note that this condition could never be true.

Chapter 4, exercise 5

5.  Use the conditional operator to assign the value of x to the variable y only if x is between 1 and 20.  Leave y unchanged if x is not in that range.

(If 'between' means 'inclusive of 1 and 20')

y = (x>=1 && x<=20) ? x : y;

(If 'between' means 'exclusive of 1 and 20')


y = (x>1 && x<20) ? x : y;


Chapter 4, exercise 4

4.  Write an if statement that assigns the value of x to the variable y only if x is between 1 and 20.  Leave y unchanged if x is not in that range.

(If 'between' means 'inclusive of 1 and 20')

if (x>=1 && x<=20)
  y = x;

(If 'between' means 'exclusive of 1 and 20')

if (x>1 && x<20)
  y = x;

Chapter 4, exercise 3

3.  Change the following code to count upward instead of downward.


#include <stdio.h>


int a, b;


int main(void)
{
  /* Set a and b both equal to 5. */


  a = b = 5;


  /* Print them, decrementing each time. */
  /* Use prefix mode for b, postfix mode for a */


  printf("\nPost Pre");
  printf("\n%d    %d", a--, --b);

  printf("\n%d    %d", a--, --b);
  printf("\n%d    %d", a--, --b);
  printf("\n%d    %d", a--, --b);
  printf("\n%d    %d\n", a--, --b);

  return 0;
}

#include <stdio.h>


int a, b;


int main(void)
{
  /* Set a and b both equal to 5. */


  a = b = 5;


  /* Print them, incrementing each time. */
  /* Use prefix mode for b, postfix mode for a */


  printf("\nPost Pre");
  printf("\n%d    %d", a++, ++b);
  printf("\n%d    %d", a++, ++b);
  printf("\n%d    %d", a++, ++b);
  printf("\n%d    %d", a++, ++b);
  printf("\n%d    %d\n", a++, ++b);

  return 0;
}

Chapter 4, exercise 2

2.  Rewrite the following code to be more readable.


#include <stdio.h>
int x,y; int main() { printf(
"\nEnter two numbers");scanf(
"%d %d",&x,&y);printf(
"\n\n%d is bigger",(x>y)?x:y);return 0;}


/* Note that I have put newlines in the two printf() parameters as well. */
#include <stdio.h>

int x, y;

int main()
{
  printf("\nEnter two numbers\n");
  scanf("%d %d", &x, &y);
  printf("\n\n%d is bigger\n", (x>y) ? x: y);
  return 0;
}

Chapter 4, exercise 1

1.  The following code is not well-written.  Enter and compile it to see whether it works.


#include <stdio.h>
int x,y; int main() { printf(
"\nEnter two numbers");scanf(
"%d %d",&x,&y);printf(
"\n\n%d is bigger",(x>y)?x:y);return 0;}


Yes, it works.

Chapter 4, question 10

10.  What are the compound assignment operators, and how are they useful?

The compound assignment operators consist of an operation followed by an equals sign, and they are shorthand.  For example,

x += 1;

is the same as

x = x+1;

They are useful because they can enhance readability, for a dumb compiler may result in more optimized output, and prevent an operand being evaluated more than once in cases such as :

* (get_int_pointer()) += 5;

Chapter 4, question 9

9.  In the following list, which has higher precedence?


a.  == or <

<

b.  * or +

*


c.  != or ==

Neither.

d.  >= or >

Neither.

Chapter 4, question 8

8.  If an expression evaluates to false, what value does the expression have?

0

Chapter 4, question 7

7.  Rewrite the following expression, adding parentheses, so that it evaluates to 16.


5 + 3 * 8 / 2 + 2

(5+3) * 8 / (2+2)


Chapter 4, question 6

6.  To what value does the expression 5 + 3 * 8 / 2 + 2 evaluate?

19

This is because of the precedence of * and / over +

Chapter 4, question 5

5.  To what value does the expression 10 % 3 evaluate?

1

Chapter 4, question 4

4.  If the variable x has the value 10, what are the values of x and a after each of the following statements is executed separately?


a = x++;

a is 10, x is 11.

a = ++x;

a is 11, x is 11.

Chapter 4, question 3

3.  In an expression that contains multiple operators, what determines the order in which operations are performed?

Operator precedence.  There is an operator precedence table that one can refer to.

Chapter 4, question 2

2.  What is an expression?

It is a piece of source code that evaluates to a numerical value.

Chapter 4, question 1

1.  What is the following C statement called, and what is its meaning?


x=5+8;

It's an assignment statement.  It calculates five plus eight and puts the value in a variable called x.