C语言 cgi(2)
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)的更多相关文章
- 搭建简易的c语言与python语言CGI和Apache服务器的开发环境
搭建简易的c语言CGI和Apache服务器的开发环境 http://www.cnblogs.com/tt-0411/archive/2011/11/21/2257203.html python配置ap ...
- 基于windows IIS的C语言CGI WEB服务器环境搭建
网页编程对我来说特别亲切,因为我就是从html.ASP.PHP一步步接触编程的.自己的编程爱好也是从那里一点一点被满足.不过离开大学之后很久没有碰过WEB了,最近看到嵌入式中的涉及到的web服务器,了 ...
- C语言 cgi(3)
1cs3157 – Advanced ProgrammingSummer 2014, Project 1, 150 pointsJune 17, 2014Follow these step-by-st ...
- C语言cgi(1)
1Columbia Universitycs3157 – Advanced ProgrammingSummer 2014, Lab #2, 60ish pointsJune 9, 2014Follow ...
- c语言cgi笔记
直接输出接收的数据 #include <stdio.h>#include <stdlib.h>main(){int i,n;printf ("Content-type ...
- 几种语言的CGI编程
为了了解PHP.JSP.ASP出现之前人们写网站的方法,洒家研究了一波CGI,使用C.Python.batch.shell script语言写了几个简单的网页. CGI即通用网关接口,指web服务器调 ...
- CGI(通用网关接口)
公共网关接口 CGI(Common Gateway Interface) 是WWW技术中最重要的技术之一,有着不可替代的重要地位.CGI是外部应用程序(CGI程序)与Web服务器之间的接口标准,是在C ...
- cgic 写CGI程序
CGIC是C语言CGI库函数,用于编写CGI程序 CGIC 主要完成以下功能: * 对数据进行语法分析 * 接收以 GET 和 PSOT 两种方式发送的数据 * 把 FORM 中的不同域连接成连续的串 ...
- libctemplate——C语言模块引擎简介及使用
前言 由先声明此libctemplate不是Google那个ctemplate.这个库是用C语言实现的,只有一个实现文件和一个头文件.Gooogl的ctemplate是C++实现的,和线程还扯上了关系 ...
随机推荐
- OCA读书笔记(3) - 使用DBCA创建Oracle数据库
Objectives: •Create a database by using the Database Configuration Assistant (DBCA) •Generate databa ...
- ecshop 微信支付插件
眼下已完毕支付測试,可以支付成功,支付逻辑自己实现.后台通知.发货通知.订单查询未測. 当中用到了redis 下载
- J2EE开发框架搭建(2) - springmvc4 + spring4 + hibernate4 整合
1. 打开hqhop-framework-parent项目下的pom.xml文件.加入springmvc4 , spring4 , hibernate4 ,以及数据源druid的依赖包,插件,依赖包版 ...
- android使用篇(四) 注解依赖注入IOC实现绑定控件
在android使用篇(三) MVC模式中提到一个问题: 1) 视图层(View):一般採用XML文件进行界面的描写叙述,使用的时候能够很方便的引入,可是用xml编写了,又须要在Acitvity声明而 ...
- 网络知识汇总(2) - Linux下如何修改ip地址
在Linux的系统下如何才能修改IP信息 以前总是用ifconfig修改,重启后总是得重做.如果修改配置文件,就不用那么麻烦了- A.修改ip地址 即时生效: # ifconfig e ...
- Modular Fibonacci
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=30506#problem/U fib数列对2^m取模的循环节为3*(2^(m-1)) #i ...
- 算法起步之动态规划LCS
原文:算法起步之动态规划LCS 前一篇文章我们了解了什么是动态规划问题,这里我们再来看动态规划另一个经典问题,最长公共子序列问题(LCS),什么是子序列,我们定义:一个给定序列将其中的0个或者多个元素 ...
- HLG 2163 方格取数 (最大网络流)
题目链接: m=ProblemSet&a=showProblem&problem_id=2163">点击打开链接 Description : 给你一个n*n的格子的棋 ...
- hdu 4291 A Short problem(矩阵+取模循环节)
A Short problem Time Limit: 2000/1000 MS (J ...
- 201215-03-19---cocos2dx内存管理--具体解释
因为cocos2dx我们的使用c++写的,所以内存管理就是一个绕只是去的坎,这个你不懂内存仅仅懂业务逻辑的话,还玩什么c++,今天看了半天这个东西,事实上本质上是理解的,可是就是有一个过不去的坎,最终 ...