1 11/8/06CS150 Introduction to Computer Science 1 Arrays Chapter 8 page 477 November 13, 2006.

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.
Programming Logic and Design Sixth Edition
Arrays. Topics Tables of Data Arrays – Single Dimensional Parsing a String into Multiple Tokens Arrays - Multi-dimensional.
Chubaka Producciones Presenta :.
Introduction to Application Programming IST 256 Application Programming for Information Systems Xiaozhong Liu
1 11/05/07CS150 Introduction to Computer Science 1 Functions Chapter 6, page 303.
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
1 6/20/2015CS150 Introduction to Computer Science 1 Functions Chapter 6, 8.
1 10/29/07CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
Multiple-Subscripted Array
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Arrays.
Arrays Chapter 8 page /24/07CS150 Introduction to Computer Science 1 Arrays (8.1)  One variable that can store a group of values of the same.
Chapter 8 Arrays and Strings
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.
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
1 11/8/06CS150 Introduction to Computer Science 1 More Functions! page 343 November 8, 2006.
CS 106 Introduction to Computer Science I 02 / 19 / 2007 Instructor: Michael Eckmann.
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.
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.
Subroutines in Computer Programming Svetlin Nakov Telerik Corporation
Reusable parts of Code Doncho Minkov Telerik Software Academy academy.telerik.com Technical Trainer
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.
ARRAYS Lecture 2. 2 Arrays Hold Multiple values  Unlike regular variables, arrays can hold multiple values.
C++ Lecture 3 Monday, 14 July Arrays, Pointers, and Strings l Use of array in C++, multi- dimensional array, array argument passing l Pointers l.
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.
Data Structure CS 322. What is an array? Initializing arrays Accessing the values of an array Multidimensional arrays LAB#1 : Arrays.
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.
Arrays Adapted from materials created by Dr. Donald Bell, Cal Poly 2000 (updated February 2004)
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
2011 Calendar Important Dates/Events/Homework. SunSatFriThursWedTuesMon January
1 Chapter 12 Arrays. 2 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating.
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.
Reusable parts of Code Doncho Minkov Telerik Software Academy academy.telerik.com Technical Trainer
© Janice Regan, CMPT 128, January CMPT 128: Introduction to Computing Science for Engineering Students Introduction to Arrays.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
C++ Array 1. C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used.
Lecture 8 – Array (Part 1) FTMK, UTeM – Sem /2014.
July 2007 SundayMondayTuesdayWednesdayThursdayFridaySaturday
For Friday Read No quiz Program 6 due. Program 6 Any questions?
CHAPTER 6 ARRAYS IN C 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 F. Alakeel.
Introduction to programming in java Lecture 21 Arrays – Part 1.
Arrays Collections of data Winter 2004CS-1010 Dr. Mark L. Hornick 1.
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.
Objectives You should be able to describe: One-Dimensional Arrays
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
Arrays.
Chapter 7 – Arrays.
© 2016 Pearson Education, Ltd. All rights reserved.
New Structure Recall “average.cpp” program
Subroutines in Computer Programming
CS150 Introduction to Computer Science 1
2300 (11PM) September 21 Blue line is meridian..
CS150 Introduction to Computer Science 1
CISC181 Introduction to Computer Science Dr
McDonald’s calendar 2007.
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
McDonald’s calendar 2007.
2015 January February March April May June July August September
Introduction to Problem Solving and Programming
Presentation transcript:

1 11/8/06CS150 Introduction to Computer Science 1 Arrays Chapter 8 page 477 November 13, 2006

2 11/8/06CS150 Introduction to Computer Science 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 Each input file will have exactly five lines of data. Use one star to represent each 1000 people. Input file Output to Screen: 5000 * 4000 * * 3000 * * 2000 * * * 1000 * * * * *

3 11/8/06CS150 Introduction to Computer Science 1 Arrays  Provides an easy way to store and access multiple (related) values of the same data type int age = 42; int ages[3]; ages[0] = 17; ages[1] = 22; ages[2] = 21;//variable_name [ index ]

4 11/8/06CS150 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

5 11/8/06CS150 Introduction to Computer Science 1 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

6 11/8/06CS150 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: int vals[4]; vals[0] = 1; vals[1] = 2; vals[2] = 4; vals[3] = 8;

7 11/8/06CS150 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.  input: 100, 90, 84, 90, 89  output: Average: You could do this with 5 integers. But what about 100 test scores? Or 1000?

8 11/8/06CS150 Introduction to Computer Science 1 Danger!  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) const int constSize = 2; int tests[constSize]; tests[0] = 89; tests[1] = 99; tests[4] = 42; // what happens? 89 99??

9 11/8/06CS150 Introduction to Computer Science 1 Initialization  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] =

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

12 11/8/06CS150 Introduction to Computer Science 1 Arrays as Function Arguments  You can pass an array as an argument to a function void printIntArray(int arr[], int size); int values[] = { 5, 6, 0, 1, 2, 3, 4}; const int SIZE = 7; printIntArray(values, SIZE);

13 11/8/06CS150 Introduction to Computer Science 1 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;

14 11/8/06CS150 Introduction to Computer Science 1 Why use 2D arrays?  Hold the scores for each student in one array.  Which values are we setting above? 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;

15 11/8/06CS150 Introduction to Computer Science 1 A 2D array in memory  The 2D array laid out by rows in memory 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; row 0 row 1 This is called row major order. Some languages use column major order.

16 11/8/06CS150 Introduction to Computer Science 1 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 values[] = { {5, 6, 0}, {2, 2, 3} }; printIntArray(values, 2);

17 11/8/06CS150 Introduction to Computer Science 1 Practice  Write the function: void printInt2DArray(int arr[][3], int size);

18 11/8/06CS150 Introduction to Computer Science 1 N-Dimensional Arrays string fifthDimension[5][5][5][5][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”;