Arrays.

Slides:



Advertisements
Similar presentations
UNIT IV.
Advertisements

Arrays.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 13 – Salary Survey Application: Introducing.
1 Arrays Chapter 9. 2 Outline  The array structure (Section 9.1)  Array declaration  Array initialization  Array subscripts  Sequential access to.
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
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 Lecture 20:Arrays and Strings Introduction to Computer Science Spring 2006.
 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 9: Arrays and Strings
Chapter 9: Arrays and Strings
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.
Arrays (Part II). Two- and Multidimensional Arrays Two-dimensional array: collection of a fixed number of components (of the same type) arranged in two.
CPS120: Introduction to Computer Science Arrays. Arrays: A Definition A list of variables accessed using a single identifier May be of any data type Can.
Program structure Four different storage-class specifications: –Automatic (auto): local to a function, normally declared variables are automatic. Does.
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.
Chapter 8 Arrays and Strings
Chapter 7 One-Dimensional Arrays 7.1 Arrays in C One of the more useful features of C is the ability to create arrays for storing a collection of related.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
 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.
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.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
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.
CPS120: Introduction to Computer Science Lecture 15 Arrays.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
Arrays. Related data items Collection of the same types of data. Static entity – Same size throughout program.
1 One Dimensional Arrays Chapter 11 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores :
1 Arrays and Strings Lecture: Design Problem l Consider a program to calculate class average Why?? ?
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.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
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.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Review Pointer Pointer Variables Dynamic Memory Allocation Functions.
COMPUTER PROGRAMMING. Array C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Module 1: Array ITEI222 - Advance Programming Language.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
Opening Input/Output Files ifstream infile; ofstream outfile; char inFileName[40]; char outFileName[40]; coutinFileName;
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 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.
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 Multidimensional Arrays Chapter 13 2 The plural of mongoose starts with a "p" Initializing a multidimensional array Processing by.
Arrays float Scores[9]; ? index: element // one dimensional array 2.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
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
Arrays Arrays exist in almost every computer language.
DATA STRUCTURE : DAT JENIS DATA DAN JENIS DATA ABSTRAK (4JAM)
Array Data Structure Chapter 6
Array Data Structure B.Ramamurthy 11/21/2018 B.Ramamurthy.
Arrays Kingdom of Saudi Arabia
EKT150 : Computer Programming
Introduction To Programming Information Technology , 1’st Semester
Arrays Chapter 8 Copyright © 2008 W. W. Norton & Company.
MSIS 655 Advanced Business Applications Programming
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.
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.
C++ Array 1.
Presentation transcript:

Arrays

What is Array An Array is a structured collection of components, all of same type, that is given a single name. Each component (array element) is accessed by an index that indicates the component’s position within the collection.

Defining Array Like other variables in C++, an array must be defined before it can be used to store information. Like other definitions, an array definition specifies a variable type and a name. But it includes another feature i.e. size. DataType ArrayName [Const Int Expression ];

Array Elements The items in an array are called array elements. All elements in an array are of the same type; only the values vary. Example int array1[4] = { 10, 5, 678, -400 } ;

Accessing Array Elements To Access an individual array component, we write the array name, followed by an expression enclosed in square brackets. The expression specifies which component to access. Syntax ArrayName [ IndexExpression] Example array1[2] array1[i] where i = 3

Initializing array in Declarations To initialize an array, you have to specify a list of initial values for the array elements, separate them with commas and enclose the list within braces. int array1[5] = {23, 10, 16, 37, 12}; We don’t need to use the array size when we initialize all the array elements, since the compiler can figure it out by counting the initializing variables. int array1[ ] = { 23, 10, 16, 37}; What happens if you do use an explicit array size, but it doesn’t agree with the number of components ? If there are too few components/ items , the missing element will be set to zero. If there are two many, an error is signaled.

Lack of Aggregate Array Operations C++ does not allow aggregate operations on arrays. int x[50], y[50] ; There is no aggregate assignment of y to x x = y; //not valid To copy array y into array x, you must do it yourself, element by element. for ( i=0; i<50; i++) x[i] = y[i]; //valid operation Similarly, there is no aggregate comparison of arrays. if (x == y ) //Not valid

Lack of Aggregate Array Operations Also, you cannot perform aggregate input / output operations on arrays. cin>>x; //not valid, where x is an array cout<<x //not valid You cannot perform aggregate arithmetic operations on arrays x = x + y // not valid, where x and y are arrays Finally, it is not possible to return an entire array as the value of a value-returning function return x; //not valid, where x is an array.

Example of One Dimensional Array void main() { double sales [6], average, total=0; cout<< “Enter sales of 6 days”; for( int j=0; j<6; j++) cin >> sales[ i ]; for (int j=0; j<6; j++) total += sales[ j ] ; average = total / 6; cout<< “Average =”<< average; }

Multidimensional Arrays A two dimensional array is used to represent items in a table with rows and columns, provided each item in the table is of same data type. Each component is accessed by a pair of indexes that represent the component’s position in each dimension.

Defining Multidimensional Array Two Dimensional Array The array is defined with two size specifiers, each enclosed in brackets DataType ArrayName[ConstIntExp][ConstIntExp] Example double array2[3][4]; Three Dimensional Array float array3[x][y][z]

Accessing Multidimensional Array Elements Array elements in two dimensional arrays required two indexes array2[1][2] Notice that each index has its own set of brackets. Don’t write commas. array2[1,2] // not valid syntax

Example Two Dimensional Array void main() { float array2[3][3]= {{ 12.2, 11.0, 9.6 }, { 23.9, -50.6, 2.3 }, { 2.2, 3.3, 4.4 } }; for (int row=0; row<3; row++) for (int col=0; col<3; col++) cout<< array2[row][col]; }