0% found this document useful (0 votes)
8 views16 pages

Us C++ 2021 03

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views16 pages

Us C++ 2021 03

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

C++: Arrays

Presenter: Dr. Ha Viet Uyen Synh.


Review of the last lecture
- Control structures:
Selection statements: if, if .. else, switch.
Repetition statements: while, do.. while, for

- Divide and Conquer technique.


Introduction
An array is a consecutive group of memory locations that all
have the same name and the same type.

To refer to a particular location or element in the array , we


specify the name of the array and the position number of
the particular element in the array.
Declaring Arrays
Arrays occupy space in memory. The programmer specifies
the type of each element and the number of elements
required by an array as follows:
type arrayName [ arraySize ] ;

For example:
int c [12] ;
int b [100], x[27];
C++ Arrays start at 0 !!!!!!!
The first element is the 0th element!

If you declare an array of n elements, the last one is


number n-1.

If you try to access element number n, it is an error!


Array Subscripts
The element numbers are called subscripts.

foo[i]

A subscript can be any integer expression:


These are all valid subscripts:
foo[17] foo[i+3] foo[a+b+c]
Initialization
You can initialize an array when you declare it (just
like with variables):

int foo[5] = { 1,8,3,6,12};

double d[2] = { 0.707, 0.707};

char s[] = { 'R', 'P', 'I' };

You don’t need to specify a size when initializing,


the compiler will count for you.
Multiple-Subscripted Arrays
Arrays in C++ can have multiple subscripts.

Tables or arrays that require two subscripts to identify


a particular element are called double-subscripted
arrays.

For example: Double-subscripted array with three


rows and four columns
Memory Organization

char A[4][3];

{
A[0]
A[0][0]
A[0][1]
A[0][2]

{
A is an array of size 4.
A[1][0]
Each element of A is A[1] A[1][1]
an array of 3 chars A[1][2]

{
A[2]
A[2][0]
A[2][1]
A[2][2]

{
A[3]
A[3][0]
A[3][1]
A[3][2]
Initialize a Multiple-Subscripted Array

const int NumStudents = 10;

const int NumHW = 3;

double grades[NumStudents][NumHW];

for (int i=0;i<NumStudents;i++) {

for (int j=0;j<NumHW;j++) {

cout << “Enter HW “ << j <<

“ Grade for student number “ << i<<


endl;

cin >> grades[i][j];

}
Exercise 1
Use a single-subscripted array to solve the following
problem.

Read in 20 numbers, each of which is between 10


and 100, inclusive. As each number is read, print it
only if it is not a duplicate of a number already read.
Provide for the "worst case" in which all 20 numbers
are different.

Use the smallest possible array to solve this problem.


Exercise 2
Draw an 8-by-8 chessboard on the text screen and
set a Knight's position by hand. List all positions
where the Knight can move to.
Exercise 3
Draw an 8-by-8 chessboard on the text screen and set a
Bishop's position by hand. List all positions where the
Bishop can move to.
Exercise 4
Write single C++ statements that sort an array with 20
elements of type integer.
Any Questions?

[email protected]

You might also like