Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 17

Chapter - 5

Arrays, Qualifiers
and Reading
Numbers
Practical C++ Programming Copyright 2003 O'Reilly and Associates Page1
Arrays
Arrays are used to store a collection of related values.
Instead of declaring a individual variable in which to store
each value, you can declare the array.
Array declaration has the format:
data type arrayname[n];
where n is a positive integer constant that represents the
maximum number of values, elements, that you want to
store in the array -- size declarator.
e.g.
double hello[3];
int alpha[20];
Practical C++ Programming Copyright 2003 O'Reilly and Associates Page2
Arrays
Simple variables allow user to declare one item, such as a single width:
int width;   // Width of the rectangle in inches
If we have a number of similar items, we can use an array to declare them. For
example, if we want declare a variable to hold the widths of 1000 rectangles.
int width_list[1000];   // Width of each rectangle
The width of the first rectangle is width[0] the width of the second rectangle is
width[1] and so on until width[999].
Warning:
Common sense tells you that the last element of the width array is width[1000].
Common sense has nothing to do with programming and the last element of the
array is width[999].

Practical C++ Programming Copyright 2003 O'Reilly and Associates Page3


Example
Suppose I want to store scores for 100 students.
I could declare the variables score1, score2, score3……..
score100.
Alternatively, I could declare the array by the statement
int score[100];
which allocates space for 100 integer values.

Practical C++ Programming Copyright 2003 O'Reilly and Associates Page4


Schematic of Memory
As separate variables

score1 score 2 score3 score4 score5 score100

As an array

score[0] score[1] score[2] score[3] score[4] score[99]

Practical C++ Programming Copyright 2003 O'Reilly and Associates Page5


Some Constraints
For this course, all the elements must have the same
data type.
All the elements should represent similar data. But
do not have to be similar.

Practical C++ Programming Copyright 2003 O'Reilly and Associates Page6


Accessing Array Elements
The individual elements of an array may be accessed
by the array name and the subscript or index.
score[3], score[i], score[99]
Subscripts in C++ begin with zero, so the first element
of the array with size declarator n has the index or
subscript 0 and the last element has the index or
subscript n-1.
Second element has the subscript or index of 1, third
element, 2, etc.

Practical C++ Programming Copyright 2003 O'Reilly and Associates Page7


Inputting Elements of an Array
Array elements may be input one at a time
score[0] = 78;
score[5] = 80;
score[99] = 67;
Or use a loop
for (int j = 0; j < n; j ++){
cout<<“Please enter the score \n”;
cin>>score[j];
}

Practical C++ Programming Copyright 2003 O'Reilly and Associates Page8


Note
You do not have to use all the elements of the
array.

It is often advantageous to declare the array


for slightly more elements that you think
you might actually need.

Practical C++ Programming Copyright 2003 O'Reilly and Associates Page9


Initializing when Declaring

Array elements can be initialized at time of declaration.


double temps[4] = {78.5, 79.8, 85.4, 86.2};
If you are initializing the elements at the point of
declaration the size declarator may be omitted.
double temps[ ] = {78.5, 79.8, 85.4, 86.2};
The size will be set implicitly equal to the number of
elements specified.

Practical C++ Programming Copyright 2003 O'Reilly and Associates Page10


Initializing after Declaration

Or you can input later manually or using a loop


e.g.
int nums[3];
nums[0]=2;
nums[1]=4;
nums[2]=6;

OR (if there is a patter or you cin each value use a for loop)
int nums[3];
for (int i=0;i<3;i++){
nums[i]=(i+1)*2;
}

Practical C++ Programming Copyright 2003 O'Reilly and Associates Page11


Outputting Data from an Array
Array elements can be output individually
cout<<score[0]<<“ “<<score[5];
Or using a loop
for (int j = 0; j < n; j ++){
cout<<score[j]<<endl;
}
Numeric arrays cannot be output by just using the array
name.
cout<<score; // will not work!

Practical C++ Programming Copyright 2003 O'Reilly and Associates Page12


Processing Array Elements
Individual array elements can be used in assignment
statements, output statements, etc. in a similar
manner as individual variables.
X = y * score[20];
y = sqrt(temper[4]) + b;
You may also use pre- or post-incrementation or
decrementation on a specific array element.
temper[6]++;
Loops are commonly used to process the array
elements.

Practical C++ Programming Copyright 2003 O'Reilly and Associates Page13


Computing the average of 6 numbers
#include <iostream>

double data[5];  // data to average and total 
double total;    // the total of the data items 
double average;  // average of the items

int main()
{
    data[0] = 34.0;
    data[1] = 27.0;
    data[2] = 46.5;
    data[3] = 82.0;
    data[4] = 22.0;

    total = data[0] + data[1] + data[2] + data[3] + data[4];
    average =  total / 5.0;
    cout << "Total "<< total <<" Average "<< average << '\n';
    system(“pause”);
}

Practical C++ Programming Copyright 2003 O'Reilly and Associates Page14


Computing the average of 6 numbers
#include <iostream>

double data[5];  // data to average and total 
double total;    // the total of the data items 
double average;  // average of the items

int main()
{
    data[0] = 34.0;
    data[1] = 27.0;
    data[2] = 46.5;
    data[3] = 82.0;
    data[4] = 22.0;

    for(int i=0;i<4;i++){
total+=data[i];
}
    average =  total / 5.0;
    cout << "Total " <<total<<" Average " <<average<< '\n';
    system(“pause”);
}

Practical C++ Programming Copyright 2003 O'Reilly and Associates Page15


Homework – Part 1
1. Write a program that declares an array with size 10 with the values stored as
random numbers between 1-10.  Output the sum of those values and the product
as well.    
2. CH4 pg 42, question 3
3.  Write a program that has the user enter 5 numbers into an array.  Output back to
the user those numbers in reverse order. e.g.
enter a number:2
enter a number:3
enter a number:4
enter a number:5
enter a number:6
output:
65432
Practical C++ Programming Copyright 2003 O'Reilly and Associates Page16
Homework – Part 2
4. Write a program that declares an array of size n, where n is given by the user. 
Ask the user for all values of the array store them into the array then display the
entire array in the following format:
array[0] = 3
array[1] = 12
...
array[34] =-2
where the last value is n-1 not 34 and the numbers are the values that the user
originally entered.
5.  Declare an array of size 30 and store it with random numbers.  Ask the user if
they would like to find a number and return the position of the number found.  If
the number is not in the list display a message that tells the user that.  Then
display the contents of the entire array to the user.  (BONUS -1 mark find all
positions of the number if there are multiple)

Practical C++ Programming Copyright 2003 O'Reilly and Associates Page17

You might also like