Wednesday, 11/6/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 11/6/02  QUESTIONS?? – HW # 4 due Monday  Today:  Return HW #3  Arrays (Chap. 10)  Reading:

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Arrays I Savitch Chapter 6.1: Introduction to Arrays.
1 Arrays Chapter 9. 2 Outline  The array structure (Section 9.1)  Array declaration  Array initialization  Array subscripts  Sequential access to.
Monday, 11/11/02, Slide #1 CS 106 Intro to Comp. Sci. 1 Monday, 11/11/02  Questions? HW 04 due today at 5.  Today – Lists and an introduction to searching.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT5: Array (1D and 2D) CS2311 Computer Programming.
Simple Arrays COMP104 Lecture 11 / Slide 2 Arrays * An array is a collection of data elements that are of the same type (e.g., a collection of integers,characters,
Monday, 9/23/02, Slide #1 CS 106 Intro to CS 1 Monday, 9/23/02  QUESTIONS??  Today:  Discuss Lab 3  Do Exercises  Introduction to functions  Reading:
Monday, 12/9/02, Slide #1 CS 106 Intro to CS 1 Monday, 12/9/02  QUESTIONS??  On HW #5 (Due 5 pm today)  Today:  Recursive functions  Reading: Chapter.
Wednesday, 12/11/02, Slide #1 CS 106 Intro to Comp. Sci. 1 Wednesday, 12/11/02  QUESTIONS??  Today: CLOSING CEREMONIES!  HW #5 – Back Monday (12/16)
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
Arrays Hanly - Chapter 7 Friedman-Koffman - Chapter 9.
©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.
What If? Write a program that prompts for the names and locations of 1000 employees. Store the information in the program for later use.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
 Wednesday, 9/25/02, Slide #1 CS106 Introduction to CS1 Wednesday, 9/25/02  QUESTIONS??  Today: More on functions  Reading: Chapter 3 through 3.7 
Multiple-Subscripted Array
PHYS 2020 Making Choices; Arrays. Arrays  An array is very much like a matrix.  In the C language, an array is a collection of variables, all of the.
Monday, 11/18/02, Slide #1 CS 106 Intro to CS 1 Monday, 11/18/02  QUESTIONS??  Today:  Hand back, discuss HW #4  Discussion of Lab 10  Exam #2 Friday.
Chapter 8 Arrays and Strings
Simple Arrays Programming COMP104 Lecture 12 / Slide 2 Arrays l An array is a collection of data elements that are of the same type (e.g., a collection.
Arrays.
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
1 Introduction to Arrays Problem: –Input 5 scores, compute total, average –Input Example –test scores,employees,temperatures.
 For Loops › for (variable set; condition; incremental or decrement){ // loop beginning › } // loop end  While loops › while (condition) { // beginning.
Chapter 8 Arrays and Strings
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
CISC105 – General Computer Science Class 9 – 07/03/2006.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays Outline Introduction Arrays Declaring Arrays Examples Using Arrays.
1 Topic: Array Topic: Array. 2 Arrays Arrays In this chapter, we will : Learn about arrays Learn about arrays Explore how to declare and manipulate data.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
1 Chapter 7 Arrays. 2 Topics 7.1 Arrays Hold Multiple Values 7.2 Accessing Array Elements 7.3 No Bounds Checking in C Array Initialization 7.5 Processing.
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 8 Arrays.
Slide 1 Chapter 5 Arrays. Slide 2 Learning Objectives  Introduction to Arrays  Declaring and referencing arrays  For-loops and arrays  Arrays in memory.
1 One Dimensional Arrays Chapter 11 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores :
Function Overloading Two different functions may have the same name as long as they differ in the number or types of arguments: int max(int x, int y) and.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 1 Introduction An array is a collection of identical boxes.
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.
Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: Desired output:
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Arrays.
BIT115: Introduction to Programming
Arrays Chapter 7. Arrays Hold Multiple Values Array: variable that can store multiple values of the same type Values are stored in adjacent memory locations.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Literals A literal (sometimes called a constant) is a symbol which evaluates to itself, i.e., it is what it appears to be. Examples: 5 int literal
Week 6 - Friday.  What did we talk about last time?  Loop examples.
Opening Input/Output Files ifstream infile; ofstream outfile; char inFileName[40]; char outFileName[40]; coutinFileName;
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
1 CSC103: Introduction to Computer and Programming Lecture No 17.
CS 1430: Programming in C++.
Array. Array is a group of data of the same type. Array elements have a common name –The array as a whole is referenced through the common name Individual.
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
1 Programming in C++ Dale/Weems/Headington Chapter 11 One-Dimensional Arrays.
Lecture 2 Arrays. Topics 1 Arrays hold Multiple Values 2 Accessing Array Elements 3 Inputting and Displaying Array Contents 4 Array Initialization 5 Using.
1 Principles of Computer Science I Honors Section Note Set 3 CSE 1341.
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.
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Computer Programming BCT 1113
C++ Arrays.
CS 1430: Programming in C++.
One-Dimensional Array Introduction Lesson xx
Lecture 12 Oct 16, 02.
CS150 Introduction to Computer Science 1
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Presentation transcript:

Wednesday, 11/6/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 11/6/02  QUESTIONS?? – HW # 4 due Monday  Today:  Return HW #3  Arrays (Chap. 10)  Reading: 10.1  Exercises: p. 252 ff, 1, 11, 12  New files/handouts: Array1.cpp

Wednesday, 11/6/02, Slide #2 Chapter 10: Arrays  Most (but not all) of our object types so far have been simple types  Each object represents a single value  Examples: int, float, char, double  C++ also has structured data types  Each object represents a collection of values  Examples: ifstream, ofstream, string  Examples: Class objects like Complex, Currency Some operations to deal with entire object; others access individual parts of it

Wednesday, 11/6/02, Slide #3 Example: Processing lists of data  Suppose we have 25 grades and want to perform the tasks shown below. In each case, how many objects need to be held in memory (not just processed) to perform the task?  Compute the average (mean)  Compute the median  Find the highest grade  Print out all grades after they have all been inputted

Wednesday, 11/6/02, Slide #4 Average (Mean) and Highest  These just process the data. They don't need a separate object for each grade: float Sum = 0; for (int i = 1; i <= 25; ++i) {cin >> Grade; Sum += Grade; } float Average = Sum / 25;  The single object Grade is used for all the grade values (Highest similar).

Wednesday, 11/6/02, Slide #5 Median and Print Whole List  For these we need to remember all the grades -- so we need 25 objects (i.e., 25 different memory locations, each holding one grade)!  The C++ Array structure is a way of storing a list of objects together,  Using a single name for the whole list,  Using an index (0 to 24 for 25 objects) to refer to individual elements in the list.

Wednesday, 11/6/02, Slide #6 Declaring arrays; diagram of memory  char C[4];  const int N = 6; float A[N];  Number in array declaration must be a constant.  Array index (or subscript) goes from 0 to N-1 (not from 1 to N).  A[k] is the name of the k th item in array. A[0] A[1] A[2] A[3] A[4] A[5] A[0] A[1] A[2] A[3] A[4] A[5] C[0] C[1] C[2] C[3] C[0] C[1] C[2] C[3] ‘x’ ‘4’ ‘&’ ‘Y’

Wednesday, 11/6/02, Slide #7 Using array elements (Array1.cpp) double A[6]; A[4] = 341.0; A[2] = A[4] + 1.5; cout << A[2]; int j = 2; cin >> A[j + 1]; cout << A[j + 4]; A[0] A[1] A[2] A[3] A[4] A[5] A[0] A[1] A[2] A[3] A[4] A[5] A[] Warning! Index is out of range!

Wednesday, 11/6/02, Slide #8 Initializing Small Arrays  The elements of arrays declared locally are not given initial values.  For small arrays, we can initialize when we declare as follows: int Val[5] = {17, 36, 92, 14, -10}; int Val[5] = {17, 36, 92, 14, -10}; char Vowels[ ] = {'a', 'e', 'i', 'o', 'u‘, ‘y’}; char Vowels[ ] = {'a', 'e', 'i', 'o', 'u‘, ‘y’};  To process large arrays, we'll need to use loops! Number of elements supplied determines size of array

Wednesday, 11/6/02, Slide #9 Initializing Large Arrays  A simple way to initialize a large (or small) array of size N is to use a for-loop that increments the index from 0 to N-1: //Declare an array of numbers, //Initialize all values to 0 const int Length = 50; double dArray[Length]; for (int i = 0; i < Length; ++i) {dArray[i] = 0; } Notice that i starts at 0 (not 1!) and stops before Length (not at Length)