Array Data Structure Chapter 6

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Chapter 9 – One-Dimensional Numeric Arrays. Array u Data structure u Grouping of like-type data u Indicated with brackets containing positive integer.
1 Arrays Chapter 9. 2 Outline  The array structure (Section 9.1)  Array declaration  Array initialization  Array subscripts  Sequential access to.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 1 Overview 7.1 Introduction to Arrays 7.2 Arrays in Functions 7.3.
Engineering Problem Solving With C++ An Object Based Approach Chapter 6 One-Dimensional 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.
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.
Multiple-Subscripted Array
Chapter 9: Arrays and Strings
Chapter 9: Arrays and Strings
Chapter 8 Arrays and Strings
Arrays (Part II). Two- and Multidimensional Arrays Two-dimensional array: collection of a fixed number of components (of the same type) arranged in two.
Copyright © 2012 Pearson Education, Inc. Chapter 8 Two Dimensional Arrays.
Chapter 8 Arrays and Strings
Chapter 6 One-Dimensional Arrays ELEC 206 Computer Tools for Electrical Engineering.
Computer Programming 12 Mr. Jean April 24, The plan: Video clip of the day Upcoming Quiz Sample arrays Using arrays More about arrays.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
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.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
1 Chapter 7 Arrays. 2 Topics 7.1 Arrays Hold Multiple Values 7.2 Accessing Array Elements 7.3 No Bounds Checking in C Array Initialization 7.5 Processing.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter Chapter 6 One-Dimensional Arrays.
 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.
Arrays.
Arrays Chapter 7. Arrays Hold Multiple Values Array: variable that can store multiple values of the same type Values are stored in adjacent memory locations.
Module 1: Array ITEI222 - Advance Programming Language.
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;
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.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
An Introduction to Programming with C++ Sixth Edition
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.
Chapter 8: Arrays Starting Out with C++ Early Objects Ninth Edition
Computer Programming BCT 1113
© 2016 Pearson Education, Ltd. All rights reserved.
Arrays Outline 1 Introduction 2 Arrays 3 Declaring Arrays
Programming fundamentals 2 Chapter 1:Array
New Structure Recall “average.cpp” program
Student Book An Introduction
Two Dimensional Arrays
Engineering Problem Solving with C++, Etter/Ingber
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
7 Arrays.
Array Data Structure B.Ramamurthy 11/21/2018 B.Ramamurthy.
Chapter 9 - Arrays Outline 6.1 Introduction 6.2 Arrays
Arrays Kingdom of Saudi Arabia
Data type List Definition:
EKT150 : Computer Programming
4.9 Multiple-Subscripted Arrays
Multidimensional Arrays
MSIS 655 Advanced Business Applications Programming
Arrays Topics to cover: Arrays Data Types One-dimensional Arrays
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.
Engineering Problem Solving with C++, Etter
7 Arrays.
CS150 Introduction to Computer Science 1
Array Data Structure Chapter 6
CS150 Introduction to Computer Science 1
C++ winter2008(by:J.Razjouyan)
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.
Data Structure(s) A way of storing and organizing data in a computer so that it can be used efficiently. e.g. Arrays Linked Lists stacks Queues Trees.
Dr. Khizar Hayat Associate Prof. of Computer Science
4.1 Introduction Arrays A few types Structures of related data items
Presentation transcript:

Array Data Structure Chapter 6 B.Ramamurthy 11/19/2018 B.Ramamurthy

Introduction We have using variables that are associated with single objects. In this discussion we will look at need for representing collection of data and C++ support for the data arrays. 11/19/2018 B.Ramamurthy

Need for Data structures Represent a collection of similar data objects. Example: List of 157 client account numbers Would you represent it as g1, g2, g3,... g157 ? Perform a set of operations on each element of collection of data. Example: Input 157 account numbers, compute and output the complete bill for each account. Are you going to do this 157 different times? cin>> g1; s1 = BillingFunc (g1); cout <<s1; We need efficient representation and manipulation of matrix-like and table-like structures. 11/19/2018 B.Ramamurthy

Arrays An array is a collection of data which shares a common identifier and data type. Individual elements of the array are specified using offsets referred to as subscripts or indices. In C++ the offsets or subscripts always start with 0. 11/19/2018 B.Ramamurthy

