Arrays. Arrays (8.1) Arrays. One variable that can store a group of values of the same type. Storing a number of related values.

Similar documents
Arrays. What if you have a 1000 line file? Arrays

Example. Section: PS 709 Examples of Calculations of Reduced Hours of Work Last Revised: February 2017 Last Reviewed: February 2017 Next Review:

Programming Logic and Design Sixth Edition

Sequential Search (Searching Supplement: 1-2)

Computer Programming, I. Laboratory Manual. Experiment #3. Selections

Tutorial 8 (Array I)

Why arrays? To group distinct variables of the same type under a single name.

Do not turn to the next page until the start of the exam.

MAP OF OUR REGION. About

A Beginner s Guide to Programming Logic, Introductory. Chapter 6 Arrays

Arrays and Pointers (part 2) Be extra careful with pointers!

MAP OF OUR REGION. About

CSE 142, Autumn 2007 Midterm Exam, Friday, November 2, 2007

Arrays and Pointers (part 2) Be extra careful with pointers!

CS Programming I: Arrays

CSCI 111 First Midterm Exam Fall Solutions 09.00am 09.50am, Wednesday, October 18, 2017

Read and fill in this page now. Your lab section day and time: Name of the person sitting to your left: Name of the person sitting to your right:

Chapter 4 - Notes Control Structures I (Selection)

V2 3/5/2012. Programming in C. Introduction to Arrays. 111 Ch 07 A 1. Introduction to Arrays

02157 Functional Programming. Michael R. Ha. Tagged values and Higher-order list functions. Michael R. Hansen

Arrays. Lecture 9 COP 3014 Fall October 16, 2017

Computer Grade 5. Unit: 1, 2 & 3 Total Periods 38 Lab 10 Months: April and May

