Arrays. Lecture 9 COP 3014 Fall October 16, 2017

Similar documents
Dynamic Memory Allocation

Arrays. Lecture 11 CGS 3416 Fall October 26, 2015

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

Arrays. Lecture 11 CGS 3416 Spring March 6, Lecture 11CGS 3416 Spring 2017 Arrays March 6, / 19

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

C Concepts - I/O. Lecture 19 COP 3014 Fall November 29, 2017

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

The C++ Language. Arizona State University 1

Homework #3 CS2255 Fall 2012

Objectives. In this chapter, you will:

array Indexed same type

CSc Introduction to Computing

Today. o main function. o cout object. o Allocate space for data to be used in the program. o The data can be changed

Object Oriented Programming COP3330 / CGS5409

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

IT 1033: Fundamentals of Programming Data types & variables

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

Data Types & Variables

File Operations. Lecture 16 COP 3014 Spring April 18, 2018

Chapter 2: Introduction to C++

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

C++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols.

Basics of Java Programming

String Objects: The string class library

LECTURE 02 INTRODUCTION TO C++

C++ Arrays. Arrays: The Basics. Areas for Discussion. Arrays: The Basics Strings and Arrays of Characters Array Parameters

Variables. Store information needed by the program

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

C Style Strings. Lecture 11 COP 3014 Spring March 19, 2018

Lecture 2 Tao Wang 1

Exercise: Using Numbers

C++ Programming: From Problem Analysis to Program Design, Third Edition

Lecture 3 Tao Wang 1

Programming for Engineers Arrays

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

These are reserved words of the C language. For example int, float, if, else, for, while etc.

! A literal represents a constant value used in a. ! Numbers: 0, 34, , -1.8e12, etc. ! Characters: 'A', 'z', '!', '5', etc.

Computer Programming

Chapter 2 Basic Elements of C++

Software Design & Programming I

C++ ARRAYS POINTERS POINTER ARITHMETIC. Problem Solving with Computers-I

Sprite an animation manipulation language Language Reference Manual

ME 172. Lecture 2. Data Types and Modifier 3/7/2011. variables scanf() printf() Basic data types are. Modifiers. char int float double

Fundamentals of Programming

1 Lexical Considerations

Lexical Considerations

Chapter 10. Arrays and Strings

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

Fundamental of Programming (C)

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program?

Full file at

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

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

Chapter-8 DATA TYPES. Introduction. Variable:

CSI33 Data Structures

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

LECTURE 15. String I/O and cstring library

Exercise: Inventing Language

Variables and numeric types

DECLARATIONS. Character Set, Keywords, Identifiers, Constants, Variables. Designed by Parul Khurana, LIECA.

Integer Data Types. Data Type. Data Types. int, short int, long int

Lexical Considerations

PES INSTITUTE OF TECHNOLOGY (BSC) I MCA, First IA Test, November 2015 Programming Using C (13MCA11) Solution Set Faculty: Jeny Jijo

Strings(2) CS 201 String. String Constants. Characters. Strings(1) Initializing and Declaring String. Debzani Deb

1/25/2018. ECE 220: Computer Systems & Programming. Write Output Using printf. Use Backslash to Include Special ASCII Characters

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

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

CONTENTS: Array Usage Multi-Dimensional Arrays Reference Types. COMP-202 Unit 6: Arrays

UNIT- 3 Introduction to C++

BASIC ELEMENTS OF A COMPUTER PROGRAM

LECTURE 3 C++ Basics Part 2

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

Characters, c-strings, and the string Class. CS 1: Problem Solving & Program Design Using C++

COP Programming Assignment #7

Java Basic Datatypees

Should you know scanf and printf?

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

Starting Out with C++: Early Objects, 9 th ed. (Gaddis, Walters & Muganda) Chapter 2 Introduction to C++ Chapter 2 Test 1 Key

printf( Please enter another number: ); scanf( %d, &num2);

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

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

CSCI 6610: Intermediate Programming / C Chapter 12 Strings

