Abstract Data Type (ADT) & ARRAYS ALGORITHMS & DATA STRUCTURES I COMP 221

Similar documents
Simple data organization, but horrible algorithms for the basic operations! const int MAX_SEATS = 10; // upper limit on the number of seats

Review of Important Topics in CS1600. Functions Arrays C-strings

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS242 ARRAYS

calling a function - function-name(argument list); y = square ( z ); include parentheses even if parameter list is empty!

Outline. Introduction. Arrays declarations and initialization. Const variables. Character arrays. Static arrays. Examples.

Chapter 7 Array. Array. C++, How to Program

Arrays array array length fixed array fixed length array fixed size array Array elements and subscripting

Chapter 8 Arrays and Strings. Objectives. Objectives (cont d.) Introduction. Arrays 12/23/2016. In this chapter, you will:

How to declare an array in C?

7.1. Chapter 7: Arrays Hold Multiple Values. Array - Memory Layout. A single variable can only hold one value. Declared using [] operator:

C++ Arrays. C++ Spring 2000 Arrays 1

C++ Programming Chapter 7 Pointers

An array is a collection of data that holds fixed number of values of same type. It is also known as a set. An array is a data type.

Chapter 7: Arrays Copyrig opy ht rig 2012 Pea e rson a Educa Educ ti a on, Inc I. nc

UEE1302 (1102) F10 Introduction to Computers and Programming (I)

C++ Programming. Arrays and Vectors. Chapter 6. Objectives. Chiou. This chapter introduces the important topic of data structures collections

Arrays. Lecture 9 COP 3014 Fall October 16, 2017

CSC 211 Intermediate Programming. Arrays & Pointers

The C++ Language. Arizona State University 1

Lesson 06 Arrays. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

CSC 1300 Exam 4 Comprehensive-ish and Structs

Chapter 2: Introduction to C++

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

Lecture 3 Tao Wang 1

7.1. Chapter 7: Arrays Hold Multiple Values. Array - Memory Layout. Array Terminology. Array Terminology 8/23/2014. Arrays Hold Multiple Values

Programming for Engineers Arrays

Arrays. Comp Sci 1570 Introduction to C++ Array basics. arrays. Arrays as parameters to functions. Sorting arrays. Random stuff

7.1. Chapter 7: Arrays Hold Multiple Values. Array - Memory Layout. Array Terminology. Array Terminology

3. Except for strings, double quotes, identifiers, and keywords, C++ ignores all white space.

Add Subtract Multiply Divide

Pointers. 1 Background. 1.1 Variables and Memory. 1.2 Motivating Pointers Massachusetts Institute of Technology

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char

UEE1302 (1102) F10 Introduction to Computers and Programming (I)

BITG 1233: Array (Part 1) LECTURE 8 (Sem 2, 17/18)

LECTURE 02 INTRODUCTION TO C++

C++ PROGRAMMING SKILLS Part 4: Arrays

A First Book of ANSI C Fourth Edition. Chapter 8 Arrays

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

! Pass by value: when an argument is passed to a. ! It is implemented using variable initialization. ! Changes to the parameter in the function body

Pointers and Arrays CS 201. This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book.

tablica: array: dane_liczbowe

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

Unit 3, Lesson 2 Data Types, Arithmetic,Variables, Input, Constants, & Library Functions. Mr. Dave Clausen La Cañada High School

Functions, Arrays & Structs

Multiple-Subscripted Arrays

Computer Programming

BITG 1113: Array (Part 1) LECTURE 8

by Pearson Education, Inc. All Rights Reserved.

Functions, Arrays & Structs

Unit 3. Constants and Expressions

Chapter 9. Pointers and Dynamic Arrays

Arrays. Week 4. Assylbek Jumagaliyev

7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

What have we learned about when we learned about function parameters? 1-1

c++ keywords: ( all lowercase ) Note: cin and cout are NOT keywords.

CSI33 Data Structures

CHAPTER 3 BASIC INSTRUCTION OF C++

UNIT-2 Introduction to C++

Review of the C Programming Language for Principles of Operating Systems

TOPICS TO COVER:-- Array declaration and use.

[0569] p 0318 garbage

CS2141 Software Development using C/C++ C++ Basics

More non-primitive types Lesson 06

In this chapter, you will learn about: Pointers. Dynamic Arrays. Introduction Computer Science 1 CS 23021

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Programming for Electrical and Computer Engineers. Pointers and Arrays

Introduction to Programming using C++

KOM3191 Object Oriented Programming Dr Muharrem Mercimek ARRAYS ~ VECTORS. KOM3191 Object-Oriented Computer Programming

CSCI 123 Introduction to Programming Concepts in C++

Computer Programming : C++

Arrays and Pointers. Overview. Arrays Introducing Pointers C-Style Character Strings Multidimensioned Arrays

CS2255 HOMEWORK #1 Fall 2012

Programming. C++ Basics

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

a data type is Types

PIC 10A Pointers, Arrays, and Dynamic Memory Allocation. Ernest Ryu UCLA Mathematics

