Arrays. Related data items Collection of the same types of data. Static entity – Same size throughout program.

Slides:



Advertisements
Similar presentations
Chapter 10.
Advertisements

Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
 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.
Chapter 9: Arrays and Strings
Chapter 9: Arrays and Strings
Chapter 9: Arrays and Strings
C++ for Engineers and Scientists Third Edition
Chapter 8 Arrays and Strings
Chapter 7 Arrays C++ Programming, Namiq Sultan1 Namiq Sultan University of Duhok Department of Electrical and Computer Engineering Reference: Starting.
Multidimensional Arrays C++ also allows an array to have more than one dimension. For example, a two-dimensional array consists of a certain number of.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
Copyright © 2012 Pearson Education, Inc. Chapter 8 Two Dimensional Arrays.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 7 Single-Dimensional.
 2006 Pearson Education, Inc. All rights reserved Arrays.
A First Book of ANSI C Fourth Edition
CHAPTER 07 Arrays and Vectors (part I). OBJECTIVES 2 In this part you will learn:  To use the array data structure to represent a set of related data.
Chapter 8 Arrays and Strings
1 DATA STRUCTURES: LISTS. 2 LISTS ARE USED TO WORK WITH A GROUP OF VALUES IN AN ORGANIZED MANNER. A SERIES OF MEMORY LOCATIONS CAN BE DIRECTLY REFERENCED.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
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.
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.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays Outline Introduction Arrays Declaring Arrays Examples Using 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.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 6 Arrays.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
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.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter Array Basics.
CHAPTER 6 ARRAYS IN C++ 2 nd Semester King Saud University College of Applied studies and Community Service CSC 1101 By: Fatimah Alakeel Edited.
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.
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.
Review Pointer Pointer Variables Dynamic Memory Allocation Functions.
Arrays.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Opening Input/Output Files ifstream infile; ofstream outfile; char inFileName[40]; char outFileName[40]; coutinFileName;
Programming Fundamentals. Today’s Lecture Array Fundamentals Arrays as Class Member Data Arrays of Objects C-Strings The Standard C++ string Class.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
Arrays C provides the option to the user to combine similar data types into a single entity It followed contiguous memory allocation.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
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.
1 Principles of Computer Science I Honors Section Note Set 3 CSE 1341.
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.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
Objectives You should be able to describe: One-Dimensional Arrays
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
Computer Programming BCT 1113
Chapter 6 Arrays Lecturer: Mrs Rohani Hassan
CSC 113: Computer Programming (Theory = 03, Lab = 01)
Two Dimensional Arrays
Chapter 7 Single-Dimensional Arrays
Engineering Problem Solving with C++, Etter/Ingber
7 Arrays.
Arrays Kingdom of Saudi Arabia
EKT150 : Computer Programming
CSC138: Structured Programming
Chapter 7 Single-Dimensional Arrays and C-Strings
7 Arrays.
Arrays Arrays A few types Structures of related data items
Introducing Arrays Array is a data structure that represents a collection of the same types of data. From resourses of Y. Daniel Liang, “Introduction.
Presentation transcript:

Arrays

Related data items Collection of the same types of data. Static entity – Same size throughout program

Arrays Simple data type => data element contains a single value Array is a structured data-type (Collection of values): ‘A’

Array Fundamentals Arrays can hold a few data items or tens of thousands. The data items grouped in an array can be simple types such as int or float, or they can be user-defined types such as structures and objects.

Array Fundamentals Arrays are like structures in that they both group a number of items into a larger unit. A structure usually group items of different types but an array group items of the same type. More importantly, the items in a structure are accessed by name, while those in an array are accessed by an index number. Using an index number to specify an item allows easy access to a large number of items.

One Dimensional Array int WeeklyTemp[7]; WeeklyTemp 4 bytes cout<< WeeklyTemp[0]; cout<< WeeklyTemp[2]; cout<< WeeklyTemp[4];

Declaring Array Variables datatype arrayName[arraySize]; Example: double myList[10]; Array Size: MUST BE constant - constant literal - constant identifier int size = 4; double myList[size]; // Wrong const int size = 4; double myList[size]; // Correct double myList[20]; // Correct

Array Elements The items in an array are called elements (in contrast to the items in a structure, which are called members). All the elements in an array are of the same type; only the values vary.

Input/Output of Array elements int marks[3]; marks[0] = 76; marks[1] = 65; marks[2] = 27; cout<<marks[2]<<marks[0]<<marks[1];

Input/Output of Array elements – Using Loops int marks[5]; for(int i=0;i<5;i++) cin>>marks[i]; for(int j=0;j<5;j++) cout<<marks[j];

Indexed Variables Array elements are accessed through the index. Array indices are 0-based; that is, they start from 0 to arraySize-1. For example: int marks[5];  Five int values: marks[0], marks[1], marks[2], marks[3], marks[4]

Using Indexed Variables An indexed variable can be used in the same way as a regular variable. For Example: myList[2] = myList[0] + myList[1];

Example Code

OUTPUT

No Bound Checking C++ does not check array’s boundary. So, accessing array elements using subscripts (index variable) beyond the boundary does not cause syntax errors, But the operating system might report a memory access violation (Compiler or System may crash!)

Arbitrary Initial Values When an array is created, its elements are assigned with arbitrary values. int marks[5]; for(int i=0;i<5;i++) cout<<marks[i];

