1
Columbia University
cs3157 – Advanced Programming
Summer 2014, Lab #3, 40 points
June 10, 2014
This lab is due June 23rd 11:59 pm.
• lab in C. We will be using Makefiles.
• Don’t forget to FREE up any memory you request!!
• Make sure you complete the relevant reading on pointers.
• This is a long lab, please start early. Please also work on the homework project concurrently.
• Please make sure to run the debugger when you run into issues.
• Some code was developed in class, please indicate in the README etc
Step 1 - Puzzles (35 points)
In this step, you will develop code that computes a magic square. A magic square is a n× nmatrix filled
with integers in the range [1 – n2] so that each entry in the matrix has a unique value. The sum of each of
the rows and each of the columns and each of the two diagonals must be equal the same number. The
number is known as the magic constant Example for n=3 the magic
constant is 15, for n=4 it will be 34 etc. Also Note that there not a single unique solution to the magic
square problem.
think of Typedef as a shortcut so instead of having to type out a full type you can use the shortcut. For
example
typedef int XX[15];
XX yyy;
will make yyy a shortcut for a 15 array int. So can say yyy[0] etc
Create a file called Maintest1.c/Magicsquare1.c/Magicsquare.h and (to help you get started) start by
putting the following code/declarations in each file (where appropriate):
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAGICSIZE 3
typedef int MSQUARE_TYPE[MAGICSIZE][ MAGICSIZE];
typedef MSQUARE_TYPE * MagSquare_PTR;
void initSquare( MagSquare_PTR, int magicsquaresize );
void printSquare( MagSquare_PTR, int magicsquaresize );
2
int main() {
srand( time( NULL )); /* seed the random number gen */
MagSquare_PTR mptr = /* replace with something with malloc to create
space to point at*/;
initSquare( mptr, MAGICSIZE );
printSquare( mptr , MAGICSIZE);
} // end of main()
Define the function initSquare(MagSquare_PTR, int size) which takes the magic square pointer
argument (as above) and the size of the n by n array and fills it with values in the range [1 - 9]. These
values must be assigned RANDOMLY, but make sure that no number appears more than once in the
array.
Also Define the function printSquare(MagSquare_PTR, int size) which takes the magic square
pointer argument (as above) and the n for the n by n size, and prints out its contents to the screen in a nice
n rows by n columns format.
You should test your code with just these 2 functions to make sure each is working (before adding the rest
of the functions).
Hint: To get random numbers look up lookup rand() and srand() from the stdlib libary.
1. maintest1.c – this will contain the main function
2. magicsquare.h – this will contain the functions declarations that you will be using.
3. magicsquare.c – this will contain the function definitions from above including the following
methods:
a. void initSquare(MagSquare_PTR, int)
(see above)
b. void printSquare(MagSquare_PTR, int)
(see above)
c. int sumColumn( int column, MagSquare_PTR, int size )
The function takes the 2-dimensional magic square array and a column number and returns
the sum of the 3 values in that column
d. int sumRow(int row, MagSquare_PTR, int size)
see above.
e. int sumDiagonal( int diagonal, MagSquare_PTR, int size )
The function takes the 2-dimensional magic square array and a diagonal number and
returns the sum of the n values in that diagonal. Use diagonal = 0 to refer to the diagonal
that runs from the northwest corner down to the southeast corner. Use diagonal = 1 to refer
to the diagonal that runs from the northeast corner down to the southwest corner.
f. Create a function called int isMagic(MagSquare_PTR, int size) which takes the
magic square pointer as an argument and checks each of the rows and columns and
diagonals to see if all the sums equal 15. The function should return 0 if the argument is
not a magic square and 1 if it is a magic square.
3
g. Modify the above main() so that it calls isMagic and prints out whether the square is a
magic square or not.
h. Modify the main() program so that after it prints out the square, it computes (using the
functions above) and prints out the sum of each of the 3 rows and each of the 3 columns
and each of the 2 diagonals.
Compile and test your program.
Here's a sample output:
the square is not a magic square of size 3
here is your square:
2 8 3
1 4 5
7 9 6
sum of row 1 = 13
sum of row 2 = 10
sum of row 3 = 22
sum of column 1 = 10
sum of column 2 = 21
sum of column 3 = 14
sum of diagonal 0 = 12
sum of diagonal 1 = 14
Note: you will need to create a makefile for this lab too. For example
make step1_run
would compile and execute the step1 code.
Hint: here is a sample of a more complicated Makefile:
CC = gcc
CCFLAGS = -Wall –lm
step1_run: maintest1.c magicsquare1.o
$(CC) $(CCFLAGS) -o test1 maintest1.c magicsquare1.o
./test1
magicsquare1.o: magicsquare1.c magicsquare1.h
$(CC) $(CCFLAGS) –c magicsquare1.c
clean:
rm –f *.o test1
(make clean
will just clean up all the temp .o files and executables, should run it before submission, if you used emacs,
you should probably include *~ also).
4
Step 2 create a running program (20 points)
Now copy maintest1.c to maintest2.c and magicsquare1.c to magicsquare2.c, and magicsquare1.h to
magicsquare1.h modify them as follows:
1. Create a function called permuteSquare(MagSquare_PTR) which takes the magic square
pointer as an argument and randomly switches two entries in the array. Do this by randomly
picking two sets of row and column indices (in the range [0 - 2]) and then swapping the entries
located at each pair of indices. Check to make sure you aren’t picking the same spot .. that is
swapping the same location with itself J
2. Modify the main() so that after it calls isMagic(), if the square is not magic, then it calls
permuteSquare() to switch around two entries in the square and then test again to see if the
square is magic. Do this repeatedly until a magic square is found. When a magic square is found,
call printSquare() again to print out the magic square.
3. Have the program count the number of times it has to permute the square in order to find a magic
square. Print that out too (Hint: count it where you call method permutesquare).
Compile and test your code. Please ask for help as you need etc
Good luck.

