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

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

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

Sequential Search (Searching Supplement: 1-2)

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

Tutorial 8 (Array I)

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

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

CS Programming I: Arrays

Programming Logic and Design Sixth Edition

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

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

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

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

Q1: Functions / 33 Q2: Arrays / 47 Q3: Multiple choice / 20 TOTAL SCORE / 100 Q4: EXTRA CREDIT / 10

CS 101, Spring 2014 April 1st Exam 2 Question 1. [3 points] What output is printed by the following code?

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:

Read and fill in this page now

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

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

ITT8060: Advanced Programming (in F#)

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

Arrays, Strings, & Pointers

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

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

Arrays. Lecture 11 CGS 3416 Fall October 26, 2015

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

1 Pointer Concepts. 1.1 Pointer Examples

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

ECE264 Fall 2013 Exam 1, September 24, 2013

Programming Assignment #2

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

Arrays. Lecture 9 COP 3014 Fall October 16, 2017

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:

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

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

CS3 Midterm 1 Fall 2007 Standards and solutions

CHIROPRACTIC MARKETING CENTER

CS 101, Spring 2016 March 22nd Exam 2

C Structures, Unions, Bit Manipulations, and Enumerations

Highline Excel 2016 Class 09: Date Functions

02157 Functional Programming Tagged values and Higher-order list functions

MONDAY TUESDAY WEDNESDAY THURSDAY FRIDAY SATURDAY SUNDAY

Arrays. Arrays. Wolfgang Schreiner Research Institute for Symbolic Computation (RISC) Johannes Kepler University, Linz, Austria

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

MAP OF OUR REGION. About

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

Different kinds of resources provided by a package

Strings and Arrays. Hendrik Speleers

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

Organizing and Summarizing Data

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

COMPUTER TRAINING CENTER

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

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

CSE 115. Introduction to Computer Science I

Marketing Opportunities

MAP OF OUR REGION. About

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

Introduction. Structures, Unions, Bit Manipulations, and Enumerations. Structure. Structure Definitions

Strings. Chuan-Ming Liu. Computer Science & Information Engineering National Taipei University of Technology Taiwan

Switch Statement (Outsource: )

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

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

Lecture07: Strings, Variable Scope, Memory Model 4/8/2013

Calendar Excel Template User Guide

Overview. Concepts this lecture String constants Null-terminated array representation String library <strlib.h> String initializers Arrays of strings

Undergraduate Admission File

Reverse Segmentable GainMaker Node OIB Shorting Condition Technical Bulletin

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

CONDITIONAL EXECUTION: PART 2

CS3: Introduction to Symbolic Programming. Lecture 8: Introduction to Higher Order Functions. Spring 2008 Nate Titterton

Special Education Room and Board Reimbursement Claim User Guide

Scheduling. Scheduling Tasks At Creation Time CHAPTER

Functions, Arrays & Structs

Arrays and Array Lists. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos and Alexandra Stefan University of Texas at Arlington

Exercise 1.1 Hello world

Arrays: Higher Dimensional Arrays. CS0007: Introduction to Computer Programming

Complex data structures. Cedric Saule

APSC 160 Review Part 2

Physics 234: Computational Physics

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

Chapter 4 - Notes Control Structures I (Selection)

Chapter 10. Arrays and Collections. Animated Version The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

CS107 Handout 13 Spring 2008 April 18, 2008 Computer Architecture: Take II

Sonoma State University Computer Science Department CS 115 Fall 2010 Watts. Project 3 Part 1

Topic 4 Expressions and variables

Basic Device Management

Miscellaneous Calendar 2017

Computer Science 217

CSI33 Data Structures

A method is a code block that contains a series of statements. Methods. Console.WriteLine(); Console.ReadKey(); Console.ReadKey(); int.

The following list represents the written codes in order:

INFOB3TC Solutions for Exam 1

Excel Functions & Tables

Guernsey Post 2013/14. Quality of Service Report

CS Programming I: Branches

CIS 110 Introduction to Computer Programming 8 October 2013 Midterm

Midterm Exam CS 251, Intermediate Programming October 8, 2014

AIR FORCE SCHOOL,BAMRAULI COMPUTER SCIENCE (083) CLASS XI Split up Syllabus (Session ) Contents

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

Transcription:

Arrays Chapter 8 page 477 11/8/06 CS150 Introduction to Computer Science 1 1 What if you have a 1000 line file? Read in the following file and print out a population graph as shown below. The maximum value for a population is 5000. Each input file will have exactly five lines of data. Use one star to represent each 1000 people. Input file 1970 1000 1980 1000 1990 2000 2000 4000 2010 5000 Output to Screen: 5000 * 4000 * * 3000 * * 2000 * * * 1000 * * * * * 1970 1980 1990 2000 2010 11/8/06 CS150 Introduction to Computer Science 1 2 Arrays Provides an easy way to store and access multiple (related) values of the same data type int age = 42; 42 int ages[3]; ages[0] = 17; ages[1] = 22; 17 22 21 ages[2] = 21; // variable_name [ index ] 11/8/06 CS150 Introduction to Computer Science 1 3 1

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]; // literal double tempatures[size]; // illegal! int tests[constsize]; // const int When the code is compiled, the exact size of the array must be known 11/8/06 CS150 Introduction to Computer Science 1 4 Using arrays 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 Must use an index to access the array 11/8/06 CS150 Introduction to Computer Science 1 5 Practice Write a snippet of code to print to the screen the sum and average of the values in this array: int vals[4]; vals[0] = 1; vals[1] = 2; vals[2] = 4; vals[3] = 8; 11/8/06 CS150 Introduction to Computer Science 1 6 2

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. input: 100, 90, 84, 90, 89 output: Average: 90.6 89 90 84 90 100 You could do this with 5 integers. But what about 100 test scores? Or 1000? 11/8/06 CS150 Introduction to Computer Science 1 7 Danger! Danger! const int constsize = 2; int tests[constsize]; 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! o you may write over other, valid data o you may write over part of the program common security problem (buffer overflow) 11/8/06 CS150 Introduction to Computer Science 1 8 Initialization 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/8/06 CS150 Introduction to Computer Science 1 9 3