ITT8060: Advanced Programming (in F#)

Calendar Excel Template User Guide

Formative Benchmark 1

CHIROPRACTIC MARKETING CENTER

Special Education Room and Board Reimbursement Claim User Guide

Read and fill in this page now

02157 Functional Programming Tagged values and Higher-order list functions

MONDAY TUESDAY WEDNESDAY THURSDAY FRIDAY SATURDAY SUNDAY

02157 Functional Programming. Michael R. Ha. Disjoint Unions and Higher-order list functions. Michael R. Hansen

Stat 428 Autumn 2006 Homework 2 Solutions

Organizing and Summarizing Data

1 Pointer Concepts. 1.1 Pointer Examples

Arrays, Strings, & Pointers

Arrays. Lecture 11 CGS 3416 Fall October 26, 2015

COMPUTER TRAINING CENTER

Chapter 5. Selection Statements. Animated Version The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

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

Marketing Opportunities

Read and fill in this page now. Your instructional login (e.g., cs3-ab): Your lab section days and time: Name of the person sitting to your left:

10/30/2010. Introduction to Control Statements. The if and if-else Statements (cont.) Principal forms: JAVA CONTROL STATEMENTS SELECTION STATEMENTS

Objectives. After studying this chapter, Stdent should be able to

CONDITIONAL EXECUTION: PART 2

Final. Your Name CS Fall 2014 December 13, points total Your Instructor and Section

Switch Statement (Outsource: )

Reverse Segmentable GainMaker Node OIB Shorting Condition Technical Bulletin

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

Arrays IT 1033: Fundamentals of Programming

typedef int Array[10]; String name; Array ages;

Highline Excel 2016 Class 09: Date Functions

Miscellaneous Calendar 2017

CURRICULUM MAP. Course / Subject: Precalculus Grade: 11/12 Teacher: Kelly, Kirk, McCollick, Palkovics, Roderick Month: September (19 days)

EACH MONTH CUTTING EDGE PEER REVIEW RESEARCH ARTICLES ARE PUBLISHED

State Water Survey Division

09/08/2017 CS2530 INTERMEDIATE COMPUTING 9/8/2017 FALL 2017 MICHAEL J. HOLMES UNIVERSITY OF NORTHERN IOWA TODAY S TOPIC: Exceptions and enumerations.

SPRINGBOARD UNIT 6 DATA ANALYSIS AND PROBABILITY

The following list represents the written codes in order:

Topic 4 Expressions and variables

Guernsey Post 2013/14. Quality of Service Report

COMPUTER SCIENCE 123. Foundations of Computer Science. 5. Strings

//If target was found, then //found == true and a[index] == target.

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

ECE264 Fall 2013 Exam 1, September 24, 2013

Sinusoidal Data Worksheet

hereby recognizes that Timotej Verbovsek has successfully completed the web course 3D Analysis of Surfaces and Features Using ArcGIS 10

CSE 115. Introduction to Computer Science I

Apple Inc. US 6,587,904 US 6,618,785 US 6,636,914 US 6,639,918 US 6,718,497 US 6,831,928 US 6,842,805 US 6,865,632 US 6,944,705 US 6,985,981

14. Other Data Types. Compound Data Types: Defined data types (typedef) Unions typedef existing_type new_type_name ;

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

Common Mistakes with Functions. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

Maryland Corn: Historical Basis and Price Information Fact Sheet 495

C Structures, Unions, Bit Manipulations, and Enumerations

Grade 4 Mathematics Pacing Guide

Complex data structures. Cedric Saule

Strings and Arrays. Hendrik Speleers

Chapter 10. Arrays. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

Objectives. After studying this chapter, Stdent should be able to

Chapter 9 Introduction to Arrays. Fundamentals of Java

Introduction to Computer Programming for Non-Majors

DATE OF BIRTH SORTING (DBSORT)

Control Structures in Java if-else and switch

CPA PEP 2018 Schedule and Fees

Lesson 1: Creating a Worksheet and a Chart Microsoft Excel 2016 IN THIS CHAPTER, YOU WILL LEARN HOW TO

Undergraduate Admission File

Grid Code Planner EU Code Modifications GC0100/101/102/104

Arrays. Here is the generic syntax for an array declaration:

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

Calendar PPF Production Cycles Non-Production Activities and Events

Computer Science 217

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

Different kinds of resources provided by a package

Scheduling. Scheduling Tasks At Creation Time CHAPTER

Programming Assignment #2

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

Nortel Enterprise Reporting Quality Monitoring Meta-Model Guide

VTC FY19 CO-OP GOOGLE QUALIFICATIONS PARAMETERS & REIMBURSEMENT DOCUMENTATION HOW-TO

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

CS 101, Spring 2016 March 22nd Exam 2

Learning Objectives. Introduction to Arrays. Arrays in Functions. Programming with Arrays. Multidimensional Arrays

Transcription:

Arrays Chapter 8 page 471 Arrays (8.1) One variable that can store a group of values of the same type Storing a number of related values o all grades for one student o all temperatures for one month o hours worked for each day 2 Arrays int age = 42; 42 int ages[3]; ages[0] = 17; ages[1] = 22; 17 22 21 ages[2] = 21; // variable_name [ index ] 3

When would I use this? Read in 5 test scores from the user. Calculate the average test score and print out the scores in reverse order. 89 90 84 90 100 Average: 90.6 You could do this with 5 integers. But what about 100 test scores? Or 1000? 4 Declaring an Array The size of the array must be a literal or a const int. int size = 99; const int constsize = 1024; string names[3]; double tempatures[size]; int tests[constsize]; // literal // illegal! // const int When the code is compiled, the exact size of the array must be known 5 Using arrays (8.2) The first element in the array is the 0 th element! You can use a single element of an array just like any other variable The index is just an int Loops are often used to access every element in an array int y, x = 3; int tests[10]; tests[0] = 2; tests[x] = 4; y = tests[0] + 9; 6

Practice Declare an array to hold the height, in inches, of six trees. Set the height of the trees as: o 32 inches o 45 inches o 99 inches o 120 inches o 500 inches o 600 inches 7 Practice (8.3) Write a snippet of code to print to the screen every value in this array: const int arraysize = 4; int vals[arraysize]; vals[0] = 1; vals[1] = 2; vals[2] = 4; vals[3] = 8; 8 Practice Write a snippet of code to print to the screen the sum and average of the values in this array: const int arraysize = 4; int vals[arraysize]; vals[0] = 1; vals[1] = 2; vals[2] = 4; vals[3] = 8; 9

Danger! Danger! (p 479) int tests[2]; 89 99?? tests[0] = 89; tests[1] = 99; tests[4] = 42; // what happens? C++ does not check to make sure the index falls within the array o no bounds checking o this will cause unpredictable results! 10 Initialization (8.4) How do you set the initial values for the array elements? What is the equivalent of: int value = 2; int tests[2] = string names[3] = Initialize just a few values: int value[4] = 11 Implicit array sizing (p 486) Set the size of the array by initializing it You must either specify a size or initialize the array string names[] = char letters[] = 12

Using Arrays (8.6) Write code that will use arrays to store the names the months and the number of days in each month (assume no leap year!). Print the following to the screen: January 31 February 28 March 31 April 30 May 31 June 30 July 31 August 31 September 30 October 31 November 30 December 31 13 Two dimensional arrays (8.9) A grid of data! int testscores[2][3]; 0 1 0 1 2 testscores[0][0] = 99; testscores[0][1] = 80; testscores[0][2] = 88; testscores[1][0] = 89; testscores[1][1] = 77; testscores[1][2] = 85; 14 Why use 2D arrays? Hold the scores for each student in one array. const int BOB = 0; const int ALICE = 1; const int MIDTERM1 = 0; const int MIDTERM2 = 1; const int FINAL = 2; int testscores[2][3] = { {0, 0, 0}, {0, 0, 0} }; testscores[bob][midterm1] = 99; testscores[alice][final] = 85; Which values are we setting above? How do we set Alice s Midterm2 score? What is stored in testscores[0][1]? 15

Practice Using the array below, calculate: o the average score on each assignment o the average score for each student o assume the array already contains data const int numberofstudents = 100; const int numberofassignments = 4; int testscores[numberofstudents][numberofassignments]; 16 A 2D array in memory The 2D array laid out by rows in memory int testscores[2][3]= { {99, 80, 88}, {89, 77, 85}}; row 0 row 1 99 80 88 89 77 85 This is called row major order. Some languages use column major order. 17 N-Dimensional Arrays (8.10) // cost of seats in a theatre // // 4 sections, each section has // 20 rows with 30 seats each. double seats[4][20][30]; seats[0][0][0] = 100.00; seats[2][0][0] = seats[1][0][0] / 2; seats[3][19][25] = 10.00; // we can have as many dimensions as // necessary in an array 18