CS 112 - Lab 1
C Programming Review / Program Specifications


Section 1: Introduction

The goal of this lab is to:

If you have not already done so, create a directory for this lab and copy the lab 1 files into it. If you start off in your home directory you can do the following:

	mkdir ~/cs112/lab1
	cd ~/cs112/lab1
	cp ~cs112/lab1/* .

Section 2: Topics for Lecture

Each week at the beginning of lab the TA's will provide a 20-30 minute lecture on concepts and programming details which will help you design/write appropriate C software. This week's lecture will include

Section 3: Generating a Magic Square

A magic square is a square array of integers such that the sum of every row, the sum of every column, and sum of each of the two diagonals are all equal (see P1. on p. 73 of text). A magic square of size 5 (shown in Figure 2.3 on p.73) is illustrated below:

17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9

You are to write a C program that will generate a magic square of odd order according to the following method:

Section 4: Code Development

You should use the following two functions nexti and nextj
           /* ----------------------------------------------- */
           /* Function:  nexti (integer)                      */
           /* Procedure: move up 1 row, looping to the bottom */
           /*            upon reaching the top                */
           /* Input:     current row i (integer), and         */
           /*            size of magic square (integer)       */
           /* Output:    returns next row index (integer)     */
           /* ----------------------------------------------- */

           int nexti(int i, int size) {
                if (i <= 0)
                     return size-1;
                else
                     return --i; }
           /* ---------------------------------------------- */
           /* Function:  nextj (integer)                     */
           /* Procedure: move right 1 column, looping to the */
           /*            left upon reaching the right        */
           /* Input:     current column j (integer), and     */ 
           /*            size of magic square (integer)      */
           /* Output:    returns next column index (integer) */
           /* ---------------------------------------------- */

           int nextj(int j, int size) {
                j++;
                if (j == size)
                     return 0;
                else
                     return j; }
to complete the outlined main( ) function provided below:
           #include <stdio.h>
           #include <stdlib.h>


            #define MAX_SIZE 13   /* maximum square size, must be odd */

            int nexti(int, int);  /* function prototypes */
            int nextj(int, int);

            void main(void)
            {
                 int i, j, size;
                 int square[MAX_SIZE][MAX_SIZE];
                 int count = 0;

                 printf("Enter magic square size (odd int): \n");
                 scanf("%d",&size);

                 /* initialize square array */

                 i = 0;
                 j = (size-1)/2;
                 do {

                       /* load array elements */

                 } while (count != size * size);

                 /* display the square  */

            } /* end of main */
In order to test your completed main( ) function, first try a magic square of order 5 (enter 5 via keyboard) and make sure you have produced the sample magic square above. Your code should produce magic squares of any odd order up to 13. You may choose to write an additional function to check the validity of the generated magic square using the definition given in Section 3.

Section 5: Handing in your C code and answers

When you have completed this lab assignment, you should have the following program(s) in your ~/cs112/lab1 directory: Then, to submit your program(s) for grading, issue the command
 ~cs112/submit lab1 
at your UNIX prompt. This utility program will copy your programs for grading and an electronic mail message will be sent to you to acknowledge your submission.

You should also answer the questions below and submit them in hardcopy to the TAs at the beginning of Lab 2 (no late submissions accepted).

This week's questions

For credit on this lab, your software must be received by midnight January 30 and your answers to the lab questions must be received at the beginning of your lab section (8:00am or 11:10am) on Friday, January 31.

Note: Always bring your ID with you to lab.