Initializing an Array dataType arrayName[Size] = {value0, value1,..., valuek}; Example: int myList[4] = {32, 11, -6, 65};

Declaring, creating, initializing Using the Shorthand Notation int myList[4] = {32, 11, -6, 65};  This shorthand notation is equivalent to the following statements: int myList[4]; myList[0] = 32; myList[1] = 11; myList[2] = -6; myList[3] = 65;

Initializing Wrong way int myList[4]; myList = {32, 11, -6, 65};

Implicit Size C++ allows you to omit the array size For example, the following declaration is fine/correct: int myList[ ] = {63, 9, 3, 13}; C++ automatically figures out how many elements are in the array. int myList [ ]; // WRONG

Partial Initialization C++ allows you to initialize a part of the array. double myList[4] = {1.9, 4.65}; //correct - The above example assigns values 1.9, 4.65 to the first two elements of the array. - The other two elements will be set to zero. - Note: If an array is declared, but not initialized, all its elements will contain “garbage”, like all other local variables.

Copying Arrays Can you copy array using a syntax like this? int list[3]; int myList[3]; list = myList; // WRONG This is not allowed in C++. You have to copy individual elements from one array to the other as follows: for (int i = 0; i < 3; i++) { list[i] = myList[i]; }

Arrays of Structures

C-Strings or Character Arrays The elements of an array can be just about anything (any-datatype) Consider an array whose elements are all characters (char type) – Called a C-String

Declaration of C-Strings Similar to declaration of any array: char name[30]; // no initialization char title[20] = “Hello World"; //initialized at declaration with a string char chList[6] = {‘H', ‘e', ‘l', ‘l‘, ’o’}; //initialized with list of char values

Initializing Character Arrays char city[ ] = "Dallas";

Printing Character Array For a character array, it can be printed using one print statement. Character arrays are handled differently than other types of arrays For example: char city[ ] = "Dallas"; cout << city; // Correct int marks [ ] = {20,65,30}; cout << marks; // Wrong

Character Array (string) Input Declare strings 1 element bigger than planned size to allow for ‘\0’ (null character) char city[10]; cin>> city; //User enters Islamabad When input takes place, C++ automatically places the ‘\0’ in memory at the end of the characters typed in

Example-2: Reversing an Array Write a program to create an array of 10 elements, assign each element value (1 to 50). Print the array values. Then, Reverse the values stored in array. Output the final array values.

Two Dimensional Arrays

A two dimensional array stores data as a logical collection of rows and columns Each element of a two-dimensional array has a row position and a column position (indicated by two indexes) To access an element in a two-dimensional array, you must specify the name of the array followed by: – a row index – a column index

Declaration and Initialization Declaration of a two-dimensional array requires a row size and a column size A consecutive block of (row size)*(column size) memory locations are allocated. All array elements must be of the same type. Elements accessed by two offsets: a row offset and a column offset.

Example //Declaration int data[2][3]; ??? ??? row 0 row 1 col 0col 1col 2

Declaring Arrays datatype arrayName[rowSize][coulmnSize]; Example: double myList[2][4]; rowSize, and coulmnSize: MUST BE constant - constant literal - constant identifier

Declaring and Initializing Arrays int myList[3][2] = {{22,33}, {44,55}, {66,77}};  myList has 3 Rows and 2 coulmns in each row  In memory: Coulmn Indexes Row Indexes

Initialization Examples int temp[4][3] = {{50, 70, 60}, {48, 75, 62}, {51, 69, 60}, {52, 78, 63}}; int temp[][3] = {{50, 70, 60}, {48, 75, 62}, {51, 69, 60}, {52, 78, 63}};

Example: Input Using cin Nested for loops are often used when inputting and assigning values to a two-dimensional array. //Declaration double table[10][10]; for (int i=0; i<10; ++i) //every row for (int j=0; j<10; ++j ) //every col cin >> table[i][j];

Example: Assignment //Declaration const int RSIZE=3; Const int CSIZE=2; double v[RSIZE][CSIZE]; for (int i=0; i<RSIZE; ++i)//every row for (int j=0; j<CSIZE; ++j )//every col v[i][j] = i+j; V

Example: Computations Compute the average value of an matrix with n rows and m columns. double sum=0; double average; for (int i=0; i<n; i++)//every row for (int j=0; j<m; j++ )//every col sum += array[i][j]; average = sum / (n*m);

Passing Arrays to Functions

Higher-Dimensional Arrays - An array can be declared with multiple dimensions. 2 Dimensional3 Dimensional - Multiple dimensions get difficult to visualize graphically. double Coord[100][100][100]; Single value 1D Array 2D Array 3D Array

Larger-Dimension Arrays Arrays with more than two dimensions allowed in C++ but not commonly used Example: int response[4][10][6]; – First element is response[0][0][0] – Last element is response[3][9][5]

Common Programming Errors Forgetting to declare an array – Results in a compiler error message Using a subscript that references a nonexistent array element – For example, declaring array to be of size 20 and using a subscript value of 25 – Not detected by C++ compilers and will probably cause a runtime error

(Nested Loops) – Example Program -Write a program to that creates a matrix of size 5 by 5 (5 Columns, and 5 Rows). The program should ask the user to enter values in each matrix element. Then the program should display the left-diagonal elements of the matrix. Example: Output 1,6,11,16,