C++ Array 1.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Arrays.
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
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.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
 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.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
C++ for Engineers and Scientists Third Edition
Chapter 8 Arrays and Strings
Arrays. Objectives Learn about arrays Explore how to declare and manipulate data into arrays Learn about “array index out of bounds” Become familiar with.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
CS1061 C Programmuing Lecture 12 Arrays A. O’Riordan, 2004.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
Chapter 8: Arrays and Functions Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
UNIT-4 1. Arrays: Definition and declaration, Initialization, Accessing elements of arrays, Storing values in arrays, Inter-function Communication: Passing.
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.
Multidimensional Arrays tMyn1 Multidimensional Arrays It is possible to declare arrays that require two or more separate index values to access an element.
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.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
Arrays. C++ Style Data Structures: Arrays(1) An ordered set (sequence) with a fixed number of elements, all of the same type, where the basic operation.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
Arrays Chapter 7.
Arrays Chapter 7.
EGR 2261 Unit 10 Two-dimensional Arrays
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.
Computer Programming BCT 1113
© 2016 Pearson Education, Ltd. All rights reserved.
Array, Strings and Vectors
C Scope Rules and Arrays
EKT120 : Computer Programming
Lecture 7 – Arrays (1) PGT 106 : C PROGRAMMING.
JavaScript: Functions.
Arrays, For loop While loop Do while loop
Java How to Program, Late Objects Version, 10/e
C Arrays Systems Programming.
Engineering Problem Solving with C++, Etter/Ingber
Multiple Dimension Arrays
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
Array Data Structure Chapter 6
Arrays Skill Area 315 Part A
7 Arrays.
Array Data Structure B.Ramamurthy 11/21/2018 B.Ramamurthy.
Arrays Kingdom of Saudi Arabia
EKT150 : Computer Programming
Declaration, assignment & accessing
Introduction To Programming Information Technology , 1’st Semester
JavaScript Arrays.
EKT120: Computer Programming
Multidimensional Arrays
MSIS 655 Advanced Business Applications Programming
Arrays Chapter 7.
Multidimensional array
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.
7 Arrays.
Abstract Data Types, Elementary Data Structures and Arrays
Array Data Structure Chapter 6
Arrays Arrays A few types Structures of related data items
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.
Programming Fundamental
Arrays.
Presentation transcript:

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 to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type. Instead of declaring individual variables, such as number0, number1, ..., and number99, you declare one array variable such as numbers and use numbers[0], numbers[1], and ..., numbers[99] to represent individual variables. A specific element in an array is accessed by an index. 2

Syntax declaring Arrays: type arrayName [arraySize]; This is called a single-dimension array. The arraySize must be an integer constant greater than zero and type can be any valid C++ data type. For example, to declare a 10-element array called balance of type double, use this statement: Ex: double balance[10]; Initializing Arrays: double balance[5]={100.0, 2.0, 3.0, 17.0, 50.0} 3

The number of values between braces { } can not be larger than the number of elements that we declare for the array between square brackets [ ]. Following is an example to assign a single element of the array: If you omit the size of the array, an array just big enough to hold the initialization is created. Therefore, if you write: double balance[]={100.0, 2.0, 3.0, 17.0, 50.0} You will create exactly the same array as you did in the previous example. balance[4]=50.0 4

The above statement assigns element number 5th in the array a value of 50.0. Array with 4th index will be 5th, i.e., last element because all arrays have 0 as the index of their first element which is also called base index. Following is the pictorial representation of the same array we discussed above: 5

Accessing Array Elements: An element is accessed by indexing the array name. This is done by placing the index of the element within square brackets after the name of the array. For example: double salary = balance[9]; The above statement will take 10th element from the array and assign the value to salary variable. 6

This program makes use of setw() function to format the output. 7

C++ Multi-dimensional Arrays 8

type name [size1] [size2] ………… [sizeN]; C++ allows multidimensional arrays. Here is the general form of a multidimensional array declaration: Syntax: type name [size1] [size2] ………… [sizeN]; For example, the following declaration creates a three dimensional 5 . 10 . 4 integer array: int threedim[5] [10] [4]; 9

Two-Dimensional Arrays: The simplest form of the multidimensional array is the two- dimensional array. A two-dimensional array is, in essence, a list of one-dimensional arrays. To declare a two-dimensional integer array of size x,y, you would write something as follows: type arrayName [x] [y] ; Where type can be any valid C++ data type and arrayName will be a valid C++ identifier. A two-dimensional array can be think as a table, which will have x number of rows and y number of columns. A 2- dimensional array a, which contains three rows and four columns can be shown as below: 10

Thus, every element in array a is identified by an element name of the form a[ i ][ j ], where a is the name of the array, and i and j are the subscripts that uniquely identify each element in a. 11

Initializing Two-Dimensional Arrays: Multidimensioned arrays may be initialized by specifying bracketed values for each row. Following is an array with 3 rows and each row have 4 columns. int a[3] [4]={ {0, 1, 2, 3}, /*initializes for row indexed by o /* {4, 5, 6, 7}, /*initializes for row indexed by 1 /* {8, 9, 10, 11} /*initializes for row indexed by 2 /* } 12

Accessing Two-Dimensional Array Elements: The nested braces, which indicate the intended row, are optional. The following initialization is equivalent to previous example: int a [3] [4] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}; Accessing Two-Dimensional Array Elements: An element in 2-dimensional array is accessed by using the subscripts, i.e., row index and column index of the array. For example: int val = a [2] [3] ; The above statement will take 4th element from the 3rd row of the array. You can verify it in the above digram. 13

As explained above, you can have arrays with any number of dimensions, although it is likely that most of the arrays you create will be of one or two dimensions. 14

Arrays as parametters 15

At some point, we may need to pass an array to a function as a parameter. In C++, it is not possible to pass the entire block of memory represented by an array to a function directly as an argument. But what can be passed instead is its address. In practice, this has almost the same effect, and it is a much faster and more efficient operation. To accept an array as parameter for a function, the parameters can be declared as the array type, but with empty brackets, omitting the actual size of the array. 16

void procedure ( int arg[] ) For example: void procedure ( int arg[] ) This function accepts a parameter of type “array of int” called arg. In order to pass to this function an array declared as: int myarray [40] ; It would be enough to write a call like this: procedure (myarray) ; 17

18