Transcript Array

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 kth item
in array.
‘x’
‘4’
‘&’
‘Y’
C[0]
C[1]
C[2]
C[3]
17.3
-4.26
22.09
17.3
A[0]
A[1]
A[2]
A[3]
341.0
A[4]
-82.75
A[5]
Wednesday, 11/6/02, Slide #7
Using array elements (Array1.cpp)
A[]
A[0]
A[1]
A[2]
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[3]
A[4]
A[5]
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};
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)