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

Similar documents
9. Arrays. Compound Data Types: type name [elements]; int billy [5];

11. Arrays. For example, an array containing 5 integer values of type int called foo could be represented as:

C++ Lecture 5 Arrays. CSci 588: Data Structures, Algorithms and Software Design.

Computer Programming: C++

Thus, to declare billy as shown above it would be something as simple as the following sentence:

Array Basics: Outline. Creating and Accessing Arrays. Creating and Accessing Arrays. Arrays (Savitch, Chapter 7)

Constructors for classes

Lecture (07) Arrays. By: Dr. Ahmed ElShafee. Dr. Ahmed ElShafee, ACU : Fall 2015, Programming I

Objectives. Order (sort) the elements of an array Search an array for a particular item Define, use multidimensional array

COMP 110/L Lecture 13. Kyle Dewey

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

Array Elements as Function Parameters

switch case Logic Syntax Basics Functionality Rules Nested switch switch case Comp Sci 1570 Introduction to C++

High Institute of Computer Science & Information Technology Term : 1 st. El-Shorouk Academy Acad. Year : 2013 / Year : 2 nd

Functions and Recursion

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #43. Multidimensional Arrays

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

Computer Science & Engineering 150A Problem Solving Using Computers

Object oriented programming C++

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

Character Functions & Manipulators Arrays in C++ CS 16: Solving Problems with Computers I Lecture #10

Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays

Arrays. Defining arrays, declaration and initialization of arrays. Designed by Parul Khurana, LIECA.

Functions. Introduction :

Arrays. COMS W1007 Introduction to Computer Science. Christopher Conway 10 June 2003

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

C++ PROGRAMMING SKILLS Part 4: Arrays

Functions, Arrays & Structs

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

NAMESPACES IN C++ You can refer the Programming with ANSI C++ by Bhushan Trivedi for Understanding Namespaces Better(Chapter 14)

Memory and Pointers written by Cathy Saxton

Advanced Computer Programming

Arrays. Chapter 7 (Done right after 4 arrays and loops go together, especially for loops)

Fundamentals of Programming Session 15

Array Basics: Outline

6. Pointers, Structs, and Arrays. 1. Juli 2011

C Functions. Object created and destroyed within its block auto: default for local variables

Computer Science 1 Honors

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

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

Global & Local Identifiers

7/21/10 MOTIVATION. Did you see Inception this weekend? COMP 10 EXPLORING COMPUTER SCIENCE. Lecture 6 Arrays

Notes from the Boards Set # 5 Page

CA4003 Compiler Construction Assignment Language Definition

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

Chapter 4 Defining Classes I

6. Pointers, Structs, and Arrays. March 14 & 15, 2011

Programming with Arrays Intro to Pointers CS 16: Solving Problems with Computers I Lecture #11

Arrays القوائم والمصفوفات. Chapter 12

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

Default arguments, documentation

Lab 9: Creating a Reusable Class

21. Exceptions. Advanced Concepts: // exceptions #include <iostream> using namespace std;

Typescript on LLVM Language Reference Manual

Templating functions. Comp Sci 1570 Introduction to C++ Administrative notes. Review. Templates. Compiler processing. Functions Overloading

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

Functions, Arrays & Structs

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

Introduction to C++ with content from

Comp Sci 1570 Introduction to C++

Module - 3 Classes, Inheritance, Exceptions, Packages and Interfaces. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

CS201 Some Important Definitions

TDDE18 & 726G77. Functions

12. Pointers Address-of operator (&)

AP Computer Science Unit 1. Programs

Expressions and Casting

C Programming for Engineers Functions

CS201- Introduction to Programming Latest Solved Mcqs from Midterm Papers May 07,2011. MIDTERM EXAMINATION Spring 2010

엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2017 EOM, HYEONSANG ALL RIGHTS RESERVED

Fundamentals of Programming Session 12

Programming for Engineers Arrays

C++ Arrays and Vectors

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

The C++ Language. Arizona State University 1

Lecture 2 Arrays, Searching and Sorting (Arrays, multi-dimensional Arrays)

Advanced Systems Programming

C++, How to Program. Spring 2016 CISC1600 Yanjun Li 1

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

by Pearson Education, Inc. All Rights Reserved.

Classes. Logical method to organise data and functions in a same structure. Also known as abstract data type (ADT).

KOM3191 Object Oriented Programming Dr Muharrem Mercimek OPERATOR OVERLOADING. KOM3191 Object-Oriented Programming

Arrays and functions Multidimensional arrays Sorting and algorithm efficiency

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

Chapter 6: Functions

AP Computer Science Unit 1. Writing Programs Using BlueJ

