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.

Slides:



Advertisements
Similar presentations
UNIT IV.
Advertisements

Copyright © 2002 Pearson Education, Inc. Slide 1.
Etter/Ingber Arrays and Matrices. Etter/Ingber One-Dimensional Arrays 4 An array is an indexed data structure 4 All variables stored in an array are of.
Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
1 Arrays Chapter 9. 2 Outline  The array structure (Section 9.1)  Array declaration  Array initialization  Array subscripts  Sequential access to.
Informática II Prof. Dr. Gustavo Patiño MJ
Dale/Weems/Headington
Introduction to arrays Data in economy size packages.
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
1 Arrays In many cases we need a group of nearly identical variables. Example: make one variable for the grade of each student in the class This results.
 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.
PHYS 2020 Making Choices; Arrays. Arrays  An array is very much like a matrix.  In the C language, an array is a collection of variables, all of the.
Chapter 9: Arrays and Strings
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Introduction of Arrays. Arrays Array form an important part of almost all programming language. It provides a powerful feature and can be used as such.
Arrays. Objectives Learn about arrays Explore how to declare and manipulate data into arrays Learn about “array index out of bounds” Become familiar with.
Arrays.
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.
Data Type. A data type defines a set of values that a variable can store along with a set of operations that can be performed on that variable. Common.
C Programming Lecture 14 Arrays. What is an Array? b An array is a sequence of data items that are: all of the same typeall of the same type –a sequence.
Pointers CS362. Pointers A Pointer is a variable that can hold a memory address Pointers can be used to: Indirectly reference existing variables (sometimes.
Arrays One-Dimensional initialize & display Arrays as Arguments Part I.
 For Loops › for (variable set; condition; incremental or decrement){ // loop beginning › } // loop end  While loops › while (condition) { // beginning.
CSE 2341 Honors Professor Mark Fontenot Southern Methodist University Note Set 04.
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.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
COMP102 Lab 081 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
CHAPTER: 12. Array is a collection of variables of the same data type that are referenced by a common name. An Array of 10 Elements of type double.
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.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Arrays Arrays in C++ An array is a data structure which allows a collective name to be given to a group of elements which all have.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
1 Topic: Array Topic: Array. 2 Arrays Arrays In this chapter, we will : Learn about arrays Learn about arrays Explore how to declare and manipulate data.
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 One Dimensional Arrays Chapter 11 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores :
1 CSC103: Introduction to Computer and Programming Lecture No 24.
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.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
Arrays.
COMPUTER PROGRAMMING. Array C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An.
Arrays An array is an indexed data structure which is used to store data elements of the same data type. An array is an indexed data structure which is.
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.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 7 – Pointers.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
C++ Array 1. C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used.
Arrays Chapter 12. One-Dimensional Arrays If you wanted to read in 1000 ints and print them in reverse order, it would take a program that’s over 3000.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Lecture #15 ARRAYS By Shahid Naseem (Lecturer). 2 ARRAYS DEFINITION An array is a sequence of objects of same data type. The objects in an array are also.
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
1 Programming in C++ Dale/Weems/Headington Chapter 11 One-Dimensional Arrays.
Introduction to programming in java Lecture 21 Arrays – Part 1.
1 Principles of Computer Science I Honors Section Note Set 3 CSE 1341.
Computer Skills2 / Scientific Colleges 1 Arrays Topics to cover: Arrays Data Types One-dimensional Arrays Two-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)
Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
Pointers A variable that holds an address value is called a pointer variable, or simply a pointer.  What is the data type of pointer variables? It’s not.
Arrays An array is a grouping of elements of the same type that share a common base name Can have any number of elements in the array Individual elements.
C++ Data Types Simple Structured Address Integral Floating
Lecture 18 Arrays and Pointer Arithmetic
Arrays Arrays A few types Structures of related data items
C Programming Pointers
Arrays An array is a grouping of elements of the same type that share a common base name Can have any number of elements in the array Individual elements.
C++ Array 1.
Introduction to Pointers
Presentation transcript:

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

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.

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.

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];

Numeric Arrays: intstocks [ 10 ] ; /* Integer numbers */ floatsales [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. */

Declare variables to store and total 3 blood pressures int bp1, bp2, bp3; // bp[3]; int total; cout << bp1 << bp2 << bp3; total = bp1 + bp2 + bp3; bp2bp1bp

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]

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.

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

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] ?

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] ? ? ? ? ?

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? } temps[0] temps[1] temps[2] temps[3] temps[4]

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]

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]

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” ; } ages[0] ages[1] ages[2] ages[3] ages[4]