Computer Science & C Programming


Simplifying code

  1. Some rules can be applied to simplyfy code:
int abs(int x) {
   if (x<0) return -x;
   return x;
}

Exercices:

a. How to simply the following code:

int i;
int r;
i=1;
r=1;
loop:
  r=r*i;
  i=i+1;
  if (i<=4) { goto loop; } else {}

b. How to transform the preceding instructions into a program factorial(n) ?
c. How to transform exercice 1 from the preceding chapter into a sub-program ?


5 - 10