Non-numeric types, boolean types, arithmetic. operators. Comp Sci 1570 Introduction to C++ Non-numeric types. const. Reserved words.

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Data types. CISC 1600/1610 Computer Science I. Array syntax. Memory allocation. Zero-indexing 4/4/2016. Arrays

Chapter 9 Introduction to Arrays. Fundamentals of Java

Structured Types. 8. Arrays. collection of components whose organization is characterized by method used to access individual components.

C++ PROGRAMMING. For Industrial And Electrical Engineering Instructor: Ruba A. Salamh

Homework #3 CS2255 Fall 2012

Pointers and Strings Prentice Hall, Inc. All rights reserved.

Arithmetic Operators. Binary Arithmetic Operators. Arithmetic Operators. A Closer Look at the / Operator. A Closer Look at the % Operator

Pointers, Arrays and C-Strings

CSCI 6610: Intermediate Programming / C Chapter 12 Strings

CS31 Discussion. Jie(Jay) Wang Week8 Nov.18

Objectives. Chapter 8 Arrays and Strings. Objectives (cont d.) Introduction 12/14/2014. In this chapter, you will:

Chapter 12: Pointers and Arrays. Chapter 12. Pointers and Arrays. Copyright 2008 W. W. Norton & Company. All rights reserved.

UNIT- 3 Introduction to C++

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7.

EDIABAS BEST/2 LANGUAGE DESCRIPTION. VERSION 6b. Electronic Diagnostic Basic System EDIABAS - BEST/2 LANGUAGE DESCRIPTION

while for do while ! set a counter variable to 0 ! increment it inside the loop (each iteration)

A First Program - Greeting.cpp

Pointers. Memory. void foo() { }//return

Transcription:

Abstract Data Type (ADT) & ARRAYS ALGORITHMS & DATA STRUCTURES I COMP 221

Abstract Data Type (ADT) Def. a collection of related data items together with an associated set of operations e.g. whole numbers (integers) and arithmetic operators for addition, subtraction, multiplication and division. e.g. Seats for TFA Basic operations: find empty seat, reserve a seat, cancel a seat assignment Why "abstract?" Data, operations, and relations are studied independent of implementation. What not how is the focus.

ADT You drive the car without know how the engine car work and daily you use many items without know how they work, separating the implementation details is called abstraction. Abstraction focuses on what the engine does and not how it works. Abstract Data type(adt): A data type that specifies the logical properties without the implementation details.

Implementation of an ADT Def. Consists of storage structures (aka data structures) to store the data items and algorithms for the basic operations. The storage structures/data structures used in implementations are provided in a language (primitive or built-in) or are built from the language constructs (user-defined). In either case, successful software design uses data abstraction: Separating the definition of a data type from its implementation.

C-Style Data Structures: Arrays Defn of an array as an ADT: An ordered set (sequence) with a fixed number of elements, all of the same type, where the basic operation is direct access to each element in the array so values can be retrieved from or stored in this element. Properties: Ordered so there is a first element, a second one, etc. Fixed number of elements fixed capacity Elements must be the same type (and size); use arrays only for homogeneous data sets. Direct access: Access an element by giving its location the time to access each element is the same for all elements, regardless of position. in contrast to sequential access (where to access an element, one must first access all those that precede it.)

Declaring arrays in C++ element_type array_name[capacity]; where element_type is any type array_name is the name of the array any valid identifier CAPACITY (a positive integer constant) is the number of elements in the array The elements (or positions) of the array are indexed 0, 1, 2,..., CAPACITY - 1. e.g., double score[100]; Better to use a named constant to specify the array capacity: const int CAPACITY = 100; double score[capacity]; Can use typedef with array declarations; e.g., const int CAPACITY = 100; typedef double ScoresArray[CAPACITY]; ScoresArray score; Can't input the capacity The compiler reserves a block of consecutive memory locations, enough to hold CAPACITY values of type element_type. score[0] score[1] score[2] score[3]. score[99].

How well does C/C++ implement an array ADT? As an ADT ordered fixed size same type elements direct access In C++ indices numbered 0, 1, 2,..., CAPACITY - 1 CAPACITY specifies the capacity of the array element_type is the type of elements subscript operator []

Subscript operator [] is an actual operator and not simply a notation/punctuation as in some other languages. Its two operands are an array variable and an integer index (or subscript) and is written array_name[i] Here i is an integer expression with 0 < i < CAPACITY 1. [] returns the address of the element in location i in array_name; so array_name[i]is a variable, called an indexed (or subscripted) variable, whose type is the specified element_type of the array. Also, it can have an index This means that it can be used on the left side of an assignment, in input statements, etc. to store a value in a specified location in the array. For example: // Zero out all the elements of score for (int i = 0; i < CAPACITY; i++) score[i] = 0.0; // Read values into the first numscores elements of score for (int i = 0; i < numscores; i++) cin >> score[i]; // Display values stored in the first numscores elements for (int i = 0; i < numscores; i++) cout << score[i] << endl;

