CSCI1300 Lab Exercise for Sept 7

Information Holders

Emergency note: The intended exercise here uses the graphics library, but part way through the 11:00 lab that library stopped working. So I've added a modified version of the exercise, AFTER the original. Use this IF the graphics stops working again.

Original:

1. Copy the following program and save it under the name lab2ex.c. Then compile and run it. Note the comment near the beginning that tells you what compile command to use: bgi++ is a special compile command that brings in some special libraries, collections of already-written programs, that are used for graphics. The program draws a multicolored square.

2. Working with one or more other students, try to dope out how the shape the program draws is determined by the assignment statements in the program. Demonstrate your understanding by changing the program so that it draws a different shape.

#include <stdio.h>

#include <winbgim.h>

//compile with bgi++ lab2ex.c -Wall -o s

//run by typing s

#define MAXL 48

struct point

{

int x;

int y;

};

struct line

{

struct point beg;

struct point end;

int color;

};

struct shape

{

struct line lines[MAXL];

int count;

};

void draw(struct shape s);

int main()

{

struct shape s;

struct line lin;

struct point p;

//the following assignment statements define a colored square

//hint: the coordinates of the corners of the square are

//(100,100), (200,100), (200,200), (100,200)

//the y coordinate runs DOWN the screen, not up

p.x=100;

p.y=100;

lin.beg=p;

p.x=200;

lin.end=p;

lin.color=1;

s.lines[0]=lin;

p.y=200;

lin.beg=p;

lin.color=2;

s.lines[1]=lin;

p.x=100;

lin.end=p;

lin.color=4;

s.lines[2]=lin;

p.y=100;

lin.beg=p;

lin.color=5;

s.lines[3]=lin;

s.count=4; //this shape contains 4 lines

//the following statements create a graphics window

//and draw the shape

initwindow(400,400);

draw(s);

getch(); //this statement waits for the user to type something

//before taking away the window

return 0;

}

void draw(struct shape s)

{

int i;

for(i=0;i<s.count;i++)

{

setcolor(s.lines[i].color);

line(s.lines[i].beg.x,

s.lines[i].beg.y,

s.lines[i].end.x,

s.lines[i].end.y);

}

}

Substitute exercise:

1. Copy the following program and save it under the name lab2ex.c. Then compile and run it. The program prints out a description of a shape.

2. Working with one or more other students, try to dope out how the shape the program draws is determined by the assignment statements in the program. Demonstrate your understanding by changing the program so that it describes a different shape.

#include <stdio.h>

//compile with gcc lab2ex.c -Wall -o s

//run by typing s

#define MAXL 48

struct point

{

int x;

int y;

};

struct line

{

struct point beg;

struct point end;

int color;

};

struct shape

{

struct line lines[MAXL];

int count;

};

void draw(struct shape s);

int main()

{

struct shape s;

struct line lin;

struct point p;

//the following assignment statements define a colored square

//hint: the coordinates of the corners of the square are

//(100,100), (200,100), (200,200), (100,200)

//the y coordinate runs DOWN the screen, not up

p.x=100;

p.y=100;

lin.beg=p;

p.x=200;

lin.end=p;

lin.color=1;

s.lines[0]=lin;

p.y=200;

lin.beg=p;

lin.color=2;

s.lines[1]=lin;

p.x=100;

lin.end=p;

lin.color=4;

s.lines[2]=lin;

p.y=100;

lin.beg=p;

lin.color=5;

s.lines[3]=lin;

s.count=4; //this shape contains 4 lines

//the following statements create a graphics window

//and draw the shape

draw(s);

return 0;

}

void mysetcolor(int color)

{

printf("Color is now color number %d\n",color);

}

void myline(int x0, int y0, int x1, int y1)

{

printf("Draw line from (%d,%d) to (%d,%d)\n",x0,y0,x1,y1);

}

void draw(struct shape s)

{

int i;

for(i=0;i<s.count;i++)

{

mysetcolor(s.lines[i].color);

myline(s.lines[i].beg.x,

s.lines[i].beg.y,

s.lines[i].end.x,

s.lines[i].end.y);

}

}