Arrays Chapter 8 page 471. 2 10/24/07CS150 Introduction to Computer Science 1 Arrays (8.1)  One variable that can store a group of values of the same.

Slides:



Advertisements
Similar presentations
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Advertisements

Arrays. What is an array An array is used to store a collection of data It is a collection of variables of the same type.
Chubaka Producciones Presenta :.
Introduction to Application Programming IST 256 Application Programming for Information Systems Xiaozhong Liu
What Data Do We Have? Sections 2.2, 2.5 August 29, 2008.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 11/3/08CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
2012 JANUARY Sun Mon Tue Wed Thu Fri Sat
Arrays Clark Savage Turner, J.D., Ph.D. Copyright © 2000 by C Scheftic. All rights reserved. These notes do rely heavily.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
1 10/29/07CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
CS 106 Introduction to Computer Science I 10 / 04 / 2006 Instructor: Michael Eckmann.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
1 11/8/06CS150 Introduction to Computer Science 1 Arrays Chapter 8 page 477 November 13, 2006.
CS 106 Introduction to Computer Science I 02 / 20 / 2008 Instructor: Michael Eckmann.
Multiple-Subscripted Array
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Arrays.
P Pathophysiology Calendar. SundayMondayTuesdayWednesdayThursdayFridaySaturday January 2012.
Chapter 7 Arrays C++ Programming, Namiq Sultan1 Namiq Sultan University of Duhok Department of Electrical and Computer Engineering Reference: Starting.
Lesson 7 Arrays CS 1 Lesson 7 -- John Cole1. Arrays Hold Multiple Values Array: variable that can store multiple values of the same type Values are stored.
CS 106 Introduction to Computer Science I 02 / 19 / 2007 Instructor: Michael Eckmann.
MONDAYTUESDAYWEDNESDAYTHURSDAYFRIDAYSAT/SUN Note: You can print this template to use as a wall calendar. You can also copy the slide for any month to add.
School Year Calendar You can print this template to use it as a wall calendar, or you can copy the page for any month to add it to your own presentation.
School Year Calendar You can print this template to use it as a wall calendar, or you can copy the page for any month to add it to your own presentation.
You can print this template to use it as a wall calendar, or you can copy the page for any month to add it to your own presentation. If you’d like to change.
2007 Monthly Calendar You can print this template to use it as a wall calendar, or you can copy the page for any month to add it to your own presentation.
You can print this template to use it as a wall calendar, or you can copy the page for any month to add it to your own presentation. If you’d like to change.
Chapter 8 Arrays and Strings
Crypto Project Sample Encrypted Data: –Turing: CS Public\CryptoProjectData Crypto_Short_Keys_P_U.out Crypto_Long_Keys_O_R.out Bonus +10 points on.
1 DATA STRUCTURES: LISTS. 2 LISTS ARE USED TO WORK WITH A GROUP OF VALUES IN AN ORGANIZED MANNER. A SERIES OF MEMORY LOCATIONS CAN BE DIRECTLY REFERENCED.
ARRAYS Lecture 2. 2 Arrays Hold Multiple values  Unlike regular variables, arrays can hold multiple values.
© Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 8 Multidimensional Arrays.
WORD JUMBLE. Months of the year Word in jumbled form e r r f b u y a Word in jumbled form e r r f b u y a february Click for the answer Next Question.
DATE POWER 2 INCOME JANUARY 100member X 25.00P2, FEBRUARY 200member X 25.00P5, MARCH 400member X 25.00P10, APRIL 800member.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
Introduction to C Programming Lecture 6. Functions – Call by value – Call by reference Arrays Today's Lecture Includes.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
Arrays. Related data items Collection of the same types of data. Static entity – Same size throughout program.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
2011 Calendar Important Dates/Events/Homework. SunSatFriThursWedTuesMon January
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
Arrays What is an array… –A data structure that holds a set of homogenous elements (of the same type) –Associate a set of numbers with a single variable.
July 2007 SundayMondayTuesdayWednesdayThursdayFridaySaturday
For Friday Read No quiz Program 6 due. Program 6 Any questions?
Computer Programming Arrays 1. Question #1 2 Question Choose the correct answer..
Arrays – two dimensional Chapter 14 This chapter explains how to do the following with a two dimensional array: Declare, use indices, obtain size, pass.
ARRAYS (C) KHAERONI, M.SI. OVERVIEW Introduction to Arrays Arrays in Functions Programming with Arrays Multidimensional Arrays.
Introduction to programming in java Lecture 21 Arrays – Part 1.
Arrays Collections of data Winter 2004CS-1010 Dr. Mark L. Hornick 1.
1 CS Oct 2008 Arrays. Reading: Secs 8.1, 8.2, 8.3 Listen to the following lectures on loops on your Plive CD. They are only 2-3 minutes long, and.
Beginning C for Engineers Fall 2005 Arrays, 2-D arrays, character strings Bettina Schimanski Lecture 5: Section 2 (9/28/05) Section 4 (9/29/05)
Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
CS 2130 Lecture 6 Pointers and Arrays. Pointers are like jumps, leading wildly from one part of the data structure to another. Their introduction into.
Chapter 7 – Arrays.
New Structure Recall “average.cpp” program
McDonald’s Kalender 2009.
McDonald’s Kalender 2009.
McDonald’s Kalender 2009.
2300 (11PM) September 21 Blue line is meridian..
CISC181 Introduction to Computer Science Dr
McDonald’s calendar 2007.
INC 161 , CPE 100 Computer Programming
CS150 Introduction to Computer Science 1
CS149D Elements of Computer Science
McDonald’s calendar 2007.
2015 January February March April May June July August September
Presentation transcript:

Arrays Chapter 8 page 471

2 10/24/07CS150 Introduction to Computer Science 1 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

3 10/24/07CS150 Introduction to Computer Science 1 Arrays int age = 42; int ages[3]; ages[0] = 17; ages[1] = 22; ages[2] = 21; // variable_name [ index ]

4 10/24/07CS150 Introduction to Computer Science 1 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 Average: 90.6 You could do this with 5 integers. But what about 100 test scores? Or 1000?

5 10/24/07CS150 Introduction to Computer Science 1 Declaring an Array  The size of the array must be a literal or a const int.  When the code is compiled, the exact size of the array must be known int size = 99; const int constSize = 1024; string names[3]; // literal double tempatures[size]; // illegal! int tests[constSize]; // const int

6 10/24/07CS150 Introduction to Computer Science 1 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;

7 10/24/07CS150 Introduction to Computer Science 1 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

8 10/24/07CS150 Introduction to Computer Science 1 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;

9 10/24/07CS150 Introduction to Computer Science 1 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;

10 10/24/07CS150 Introduction to Computer Science 1 Danger! Danger! (p 479)  C++ does not check to make sure the index falls within the array o no bounds checking o this will cause unpredictable results! int tests[2]; tests[0] = 89; tests[1] = 99; tests[4] = 42; // what happens? ??

11 10/24/07CS150 Introduction to Computer Science 1 Initialization (8.4)  How do you set the initial values for the array elements?  What is the equivalent of:  Initialize just a few values: int value = 2; int tests[2] = string names[3] = int value[4] =

12 10/24/07CS150 Introduction to Computer Science 1 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[] =

13 10/24/07CS150 Introduction to Computer Science 1 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: January31 February28 March31 April30 May31 June30 July31 August31 September30 October31 November30 December31

14 10/24/07CS150 Introduction to Computer Science 1 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; Two dimensional arrays (8.9)  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;

15 10/24/07CS150 Introduction to Computer Science 1 Why use 2D arrays?  Hold the scores for each student in one array.  Which values are we setting above?  How do we set Alice’s Midterm2 score?  What is stored in testScores[0][1] ? 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;

16 10/24/07CS150 Introduction to Computer Science 1 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];

17 10/24/07CS150 Introduction to Computer Science 1 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 This is called row major order. Some languages use column major order.

18 10/24/07CS150 Introduction to Computer Science 1 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] = ; 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