FORM 2 (Please put your name and form # on the scantron!!!!) CS 161 Exam II:

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

CSC 1300 Exam 4 Comprehensive-ish and Structs

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

BITG 1113: Array (Part 2) LECTURE 9

Week 3 Lecture 2. Types Constants and Variables

CHAPTER-6 GETTING STARTED WITH C++

CSC 211 Intermediate Programming. Arrays & Pointers

4 Programming Fundamentals. Introduction to Programming 1 1

.. Cal Poly CPE 101: Fundamentals of Computer Science I Alexander Dekhtyar..

2 nd Week Lecture Notes

BITG 1233: Introduction to C++

Piyush Kumar. input data. both cout and cin are data objects and are defined as classes ( type istream ) class

Chapter 2 C++ Fundamentals

Peer Instruction 1. Elementary Programming

Transcription:

Arrays Lecture 9 COP 3014 Fall 2017 October 16, 2017

Array Definition An array is an indexed collection of data elements of the same type. Indexed means that the array elements are numbered (starting at 0). The restriction of the same type is an important one, because arrays are stored in consecutive memory cells. Every cell must be the same type (and therefore, the same size).

Declaring Arrays An array declaration is similar to the form of a normal declaration (typename variablename), but we add on a size: typename variablename[size]; This declares an array with the specified size, named variablename, of type typename. The array is indexed from 0 to size-1. The size (in brackets) must be an integer literal or a constant variable. The compiler uses the size to determine how much space to allocate (i.e. how many bytes). Examples: int list[30]; // an array of 30 integers char name[20]; // an array of 20 characters double nums[50]; // an array of 50 decimals int table[5][10]; //two dimensional array of integers

Initializing Arrays With normal variables, we could declare on one line, then initialize on the next: int x; x = 0; Or, we could simply initialize the variable in the declaration statement itself: int x = 0; Can we do the same for arrays? Yes, for the built-in types. Simply list the array values (literals) in set notation { } after the declaration. Here are some examples: int list[4] = {2, 4, 6, 8}; char letters[5] = { a, e, i, o, u }; double numbers[3] = {3.45, 2.39, 9.1}; int table[3][2] = {{2, 5}, {3,1}, {4,9}};

C-style strings Arrays of type char are special cases. We use strings frequently, but there is no built-in string type in the language A C-style string is implemented as an array of type char that ends with a special character, called the null character. The null character has ASCII value 0 The null character can be written as a literal in code as \0 Every string literal (something in double-quotes) implicitly contains the null character at the end Since character arrays are used to store C-style strings, you can initialize a character array with a string literal (i.e. a string in double quotes), as long as you leave room for the null character in the allocated space. char name[7] = Johnny"; Notice that this would be equivalent to: char name[7] = { J, o, h, n, n, y, \0 };

Variations in initializing Array declarations must contain the information about the size of the array. It is possible to leave the size out of the [ ] in the declaration as long as you initialize the array inline, in which case the array is made just large enough to capture the initialized data. Examples: char name[] = Johnny"; // size is 7 int list[] = {1, 3, 5, 7, 9}; // size is 5 Another shortcut with initializer sets is to use fewer elements than the size specifies. Remaining elements will default to 0. It is illegal to use a set containing more elements than the allocated size. int list[5] = {1, 2}; // array is {1, 2, 0, 0, 0} int nums[3] = {1, 2, 3, 4}; // illegal

Variations in initializing Using initializers on the declaration, as in the examples above, is probably not going to be as desirable with very large arrays. Another common way to initialize an array with a for loop: This example initializes the array numlist to {0, 2, 4, 6, 8, 10, 12, 14, 16, 18}. int numlist[10]; int i; for (i = 0; i <10; i++) numlist[i] = i * 2;

Using Arrays Once your arrays are declared, you access the elements in an array with the array name, and the index number inside brackets [ ]. If an array is declared as: typename varname[size], then the element with index n is referred to as varname[n]. Examples: int x, list[5]; // declaration double nums[10]; // declaration list[3] = 6; // assign value 6 to item on index 3 cout <<nums[2]; //output array item with index 2 list[x] = list[x+1]; It would not be appropriate, however, to use an array index that is outside the bounds of the valid array indices: list[5]=10; //bad statement, 5 is invalid index The statement above is syntactically legal, however. It is the programmer s job to make sure that out of bounds indices are not used.

Copying arrays If we have these two arrays, how do we copy the contents of list2 to list1? int list1[5]; int list2[5] = {3, 5, 7, 9, 11}; With variables, we use the assignment statement, so this would be the natural tendency but it is wrong! list1 = list2; // does NOT copy array contents We must copy between arrays element by element. A for loop makes this easy, however: for (int i = 0; i <5; i++) list1[i] = list2[i];

Simple I/O with strings In the special case of strings (null-terminated character arrays), they can be used like normal arrays. Accessing a single array element means accessing one character. char greeting[] = Hello"; char word1[20]; cout <<greeting[1]; // prints the letter e cout <<greeting[4]; // prints the letter o Strings can also be output and input in their entirety, with the standard input and output objects (cin and cout) The following line outputs the word Hello : cout <<greeting;

Simple I/O with strings Be careful to only use this on char arrays that are being used as C-style strings. (This means, only if the null character is present as a terminator). The following line allows the entry of a word (up to 19 characters and a terminating null character) from the keyboard, which is stored in the array word1: cin >>word1; Characters are read from the keyboard until the first white space (space, tab, newline, etc) character is encountered. The input is stored in the character array and the null character is automatically appended.