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

Slides:



Advertisements
Similar presentations
Programming in C Chapter 10 Structures and Unions
Advertisements

Structures Often we want to be able to manipulate logical entities as a whole For example, complex numbers, dates, student records, etc Each of these must.
C Language.
C Structures and Memory Allocation There is no class in C, but we may still want non- homogenous structures –So, we use the struct construct struct for.
Structures Spring 2013Programming and Data Structure1.
1 Homework Turn in HW2 at start of next class. Starting Chapter 2 K&R. Read ahead. HW3 is on line. –Due: class 9, but a lot to do! –You may want to get.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
CSSE221: Software Dev. Honors Day 28 Announcements Announcements Simulation grades coming back Simulation grades coming back All C Projects due Friday.
R-1 University of Washington Computer Programming I Lecture 17: Multidimensional Arrays © 2000 UW CSE.
Chapter 7 & 8- Arrays and Strings
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Yet More Array Practice Lecture 24, Mon Mar
COP 3275 COMPUTER PROGRAMMING USING C Instructor: Diego Rivera-Gutierrez
Pointers 2 COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ.
The preprocessor and the compilation process COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ.
S-1 University of Washington Computer Programming I Lecture 18: Structures © 2000 UW CSE.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
'C' Programming With Structure Records Purpose of structures Coding a structure template Defining a new data type Functions which communicate using structures.
Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Structures  2 nd aggregate data type: struct  Recall:
Files COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ.
C Programming Tutorial – Part I CS Introduction to Operating Systems.
1 Structures. Structure (struct) Definition A Structure is a container, it can hold a bunch of things. –These things can be of any type. Structures are.
COP 3275 COMPUTER PROGRAMMING USING C Instructor: Diego Rivera-Gutierrez
Arrays Part 9 dbg. Arrays An array is a fixed number of contiguous memory locations, all containing data of the same type, identified by one variable.
COP 3275 COMPUTER PROGRAMMING USING C Instructor: Diego Rivera-Gutierrez
CS107 References and Arrays By Chris Pable Spring 2009.
11 Adding Tomato Targets Session Session Overview  We now have a game which lets a player bounce a piece of cheese on a bread bat  Now we have.
 Introduction to Computer Science COMP 51 – Fall 2012 – Section 2 Structures.
COP 3275 COMPUTER PROGRAMMING USING C Instructor: Diego Rivera-Gutierrez
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
Arrays II (Strings). Data types in C Integer : int i; Double: double x; Float: float y; Character: char ch; char cha[10], chb[]={‘h’,’e’,’l’,’l’,’o’};
 Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create.
1 C Language Structures. 2 Topics Concept of a structure Concept of a structure Structures in c Structures in c Structure declaration Structure declaration.
COP 3275 – Iteration and loops Instructor: Diego Rivera-Gutierrez.
1 Homework –Continue Reading K&R Chapter 2 –We’ll go over HW2 at end of class today –Continue working on HW3 Questions?
CSC 212 – Data Structures Lecture 2: Primitives, References, & Classes.
Pointers *, &, array similarities, functions, sizeof.
Pointers COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ.
Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: Desired output:
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 12 – C: structs, linked lists, and casts.
Arrays as pointers and other stuff COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
COP 3275 – Character Strings Instructor: Diego Rivera-Gutierrez.
COP 3275 – Character Strings and Introduction to Pointers Instructor: Diego Rivera-Gutierrez.
Fundamental Programming Fundamental Programming More Expressions and Data Types.
Arrays. Topics to be Covered... Arrays ◦ Declaration ◦ Assigning values ◦ Array manipulation using loops Multi-dimensional arrays ◦ 2D arrays ◦ Declaration.
Extra Recitations Wednesday 19:40-22:30 FENS L055 (tomorrow!) Friday 13:40-16:30 FENS L063 Friday 17: :30 FENS L045 Friday 19:40-22:30 FENS G032.
Programming in C Arrays, Structs and Strings. 7/28/092 Arrays An array is a collection of individual data elements that is:An array is a collection of.
CGS 3460 Thus Far n Whenever we declare a variable, we specified its data type. n The data type helped us identify the type of information that a variable.
CCSA 221 Programming in C CHAPTER 11 POINTERS ALHANOUF ALAMR 1.
Midterm Review Tami Meredith. Primitive Data Types byte, short, int, long Values without a decimal point,..., -1, 0, 1, 2,... float, double Values with.
Fundamental Programming Fundamental Programming Introduction to Functions.
CS 31 Discussion, Week 7 Faisal Alquaddoomi, Office Hours: BH 2432, W 4:30-6:30pm, F 12:30-1:30pm.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
13/10/2016CS150 Introduction to Computer Science 1 Multidimensional Arrays  Arrays can have more than one column  Two dimensional arrays have two columns.
Week91 APCS-A: Java Problem Solving November 2, 2005.
A Sample Program #include using namespace std; int main(void) { cout
Beginning C for Engineers Fall 2005 Arrays, 2-D arrays, character strings Bettina Schimanski Lecture 5: Section 2 (9/28/05) Section 4 (9/29/05)
Lecture 4.1: More on Scanner, Methods, and Codingbat Michael Hsu CSULA.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
COP 3275 – Finishing Loops and Beginning Arrays Instructor: Diego Rivera-Gutierrez.
User-Written Functions
User Interaction and Variables
CS1010 Programming Methodology
הגדרת משתנים יום שלישי 27 נובמבר 2018
Java Programming Language
Arrays as pointers and other stuff
Structures, Unions, and Enumerations
Presentation transcript:

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

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.

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.

Quick: printing colors! Couple of resources: -text-in-terminal-aplications-in-unix -text-in-terminal-aplications-in-unix Try this piece of code: printf("\x1B[31mRED\033[0m"); Good luck!

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.

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]); }

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

Structures

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)

Defining Structures struct { };

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

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

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

Things to cover Initialization inline. Functions that receive structs.

More on structures

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.

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.

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.

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

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 mines not marked x non-mine marked by the user as mine

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

Structures struct { }; struct { } [ ];

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

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

Distance between two points

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

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