lecture06_4on1 - CS1010E Lecture 6 Structures Henry Chia

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS1010E Lecture 6 Structures Henry Chia hchia@comp.nus.edu.sg Semester 1 2011 / 2012 Department of Computer Science School Of Computing National University Of Singapore CS1010E Lecture 6 – p.1/24 Lecture Outline Structures. Structure definition. Declaration with structures. Assigning structures. Accessing members of a structure. Using functions with structures. Passing structures by value to a function. Returning a structure from a function. Problem solving involving structures. CS1010E Lecture 6 – p.2/24 Problem: Euclidean Distance Write a function distance that finds the straight line distance between two points ( x 1 , y 1 ) and ( x 2 , y 2 ) . Analysis: How do we represent a point in the Cartesian plane? As two separate values, or as one aggregate value? Calculate the distance between two points ( x 1 , y 1 ) and ( x 2 , y 2 ) using distance = radicalbig ( x 2- x 1 ) 2 + ( y 2- y 1 ) 2 CS1010E Lecture 6 – p.3/24 Using Functions with Structures double computeDist( double x1, double y1, double x2, double y2) { double dx = x2 - x1, dy = y2 - y1; return sqrt((dx * dx) + (dy * dy)); } main x1 1.0 y1 1.0 x2 2.0 y2 2.0 distance ? distance = computeDist(x1,y1,x2,y2); a18 a9 1.0,1.0,2.0,2.0 1.414.. computeDist x1 1.0 y1 1.0 x2 2.0 y2 2.0 return 1.414..; CS1010E Lecture 6 – p.4/24 Structures Thus far, declaring variables of int and double allow us to work with individual numeric values. To work with practical problems, each variable or value may constitute a set of information/data – record A point ( x, y ) comprises two floating-point values. A fraction x y comprises two integers for the numerator and denominator. A bank account is associated with an integer account number, and a floating-point balance. A structure defines a set of heterogeneous data for a record, i.e. the individual parts of the data do not have to be of the same type. CS1010E Lecture 6 – p.5/24 Structure Definition struct struct_identifier { declarations }; // Note the semi-colon Examples of a structure definitions: struct Point { double x, y; }; struct Fraction { int num; int den; }; struct BankAccount { int accountNum; double balance; }; Structures defined above function prototypes....
View Full Document

This note was uploaded on 12/13/2011 for the course ELECTRICAL CS1010E taught by Professor Henrytan during the Spring '11 term at National University of Singapore.

Page1 / 6

lecture06_4on1 - CS1010E Lecture 6 Structures Henry Chia

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online