Maltepe University Computer Engineering Department. BİL 133 Algorithms and Programming. Chapter 8: Arrays

CSI33 Data Structures

10. Functions (Part 2)

INTRODUCTION TO PROGRAMMING

Pace University. Fundamental Concepts of CS121 1

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

Functions. Function Prototypes. Function prototype is needed if the function call comes before the function definition in the program.

PLT Fall Shoo. Language Reference Manual

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

OCA Java SE 7 Programmer I Certification Guide By Mela Gupta. Arrays

Nested Loops. A loop can be nested inside another loop.

Object Oriented Design

Array Basics: Outline

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

Array. Prepared By - Rifat Shahriyar

Transcription:

and Arrays Comp Sci 1570 Introduction to C++

Outline and 1 2 Multi-dimensional and 3 4 5

Outline and 1 2 Multi-dimensional and 3 4 5

Array declaration and An array is a series of elements of the same type placed in contiguous memory locations that can be individually referenced by adding an index to a unique identifier. That means that, for example, five values of type int can be declared as an array without having to declare 5 different variables (each with its own identifier). Instead, using an array, the five int values are stored in contiguous memory locations, and all five can be ed using the same identifier, with the proper index. For example, an array containing 5 integer values of type int called foo could be represented as: i n t foo [ 5 ] ;

Outline and 1 2 Multi-dimensional and 3 4 5

and i n t foo [ 5 ] = { 16, 2, 77, 40, 12071 } ; The number of values between braces shall not be greater than the number of elements in the array. For example, in the example above, foo was declared having 5 elements (as specified by the number enclosed in square brackets, []), and the braces contained exactly 5 values, one for each element.

Fill remainder with 0 s and If declared with less, the remaining elements are set to their default values (which for fundamental types, means they are filled with zeroes). For example: i n t bar [ 5 ] = { 10, 20, 30 } ;

Fill all with 0 s and The initializer can even have no values, just the braces: i n t baz [ 5 ] = { } ;

Outline and 1 2 Multi-dimensional and 3 4 5

to and from and For example, the following statement stores the value 75 in the third element of foo: foo [ 2 ] = 7 5 ; x = foo [ 2 ] ; and, for example, the above copies the value of the third element of foo to a variable called x:

Initialize to data size and It is possible to declare an array and initialize it with a sequence of elements, but no size specified: double c o l l e c t i o n [ ] = { 1. 0, 3. 5, 0. 5, 7. 2 } ;

Outline and 1 2 Multi-dimensional and 3 4 5

Indexing in multidimensional and

Outline and 1 2 Multi-dimensional and 3 4 5

and i n t jimmy [ 3 ] [ 5 ] ;

Outline and 1 2 Multi-dimensional and 3 4 5

and cout << jimmy [ 1 ] [ 3 ] ;

Outline and 1 2 Multi-dimensional and 3 4 5

and and const i n t WIDTH 5 const i n t HEIGHT 3 i n t jimmy [ HEIGHT ] [ WIDTH ] ; i n t n,m; i n t main ( ) { f o r ( n=0; n < HEIGHT ; n++) f o r (m=0; m < WIDTH; m++) { jimmy [ n ] [m]=( n+1) (m+1); } }

array options and Two-dimensional with initializer lists can omit (only) the leftmost length specification: i n t a r r a y [ ] [ 5 ] = { { 1, 2, 3, 4, 5 }, { 6, 7, 8, 9, 10 }, { 11, 12, 13, 14, 15 } } ;

array options and NOT OK. The compiler can do the math to figure out what the array length is. However, the following is not allowed: i n t a r r a y [ ] [ ] = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 } } ;

Outline and 1 2 Multi-dimensional and 3 4 5

and In C++, we don t want to pass the entire block of memory represented by an array to a function directly as an argument. But what can be passed instead is its address. In practice, this has almost the same effect, and it is a much faster and more efficient operation. void p r o c e d u r e ( i n t arg [ ] ) i n t main ( ) { i n t myarray [ 4 0 ] ; p r o c e d u r e ( myarray ) ;...

Outline and 1 2 Multi-dimensional and 3 4 5

and Notice that the first brackets [] are left empty, while the following ones specify sizes for their respective dimensions. Including the other dimensions is necessary in order for the compiler to be able to determine the depth of each additional dimension. void p r o c e d u r e ( i n t myarray [ ] [ 3 ] [ 4 ] ) ;

Outline and 1 2 Multi-dimensional and 3 4 5

See code demos and Bubble, selection, insertion

Outline and 1 2 Multi-dimensional and 3 4 5

More tips on PRNG and Please see code from day on random number generators, since it has been updated with some new tricks.