Tuesday, August 9, 2011

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

No comments:

Post a Comment