CSC 107 – Programming For Science. Today’s Goal  Discover best uses of structures in a program  Using typedef to make meaningful names.

Slides:



Advertisements
Similar presentations
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.
Advertisements

C Structures Basics of structures Typedef. Data Hierarchy Byte –8 bits (ASCII character ‘A’ = ) Field –Group of characters (character string “Fred”)
Structures Spring 2013Programming and Data Structure1.
CSC 107 – Programming For Science. Today’s Goal  Get familiar with multi-dimensional arrays  Creating variables for multi-dimensional array  Multi-dimensional.
CSE 451: Operating Systems Section 1. Why are you here? 9/30/102.
CSC Programming for Science Lecture 30: Pointers.
Encapsulation by Subprograms and Type Definitions
CS 61C L4 Structs (1) A Carle, Summer 2005 © UCB inst.eecs.berkeley.edu/~cs61c/su05 CS61C : Machine Structures Lecture #4: Strings & Structs
Structures, Unions, and Typedefs CS-2303, C-Term Structures, Unions, and Typedefs CS-2303 System Programming Concepts (Slides include materials from.
CSC 107 – Programming For Science. Today’s Goal  Learn how pointers really used with arrays  Exploit the similarity between pointers & arrays  Take.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 23P. 1Winter Quarter Structs and Enumeration.
CSC 107 – Programming For Science. Today’s Goal  Learn C functions to input and output data  Using printf to print out variables and strings  Read.
CSC 107 – Programming For Science. Today’s Goal  Learn how arrays normally used in real programs  Why a function returning an array causes bugs  How.
Announcements  If you need more review of Java…  I have lots of good resources – talk to me  Use “Additional Help” link on webpage  Weekly assignments.
CSC 107 – Programming For Science. Today’s Goal ALL  Understand why ALL I/O is file I/O  Common bugs to avoid when coding with files in C++  Get a.
CSC 107 – Programming For Science. Today’s Goal  Learn relationship between pointers & arrays  How and why they both are similar  Take advantage of.
Recap, Test 1 prep, Composition and Inheritance. Dates Test 1 – 12 th of March Assignment 1 – 20 th of March.
Engineering Computing I Chapter 6 Structures. Sgtructures  A structure is a collection of one or more variables, possibly of different types, grouped.
CSC 107 – Programming For Science. Announcements  Textbook available from library’s closed reserve.
CSC 107 – Programming For Science. Today’s Goal Variables  Variable  Variable name location to store data  Only for humans; 0 x 7E8A2410 harder to.
1 Principles of Computer Science I Honors Section Note Set 10 CSE 1341.
CSC 107 – Programming For Science. Today’s Goal  Learn how pointers really used with arrays  Exploit the similarity between pointers & arrays  Take.
CSC 107 – Programming For Science. Today’s Goal  Discuss how to hand data to functions  Review loopholes in variables & scoping rules  Ways to get.
PHY 107 – Programming For Science. Announcements  Slides, activities, & solutions always posted to D2L  Note-taking versions before class, for those.
CSC 107 – Programming For Science. Today’s Goal  When lecture over, start understanding pointers  What a pointer is and what it is not  Why pointers.
ECE 103 Engineering Programming Chapter 48 Typedef and Enum Type Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103.
CSC 107 – Programming For Science. Announcements  Memorization is not important, but…  … you will all still be responsible for information  Instead.
CSC 107 – Programming For Science. Today’s Goal  Understand what cStrings are  Relationship to arrays & primitives  What the null character is; why.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 23P. 1Winter Quarter Structs and Enumeration Lecture 23.
1 CSC241: Object Oriented Programming Lecture No 22.
CSC 107 – Programming For Science. Today’s Goal  When lecture over, start understanding pointers  What a pointer is and what it is not  Why pointers.
Question of the Day You overhear a boy & his mother talking: Mom:What is ? Boy: That's easy, 33. Mom: Good. What's ? Boy:Simple. It's 40. Mom:Excellent!
CSC 107 – Programming For Science. The Week’s Goal.
CSC 107 – Programming For Science. Today’s Goal  Become familiar with simple arrays  Declaring an array variable  Assigning data to array entries 
Question of the Day You overhear a boy & his mother talking: Mom:What is ? Boy: That's easy, 33. Mom: Good. What's ? Boy:Simple. It's 40. Mom:Excellent!
CSC 107 – Programming For Science. Today’s Goal  Discuss writing functions that return values  return statement’s meaning and how it works  When and.
 Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create.
CSC 107 – Programming For Science. Today’s Goal  Better understand arrays and how they work  Using array variable & its entries  When calling function,
Introduction to C Programming Lecture 6. Functions – Call by value – Call by reference Arrays Today's Lecture Includes.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 13: Data structures in C.
CSC 212 – Data Structures Lecture 2: Primitives, References, & Classes.
CSC Programming for Science Lecture 36: Structures.
ECE Application Programming
CSC 107 – Programming For Science. Today’s Goal  Learn what structures are & how they are used  Why we need & why would use a structure  What fields.
11/5/2016CS150 Introduction to Computer Science 1 Announcements  Assignment 6 due on Wednesday, December 3, 2003  Final Exam on Tuesday, December 9,
Today’s Lecture  Literal  Constant  Precedence rules  More assignment rules  Program Style.
Weekly C-minar Week 0. Today: Steps of the compile Basic inclusion/syntax rules Low-cost containment Debugging.
CSC Programming for Science Lecture 34: Dynamic Pointers.
Structures and Union. Review bitwise operations –you need them for performance in terms of space and time –shifts are equivalent to arithmetics enumeration.
Week 7 Part I Kyle Dewey. Overview Code from last time Array initialization Pointers vs. arrays Structs typedef Bubble sort (if time)
Introduction to Computers and Programming Class 24 Structures (structs) Professor Avi Rosenfeld.
CSC Programming for Science Lecture 37 : Course Review.
LECTURE 21: RECURSION & LINKED LIST REVIEW CSC 212 – Data Structures.
CSC 107 – Programming For Science. Today’s Goal  Discover best uses of structures in a program  How we can mix pointers inside structures  Assigning.
1 11/30/05CS150 Introduction to Computer Science 1 Structs.
CSC 107 – Programming For Science. Announcements  Lectures may not cover all material from book  Material that is most difficult or challenging is focus.
PHY 107 – Programming For Science. Today’s Goal  Understand what cStrings are  Relationship to arrays & primitives  What it is and why null character.
CSC Programming for Science Lecture 26: Arrays.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Reminders: Have you filled out the questionnaire in Moodle? (3 left – as of last night). Office hours.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Pointers  * symbol and & symbol  Pointer operations  Pointer.
Advanced Programming Constants, Declarations, and Definitions Derived Data Types.
CSC Programming for Science Lecture 23: More on Function Parameters.
Current Assignments Project 3 has been posted, due next Tuesday. Write a contact manager. Homework 6 will be posted this afternoon and will be due Friday.
University of Malta CSA2090: Lecture 5 © Chris Staff 1 of 22 CSA2090: Systems Programming Introduction to C Dr. Christopher Staff.
CSC 107 – Programming For Science. Today’s Goal  Write functions that take & return values  How parameters declared and how we call functions  What.
Ex-1 #include <stdio.h> struct sample { int a=0; char b='A';
CS1010 Discussion Group 11 Week 12 – Structured Structures.
CSC 253 Lecture 13.
Structures and Union.
C Structures, Unions, Bit Manipulations and Enumerations
Presentation transcript:

CSC 107 – Programming For Science

Today’s Goal  Discover best uses of structures in a program  Using typedef to make meaningful names

Creating A struct

Using structs

Assigning struct s  Like all variables, can assign struct s  Identical struct type needed for source & target  Works like assigning fields in source to fields in target  Primitive values are copied like primitive variables  With assignment, arrays & pointers alias same value  Fields just like variables of the same type  Can be used just like any variable of that type  Need struct to get access to the field initially

Pointers to struct

typedef s For Simplicity  Make synonyms for types using a typedef: typedef original type new_name;  Simplify & clarify code by making types meaningful  Synonym only for coder; computer ignores difference  Really creates shorthand; does NOT make new type typedef long BIG_INTEGER; typedef unsigned long bob; typedef char * CSTRING; typedef struct energy ENERGY;

Will It Compile? typedef int MIDDLE; typedef char* CSTRING; typedef struct energy ENERGY; MIDDLE i = 5; CSTRING str = “Bob”; struct energy ball; ENERGY *nerf = &ball; int j = i; strcpy(str, “Yo, whatup dog”); nerf = new struct energy[100]; i++; str = new CSTRING[23]; nerf = new ENERGY[12]; nerf[2] = ball; str = new char[42];

Will It Compile? typedef int MIDDLE; typedef char* CSTRING; typedef struct energy ENERGY; MIDDLE i = 5; CSTRING str = “Bob”; struct energy ball; ENERGY *nerf = &ball; int j = i; strcpy(str, “Yo, whatup dog”); nerf = new struct energy[100]; i++; str = new CSTRING[23]; nerf = new ENERGY[12]; nerf[2] = ball; str = new char[42];

Will It Compile? typedef int MIDDLE; typedef char* CSTRING; typedef struct energy ENERGY; MIDDLE i = 5; CSTRING str = “Bob”; struct energy ball; ENERGY *nerf = &ball; int j = i; strcpy(str, “Yo, whatup dog”); nerf = new struct energy[100]; i++; str = new CSTRING[23]; nerf = new ENERGY[12]; nerf[2] = ball; str = new char[42];

Will It Compile? typedef int MIDDLE; typedef char* CSTRING; typedef struct energy ENERGY; MIDDLE i = 5; CSTRING str = “Bob”; struct energy ball; ENERGY *nerf = &ball; int j = i; strcpy(str, “Yo, whatup dog”); nerf = new struct energy[100]; i++; str = new CSTRING[23]; nerf = new ENERGY[12]; nerf[2] = ball; str = new char[42];

Will It Compile? typedef int MIDDLE; typedef char* CSTRING; typedef struct energy ENERGY; MIDDLE i = 5; CSTRING str = “Bob”; struct energy ball; ENERGY *nerf = &ball; int j = i; strcpy(str, “Yo, whatup dog”); nerf = new struct energy[100]; i++; str = new CSTRING[23]; nerf = new ENERGY[12]; nerf[2] = ball; str = new char[42];

Will It Compile? typedef int MIDDLE; typedef char* CSTRING; typedef struct energy ENERGY; MIDDLE i = 5; CSTRING str = “Bob”; struct energy ball; ENERGY *nerf = &ball; int j = i; strcpy(str, “Yo, whatup dog”); nerf = new struct energy[100]; i++; str = new CSTRING[23]; nerf = new ENERGY[12]; nerf[2] = ball; str = new char[42];

Will It Compile? typedef int MIDDLE; typedef char* CSTRING; typedef struct energy ENERGY; MIDDLE i = 5; CSTRING str = “Bob”; struct energy ball; ENERGY *nerf = &ball; int j = i; strcpy(str, “Yo, whatup dog”); nerf = new struct energy[100]; i++; str = new CSTRING[23]; nerf = new ENERGY[12]; nerf[2] = ball; str = new char[42];

Will It Compile? typedef int MIDDLE; typedef char* CSTRING; typedef struct energy ENERGY; MIDDLE i = 5; CSTRING str = “Bob”; struct energy ball; ENERGY *nerf = &ball; int j = i; strcpy(str, “Yo, whatup dog”); nerf = new struct energy[100]; i++; str = new CSTRING[23]; nerf = new ENERGY[12]; nerf[2] = ball; str = new char[42];

Will It Compile? typedef int MIDDLE; typedef char* CSTRING; typedef struct energy ENERGY; MIDDLE i = 5; CSTRING str = “Bob”; struct energy ball; ENERGY *nerf = &ball; int j = i; strcpy(str, “Yo, whatup dog”); nerf = new struct energy[100]; i++; str = new CSTRING[23]; nerf = new ENERGY[12]; nerf[2] = ball; str = new char[42];

Will It Compile? typedef int MIDDLE; typedef char* CSTRING; typedef struct energy ENERGY; MIDDLE i = 5; CSTRING str = “Bob”; struct energy ball; ENERGY *nerf = &ball; int j = i; strcpy(str, “Yo, whatup dog”); nerf = new struct energy[100]; i++; str = new CSTRING[23]; nerf = new ENERGY[12]; nerf[2] = ball; str = new char[42];

Will It Compile? typedef int MIDDLE; typedef char* CSTRING; typedef struct energy ENERGY; MIDDLE i = 5; CSTRING str = “Bob”; struct energy ball; ENERGY *nerf = &ball; int j = i; strcpy(str, “Yo, whatup dog”); nerf = new struct energy[100]; i++; str = new CSTRING[23]; nerf = new ENERGY[12]; nerf[2] = ball; str = new char[42];

Will It Compile? typedef int MIDDLE; typedef char* CSTRING; typedef struct energy ENERGY; MIDDLE i = 5; CSTRING str = “Bob”; struct energy ball; ENERGY *nerf = &ball; int j = i; strcpy(str, “Yo, whatup dog”); nerf = new struct energy[100]; i++; str = new CSTRING[23]; nerf = new ENERGY[12]; nerf[2] = ball; str = new char[42];

Your Turn  Get into your groups and try this assignment

For Next Lecture  Spend 1 last day on struct s on Wednesday  Write better solution to well-known problem  Understand why struct s used in real-world problems  Before moving on in class, solidify students knowledge  Angel has Weekly Assignment #12 for Tuesday  Last programming assignment  Last programming assignment due next week