C语言 cgi(2)的更多相关文章

  1. 搭建简易的c语言与python语言CGI和Apache服务器的开发环境

    搭建简易的c语言CGI和Apache服务器的开发环境 http://www.cnblogs.com/tt-0411/archive/2011/11/21/2257203.html python配置ap ...

  2. 基于windows IIS的C语言CGI WEB服务器环境搭建

    网页编程对我来说特别亲切,因为我就是从html.ASP.PHP一步步接触编程的.自己的编程爱好也是从那里一点一点被满足.不过离开大学之后很久没有碰过WEB了,最近看到嵌入式中的涉及到的web服务器,了 ...

  3. C语言 cgi(3)

    1cs3157 – Advanced ProgrammingSummer 2014, Project 1, 150 pointsJune 17, 2014Follow these step-by-st ...

  4. C语言cgi(1)

    1Columbia Universitycs3157 – Advanced ProgrammingSummer 2014, Lab #2, 60ish pointsJune 9, 2014Follow ...

  5. c语言cgi笔记

    直接输出接收的数据 #include <stdio.h>#include <stdlib.h>main(){int i,n;printf ("Content-type ...

  6. 几种语言的CGI编程

    为了了解PHP.JSP.ASP出现之前人们写网站的方法,洒家研究了一波CGI,使用C.Python.batch.shell script语言写了几个简单的网页. CGI即通用网关接口,指web服务器调 ...

  7. CGI(通用网关接口)

    公共网关接口 CGI(Common Gateway Interface) 是WWW技术中最重要的技术之一,有着不可替代的重要地位.CGI是外部应用程序(CGI程序)与Web服务器之间的接口标准,是在C ...

  8. cgic 写CGI程序

    CGIC是C语言CGI库函数,用于编写CGI程序 CGIC 主要完成以下功能: * 对数据进行语法分析 * 接收以 GET 和 PSOT 两种方式发送的数据 * 把 FORM 中的不同域连接成连续的串 ...

  9. libctemplate——C语言模块引擎简介及使用

    前言 由先声明此libctemplate不是Google那个ctemplate.这个库是用C语言实现的,只有一个实现文件和一个头文件.Gooogl的ctemplate是C++实现的,和线程还扯上了关系 ...

随机推荐

  1. FOJ 2170 花生的序列 dp

    题目链接:http://acm.fzu.edu.cn/problem.php? pid=2170 贴个baka爷的代码留念.. 数据出的有问题.输入的字符串长度不超过1000 #include< ...

  2. Ajax 下拉列表联动显示

    一般处理程序文件 代码 using System;using System.Web;using System.Linq;using System.Data.Linq;using System.Text ...

  3. svn自动发用户名密码到邮件(明文密码)

    #!/bin/sh touch testlist cat /dev/null > testlist grep "=" passwd |grep -v "#" ...

  4. Js实现select联动,option从数据库中读取

    待要实现的功能:页面有两个select下拉列表,从第一个select中选择后,在第二个select中出现对应的列表选择,再从第二个select中选择后,在一个text中显示对应的信息.两个select ...

  5. 深入分析 Java 中的中文编码问题(1)

    几种常见的编码格式 为什么要编码 不知道大家有没有想过一个问题,那就是为什么要编码?我们能不能不编码?要回答这个问题必须要回到计算机是如何表示我们人类能够理解的符号的,这些符号也就是我们人类使用的语言 ...

  6. Java的一些基础小知识之JVM与GC (转)

    一.JVM是什么 Java虚拟机(英语:Java Virtual Machine,缩写为JVM),又名爪哇虚拟器,一种能够运行Java bytecode的虚拟机,以堆栈结构机器来进行实做.最早由太阳微 ...

  7. Memcahce(MC)系列(一)Memcache介绍、使用、存储、算法、优化

    写在前面:前不久在工作中被问到关于MC一致哈希的问题,由于时隔太久差点儿忘记,特前来恶补一下MC,下面是前几年在工作中学习MC时的一些资料,来历不明,特整理一下,希望对大家的学习也能有帮助. 32 的 ...

  8. 解决Eclipse中文乱码的方法

    (1)设置Project的编码格式: 在 Workspace中新建的项目默认继承Workspace的编码设置.我们也能够单独更改某个项目的编码格式.右键点击project.选择 Properties, ...

  9. Selenium来抓取动态加载的页面

    一般的爬虫都是直接使用http协议,下载指定url的html内容,并对内容进行分析和抽取.在我写的爬虫框架webmagic里也使用了HttpClient来完成这样的任务. 但是有些页面是通过js以及a ...

  10. 使用独立PID namespace防止误杀进程

    一段错误的代码 首先看一段错误的代码: #!/bin/bash SLICE=100; slppid=1; pidfile=/var/run/vpnrulematch.pid # 停止之前的sleep ...