1 / 16

Arrays in C++ Numeric Character

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 name and allows individual components to be stored and retrieved.

zoe-bowen
Télécharger la présentation

Arrays in C++ Numeric Character

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Arrays in C++ Numeric Character

  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. An array is a named block of memory that can store a group of related data. The individual elements in the group will be related. For example if one needs to keep records on the sales information for a car dealership the best way would be a column to represent the data, with a header to represent the type of data being shown.

  4. Array in C++ : Depending on what type of data is being stored the array type is either Numeric or Character type. Numeric type will have either real or integer values and arithmetic operations are allowed on the elements of the array. The components of an array are: Index Name Name of the array is used to access the block of memory used and the Index is used to find the particular element that we are looking for.

  5. 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 [DeclaredSize];

  6. Numeric Arrays: int stocks [ 10 ] ; /* Integer numbers */ float sales [10] ; /* Real numbers */ /* Examples on using and accessing an array. All arrays start with index 0 and end with index size-1. */ stocks[0] = 0 ; /* Would access the first element of the array */ sales[0] = 0.0 ; /* and initialize it to 0. */

  7. Declare variables to store and total 3 blood pressures int bp1, bp2, bp3; // bp[3]; int total; 4000 4004 4008 bp1 bp2 bp3 cout << bp1 << bp2 << bp3; total = bp1 + bp2 + bp3;

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

  9. 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 storedinadjacentmemorylocations. 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.

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

  11. Assigning Values to Individual Array Elements float temps[ 5 ] ;// allocates memory for array int m = 4 ; temps[ 2 ] = 98.6 ; temps[ 3 ] = 101.2 ; temps[ 0 ] = 99.4 ; temps[ m ] = temps[ 3 ] / 2.0 ; temps[ 1 ] = temps[ 3 ] - 1.2 ; // what value is assigned? 7000 7004 7008 7012 7016 99.4 ? 98.6 101.2 50.6 temps[0] temps[1] temps[2] temps[3] temps[4]

  12. What values are assigned? float temps[ 5 ] ;// allocates memory for array int m ; for (m = 0; m < 5; m++) { temps[ m ] = 100.0 + m *0.2 ; } 7000 7004 7008 7012 7016 ? ? ? ? ? temps[0] temps[1] temps[2] temps[3] temps[4]

  13. Now what values are printed? float temps[ 5 ] ;// allocates memory for array int m ; . . . . . for (m = 4; m >= 0; m-- ) { cout << temps[ m ] << endl; // Output? } 7000 7004 7008 7012 7016 100.0 100.2 100.4 100.6 100.8 temps[0] temps[1] temps[2] temps[3] temps[4]

  14. Variable Subscripts float temps[ 5 ] ;// allocates memory for array int m = 3 ; . . . . . . What is temps[ m + 1] ? What is temps[ m ] + 1 ? 7000 7004 7008 7012 7016 100.0 100.2 100.4 100.6 100.8 temps[0] temps[1] temps[2] temps[3] temps[4]

  15. A Closer Look at the Compiler 7000 7004 7008 7012 7016 100.0 100.2 100.4 100.6 100.8 temps[0] temps[1] temps[2] temps[3] temps[4] float temps[5]; // this declaration allocates memory To the compiler, the value of the identifiertempsaloneis the base address of the array.We say temps is a pointer (because its value is an address).It “points” to a memory location.

  16. 6000 6004 6008 6012 6016 40 13 20 19 36 ages[0] ages[1] ages[2] ages[3] ages[4] Initializing in a Declaration int ages[ 5 ] = { 40, 13, 20, 19, 36 } ; int list [ 5 ] = { 0 }; int array [ 6 ] = { 1} ; for ( int m = 0; m < 5; m++ ) { cout << ages[ m ] << “\t” ; }

More Related