1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.

Slides:



Advertisements
Similar presentations
Array. Convert Numbers in Different Base Systems Generate values to a series of numbers in different base systems: Base is between 2 and 9; Maximum number.
Advertisements

1 Arrays Chapter 9. 2 Outline  The array structure (Section 9.1)  Array declaration  Array initialization  Array subscripts  Sequential access to.
1 Chapter Structured Types, Data Abstraction and Classes Dale/Weems.
Informática II Prof. Dr. Gustavo Patiño MJ
Dale/Weems/Headington
Introduction to arrays Data in economy size packages.
1 Chapter 12 Arrays Dale/Weems/Headington. 2 Chapter 12 Topics l Declaring and Using a One-Dimensional Array l Passing an Array as a Function Argument.
Array What it is. How to use it How to declare it How to initialize it.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
Multiple-Subscripted Array
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
1 Chapter 12 Arrays Dale/Weems/Headington. 2 Chapter 12 Topics l Declaring and Using a One-Dimensional Array l Passing an Array as a Function Argument.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Chapter 7 Functions.
1 Chapter 7 Functions. 2 Chapter 7 Topics l Writing a Program Using Functional Decomposition l Writing a Void Function for a Task l Using Function Arguments.
Functions Parameters & Variable Scope Chapter 6. 2 Overview  Using Function Arguments and Parameters  Differences between Value Parameters and Reference.
Arrays One-Dimensional initialize & display Arrays as Arguments Part I.
1 Programming in C++ Dale/Weems/Headington Chapter 7 Functions.
1 Chapter 11 One-Dimensional Arrays. 2 Chapter 11 Topics l Atomic and composite data types l Declaring and instantiating an array l The length of an array.
Arrays in C++ Numeric Character. Structured Data Type A structured data type is a type that stores a collection of individual components with one variable.
1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.
1 C++ Syntax and Semantics, and the Program Development Process.
Review of ICS 102. Lecture Objectives To review the major topics covered in ICS 102 course Refresh the memory and get ready for the new adventure of ICS.
1 Functions every C++ program must have a function called main program execution always begins with function main any other functions are subprograms and.
Chapter 8 Functions. Chapter 8 Topics l Writing a Program Using Functional Decomposition l Writing a Void Function for a Task l Using Function Arguments.
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
Functions Modules in C++ are called functions and classes Functions are block of code separated from main() which do a certain task every C++ program must.
1 Functions. 2 Chapter 7 Topics  Writing a Program Using Functional Decomposition  Writing a Void Function for a Task  Using Function Arguments and.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 13: Pointers You are not responsible for virtual functions (starting on.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
Arrays in C++: Numeric Character (Part 2). Passing Arrays as Arguments in C++, arrays are always passed by reference (Pointer) whenever an array is passed.
1 Chapter 7 Functions Dale/Weems/Headington. 2 Chapter 7 Topics l Writing a Program Using Functional Decomposition l Writing a Void Function for a Task.
1 Chapter 11 Arrays. 2 Chapter 11 Topics l Declaring and Using a One-Dimensional Array l Passing an Array as a Function Argument Using const in Function.
1 Chapter 12-2 Arrays Dale/Weems. 2 Using Arrays as Arguments to Functions Generally, functions that work with arrays require 2 items of information n.
1 Chapter 12 Arrays. 2 Chapter 12 Topics l Declaring and Using a One-Dimensional Array l Passing an Array as a Function Argument Using const in Function.
1 Arrays. 2 Chapter 12 Topics  One-Dimensional Arrays  Using const in Function Prototypes  Using an Array of struct or class Objects  Using an enum.
1 One Dimensional Arrays Chapter 11 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores :
Chapter 7 Arrays. Introductions Declare 1 variable to store a test score of 1 student. int score; Declare 2 variables to store a test score of 2 students.
1 Chapter 12 Arrays Dale/Weems. 2 Chapter 12 Topics l Declaring and Using a One-Dimensional Array l Passing an Array as a Function Argument Using const.
1 Chapter 11 Arrays. 2 Chapter 11 Topics l Declaring and Using a One-Dimensional Array l Passing an Array as a Function Argument Using const in Function.
1 Chapter 12 Arrays. 2 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating.
Fall 1999CS Composite Data Types. Fall 1999CS Topics to be Covered l Abstract data types l One-dimensional arrays l Two-dimensional arrays.
1 Chapter 12 Arrays Dale/Weems. 2 Chapter 12 Topics l Declaring and Using a One-Dimensional Array l Passing an Array as a Function Argument Using const.
Arrays.
Module 1: Array ITEI222 - Advance Programming Language.
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
1 Chapter 12 Arrays Dale/Weems. 2 Chapter 12 Topics l Declaring and Using a One-Dimensional Array l Passing an Array as a Function Argument Using const.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 7 Pointers and C-Strings.
1 Chapter 7 Pointers and C-Strings. 2 Objectives  To describe what a pointer is (§7.1).  To learn how to declare a pointer and assign a value to it.
A Sample Program #include using namespace std; int main(void) { cout
1 Programming in C++ Dale/Weems/Headington Chapter 11 One-Dimensional Arrays.
Beginning C for Engineers Fall 2005 Arrays, 2-D arrays, character strings Bettina Schimanski Lecture 5: Section 2 (9/28/05) Section 4 (9/29/05)
Chapter 11 Arrays.
Lecture 7 1 Dimension Arrays Richard Gesick.
Dale/Weems/Headington
C++ Data Types Simple Structured Address Integral Floating
Chapter 15 Pointers, Dynamic Data, and Reference Types
Pointers, Dynamic Data, and Reference Types
Chapter 15 Pointers, Dynamic Data, and Reference Types
Chapter 11 Arrays.
Arrays Arrays A few types Structures of related data items
Structures Structured Data types Data abstraction structs ---
Pointers, Dynamic Data, and Reference Types
Presentation transcript:

1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long double

2 Structured Data Type A structured data type is a type that stores a collection of individual components with one variable name and allows individual components to be stored and retrieved

3 Declare variables to store and total 3 blood pressures int bp1, bp2, bp3; int total; bp2bp1bp3 cin >> bp1 >> bp2 >> bp3; total = bp1 + bp2 + bp3;

4 What if you wanted to store and total 1000 blood pressures? int bp[ 1000 ] ; // declares an array of 1000 int values bp[0] bp[1] bp[2].... bp[999]

5 One-Dimensional Array Definition An array is a structured collection of components (called array elements), all of the same data type, given a single name, and stored in adjacent memory locations. The individual components are accessed by using the array name together with an integral valued index in square brackets. The index indicates the position of the component within the collection.

6 Another Example Declare an array called temps which will hold up to 5 individual float values. float temps[5]; // declaration allocates memory temps[0] temps[1] temps[2] temps[3] temps[4] number of elements in the array indexes or subscripts Base Address

7 Declaration of an Array the index is also called the subscript in C++, the first array element always has subscript 0. The second array element has subscript 1, etc. the base address of an array is its beginning address in memory SYNTAX DataType ArrayName [ConstIntExpression];

8 Yet Another Example Declare an array called name which will hold up to 10 individual char values. char name[10]; // declaration allocates memory number of elements in the array name[0] name[1] name[2] name[3] name[4]..... name[9] Base Address

9 Assigning Values to Individual Array Elements float temps[ 5 ] ; // allocates memory for array int m = 4 ; temps[ 2 ] = 98.6 ; temps[ 3 ] = ; temps[ 0 ] = 99.4 ; temps[ m ] = temps[ 3 ] / 2.0 ; temps[ 1 ] = temps[ 3 ] ; // what value is assigned? temps[0] temps[1] temps[2] temps[3] temps[4] ?

10 What values are assigned? float temps[ 5 ] ; // allocates memory for array int m ; for (m = 0; m < 5; m++) { temps[ m ] = m  0.2 ; } temps[0] temps[1] temps[2] temps[3] temps[4] ? ? ? ? ?

11 Now what values are printed? float temps[ 5 ] ; // allocates memory for array int m ;..... for (m = 4; m >= 0; m-- ) { cout << temps[ m ] << endl ; } temps[0] temps[1] temps[2] temps[3] temps[4]

12 Variable Subscripts float temps[ 5 ] ; // allocates memory for array int m = 3 ; What is temps[ m + 1] ? What is temps[ m ] + 1 ? temps[0] temps[1] temps[2] temps[3] temps[4]

13 A Closer Look at the Compiler float temps[5]; // this declaration allocates memory To the compiler, the value of the identifier temps alone is the base address of the array. We say temps is a pointer (because its value is an address). It “points” to a memory location. temps[0] temps[1] temps[2] temps[3] temps[4]

14 Initializing in a Declaration int ages[ 5 ] = { 40, 13, 20, 19, 36 } ; for ( int m = 0; m < 5; m++ ) { cout << ages[ m ] ; } ages[0] ages[1] ages[2] ages[3] ages[4]

15 Passing Arrays as Arguments in C++, arrays are always passed by reference whenever an array is passed as an argument, its base address is sent to the called function

16 Using Arrays as Arguments to Functions Generally, functions that work with arrays require 2 items of information as arguments: the beginning memory address of the array (base address) the number of elements to process in the array

17 #include void Obtain ( int [ ], int ) ; // prototypes here void FindWarmest ( const int[ ], int, int & ) ; void FindAverage ( const int[ ], int, int & ) ; void Print ( const int [ ], int ) ; using namespace std ; int main ( ) { int temp[31] ; // array to hold up to 31 temperatures int numDays ; int average ; int hottest ; int m ; Example with Array Parameters 17

18 cout << “How many daily temperatures? ” ; cin >> numDays ; Obtain( temp, numDays ) ; // call passes value of numDays and // address of array temp to function cout << numDays << “ temperatures“ << endl ; Print ( temp, numDays ) ; FindAverage ( temp, numDays, average ) ; FindWarmest ( temp, numDays, hottest ) ; cout << endl << “Average was: “ << average << endl ; cout << “Highest was: “ << hottest << endl ; return 0 ; } Example continued 18

19 Memory Allocated for Array temp[0] temp[1] temp[2] temp[3] temp[4]..... temp[30] 6000 Base Address int temp[31] ; // array to hold up to 31 temperatures

20 void Obtain ( /* out */ int temp [ ], /* in */ int number ) // Has user enter number temperature values at keyboard // Precondition: // number is assigned && number > 0 // Postcondition: // temp [ 0.. number -1 ] are assigned { int m; for ( m = 0 ; m < number; m++ ) { cout << “Enter a temperature : “ ; cin >> temp [m] ; } 20

21 void Print ( /* in */ const int temp [ ], /* in */ int number ) // Prints number temperature values to screen // Precondition: // number is assigned && number > 0 // temp [0.. number -1 ] are assigned // Postcondition: // temp [ 0.. number -1 ] have been printed 5 to a line { int m; cout << “You entered: “ ; for ( m = 0 ; m < number; m++ ) { if ( m % 5 == 0 ) cout << endl ; cout << setw(7) << temp [m] ; } 21

22 Use of const because the identifier of an array holds the base address of the array, an & is never needed for an array in the parameter list arrays are always passed by reference to prevent elements of an array used as an argument from being unintentionally changed by the function, you place const in the function heading and prototype

23 Use of const in prototypes void Obtain ( int [ ], int ) ; void FindWarmest ( const int [ ], int, int & ) ; void FindAverage ( const int [ ], int, int & ) ; void Print ( const int [ ], int ) ; do not use const with outgoing array because function is supposed to change array values use const with incoming array values to prevent unintentional changes by function

24 void FindAverage ( /* in */ const int temp [ ], /* in */ int number, /* out */ int & avg ) // Determines average of temp[0.. number-1] // Precondition: // number is assigned && number > 0 // temp [0.. number -1 ] are assigned // Postcondition: // avg == arithmetic average of temp[0.. number-1] { int m; int total = 0; for ( m = 0 ; m < number; m++ ) { total = total + temp [m] ; } avg = int (float (total) / float (number) +.5) ; } 24

25 void FindWarmest ( /* in */ const int temp [ ], /* in */ int number, /* out */ int & largest ) // Determines largest of temp[0.. number-1] // Precondition: // number is assigned && number > 0 // temp [0.. number -1 ] are assigned // Postcondition: // largest== largest value in temp[0.. number-1] { int m; largest = temp[0] ; // initialize largest to first element // then compare with other elements for ( m = 0 ; m < number; m++ ) { if ( temp [m] > largest ) largest = temp[m] ; } 25

26 Using arrays for Counters Write a program to count the number of each alphabet letter in a text file. letterASCII ‘A’ 65 ‘B’ 66 ‘C’ 67 ‘D’ 68.. ‘Z’ 90 This is my text file. It contains many things! is not 14. Is it? A:\my.dat

27 const int SIZE 91; int freqCount[SIZE]; freqCount [ 0 ] 0 freqCount [ 1] 0.. freqCount [ 65 ] 2 freqCount [ 66 ] 0.. freqCount [ 89] 1 freqCount [ 90 ] 0 unused counts ‘A’ and ‘a’ counts ‘B’ and ‘b’. counts ‘ Y’ and ‘y’ counts ‘Z’ and ‘z’

28 void PrintOccurrences ( /* in */ const int freqCount [ ] ) // Prints each alphabet character and its frequency // Precondition: //freqCount [ ‘A’.. ‘Z’ ] are assigned // Postcondition: //freqCount [ ‘A’.. ‘Z’ ] have been printed {char index ; cout << “File contained “ << endl ; cout << “LETTER OCCURRENCES” << endl ; for ( index = ‘A’ ; index < = ‘Z’ ; index ++ ) { cout << setw ( 4 ) << index << setw ( 10 ) << freqCount [ index ] << endl ; } 28

29 More about Array Index array index can be any integral type. This includes char it is programmer’s responsibility to make sure that an array index does not go out of bounds. The index must be within the range 0 through the declared array size minus one using an index value outside this range causes the program to access memory locations outside the array. The index value determines which memory location is used