Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "COP 3275 - Structures Instructor: Diego Rivera-Gutierrez I’m back baby!"— Presentation transcript:

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

2 Administrative stuff Next week is break week! (From M 22 nd to F 26 th ) Quiz this week is definitely take home! Will be posted Friday morning – due Monday 29 th 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.

3 Administrative stuff Homework #4 will be posted tonight! Interactive playing of Minesweeper. Due July 2 nd 11:59pm. (Friday July 3 rd is being observed instead of the 4 th 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.

4 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 http://stackoverflow.com/questions/3585846/color -text-in-terminal-aplications-in-unix Try this piece of code: printf("\x1B[31mRED\033[0m"); Good luck!

5 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.

6 Pass by value – pass by reference Value void 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]); }

7 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).

8 Structures

9 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)

10 Defining Structures struct { };

11 Defining Structures struct { }; struct name { char first[20]; //we will talk about this later… char middleInitial; char last [20]; };

12 Defining Structures struct { }; struct dateTime { int year; int month; int day; int hours; int minutes; float seconds; };

13 Declaring a variable of a struct type and accessing fields struct name meanInstructor; meanInstructor.first = "Diego"; meanInstructor.middleInitial = 'J'; meanInstructor.last = "Rivera-Gutierrez";

14 Things to cover Initialization inline. Functions that receive structs.

15 More on structures

16 Administrative stuff How did you guys do with homework #3? Homework #4 is posted! Due June 2 nd 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.

17 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.

18 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.

19 O (open a tile) O: Open the tile. If it is an empty space…. All adjacent tiles that are not mines get open too.

20 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

21 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

22 Structures struct { }; struct { } [ ];

23 Variable of structure type struct ; Access members:. This access is very similar to variables, same rules apply to assignment and to use in an expression

24 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} };

25 Distance between two points

26

27 Example #include float distance(struct Point2D a, struct Point2D b) { return sqrt(pow(a.x – b.x, 2.0f) + pow(a.y – b.y, 2.0f)); }

28 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; }


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

Similar presentations


Ads by Google