Week 13 Structures Aaron Tan. Q1 What is the output? 2 #include typedef struct { int p; float q; } one_t; typedef struct { int p; float q; } two_t; int.

Slides:



Advertisements
Similar presentations
C Structures What is a structure? A structure is a collection of related variables. It may contain variables of many different data types---in contrast.
Advertisements

Enumerated Types 4 Besides the built-in types, ANSI C allows the definition of user-defined enumerated types –To define a user-define type, you must give.
1 CSE 303 Lecture 12 structured data reading: Programming in C Ch. 9 slides created by Marty Stepp
CS1061 C Programming Lecture 6: Operators and Expressions A. O’Riordan, 2004.
Basic Input/Output and Variables Ethan Cerami New York
Pointers| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 1 Pointers by Jumail Bin Taliba Faculty of Computer.
Lecture No: 16. The scanf() function In C programming language, the scanf() function is used to read information from standard input device (keyboard).
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Structs. Structures We already know that arrays are many variables of the same type grouped together under the same name. Structures are like arrays except.
'C' Programming With Structure Records Purpose of structures Coding a structure template Defining a new data type Functions which communicate using structures.
Pointers CS362. Pointers A Pointer is a variable that can hold a memory address Pointers can be used to: Indirectly reference existing variables (sometimes.
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
ARRAY Prepared by MMD, Edited by MSY1.  Introduction to arrays  Declaring arrays  Initializing arrays  Examples using arrays  Relationship with pointers.
1 Pointers and Arrays. 2 When an array is declared,  The compiler allocates sufficient amount of storage to contain all the elements of the array in.
UNIT 14 Functions with Pointer Parameters.
Chapter 6: Control Structures Computer Programming Skills Second Term Department of Computer Science Foundation Year Program Umm Alqura.
Chapter 7 Formatted input and output. 7.1 introduction Tax: This result is correct; but it would be better Maybe as $13, Make formatting.
Chapter 11: Pointers Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 11 Pointers.
Introduction to Programming 3D Applications CE Lecture 12 Structure Pointers and Memory Management in C.
CS140: Intro to CS An Overview of Programming in C (part 3) by Erin Chambers.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
Spring 2005, Gülcihan Özdemir Dağ Lecture 11, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 11 Outline 11.1.
1 Original Source : and Problem and Problem Solving.ppt.
CS140: Intro to CS An Overview of Programming in C by Erin Chambers.
ECE 103 Engineering Programming Chapter 49 Structures Unions, Part 1 Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE.
 Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create.
Welcome to Concepts Pointer Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्जेण्डर )PGT(CS) KV JHAGRAKHAND.
Welcome to Concepts of Pointers. Prepared by:- Sumit Kumar PGT(Computer Science) Kv,Samba.
CPS120: Introduction to Computer Science Lecture 15A Structures.
Prepared by MMD, Edited by MSY1 CHAPTER 4 ARRAY. Prepared by MMD, Edited by MSY2 Arrays  Introduction to arrays  Declaring arrays  Initializing arrays.
Pointers Value, Address, and Pointer. Values and Addresses int x, y, z; y x z values of x,
CSCI 171 Presentation 3. Operators Instructs C to perform some operation Assignment = Mathematical Relational Logical.
CPS120: Introduction to Computer Science Data Structures.
Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.
WEEK 8 Class Activities Lecturer’s slides.
Computer Programming for Engineers
COMPUTER PROGRAMMING. Array C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An.
Structures and Union. Review bitwise operations –you need them for performance in terms of space and time –shifts are equivalent to arithmetics enumeration.
Advanced Pointer Topics. Pointers to Pointers u A pointer variable is a variable that takes some memory address as its value. Therefore, you can have.
CSE 251 Dr. Charles B. Owen Programming in C1 structs Aggregating associated data into a single variable Box width length height Circle radius int main()
8. ARRAYS. Aggregate variables Scalar variables –Single value Aggregate variables –Collection of values –Arrays: elements have the same type.
2. C FUNDAMENTALS. Example: Printing a Message /* Illustrates comments, strings, and the printf function */ #include int main(void) { printf("To C, or.
UNIT 8 Pointers.
STRUCTURES. INTRODUCTION A structure is same as that of records. It stores related information about an entity. Structure is basically a user defined.
Chapter 3: Formatted Input/Output 1 Chapter 3 Formatted Input/Output.
Lecture 10: Structures. Outline Introduction Structure Definitions and declarations Initializing Structures Operations on Structures members Structures.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
Arrays Name, Index, Address. Arrays – Declaration and Initialization int x; y[0] y[1] y[2]
Ex-1 #include <stdio.h> struct sample { int a=0; char b='A';
CS1010 Programming Methodology
CS1010 Programming Methodology
Computer Programming Techniques Semester 1, 1998
CS1010 Programming Methodology
CS1010 Programming Methodology
CS1010 Discussion Group 11 Week 12 – Structured Structures.
Visit for more Learning Resources
Introduction to C Programming
Pointers and References
Buy book Online -
Pointer to a Structure & Structure Containing a Pointer Difference Lesson xx  In this presentation, we will illustrate the difference between a pointer.
Structures and Union.
Introduction to C Programming
Chapter 2.1 Repetition.
Structures and Union.
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Structures and Union.
Programming in C Pointer Basics.
Pointers and References
Presentation transcript:

