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

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

Arrays in C++ Instructor: Andy Abreu

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

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

Array. Arrays. Declaring Arrays. Using Arrays

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

Chapter 01 Arrays Prepared By: Dr. Murad Magableh 2013

Arrays. int Data [8] [0] [1] [2] [3] [4] [5] [6] [7]

Before we start - Announcements: There will be a LAB TONIGHT from 5:30 6:30 in CAMP 172. In compensation, no class on Friday, Jan. 31.

STRUCTURED DATA TYPE ARRAYS IN C++ ONE-DIMENSIONAL ARRAY TWO-DIMENSIONAL ARRAY

C++ PROGRAMMING SKILLS Part 4: Arrays

Chapter 3 Objects, types, and values. Bjarne Stroustrup

Arrays. Week 4. Assylbek Jumagaliyev

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

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

Pointers Pointer Variables. Pointer Variables Getting the Address of a Variable. Each variable in program is stored at a

Consider the above code. This code compiles and runs, but has an error. Can you tell what the error is?

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

Arrays and functions Multidimensional arrays Sorting and algorithm efficiency

The University Of Michigan. EECS402 Lecture 05. Andrew M. Morgan. Savitch Ch. 5 Arrays Multi-Dimensional Arrays. Consider This Program

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

BITG 1113: Array (Part 1) LECTURE 8

EECS402 Lecture 02. Functions. Function Prototype

Chapter 3 Objects, Types, and Values

a data type is Types

APS105. Malloc and 2D Arrays. Textbook Chapters 6.4, Datatype Size

Declaring a 2D Array

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

Lecture 4. 1 Statements: 2 Getting Started with C++: LESSON FOUR

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

Control Structures in Java if-else and switch

Review. Outline. Array Pointer Object-Oriented Programming. Fall 2017 CISC2200 Yanjun Li 1. Fall 2017 CISC2200 Yanjun Li 2

CSci 1113 Midterm 2. Name: Student ID:

The University Of Michigan. EECS402 Lecture 02. Andrew M. Morgan. Savitch Ch. 3-4 Functions Value and Reference Parameters.

Computer Science & Engineering 150A Problem Solving Using Computers

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

Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays

Arrays in Functions!

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

Administrivia. Introduction to Computer Systems. Pointers, cont. Pointer example, again POINTERS. Project 2 posted, due October 6

Arrays. Returning arrays Pointers Dynamic arrays Smart pointers Vectors

What we will learn about this week: Declaring and referencing arrays. arrays as function arguments. Arrays

2-D Arrays. Of course, to set each grid location to 0, we have to use a loop structure as follows (assume i and j are already defined):

CHAPTER 3 ARRAYS. Dr. Shady Yehia Elmashad

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. C++ Kitchen Sink. Lecture 14.

CS31 Discussion 1E Spring 17 : week 08

CIS 190: C/C++ Programming. Classes in C++

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

Arrays. Arizona State University 1

Review. Outline. Array Pointer Object-Oriented Programming. Fall 2013 CISC2200 Yanjun Li 1. Fall 2013 CISC2200 Yanjun Li 2

Class 2: Variables and Memory. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

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

Control Structures in Java if-else and switch

CSCE 2004 Midterm Exam Spring 2017

