## CS 112 - Lab 1 C Programming Review / Program Specifications

### Section 1: Introduction

The goal of this lab is to:
• review the use of repetition structures (for and do-while loops) in C programming,
• gain practice in following problem specifications, and
• test software with several inputs.

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
• quick review of UNIX file system commands (gcc version 2.5.8, vi, mail, mkdir, cd, etc.),
• iteration structures used in C (for, do-while, and while), and
• procedure for designing a magic square.

### 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:

• Start by placing a 1 in the middle of the top row.
• Write down successive integers 2,3,... along a diagonal going upward and to the right.
• When you reach the top row (as you do immediately since 1 is in the top row), continue to the bottom row as though the bottom row were immediately above the top row.
• When you reach the rightmost column, continue to the leftmost column as though it were immediately to the right of the rightmost one.
• When you reach a position that is already occupied, instead drop straight down one position from the previous number to insert the new one.

### 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 {

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

When you have completed this lab assignment, you should have the following program(s) in your ~/cs112/lab1 directory:
• square.c
` ~cs112/submit lab1 `