Week 13 Structures Aaron Tan

Q1 What is the output? 2 #include typedef struct { int p; float q; } one_t; typedef struct { int p; float q; } two_t; int main(void) { one_t one = { 1, 2.3 }; two_t two; two = one; printf("%d %f\n", one.p, one.q); printf("%d %f\n", two.p, two.q); return 0; } Compilation error! Incompatible types when assigning to type 'two_t' from type 'one_t'

Q2a Which statements are correct? 3 typedef struct { int a; float b; } s_t; s_t s; i.scanf("%d", &(s.a)); ii.scanf("%d", s.&a); iii.scanf("%d", (&s).a); iv.scanf("%d", &s.a); a b s  Target = s.a  Hence, address of target is &(s.a)  Since dot operator ‘.’ has higher precedence than address operator ‘&’, parentheses can be omitted: &s.a To read a value into member a of variable s :

Q2b Which statements are correct? 4 typedef struct { int a; float b; } s_t; s_t s; s_t *p; p = &s; i.scanf("%d", &p.a); ii.scanf("%d", &(p.a)); iii.scanf("%d", *p.a); iv.scanf("%d", p.a); v.scanf("%d", (*p).a); vi.scanf("%d", p->a); vii.scanf("%d", &(*p).a); viii.scanf("%d", &(*p.a)); ix.scanf("%d", &(p->a)); a b s  *p is equivalent to s, hence target = (*p).a  [Target is not *p.a because dot ‘.’ has higher precedence than ‘*’, hence *p.a is *(p.a)]  Hence, address of target is &((*p).a) or &(*p).a  Since (*p).a is equivalent to p->a, hence &((*p).a) is equivalent to &(p->a) To read a value into member a of variable s through p : p

Q3 Tiles (1/4) 5  Read an integer (>1) indicating number of tiles, followed by tiles’ data (length, width, price per m 2 )  Compute and output the difference in cost between the cheapest and most expensive tile. int scan_tiles(tile_t tiles[]); float difference(tile_t tiles[], int size); Enter number of tiles: 5 Enter data for 5 tiles: Largest difference = $15.90 Enter number of tiles: 5 Enter data for 5 tiles: Largest difference = $15.90

Q3 Tiles (2/4) 6 int scan_tiles(tile_t []); float difference(tile_t [], int); // Define tile_t below int main(void) { tile_t tiles[MAX_TILES]; int num_tiles; printf("Largest difference = $%.2f\n", difference(tiles, num_tiles)); return 0; } typedef struct { int length, width; float price; } tile_t; num_tiles = scan_tiles(tiles);

Q3 Tiles (3/4) 7 // To read tiles' data into array tiles // Return the number of tiles read int scan_tiles(tile_t tiles[]) { int num_tiles; printf("Enter number of tiles: "); printf("Enter data for %d tiles:\n", num_tiles); } int i; scanf("%d", &num_tiles); for (i=0; i<num_tiles; i++) { scanf("%d %d %f", &tiles[i].length, &tiles[i].width, &tiles[i].price); } return num_tiles;

Q3 Tiles (4/4) 8 // Return the difference in cost between // the cheapest tile and the most expensive tile. float difference(tile_t tiles[], int size) { } int i; float cost, min_cost, max_cost; min_cost = max_cost = tiles[0].length * tiles[0].width * tiles[0].price; for (i=1; i<size; i++) { cost = tiles[i].length * tiles[i].width * tiles[i].price; if (cost < min_cost) min_cost = cost; if (cost > max_cost) max_cost = cost; } return max_cost - min_cost; Do you need to sort? No, sorting takes O(n 2 ) comparisons, but this takes only O(n) comparisons.

Q6 Class Schedule Free period Most concurrent lessons Approach 1: Using a timeline array

Q6 Class Schedule 10 Approach 2: Using an array of endpoints typedef struct { int time; int type; } endpoint_t; #define START 1 #define FINISH -1

11 End of file