Array Initialization In C++, arrays can be initialized when they are declared. an array literal Numeric arrays: element_type num_array[capacity] = {list_of_initial_values}; Example: double rate[5] = {0.11, 0.13, 0.16, 0.18, 0.21}; 0 1 2 3 4 rate 0.11 0.13 0.16 0.18 0.21 Note 1: If fewer values supplied than array's capacity, remaining elements assigned 0. double rate[5] = {0.11, 0.13, 0.16}; rate 0 1 2 3 4 0.11 0.13 0.16 0 0 What s an easy way to initialize an array to all zeros? Note 2: It is an error if more values are supplied than the declared size of the array. How this error is handled, however, will vary from one compiler to another. Note 3: If no values supplied, array elements are undefined (i.e., garbage values).

Character arrays: Character arrays may be initialized in the same manner as numeric arrays. char vowel[5] = {'A', 'E', 'I', 'O', 'U'}; declares vowel to be an array of 5 characters and initializes it as follows: vowel 0 1 2 3 4 A E I O U Note 1: If fewer values are supplied than the declared size of the array, the zeroes used to fill uninitialized elements are interpreted as the null character '\0' whose ASCII code is 0. const int NAME_LENGTH = 10; char collegename[name_length]={'c', 'a', 'l', 'v', 'i', 'n'}; collegename 0 1 2 3 4 5 6 7 8 9 C a l v i n \0 \0 \0 \0

Note 2: Character arrays may be initialized using string constants. For the following declaration is equivalent to the preceding: char collegename[name_length] = "Calvin"; example, Note 3: The null character '\0' (ASCII code is 0) is used as an end-of-string mark. Thus, character arrays used to store strings should be declared large enough to store the null character. If it is not, one cannot expect some of the string functions and operations to work correctly. If a character array is initialized with a string constant, the end-of-string mark is added automatically, provided there is room for it. char collegename[7] = {'C', 'a', 'l', 'v', 'i', 'n', '\0'}; char collegename[7] = "Calvin";

Initializations with no array size specified The array capacity may be omitted in an array declaration with an initializer list. In this case, the number of elements in the array will be the number of values in the initializer list. Example: double rate[] = {0.11, 0.13, 0.16}; 0 1 2 rate 0.11 0.13 0.16 Note: This explains the brackets in constant declarations such as const char IN_FILE[] = "employee.dat";

Addresses When an array is declared, the address of the first byte (or word) in the block of memory associated with the array is called the base address of the array. Each array reference must be translated into an offset from this base address. For example, if each element of array score will be stored in 8 bytes and the base address of score is 0x1396. A statement such as cout << score[3] << endl; requires that array reference score[3] be translated into a memory address: score[3] 0x1396 + 3 * (sizeof double) = 0x1396 + 3 * 8 = 0x13ae The contents of the memory word with this address 0x13ae can then be retrieved and displayed. An address translation like this is carried out each time an array element is accessed. score 0x1396 0x13ae. [0] [1] [2] [3]. [99]

The value of array_name is actually the base address of array_name array_name + index is the address of array_name[index]. An array reference is equivalent to array_name[index] *(array_name + index) * is the dereferencing operator *ref returns the contents of the memory location with address ref For example, the following statements are equivalent: cout << score[3] << endl; cout << *(score + 3) << endl;

C-Style Multidimensional Arrays Example: A table of test scores for several different students on several different tests. p.52 Test 1 Test 2 Test 3 Test 4 Student 1 99.0 93.5 89.0 91.0 Student 2 66.0 68.0 84.5 82.0 Student 3 88.5 78.5 70.0 65.0 : : : : : : : : : : Student-n 100.0 99.5 100.0 99.0 For storage and processing, use a two-dimensional array.

Declaring Two-Dimensional Arrays Standard form of declaration: element_type array_name[num_rows][num_columns]; Example: const int NUM_ROWS = 30, NUM_COLUMNS = 4; double scorestable[num_rows][num_columns]; or typedef double TwoDimArray [NUM_ROWS][NUM_COLUMNS]; TwoDimArray scorestable; Initialization List the initial values in braces, row by row; May use internal braces for each row to improve readability. Example: double rates[2][3] = {{0.50, 0.55, 0.53}, // first row {0.63, 0.58, 0.55}}; // second row [0] [1] [2] [3] [29] [0] [[1] [2] [3].

Processing Two-Dimensional Arrays Remember: Rows (and) columns are numbered from zero!! Use doubly-indexed variables: scorestable[2][3] is the entry in row 2 and column 3 row index column index Use nested loops to vary the two indices, most often in a rowwise manner. int numstudents, numtests, i, j; cout << "# students and # of tests? "; cin >> numstudents >> numtests; cout << "Enter test scores for students\n"; for (i = 0; i < numstudents; i++) { cout << '#' << i + 1 << ':'; for (j = 0; j < numtests; j++) cin >> scorestable[i][j]; } Counting from 0

THANK YOU