P-1 5/18/98 CSE / ENGR 142 Programming I Arrays of Structures © 1998 UW CSE.

Slides:



Advertisements
Similar presentations
David Notkin Autumn 2009 CSE303 Lecture 13 This space for rent.
Advertisements

Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
Arrays Hanly - Chapter 7 Friedman-Koffman - Chapter 9.
CS102--Object Oriented Programming Lecture 6: – The Arrays class – Multi-dimensional arrays Copyright © 2008 Xiaoyan Li.
Heterogeneous Structures 4 Collection of values of possibly differing types 4 Name the collection; name the components 4 Example: student record harveyC.
R-1 University of Washington Computer Programming I Lecture 17: Multidimensional Arrays © 2000 UW CSE.
Describing algorithms in pseudo code To describe algorithms we need a language which is: – less formal than programming languages (implementation details.
Advance Use of Structures CHAPTER 5. C.10 1 Using Structures with Functions » A function can return only one value back. » Some of the processes may yield.
Structures ANSI-C. Review: Data Structures Functions give us a way to organize programs. Data structures are needed to organize data, especially: –1.
Chapter 7 One-Dimensional Arrays 7.1 Arrays in C One of the more useful features of C is the ability to create arrays for storing a collection of related.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
14/3/02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Structures, ADT Lecture 25 14/3/2002.
1 Structures UniMAP SEM I - 11/12EKT 120 Computer Programming.
 Introduction to Computer Science COMP 51 – Fall 2012 – Section 2 Structures.
Arrays & Structures II Chapter 9. 2 Organizing Heterogeneous Data  Arrays allow a programmer to organize values of the same type  Homogeneous data 
Structures. Heterogeneous Structures Collection of values of possibly different types. Name the collection. Name the components. Example : Student record.
5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur1 Structures Lecture
Structured Data Types struct class Structured Data Types array – homogeneous container collections of only one type struct – heterogeneous data type.
11/5/2016CS150 Introduction to Computer Science 1 Announcements  Assignment 6 due on Wednesday, December 3, 2003  Final Exam on Tuesday, December 9,
String Review Strings are just arrays of charecters Since strings are arrays: –You cannot compare them with >, >=, !=, etc –You cannot copy/assign them.
Multiple Items in one Data Object Arrays are a way to store more than one piece of data in a data object, provided that all the data is of the same type.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
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.
1 11/30/05CS150 Introduction to Computer Science 1 Structs.
13/10/2016CS150 Introduction to Computer Science 1 Multidimensional Arrays  Arrays can have more than one column  Two dimensional arrays have two columns.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Pointers  * symbol and & symbol  Pointer operations  Pointer.
Arrays Chapter 7.
Arrays 4/4 By Pius Nyaanga. Outline Multidimensional Arrays Two-Dimensional Array as an Array of Arrays Using the length Instance Variable Multidimensional.
C Structures and Memory Allocation
Lesson #8 Structures Linked Lists Command Line Arguments.
Passing Objects to Methods
Arrays.
Arrays Low level collections.
Advance Use of Structures
CS1010 Programming Methodology
Chapter 10-1: Structure.
ECE Application Programming
Learning Objectives Pointers Pointer in function call
MIS 120 Structures.
CS150 Introduction to Computer Science 1
Arrays in C.
Structures.
C++ Arrays.
CS 1430: Programming in C++.
Methods and Parameters
For Monday Read WebCT quiz 18.
Arrays … The Sequel Applications and Extensions
Pass by Reference, const, readonly, struct
Describing algorithms in pseudo code
Structs And Arrays.
Array of Structures A structure holds only one record But a record
CSI 121 Structured Programming Language Lecture 24 Structures (Part 2)
Engr 0012 (04-1) LecNotes
Arrays and Arrays as Parameters
Arrays An array is a collection of variables that all have the same name and the same data type. Each member of the array is known as an element of the.
CS148 Introduction to Programming II
Sudeshna Sarkar 7th March 2017
CS2011 Introduction to Programming I Arrays (II)
Topics discussed in this section:
More on Asymptotic Analysis and Performance Measurement
Lec17 Structs.
More on Structs Sometimes we use structs even when the fields are all of the same type. If the fields are different conceptually, that is, the data stands.
Lecture 03 & 04 Method and Arrays Jaeki Song.
Multidimensional Arrays Section 6.4
Structures in c By Anand George.
Chapter 10 C Structures and Unions
Sorting Sorting is a fundamental problem in computer science.
Arrays.
Structures, Unions, and Enumerations
CS Problem Solving and Object Oriented Programming Spring 2019
Presentation transcript:

P-1 5/18/98 CSE / ENGR 142 Programming I Arrays of Structures © 1998 UW CSE

P-2 5/18/98 Structures and Arrays  A struct represents a single record  Typically, structs are used to deal with collections of such records  Examples: student records, employee records, customer records, parts records  In each case we will have multiple instances of the record (struct) type Arrays of structs are the natural way to do this

P-3 5/18/98 Recall… #define MAX_NAME 40 typedef struct { char name [MAX_NAME+1] ; int id ; double score ; } student_record ; typedef struct { int hours, minutes ; double seconds ; } time ; typedef struct { double x, y ; } point ;

P-4 5/18/98 Arrays of structs Each declaration below declares an array, where each array element is a structure: point corner_points[10]; time meeting_times[MAX_MEETINGS]; student_record cse_142[MAX_STUDENTS]; Using arrays of structs is a natural extension of principles we're already learned.

P-5 5/18/98 Using Arrays of structs  We access a field of a struct in an array by specifying the array element and then the field: cse_142[i].name corner_points[j+1].x meeting_times[4].hours

P-6 5/18/98 Naming in struct Arrays point pentagon[5]; x y x x y y x y x y pentagon -- an array of points pentagon[1] -- a point structure pentagon[4].x -- a double

P-7 5/18/98 Using Arrays of structs student_record class[MAX_STUDENTS] ;... for ( i = 0 ; i < nstudents ; i = i + 1 ) { scanf(“%d %d”, &class[i].hw, &class[i].exams) ; class[i].grade = (double) (class[i].hw + class[i].exams) / 50.0 ; }

P-8 5/18/98 struct Array Elements as Parameters void draw_line ( point p1, point p2) {.....}... point pentagon[5] ;... for ( i = 0 ; i < 4 ; i = i + 1) draw_line (pentagon[i], pentagon[i+1]) ; draw_line (pentagon[4], pentagon[0]) ;

P-9 5/18/98 structs and struct Arrays as Parameters A single struct is passed by value –all of its components are copied from the argument (actual parameter) to initialize the (formal) parameter. point set_midpt (point a; point b) {...} int main (void) { point p1, p2, m; /* declare 3 points */... m = set_midpt ( p1, p2); }

P-10 5/18/98 Passing Arrays of structs An array of structs is an array. When any array is an argument (actual parameter), it is passed by reference (not copied). [As for any array] –The parameter is an alias of the actual array argument int avg (student_rec class_db[MAX_N]) {...} int main (void) { student_rec cse_142[MAX_N]; int average;.... average = avg ( cse_142 ); /* by reference */ }

P-11 5/18/98 Sorting Arrays of structs Casey Sarah Phil Kathryn David David Casey Sarah Phil Kathryn typedef struct { char name [MAX_NAME + 1] ; int id ; double score ; } student_record ;

P-12 5/18/98 Insertion Sort for student_record /* sort student records a[0..size-1] in */ /* ascending order by score */ void sort (student_record a[ ], int size) { int j ; for ( j = 1 ; j < size ; j = j + 1 ) insert (a, j) ; }

P-13 5/18/98 /* given that a[0..j-1] is sorted, move a[j] to the correct */ /* location so that that a[0..j] is sorted by score */ void insert ( student_record a[ ], int j ) {int i; student_record temp ; temp = a[j] ; for ( i = j; i > 0 && a[i-1].score > temp.score; i = i - 1 ) { a[i] = a[i-1]; } a[i] = temp ; } Insert for sorting records

P-14 5/18/98 Alphabetical Order Casey Sarah Phil Kathryn David David Casey Sarah Phil typedef struct { charname[MAX_NAME + 1]; intid; doublescore; } student_record; Kathryn

P-15 5/18/98 String Comparison: strcmp “Alice”is less than“Bob” “Dave”is less than“David” “Rob”is less than“Robert” #include int strcmp (char str1[ ], char str2[ ]) returnsnegative integerif str1 is less than str2 0 if str1 equals str2 positive integerif str1 is greater than str2

P-16 5/18/98 /* given that a[0..j-1] is sorted, move a[j] to the correct */ /* location so that that a[0..j] is sorted by name */ void insert ( student_record a[ ], int j ) {int i; student_record temp ; temp = a[j] ; for ( i = j ; i > 0 && strcmp (a[i-1].name, temp.name) > 0 ; i = i - 1 ){ a[i] = a[i-1]; } a[i] = temp ; } Insert for Alphabetic Sorting

P-17 5/18/98 Type Quiz student_record a [MAX_STUDENTS]; a a[0] a[5].name a[4].id &a[6].score a[2].name[1] typedef struct { char name [MAX_NAME+1] ; int id ; double score ; } student_record ;

P-18 5/18/98 Data Structures: What If......you wanted to keep information about one song on the computer. –What pieces of data would you want? –How would you organize them? –How would it look in C? And then… –What if you wanted information about an entire CD of songs? –And then… how about a whole collection of CD's?