FORM 2 (Please put your name and form # on the scantron!!!!)

Here, type declares the base type of the array, which is the type of each element in the array size defines how many elements the array will hold

For example, let s say we define an array of char of size six:

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

C: How to Program. Week /Apr/23

C++ Programming for Non-C Programmers. Supplement

LAB 4.1 Relational Operators and the if Statement

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

Sample Final Exam. 1) (24 points) Show what is printed by the following segments of code (assume all appropriate header files, etc.

Using Parallel Arrays. Parallel Array Example

Arrays. CS10001: Programming & Data Structures. Pallab Dasgupta Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur

C++ Programming for Non-C Programmers. Supplement

Chapter 2 C++ Fundamentals

Chapter 4 - Arrays. 4.1 Introduction. Arrays Structures of related data items Static entity (same size throughout program)

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

Add Subtract Multiply Divide

CSCE Practice Midterm. Data Types

CSCI 262 Data Structures. Arrays and Pointers. Arrays. Arrays and Pointers 2/6/2018 POINTER ARITHMETIC

pointers + memory double x; string a; int x; main overhead int y; main overhead

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

Computer Science II Lecture 1 Introduction and Background

Lecture 04 FUNCTIONS AND ARRAYS

Introduction to C++ 2. A Simple C++ Program. A C++ program consists of: a set of data & function definitions, and the main function (or driver)

Chapter 4 - Arrays. 4.1 Introduction. Arrays Structures of related data items Static entity (same size throughout program) A few types

Programación de Computadores. Cesar Julio Bustacara M. Departamento de Ingeniería de Sistemas Facultad de Ingeniería Pontificia Universidad Javeriana

COMP322 - Introduction to C++ Lecture 02 - Basics of C++

CSCE 110 PROGRAMMING FUNDAMENTALS. Prof. Amr Goneid AUC Part 7. 1-D & 2-D Arrays

C++ Arrays and Vectors

Objectivities. Experiment 1. Lab6 Array I. Description of the Problem. Problem-Solving Tips

C++ Programming. Pointers and Memory Management. M1 Math Michail Lampis

The American University in Cairo Computer Science & Engineering Department CSCE Dr. KHALIL Exam II Spring 2010

CSE101-Lec#17. Arrays. (Arrays and Functions) Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU. LPU CSE101 C Programming

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

Module 7 b. -Namespaces -Exceptions handling

Gabriel Hugh Elkaim Spring CMPE 013/L: C Programming. CMPE 013/L: C Programming

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

CSCI-1200 Data Structures Spring 2014 Lecture 5 Pointers, Arrays, Pointer Arithmetic

CHAPTER 4 FUNCTIONS. 4.1 Introduction

CSCE 2004 Final Exam Spring Version A

Exam 3 Chapters 7 & 9

Content. In this chapter, you will learn:

Introduction to Computer Science Midterm 3 Fall, Points

CSCI 104 Polymorphism. Mark Redekopp David Kempe

C:\Temp\Templates. Download This PDF From The Web Site

Transcription:

C++ Arrays C++ Spring 2000 Arrays 1

C++ Arrays An array is a consecutive group of memory locations. Each group is called an element of the array. The contents of each element are of the same type. Could be an array of int, double, char, We can refer to individual elements by giving the position number (index) of the element in the array. C++ Spring 2000 Arrays 2

Memory and Arrays Each int is 4 bytes int foo[6]; 4 bytes foo[0] foo[1] foo[5] C++ Spring 2000 Arrays 3

C++ Arrays start at 0!!!!!!! The first element is the 0 th element! If you declare an array of n elements, the last one is number n-1. If you try to access element number n it is an error! If only millenniums started at 0 C++ Spring 2000 Arrays 4

Array Subscripts The element numbers are called subscripts. foo[i] Array name subscript A subscript can be any integer expression: These are all valid subscripts: foo[17] foo[i+3] foo[a+b+c] C++ Spring 2000 Arrays 5

int main(void) { int facs[10]; Array Example for (int i=0;i<10;i++) facs[i] = factorial(i); } for (int i=0;i<10;i++) cout << "factorial(" << i << ") is " << facs[i] << endl; C++ Spring 2000 Arrays 6

Declaring An Array element_type array_name[number_of_elements]; element_type can be any C++ variable type. array_name can be any valid variable name. number_of_elements can be an expression. C++ Spring 2000 Arrays 7

Initialization You can initialize an array when you declare it (just like with variables): int foo[5] = { 1,8,3,6,12}; double d[2] = { 0.707, 0.707}; char s[] = { 'R', 'P', 'I' }; You don t need to specify a size when initializing, the compiler will count for you. C++ Spring 2000 Arrays 8

An array printing function Can pass an array as a parameter. You don't have to say how big it is! void print_array(int a[], int len) { for (int i=0;i<len;i++) cout << "[" << i << "] = " << a[i] << endl; } C++ Spring 2000 Arrays 9

What if we want to print doubles? The print_array function is declared to handle only ints. We can write another function that can be used to print doubles. We have to write another function (we can't use the same one). Not really true this is what templates can do for you! C++ Spring 2000 Arrays 10

print_array() for doubles void print_array(double a[], int len) { for (int i=0;i<len;i++) cout << "[" << i << "] = " << a[i] << endl; } C++ Spring 2000 Arrays 11

Which is it? We now have two functions with the same name: void print_array(double a[], int len); void print_array(int a[], int len); This is fine as long as the prototypes are different everything works. This is called "overloading", using the same name for two (or more) different functions. C++ Spring 2000 Arrays 12

Arrays of char are special C++ provides a special way to deal with arrays of characters: char string1[] = "RPI without PI is like meat without eat"; char arrays can be initialized with string literals. C++ Spring 2000 Arrays 13

Arrays of Arrays You can create an array of arrays: int a[2][2]; for (int i=0;i<2;i++) for (int j=0;j<2;j++) a[i][j] = i+j; C++ Spring 2000 Arrays 14

2-D Array: int A[3][4] Col 0 Col 1 Col 2 Col 3 Row 0 A[0][0] A[0][1] A[0][2] A[0][3] Row 1 A[1][0] A[1][1] A[1][2] A[1][3] Row 2 A[2][0] A[2][1] A[2][2] A[2][3] C++ Spring 2000 Arrays 15

2-D Memory Organization char A[4][3]; A is an array of size 4. Each element of A is an array of 3 chars { A[0] { A[1] { A[2] { A[3] A[0][0] A[0][1] A[0][2] A[1][0] A[1][1] A[1][2] A[2][0] A[2][1] A[2][2] A[3][0] A[3][1] A[3][2] C++ Spring 2000 Arrays 16

2-D Array Example const int NumStudents = 10; const int NumHW = 3; double grades[numstudents][numhw]; for (int i=0;i<numstudents;i++) { for (int j=0;j<numhw;j++) { cout << Enter HW << j << Grade for student number << i << endl; cin >> grades[i][j]; } } C++ Spring 2000 Arrays 17

You don t need to specify the size of the first dimension You must include all other sizes! 2-D Array (cont.) double student_average( double g[][numhw], int stu) { double sum = 0.0; for (int i=0;i<numhw;i++) sum += g[stu][i]; } return(sum/numhw); C++ Spring 2000 Arrays 18

Another way double array_average( double a[], int len) { double sum = 0.0; for (int i=0;i<len;i++) sum += a[i]; if (len==0) return(0); else return(sum/len); } Division by 0 is bad idea! C++ Spring 2000 Arrays 19

Two ways to do it // Using student_average with grades for (int i=0;i<numstudents;i++) cout << Student # << i << has average << student_average( grades, i ) << endl; -or- // Using array_average with grades for (int i=0;i<numstudents;i++) { cout << Student # << i << has average << array_average( grades[i], NumHW ) << endl; C++ Spring 2000 Arrays 20

Arrays and pass-by-reference Arrays are always passed by reference Inside a function any changes you make to array values are for keeps! You can write functions that modify the contents of an array. You need to make sure that a function knows how big the array is!!! C++ Spring 2000 Arrays 21

A Bad Idea int read_array( int a[] ) { int i=0; int val; do { cout << Enter next value, 0 to end\n ; cin >> val; if (val) a[i++] = val; } while (val); return(i); // returns the number or numbers } The problem is that the function might go beyond the size of the array. Result: Segmentation Violation (or worse!). C++ Spring 2000 Arrays 22

C++ does not have bounds checking int a[6]; int foo; Memory This is the array This is something else a[0] a[1] a[2] a[3] a[4] a[5] foo C++ Spring 2000 Arrays 23