COP 3275 - Structures Instructor: Diego Rivera-Gutierrez I’m back baby!

28
COP 3275 - Structures Instructor: Diego Rivera-Gutierrez I’m back baby!

Transcript of COP 3275 - Structures Instructor: Diego Rivera-Gutierrez I’m back baby!

Page 1: COP 3275 - Structures Instructor: Diego Rivera-Gutierrez I’m back baby!

COP 3275 - StructuresInstructor: Diego Rivera-Gutierrez

I’m back baby!

Page 2: COP 3275 - Structures Instructor: Diego Rivera-Gutierrez I’m back baby!

Administrative stuff

• Next week is break week! (From M 22nd to F 26th)

• Quiz this week is definitely take home!• Will be posted Friday morning – due Monday

29th before class. (Meaning first day after the break)

• Homework #3 is due tonight!

• My solution for Homework #3 will be available as soon as everyone has submitted.

Page 3: COP 3275 - Structures Instructor: Diego Rivera-Gutierrez I’m back baby!

Administrative stuff

• Homework #4 will be posted tonight!• Interactive playing of Minesweeper. Due July

2nd 11:59pm. (Friday July 3rd is being observed instead of the 4th so I can’t make it due that day)

• Will have an opportunity for 10 points of extra credit!• Adding colors to your game! • I won’t teach much about how to do this, which

is why it’s extra credit. • Good exercise in figuring things out on your

own! Which is possibly the most important skill a programmer must develop.

Page 4: COP 3275 - Structures Instructor: Diego Rivera-Gutierrez I’m back baby!

Quick: printing colors!

• Couple of resources:• http://

ascii-table.com/ansi-escape-sequences.php• http://

stackoverflow.com/questions/3585846/color-text-in-terminal-aplications-in-unix

• Try this piece of code:

printf("\x1B[31mRED\033[0m");

• Good luck!

Page 5: COP 3275 - Structures Instructor: Diego Rivera-Gutierrez I’m back baby!

Let’s go back to my embarrassing moment on Friday

void foo(int array[1]) {

array[0] = 12345;

}

int main(void) {

int m[1] = {0};

foo(m);

printf("m[0] = %d\n", m[0]);

}

• That was my failed attempt to get a good example to talk about a function concept related to parameters.

Page 6: COP 3275 - Structures Instructor: Diego Rivera-Gutierrez I’m back baby!

Pass by value – pass by reference

Valuevoid foo(int a) {

a = 12345;

}

