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.
- 浏览: 1001729 次
文章分类
最新评论
-
18335864773:
很多公司项目 都在使用pageoffice 来操作word,e ...
用java生成word文档 -
Gozs_cs_dn:
请问下博主, 怎样将sitemesh3.xsd绑定 sitem ...
SiteMesh3配置 -
Rose_06:
springside4.0quick-start.bat报错原因 -
ilemma:
我也是刚参见工作啊,经理让自学这个,有些东西不太懂,能不能发个 ...
Apache Shiro在Web中的应用 -
shanbangyou:
你废了
程序员上班打酱油的方法
发表评论
-
递归归并排序
2016-02-11 20:26 350/* MergeSort.java CSC 225 - ... -
java冒泡排序对布尔类型进行排序
2015-12-11 23:06 640QQ 928900200 程序代写 java不能对 ... -
判断宏是否是“安全”的
2014-11-22 22:54 559给了一系列C++的宏定义,问你一个表达式是否是“安全”的。 ... -
C语言求平均值
2014-11-19 19:14 554木其工作室:QQ928900200 Computing I ... -
C语言连连看
2014-11-18 16:34 599(1)定义一个矩阵,随机产生字符布置地图,例如下面这个4x ... -
The Monty Hall Problem
2014-10-19 12:58 638GNG1106 Lab 3The Monty Hall Pro ... -
java类
2014-10-16 08:27 298木其工作室 qq 928900200 You are ... -
ECE/CPSC 3520
2014-10-13 09:49 528ECE/CPSC 3520Fall 2014Software ... -
计算机安全
2014-10-07 14:52 432CS461 MP 1: Due Wednesday 09/17 ... -
java星球机器人建模UML
2014-10-06 22:29 393Your task is to design and imp ... -
数据库sql
2014-10-06 22:25 609service QQ 928900200 ... -
C语言 cgi(3)
2014-08-04 09:17 3501cs3157 – Advanced ProgrammingS ... -
C语言cgi(1)
2014-08-04 09:08 3311Columbia Universitycs3157 – Ad ... -
c++ input,output
2014-08-04 08:37 469You should be comfortable w ... -
Array of Objects
2014-08-04 08:30 656You should be comfortable w ... -
bat脚本打开网页
2014-07-13 09:54 880start iexplore "http://ww ... -
java 汉诺塔实现自动演示
2014-07-10 11:53 5051、增加计时功能,显 ... -
代写java程序qq:928900200
2014-06-18 12:46 3学校为全面提升学校教学质量,提高管理水平,决定开发一套小型成 ... -
基于MVC的系统代写
2014-06-16 12:13 441人力资源管理系统 完成系统静态页面设计,页面数 ... -
程序设计实践C++ 程序代写(QQ 928900200)
2014-06-10 08:15 311程序设计实践 采用C++ ...
相关推荐
### C语言CGI编程知识点详解 #### 一、公共网关接口(CGI)概述 公共网关接口(Common Gateway Interface,简称CGI)是一种用于Web服务器与外部应用程序之间的通信标准,它允许Web服务器调用外部程序处理客户端的...
C语言写CGI程序详细教程 CGI(Common Gateway Interface,公共网关接口)是一种标准的web服务器和脚本程序之间的接口协议。CGI程序可以用任何编程语言编写,如Shell脚本语言、Perl、Fortran、Pascal、C语言等。但是...
### C语言CGI编写WEB查询系统 #### 一、CGI概述 公共网关界面(Common Gateway Interface,简称CGI)是一种编程规范,它定义了Web服务器如何与外部应用程序(即CGI程序)进行交互的标准接口。通过CGI,Web服务器...
**C语言CGI(Common Gateway Interface)学习指南** CGI,即通用网关接口,是一种标准,用于将Web服务器与各种脚本语言或应用程序连接起来,以动态生成网页内容。在C语言中实现CGI,可以让你创建更复杂的Web应用...
2. **环境变量**:CGI程序运行时,Web服务器会设置一系列环境变量,比如HTTP_METHOD(表示请求类型,如GET或POST)、SERVER_NAME(服务器的主机名)以及REQUEST_URI(请求的URL)等,这些变量提供了关于请求的重要上...
总结来说,"C语言CGI商业程序,可上传图片存入mysql数据库"是一个结合了C语言编程、CGI技术和MySQL数据库的Web应用解决方案。它提供了一种方式让用户通过Web界面上传图片,并将这些图片的信息安全地存储在MySQL...
总结来说,嵌入式系统中C语言CGI的设计与应用是网络远程控制技术中一个重要的领域。通过CGI技术,可以实现对嵌入式系统网络接口的远程管理与控制,其优点在于技术成熟、语言无关且具有较好的平台兼容性。在具体的...
下面是一个简单的C语言CGI程序示例,用于演示如何处理表单数据: ```c #include #include int main() { int i, n; printf("Content-type: text/plain\n\n"); if (getenv("CONTENT_LENGTH")) { n = atoi...
c语言编写的多用户登录功能cgi程序,适用于嵌入式,需编译。需要在程序同目录下创建文件user.txt并写入用户名密码用于验证,格式为:用户名空格密码空格用户名空格密码
c语言写的CGI 计数器 在linux 下运行的,要自己加一个count.dat的文件,不然会访问失败的。
2. **C语言编程**:C语言是一种底层、高效且广泛使用的编程语言,适用于编写CGI程序。它提供直接访问内存和系统调用的能力,适合处理网络通信和文件操作。 3. **HTTP协议**:理解GET请求是理解CGI程序的关键。GET...
在这个案例中,我们讨论的是`cgi-util`,一个用C语言编写的CGI库。 `cgi-util`库为C语言开发者提供了一系列方便的函数和结构,帮助他们快速、有效地编写CGI程序。C语言是系统级编程的首选语言,因此这个库对于那些...
2. **请求数据解析**:库提供API如`cgi_get_parameter()`用于获取HTTP请求中的参数值,`cgi_get_form_content()`则可以解析POST请求的表单数据。 3. **响应生成**:使用`cgi_printf()`或`cgi_puts()`向HTTP响应体...
C语言CGI程序的核心部分通常包括以下步骤: 1. **环境变量解析**:CGI程序首先需要获取来自HTTP请求的环境变量,如QUERY_STRING(GET方法传递的参数)、REQUEST_METHOD(GET或POST请求类型)和CONTENT_TYPE(POST...
在本资源中,我们探讨的是如何使用C语言来编写CGI程序。 C语言是一种强大且高效的编程语言,适用于系统级编程和网络应用。在CGI编程中,C语言能够提供对底层操作系统接口的直接访问,使得开发者可以精确控制程序的...
3. **C语言CGI示例**:以下是一个简单的C语言CGI程序示例,用于展示如何接收表单数据并输出结果: ```c #include #include int main() { char *query_string = getenv("QUERY_STRING"); if (query_string !=...
【标题】:“C语言开发的CGI简单论坛源码”是一个基于C语言实现的CGI(Common Gateway Interface)应用程序,用于构建一个基本的在线论坛。这个项目是学习CGI编程和C语言结合的理想实例,因为它提供了对用户管理、...
以下是一个简单的C语言CGI程序示例: ```c #include #include int main(void) { char *data; long m, n; // 设置HTTP响应头 printf("%s%c%c", "Content-Type:text/html;charset=gb2312", 13, 10); printf...