UP ONE LEVEL: ENEL 339 Fall 1999 Course Home Page

ENEL 339: Programming Fundamentals
Tutorial #1 (September 23)

Author: Steve Norman
Last modified: Thu Sep 23 16:51:18 MDT 1999

Contents


Exercise 1

You were asked to draw diagrams of the stack for points one, two, three, and four in the following program:
/* ENEL 339 Fall 1999
 * example program for tutorial Sept. 23
 */
int foo(int a);

int bar(int b, int c);

int quux(int d, int e);

int main(void)
{
  int x = 77;
  int y;
  y = foo(x);

  /* point two */

  y = bar(x, y);

  /* point four */

  return 0;
}

int foo(int a)
{
  a = -a;

  /* point one */

  return a;
}

int bar(int b, int c)
{
  return quux(b, c);
}

int quux(int d, int e)
{
  int y = 10;
  y += (d + e + 5);

  /* point three */

  return y;
}
The solution was presented in the tutorial period. I do not plan to spend time on putting the solution up on the Web.

[back to top of document]


Exercise 2

You were asked to write a program to produce the following multiplication table:
     1   2   3  ...  11  12
 1   1   2   3  ...  11  12
 2   2   4   6  ...  22  24

 .   .   .   .        .   .
 .   .   .   .        .   .
 .   .   .   .        .   .

11  11  22      ... 121 132
12  12  24      ... 132 144
I didn't have time to present a solution, so here's an outline of what I would have said ...

The first row has a `blank' in the first column, so the first row can't be produced by the same code as the other rows. Here's a sketch of what I've decided so far:

/* 1st DRAFT */
#include <stdio.h>

int main()
{

  /* Print the first row. */

  /* Print all the other rows. */

  return 0;
}
Let's work out the main part of the table first. Obviously we loop from 1 to 12 to produce all the rows. Each row starts with the row index, then has a bunch of products, and finally has a newline. So I can fill in a bit more of the solution:
/* 2nd DRAFT */
#include <stdio.h>

int main()
{
  int row;

  /* Print the first line of the table. */

  /* Print the second through last line of the table. */
  for (row = 1; row <= 12; row++) {
    printf("%2d", row);

    /* Print all the products. */

    printf("\n");
  }

  return 0;
}
How do I get all the products to line up? The largest product, 144, is three digits wide, so each column needs three digits for the number plus a leading space so the columns don't run together:
/* 3rd DRAFT */
#include <stdio.h>

int main()
{
  int row;
  int column;

  /* Print the first line of the table. */

  /* Print the second through last line of the table. */
  for (row = 1; row <= 12; row++) {
    printf("%2d", row);
    for (column = 1; column <= 12; column++)
      printf(" %3d", row * column);
    printf("\n");
  }

  return 0;
}
Now that I know what the body of the table looks like, I can write the code for the first line. I need two blanks, then 12 column headings each four characters wide, then a newline:
/* COMPLETE PROGRAM */
#include <stdio.h>

int main()
{
  int row;
  int column;

  /* Print the first line of the table. */
  printf("  ");
  for (column = 1; column <= 12; column++)
    printf("%4d", column);
  printf("\n");

  /* Print the second through last line of the table. */
  for (row = 1; row <= 12; row++) {
    printf("%2d", row);
    for (column = 1; column <= 12; column++)
      printf(" %3d", row * column);
    printf("\n");
  }

  return 0;
}

[back to top of document]