Array data structure Array represents a collection of data objects of the same type. Syntax: Element_type Array_name [Number_of_elements] The identifier Array_name describes a collection of array elements, each of which may be used to store data values of type Element_type. The number of elements in the collection is given by Number_of_elements enclosed in brackets []. Each element in the collection is referred by their common name the Array_name and by a unique index. The index range starts from 0 and extends to Number_of_elements -1. 11/19/2018 B.Ramamurthy

Defining Arrays General form data_type array_identifier[size]; The size of the array may be specified by a defined constant or constant literal. Example: double m[8]; m[0] m[1] m[2] m[3] m[4] m[5] m[6] m[7] 11/19/2018 B.Ramamurthy

Array Declaration: Examples a) float X[8]; b) const int buf_size = 256; char buffer[buf_size]; c) int roll_value; float payroll[roll_value]; invalid : because roll_value is a variable. 11/19/2018 B.Ramamurthy

Initializing Arrays Some arrays need initialization at the time of declaration itself: Example constant arrays representing statistical tables. int n[5] = {32, 27, 64, 18, 95}; int crt[] = {6, 7, 8}; int B[10] = {0}; How about int A[4] = {3,4}; ? A[0] = 3, A[1] = 4, A[2] = 0, A[3] = 0 11/19/2018 B.Ramamurthy

Initializing Arrays Initializing the array when declared char vowels[5] = {'a', 'e', 'i', 'o', 'u'}; bool ansKey[] ={true, true, false, true, false, false}; char word[] = "Hello"; vowels 'a' 'e' 'i' 'o' 'u' ansKey true true false true false false word 'H' 'e' 'l' 'l' 'o' '\0' 11/19/2018 B.Ramamurthy

Accessing Array Elements Subscripts are used to access individual elements of an array. General format: array_identifier[offset] Example for (int I=0; I<8; I++) m[I] = double(I) + 0.5; Subscripts may be any integer expression. 11/19/2018 B.Ramamurthy

Array Reference How to reference an element of an array ? Array_name[subscript] where subscript can be constant, variable or expression. Examples: cout << X[3]; cin>> X[5]; X[6] = 78; X[i] = X[j] + X[k]; X[i] = X[j - 2] + 7; if (X[j*2] >= X[i*2/4]) ... 11/19/2018 B.Ramamurthy

A Simple Array Example Problem 1: Write a program to input 10 integers, sum them up and output the sum. int main() { int Sample[10]; int Sum = 0; int i; cout <<“please input 10 integers\n”; for (i = 0; i < 10; i++) { cin >> Sample[i]; Sum = Sum + Sample[i];} cout<<“the sum = “<<Sum <<endl; } 11/19/2018 B.Ramamurthy

Arrays as parameters Array by default is passed by reference in C++. The reserved word “const” is placed in front of the prototype and formal declaration in case the array values should remain unchanged during the execution of the function called. 11/19/2018 B.Ramamurthy

Passing Array as Parameter Problem : Input two arrays of numbers of 10 elements each, determine their sum in another array, and output the sum array. Design: Lets discuss the design in terms of function prototypes. void InputAray(int [] X); // Inputs values into array X void ArraySum(const int [] A, const int [] B, int [] C); // Computes C = A+B void OutputSum(int [] Y); // Prints out the values in array Y 11/19/2018 B.Ramamurthy

Multi-dimensional Arrays To represent tables of data and for modeling activities such as fluid flow and heat flow we need more then one dimensional arrays. Multiple-subscripted arrays are used represent multidimensional arrays. Example: int b[2][2]; int b[2][2] = {{1,2}, {3,4}}; int b[2][2] = {{1}, {3,4}}; 11/19/2018 B.Ramamurthy

…Arrays The elements are stored in row-major order. When specifying the multi-subscripted arrays as parameters, number of elements in every dimension other than the first has to be specified. 11/19/2018 B.Ramamurthy

Example (contd.) void PrintArray( int[10][20] a) { for (int i = 0; i <= 10; i++) for (int j = 0; j <= 20; j++) cout << a[i][j] << ‘ ‘; cout << endl; } 11/19/2018 B.Ramamurthy

Functions and arrays An array identifier references the first element of the array. When arrays are passed as arguments to functions, the address of the array is passed, thus by default, arrays are always passed by reference. This means that any changes made to the array is permanent unless you specify it as a const parameter. Generally we specify additional parameters with the array to provided information regarding the number of elements in the array. 11/19/2018 B.Ramamurthy