DOC PREVIEW
MIT 16 070 - Composite Data Types

This preview shows page 1-2-3-4-5-6 out of 17 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Fesq, 3/12/01 1 16.070Composite Data Types3/12/01 Lecture #14 16.070• Many programs operate on large amounts of data• Useful to store/manipulate data in logical manner• Two techniques for representing collection of related data types! Arrays -- to handle collection of data of same data type! Structures -- to handle collection of data of different data typesFesq, 3/12/01 2 16.070Arrays• An array is a collection of identically typed variables storedcontiguously in memory• An array is made up of a fixed number of elements, which arereferenced by subscripts• The basic purpose of an array is to store large amounts of related datathat share the same data type! Example: Analyze temperature fluctuations over the course of the year− Store average temperature for each day− Requires 365 memory locations− Declare 365 variables, each with a unique name?− In linear algebra, use a one-dimensional matrix (vector): t(1), t(2), …, t(365)Fesq, 3/12/01 3 16.070Array Declaration in C• Array Declaration defines a template that describes the number ofelements, and the type of the elements:Format: <type> <array-name>[<num-of-elements>];! Example: int daily_temp[365];Memory now is allocated – storage space for 365 integer values for Arraydaily_temp! Example: float sc_torque[3];Memory now is allocated – storage space for 3 float values for Arraysc_torque• Memory size is determined by array type and number of elements• Array Declaration defines a variable object, and allocates memory ofthe specified range and of the specified type for the elementsFesq, 3/12/01 4 16.070Array Subscripts• Array elements are referenced by subscript, also known as indexsc_torque[0] – the first element in array sc_torquesc_torque[1] – the secondsc_torque[2] – the third• Subscripts must be discrete (integers) and within the range defined inthe Array Declaration Statement! Example: sc_torque[1] = 1.1;Sets the second element of sc_torque equal to 1.1! Example: x = sc_torque[0] + 1.0;Reads the value stored in first element of sc_torque, adds 1 to it, and storesresult into variable x• Subscripts can be any legal C expression of integer typesc_torque[y+1] = sc_torque[y] + 2.0;! Remember: C provides zero-based subscripting! First subscript is 0, not 1.Fesq, 3/12/01 5 16.070Initializing Arrays• Arrays, just like variables, should be initialized• Without initialization, array elements contain random data - garbage! Initialization can be defined at the time of declaration, and will beperformed at compile time:float sc_torque[3] = {0.0, 0.0, 0.0};− Initializing more values than elements will cause compiler error− Initializing fewer values than elements, remaining elements are initializedto zero− If no array size is specified, compiler defines array size based on numberof initial values! Initialization can also be done at run time, e.g., with a loop:for (j = 0; j < 3; j++)sc_torque[j] = 0.0;• Consequences for not initializing arrays?Fesq, 3/12/01 6 16.070ElementAddress(in hex)Contents8 bytessc_torque[0]sc_torque[2]sc_torque[1]1001081101181.01.5?How Arrays are Stored in Memory• Consider the 3-element float array sc_torqueExample 1:float sc_torque[3] = {1.0, 1.5};Example 2:float sc_torque[3];sc_torque[0] = 1.0;sc_torque[1] = 1.5;Fesq, 3/12/01 7 16.070Array Attributes• C defines attribute function sizeof that can be used to determine thebounds of an array! sizeof (<array-name>) returns the size of the array in bytessizeof (sc_torque) evaluates to 24 - three 8-byte floats! sizeof (<array-name> [<element number>]) returns the size of theelement in bytessizeof (sc_torque[0]) evaluates to 8! Can you use sizeof to determine the number of elements in an array?num_elements = (sizeof (sc_torque)) / (sizeof (float))Fesq, 3/12/01 8 16.070Operations on Arrays• Operations performed on arrays are ones that can be performed on anyvariable whose type is the same as the array/* print # of days for each month for non-leap years */#include <stdio.h>int main (void){const int months = 12;int days[months] = {31,28,31,30,31,30,31,31,30,31,30,31};int index;for (index = 0; index < months; index++) printf (Month %d has %d days.\n", index+1, days[index]);/* end for */return 0;}Fesq, 3/12/01 9 16.070Example of Array Operations• Add two vectors, represented by arrays, by adding correspondingelements from each array to calculate the sum#define Vector_size 6int main (void){int i;int j;int VectorA[Vector_size];int VectorB[Vector_size];int VectorSum[Vector_size];…… /* input Vectors A and B */…for (i = 0; i < Vector_size; i++)Vector_Sum[i] = VectorA[i] + VectorB[i];}…Fesq, 3/12/01 10 16.070Exceeding Array Boundaries• !It is the responsibility of the programmer to make sure that arraysubscripts are within bounds!• Exceeding array subscript bounds will overwrite other memorylocations, often producing garbage output -- a difficult error to debug! Compiler does no checking to determine if a subscript is within the boundsof an array! Exception: Compiler will report an error if number of values in a compile-time initialization statement exceeds the number of elements in the arrayfloat sc_torque[3] = {1.0, 1.5, 2.0, 2.5};/*compiler-detected error*/! Example of error not detected by compilerint sc_torque[3];for (j = 1; j <= 3; j++)sc_torque[j] = 0.0;Fesq, 3/12/01 11 16.070Structures• A structure is a collection of related data values that can be of differenttypes• A structure is made up of components or members, which can be ofdifferent types• Components have names instead of subscript values• A structure is a programmer-defined data type (unlike arrays); theprogrammer defines names of components, types of components, andorder of components! Example: Characterize reaction wheel behavior− Determine power state: 0 = off, 1 = on− Identify torque for each axis: roll, pitch, yaw− Read speed command: 0 = 0 speed, 255 = full speed (e.g., 2000rpm)Fesq, 3/12/01 12 16.070Declaring Structure Data Types• Declaring a structure defines a template that describes the format of thestructure, and the name and format of each component:Format: struct <struct_name>{<type component_name1><type component_name2>…};Example: struct rw_struct{int power;float roll_torque;float pitch_torque;float yaw_torque;int cmd;};• A structure declaration is the master plan that describes how thestructure is put togetherFesq, 3/12/01 13 16.070 Declaring Structure Variables• Structure


View Full Document

MIT 16 070 - Composite Data Types

Documents in this Course
optim

optim

20 pages

Load more
Download Composite Data Types
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Composite Data Types and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Composite Data Types 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?