Implicit array sizing Set the size of the array by initializing it You must either specify a size or initialize the array string names[] = char letters[] = 11/8/06 CS150 Introduction to Computer Science 1 10 Using Arrays 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 11/8/06 CS150 Introduction to Computer Science 1 11 Arrays as Function Arguments You can pass an array as an argument to a function void printintarray(int arr[], int size); int main() { int values[] = { 5, 6, 0, 1, 2, 3, 4}; const int SIZE = 7; printintarray(values, SIZE); return 0; } 11/8/06 CS150 Introduction to Computer Science 1 12 4

Two dimensional arrays A grid of data! int testscores[2][3]; testscores[0][0] = 99; testscores[0][1] = 80; testscores[0][2] = 88; testscores[1][0] = 89; testscores[1][1] = 77; testscores[1][2] = 85; 11/8/06 CS150 Introduction to Computer Science 1 13 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? 11/8/06 CS150 Introduction to Computer Science 1 14 A 2D array in memory The 2D array laid out by rows in memory int testscores[2][3]; row 0 row 1 99 80 88 89 77 85 testscores[0][0] = 99; testscores[0][1] = 80; testscores[0][2] = 88; testscores[1][0] = 89; testscores[1][1] = 77; testscores[1][2] = 85; This is called row major order. Some languages use column major order. 11/8/06 CS150 Introduction to Computer Science 1 15 5

Passing a 2D array to a function Must specify at least one of the sizes in the function s parameter list so the compiler knows how to access the array void printint2darray(int arr[][3], int size); int main() { int values[] = { {5, 6, 0}, {2, 2, 3} }; printintarray(values, 2); return 0; } 5 6 0 2 2 3 11/8/06 CS150 Introduction to Computer Science 1 16 Practice Write the function: void printint2darray(int arr[][3], int size); 11/8/06 CS150 Introduction to Computer Science 1 17 N-Dimensional Arrays string fifthdimension[5]; fifthdimension[0][0][0][0][0] = up ; fifthdimension[0][1][0][0][0] = up ; fifthdimension[0][0][1][0][0] = and ; fifthdimension[0][0][0][1][0] = away ; 11/8/06 CS150 Introduction to Computer Science 1 18 6