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.

转载于:https://www.cnblogs.com/oversea201405/p/3889352.html

C语言 cgi(2)相关推荐

  1. C语言,后门程序,C语言CGI编程二 - 编写cmd后门

    里面该说的都说的很清楚.今天我们来用它来做一个后门,将后门放在有执行cgi权限的服务器上,通过客户端POST命令到cgi上并执行,达到控制服务器的目的. 首先下载官网上的压缩包.解压里面有cgic.c ...

  2. c语言编制编程语言,C语言CGI编程入门(一)

    CGI是指web服务器调用编程语言编写的流程的一个接口.比如我们可以编写一个用户注册的界面,用户将其输入的邮件.用户名.密钥输入并点击注册,此时浏览器就将用户输入的内容发送给了我们后台的CGI程序,我 ...

  3. c语言cgi函数库,cgic: 为C语言编写CGI的C函数库

    CGIC介绍 怎样写CGIC应用程序 怎样产生图片在CGIC中? CGI调试特征: 利用捕获 cgic函数参考 cgic变量参考 cgic结果编码参考 cgic快速索引 一般的Unix系统都支持ANS ...

  4. c 语言cgi编程,几种语言的CGI编程

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

  5. c语言cgi函数库,cgic: 为C语言设计CGI地C函数库

    cgic: 为C语言设计CGI地C函数库 cgic: 为C语言编写CGI的C函数库 ---由Thomas Boutell开发 ************************************* ...

  6. c语言 cgi php,C语言CGI编程入门(一)

    CGI是指web服务器调用编程语言编写的程序的一个接口.比如我们可以编写一个用户注册的页面,用户将其输入的邮箱.用户名.密码输入并点击注册,此时浏览器就将用户输入的内容发送给了我们后台的CGI程序,我 ...

  7. C 语言 cgi 程序简单总结

    C的示例程序表面上是从linux系统的stdint读取所有的网页发过来的信息,而实际上是,web服务器将这些接收到的网页信息,写入linux的stdin,至于具体为什么可以看看boa的源码,工程小方便 ...

  8. C语言cgi解析上传文件的本地测试

    首先用浏览器提交提交一个multipart/form-data类型的数据包到本地服务器,随便用什么脚本写个接受数据包的程序,保存数据包到文件,数据包的格式大概就是这样: ------WebKitFor ...

  9. c语言cgi json,CGI程序学习(4)-C程序中JSON解析

    C程序中数据封装和解析也可以用到JSON,不过需要额外添加json.h库 #include //用于存储数据 int array_number; char wifiname_user[100][100 ...

最新文章

  1. 在windows平台使用Apache James搭建邮件服务器以及使用C#向外网发送邮件
  2. weblogic服务器保存图片失败解决办法
  3. java 协程线程的区别_为什么 Java 坚持多线程不选择协程?
  4. C语言实现的ABCI
  5. Java快速排序的调试
  6. ★LeetCode(784)——字母大小写全排列(JavaScript)
  7. ECMAScript中基本类型和引用类型的值在复制变量值时的区别
  8. 信息安全——密码学之DES介绍
  9. 用python画多来a梦-使用Python的Turtle绘制哆啦A梦实例
  10. kali 创建php可执行文件_Kali安装wordpress
  11. 【redis】redis的bind配置
  12. JSP电子病历管理系统
  13. 数字电子技术基础-2-逻辑函数的最小项与最大项
  14. 【毕设教程】物联网/嵌入式/单片机毕业设计项目开发流程
  15. java实现excel、word在线预览
  16. 联想用u盘重装系统步骤_练习联想使用u盘重装win7教程
  17. 用python画竹子_基于Python的一个简单爬虫(糗事百科爬虫)
  18. 今年的第几天?(p16)模拟
  19. 如何给网站添加IE浏览器升级提示
  20. windows下同一个显卡配置多个CUDA工具包以及它们之间的切换

热门文章

  1. springMVC获取file,几种转换
  2. Exploring the 7 Different Types of Data Stories
  3. Repeater嵌套绑定Repeater[转]
  4. 【深入浅出项目管理视频1】-项目文档评审
  5. pycharm工程中的pip用不了怎么办?
  6. leetcode算法题--煎饼排序
  7. Docker网络模型(八)
  8. OVS之vhost-net中VM通信(十)
  9. Elide 4.3.1 发布,雅虎开源的应用数据 API 搭建平台
  10. 新零售模式开启,2018杭州无人店展览会