C/C++ Programming Answers. Tutorial 3. Decisions and Loops

1. Explain specifically why GOTO is considered harmful.

Solution: Because it can lead to ``spaghetti code''. With undisciplined use of gotos, you can branch from anywhere in a program to anywhere in a program. So programmers can physically separate portions of a program that should be kept close together (i.e., parts of an if-then-else structure). Ultimately, the use of gotos makes programs harder to read and maintain.


2. Assume that the collateral operator is ``,'' and that the sequential operator is ``;''. Rewrite the following program using the collateral operators whenever you can (hint: look for read/write conflicts). In other words it is required to define the operations that can be executed in parallel (,) and consequently (;).

   x = 4;
   y = 23;
   z = x + y;
   printf("%d", z);
   x = zoo(x, y, z);

Solution:

   x = 4, y = 23;
   z = x + y;
   printf("%d", z), x = zoo(x, y, z);  /* Assuming zoo is side effect free!*/

3. Write a simple C program that converts Fahrenheit temperature between 0 to 300 degree into equivalent Celsius.

Solution:


void main(int argc, char* argv[])
 {
                int fahr, cel;
                int lower, upper, step;
                lower=0;
                upper=300;
                step=20;

                fahr=lower;
                while (fahr <=upper) {
            cel=5*(fahr-32/9);
            printf("fahr = %d",fahr);
            printf("\n");
            printf("cel = %d \n",cel);
            fahr=fahr+step;
                }
          }
}

4. Write a weight conversion program on C. Hint: the ratio of kilo/pound = 0.45359.

Solution:
#define KILOS_PER_POUND .45359
main()
{       int pounds;

        printf(" US lbs      UK st. lbs       INT Kg\n");

        for(pounds=10; pounds < 250; pounds+=10)
        {       int stones = pounds / 14;
                int uklbs = pounds % 14;
                float kilos = pounds * KILOS_PER_POUND;
                printf("   %d           %d   %d        %f\n",
                                   pounds, stones, uklbs, kilos);
        }
}

5. Interpret the following command:

for (hi = 100, lo = 0; hi >= lo; hi--, lo++)

SolutionIt is possible to squeeze several statements into a first or third position, separating them with commas. This allows a loop with more than one controlling variable. The example above illustrates the definition of such a loop, with variables hi and lo starting at 100 and 0 respectively and converging.


6. A function returning the length of a string is given below. Will this function shown below produce correct result? Hint: Remember that the string is represented as an array of characters terminated by a null character '\0'.

int string_length(string)
char string[];
{       int i = 0;
        do { i++;
        } while (string[i] != '\0')

        return(i);
}

SolutionIt will not produce a correct result because of the looping. It will give the following answer: (length + 1).

Correct program code:

int string_length(string)
char string[];
{       int i = 0;

        while (string[i] != '\0')
                i++;

        return(i);
}