int main(void) {

int b = 0;

foo(b);

printf(“b = %d\n", b);

}

Reference

void foo(int array[1]) {

array[0] = 12345;

}

int main(void) {

int m[1] = {0};

foo(m);

printf("m[0] = %d\n", m[0]);

}

Page 7: COP 3275 - Structures Instructor: Diego Rivera-Gutierrez I’m back baby!

Pass by value – pass by reference

• So what is the rule?

• Simple variable types int, char, _Bool, float, double all are passed by value

• Arrays and strings (which we haven’t talked about yet) are passed by reference• This is because both arrays and strings are

special kinds of pointers!

• There are ways to change this behavior! We will cover those as we start talking about pointers (maybe on Friday, maybe after the break).

Page 8: COP 3275 - Structures Instructor: Diego Rivera-Gutierrez I’m back baby!

Structures

Page 9: COP 3275 - Structures Instructor: Diego Rivera-Gutierrez I’m back baby!

Grouping Data in Logical Units

• So far we have covered arrays and multidimensional arrays

• What is the main constraint for those?• Elements are the same type.

• Examples of logical units that don’t work well for that?• Names (First and Last).• Complex numbers (real and imaginary parts)• Date and Time (year,month,day,hours,min,sec)• Book (Title, author, year of publication, record

locator)• Flights (Plane, Airport, Time, Date, Number)

Page 10: COP 3275 - Structures Instructor: Diego Rivera-Gutierrez I’m back baby!

Defining Structures

struct <identifier> {

<variable declarations>

};

Page 11: COP 3275 - Structures Instructor: Diego Rivera-Gutierrez I’m back baby!

Defining Structures

struct <identifier> {

<variable declarations>

};

struct name {

char first[20]; //we will talk about this later…

char middleInitial;

char last [20];

};

Page 12: COP 3275 - Structures Instructor: Diego Rivera-Gutierrez I’m back baby!

Defining Structures

struct <identifier> {

<variable declarations>

};

struct dateTime {

int year;

int month;

int day;

int hours;

int minutes;

float seconds;

};

Page 13: COP 3275 - Structures Instructor: Diego Rivera-Gutierrez I’m back baby!

Declaring a variable of a struct type and accessing fields

struct name meanInstructor;

meanInstructor.first = "Diego";

meanInstructor.middleInitial = 'J';

meanInstructor.last = "Rivera-Gutierrez";

Page 14: COP 3275 - Structures Instructor: Diego Rivera-Gutierrez I’m back baby!

Things to cover

• Initialization inline.

• Functions that receive structs.

Page 15: COP 3275 - Structures Instructor: Diego Rivera-Gutierrez I’m back baby!

More on structures

Page 16: COP 3275 - Structures Instructor: Diego Rivera-Gutierrez I’m back baby!

Administrative stuff

• How did you guys do with homework #3?• Homework #4 is posted! Due June 2nd

• Still 9x9 board (this will change for #5)• Interactive game play.• Detect losing condition (winning condition is for

#5)• Extra credit: add color.

• Inputs: • We keep same two inputs (seed, mines)• After that any number of plays until the player

loses.

Page 17: COP 3275 - Structures Instructor: Diego Rivera-Gutierrez I’m back baby!

Homework #5 plays

• With a scanf you would read the pattern %c(%c,%d).

• In between parenthesis (%c,%d) is column letter, row number.• Is the common convention for tables with that

numbering scheme.• You will need to validate the tile exists.

• The first character is the move type. Can be three letters: O,M,Q• M marks as mine.• Q marks as question mark.

Page 18: COP 3275 - Structures Instructor: Diego Rivera-Gutierrez I’m back baby!

O (open a tile)

• O: Open the tile.

• If it is a mine. The user loses.

• If it is a number. Only the number gets open.

Page 19: COP 3275 - Structures Instructor: Diego Rivera-Gutierrez I’m back baby!

O (open a tile)

• O: Open the tile.

• If it is an empty space…. All adjacent tiles that are not mines get open too.

Page 20: COP 3275 - Structures Instructor: Diego Rivera-Gutierrez I’m back baby!

Printing current state

• Details are on the specification. • * unopened tile• ! Tile marked as mine• ? Tile marked as question mark.• _ open tile with no mines surrounding.• 1-8 open tile with number (use corresponding

number)

• In losing board• # mine that caused player to lose• @ mines not marked • x non-mine marked by the user as mine

Page 21: COP 3275 - Structures Instructor: Diego Rivera-Gutierrez I’m back baby!

Recap

• Pass-by-value, pass-by-reference. • Value:

• We send a copy of the variable to the function. • Changes to the parameter don’t affect the

original variable.• Usually used for basic types:

int,char,_Bool,float, double• Reference

• We send the identity of the variable (the variable itself)

• Changes to the parameter are the same as changes to the variable itself

• All pointer types: Arrays, character strings, other pointers

Page 22: COP 3275 - Structures Instructor: Diego Rivera-Gutierrez I’m back baby!

Structures

struct <type-identifier> {

<variable declarations>

};

struct <type-identifier> {

<variable-declarations>

} [<identifiers>];

Page 23: COP 3275 - Structures Instructor: Diego Rivera-Gutierrez I’m back baby!

Variable of structure type

struct <type-identifier> <identifiers>;

Access members:

• <identifier>.<member-name>

This access is very similar to variables, same rules apply to assignment and to use in an expression

Page 24: COP 3275 - Structures Instructor: Diego Rivera-Gutierrez I’m back baby!

Assigning struct variables

struct Point2D {

float x;

float y;

};

struct Point2D a = {0.0, 0.0};

struct Point2D b = {.y = 10.0, .x = 5.0};

struct Point2D array[2] = { {0.0, 0.0}, { .y = 10.0, .x = 5.0} };

Page 25: COP 3275 - Structures Instructor: Diego Rivera-Gutierrez I’m back baby!

Distance between two points

• We need math… that we haven’t covered!

• #include <math.h>

• Requires –lm added to gcc compile line• Instead of “gcc myprog.c”• We do “gcc myprog.c –lm”

• sqrt(x) – square root

• pow(base, exponent) – power of a number

• http://www.cplusplus.com/reference/cmath/

Page 26: COP 3275 - Structures Instructor: Diego Rivera-Gutierrez I’m back baby!

Distance between two points

• pow(base, exponent)

• pow(a.x - b.x, 2.0f)

• pow(base, exponent)

• pow(a.y - b.y, 2.0f)

Page 27: COP 3275 - Structures Instructor: Diego Rivera-Gutierrez I’m back baby!

Example

#include <math.h>

float distance(struct Point2D a, struct Point2D b) {

return sqrt(pow(a.x – b.x, 2.0f) + pow(a.y – b.y, 2.0f));

}

Page 28: COP 3275 - Structures Instructor: Diego Rivera-Gutierrez I’m back baby!

Do structs pass by value or reference?

float distance(struct Point2D a, struct Point2D b) {

float dist = sqrt(pow(a.x – b.x, 2.0f) + pow(a.y – b.y, 2.0f));

a.x = a.y = b.x = b.y = 100.0f;

return dist;

}