Vous êtes sur la page 1sur 23

C++ Programming Structures (Part IV)

QF002/5/1

UNIT 5

C++ PROGRAMMING STRUCTURES (Part IV)

OBJECTIVES

General Objective

To understand and apply the fundamental concept of Array in C++ Programming Structures.

Specific Objectives

At the end of the unit you should be able t o :-

 Explain the declarations of arrays.  Explain how to declare and initialize arrays.  Use arrays in functions.  Write and design a simple arrays program.

___________________________________________________ ________________________

C++ Programming Structures (Part IV)

QF002/5/2

INPUT

5.0 Introduction To Array

An array is a collection of data storage locations, each of which holds the same type of data. Each storage location is called an element of the array .

5.1 Array Declarations

 An array is a structured collection of components (often called array elements) that can be accessed individually by specifying the position of a component with a single index value.  The following is the general form to declare an array
Data_type Array_name [ Array_size]

Note:
y y y

Data_type is the specifier that indicates the type of data. Array_name is the name of the declared array. Array_size defines how many elements the array can contain

___________________________________________________ ________________________

C++ Programming Structures (Part IV)

QF002/5/3

 For example, an array of an integer is declared in the following statement: int kolejkomuniti [ 13]

Note:
y y
y

int specifies as the data type. kolejkomuniti is the name of the declared array. The size of the array is 13.

 In C++, user have to declare an array explicitly, as you do for other variables before you can use it.

5.2 Initializing An Array

 In C++, an array may be initialize at the time of its declaration.  A variable can be initialized in its declaration. For example:
int value = 25;

Note:
The value 25 is called an initializer. Similarly, an array can be initialized in its declaration. A list of initial values for array elements can be specified. They are separated with commas and enclosed within braces.

___________________________________________________ ________________________

C++ Programming Structures (Part IV)

QF002/5/4

 For example, the following statement initializes an integer array number:


int number[6]={ 1, 2, 3, 4, 5, 6};

Note:
The integer inside the braces { } are assigned to the corresponding elements of the array number. That is, 1 is given to the first element ( number[0] ), 2 to the second element (number[1] ), 3 to the third ( number[2 ] ) and so on.

 Here are same examples of array initialization. i. int x [9] = {0,1,2,3,4,5,6,7,8 } ii. double x [8] = {1.2, 1.4, 1.7, 2.6, 3.4, 2.6, 1.2, 3.1} iii. char x[9] = {a,b,c,d,e,f,g,h,h,s}  the results of the initial assignments in both cases would be as follows: i.
x[0]=0 x[1]=1 x[2]=2 x[3]=3 x[4]=4 x[5]=5 x[6]=6 x[7]=7 x[8]=8

ii.
x[0]=1.2 x[1]=1.4 x[2]=1.7 x[3]=2.6 x[4]=3.4 x[5]=2.6 x[6]=1.2 x[7]=3.1

iii.
x[0]=a x[1]=b x[2]=c x[3]=d x[4]=e x[5]=f x[6]=h x[7]=h x[8]=s

___________________________________________________ ________________________

C++ Programming Structures (Part IV)

QF002/5/5

 Figure below shows another example of initializing array .


# include <iostream.h> main() { int a; int number[6]; for (a=0;a<6;a++) { number[a]= a+5; cout <<"number["<<a<<"]"<<"is initialized with=" <<number[a]<<"\n"; } return 0; }

Figure 5.1: A Simple Example Program Of Initializing Array

Figure 5.2: Output From The Sample

An array is a collection of variables that are of the same data type

___________________________________________________ ________________________

C++ Programming Structures (Part IV)

QF002/5/6

5.3 Multidimensional Arrays For String Functions

 It is possible to have array s of more than one dimension. Each dimension is represented as a subscript in the arra y. Therefore, a two-dimensional
array has two subscripts; a three-dimensional array has three subscripts

and so on. Arrays can have any number of dimensions although it is likely that most of the array s you create will be of one or two dimensions.  When an array has more than one dimension, we call it a multidimensional array.  A good example of a two-dimensional array is a chess board. One dimension represents the eight rows; the other dimension represents the eight columns. The figure below illustrates this idea.

Figure 5.3: A Chess Board And A Two-Dimensional Array Note:


Suppose that you have a class named SQUARE . The declaration of an array named Board -the representation would be
SQUARE Board[8][8];

___________________________________________________ ________________________

C++ Programming Structures (Part IV)

QF002/5/7

 A multidimensional array declaration in C++ takes the form:


Type name [size1] [size2]..[size N]

 For example
Char X[4][5][2]

Declares a 3 dimensional array with a depth of 4, 5 rows and 3 columns

In many cases, I need to declare a set of variables Why do you need to use array? that are of the same data type. Instead of declaring each variable separately, I can declare all variables collectively in the format of an array. Each variable, as an element of the array, can be accessed either through the array element reference or through a pointer that refer to that array.

___________________________________________________ ________________________

C++ Programming Structures (Part IV)

QF002/5/8

5.4 Initializing Multidimensional Arrays

In order to initialize multidimensional arrays. One has to list values to arrays elements in order while the former arrays remain the same. int theArray[5][3]

Note: The first three elements go into theArray[0]; the next three into theArray[1]; and so forth.

 User can initialize this array by writing: int Kolej[3][4] = { 1,2,3,4,11,12,13,14,21,22,23,24 }

Note:
Kolej [0][0] = 1 Kolej [1][0] = 11 Kolej [2][0] = 21 Kolej [0][1] = 2 Kolej [1][1] = 12 Kolej [2][1] = 22 Kolej [0][2] = 3 Kolej [1][2] = 13 Kolej [2][2] = 23 Kolej [0][3] = 4 Kolej [1][3] = 14 Kolej [2][3] = 24

___________________________________________________ ________________________

C++ Programming Structures (Part IV)

QF002/5/9

Arrays can be one dimensional or multidimensional.

Emm, thanks.

5.5 A Simple Two Dimensional Arrays For String Functions

#include <iostream.h> int main() { int kolej[5][2] = {{0,0},{1,2},{2,4},{3,6}, {4,8}}; for (int i = 0; i<5; i++) for (int j=0; j<2; j++) { cout << "SomeArray[" << i << "][" << j << "]: "; cout << kolej[i][j]<< endl; } return 0; }

Figure 5.4: A Simple Two Dimensional Arrays

___________________________________________________ ________________________

C++ Programming Structures (Part IV)

QF002/5/10

Figure 5.5: An Output Of Two Dimensional Arrays

Notes: To declares pendidikan into two-dimensional array. The first dimension consists of five integers; the second dimension consists of two integers. This creates a 5x2 grid.

What is a TwoDimension Array?

In two-dimensional array , the first dimension consists of five integers; the second dimension consists of two integers. This creates a 5x2 grid, as Figure 5.6 shows

___________________________________________________ ________________________

C++ Programming Structures (Part IV)

QF002/5/11

Figure5.6:

A 5x2 array

5.6 A Simple Multidimensional Arrays For String Functions

Figure 5.7: A Simple Multidimensional Arrays for String Functions

What is the minimum index in an array ?

The minimum index in a onedimensional array is 0, which marks the first element of the array.

___________________________________________________ ________________________

C++ Programming Structures (Part IV)

QF002/5/12

5.7 Output of the program

Figure 5.8: An Output From The Sample C Programming

The compiler can automatically calculate the memory space needed by an unsized array
Yes, sir !!

___________________________________________________ ________________________

C++ Programming Structures (Part IV)

QF002/5/13

Activity 5

Test your comprehension before continuing the next input. Check your answers on the next page.

5.1. 5.2. 5.3. 5.4. 5.5. 5.6.

Why do you need to use arrays ? What is the minimum index in an array ? What are the first and last elements in kolejkomuniti[100]? How do you declare a multidimensional array ? Initialize the members of the array in Question 5.4. How many elements are there in the array program[10][5][20]?

5.7.

What is the output of the given program below?

a.
#include <iostream.h> #include <string.h> main() { char array_ch[7] = {'H', 'e', 'l', 'l', 'o', '!', '\0'}; int i; for (i=0; i<7; i++) cout<<"array_ch["<<i<<"] contains : "<< array_ch[i]<<" \n"; cout<< "Put all elements together(Method I): \n"; for (i=0; i<7; i++) cout<< array_ch[i]; cout<<"\nPut all elements together(Method II): \n"; cout<<array_ch; return 0; }

___________________________________________________ ________________________

C++ Programming Structures (Part IV)

QF002/5/14

b.
#include <iostream.h> #include <string.h> main() { char array_ch[12] = {'A', 'n ','d','a',' ','h','e','b','a','t','!',' \0'}; int i; for (i=0; array_ch[i]; i++) cout<< array_ch[i]; return 0; }

c.
#include <iostream.h> #include <string.h> main() { char array_ch[3][4] = {'A', 'n ','d','a','h','e','b','a','t','!','!',' \0'}; int i, j; for (i=0; i<3; i++) { cout<<"\n"; for (j=0; j<4; j++) cout<< array_ch[i][j]; } return 0; }

___________________________________________________ ________________________

C++ Programming Structures (Part IV)

QF002/5/15

Feedback 5

Make sure you have tried to answer all the questions given. You can check your answers with the answers below.

5.1

To declare a set of variable that are of the same data type. To declare all variable collectively in the format of an array.

5.2.

Minimum index of dimensional array is 0 which marks the first element of the array. In multidimensional arrays, the minimum index of each dimension starts at 0.

5.3.

kolejkomuniti[0], kolejkomuniti[99].

5.4.

Write a set of subscripts for each dimension. For example, kolejkomuniti[3][2][3] is a three-dimensional array . The first dimension has three elements, the second has two and the third has three.

5.5.

Program[2][3][2]={ { {11,22},{33,44},{5,6} }, { {7,8},{9,10},{11,12} } };

5.6.

10x5x20=1,000

___________________________________________________ ________________________

C++ Programming Structures (Part IV)

QF002/5/16

5.7. a.

b.

c.

___________________________________________________ ________________________

C++ Programming Structures (Part IV)

QF002/5/17

Key Facts

 An array is a structured collection of components (often called array elements) that can be accessed individually by specifying t he position of a component with a single index value.  The general form to declare an array: Data_type Array_name [ Array_size]  The minimum of one-dimensional array is 0, which marks the first element of the array.  When an array has more than one dimension, we call it a multidimensional array.  A multidimensional array declaration in C++ takes the form: Type name [size1] [size2]..[size N]

___________________________________________________ ________________________

C++ Programming Structures (Part IV)

QF002/5/18

Self-Assessment

You are approaching success, please answer the questions below. If you have any problems, please discuss it with your lecturer. Wish you good luck and all the best.
Question 5-1

a. How many different types of an arrays are there? b. What happens if I write to element 25 in a 24 -member arrays? c. What type and range must an array subscript have? d. What values will the elements of an array have when it is declared if it does not include an initial? e. What happens if an array initialized has more value than size of the
array ?

f. How many elements are there in the array : i. kolejkomuniti[2][3][4] ii. kolejkomuniti [4][6][8] iii. kolejkomuniti [6][6][6] iv. kolejkomuniti [10][10][10] v. kolejkomuniti [15][30][100]

___________________________________________________ ________________________

C++ Programming Structures (Part IV)

QF002/5/19

Question 5-2

a.

Write program that can get the output as below. i.

ii.

iii.

___________________________________________________ ________________________

C++ Programming Structures (Part IV)

QF002/5/20

b.

What is wrong with this code fragment? unsigned short Program[8][5]; for (int i = 0; i<5; i++) for (int j = 0; j<8; j++) program[i][j] = i+j;

c.

What is wrong with this code fragment? unsigned short Program[10][8]; for (int i = 0; i<=10; i++) for (int j = 0; j<=8; j++) Program[i][j] = 0;

___________________________________________________ ________________________

C++ Programming Structures (Part IV)

QF002/5/21

Feedback On Self-Assessment

Make sure you have tried to answer all the questions given. You can check your answers with the answers below.
Answer 5-1

a. Only one of all the arrays elements must be the same type. b. You will write to other memory with potentially disastrous effects on your program. c. An arrays subscript must be an integer type with range from 0 to n-1, where n is the array s size. d. In the absence of an initial, the elements of an array will have unpredictable initial values. e. It is an error to have initial value than the size of the array . f. i. ii. iii. iv. v. 2x3x4 4x6x8 6x6x6 10 x 10 x 10 15 x 30 x 100 = = = = = 24 192 216 1000 45000

___________________________________________________ ________________________

C++ Programming Structures (Part IV)

QF002/5/22

Answer 5-2

a. i.
#include <iostream.h> #include <string.h> main() { char array_ch[7] = {'H','e','l','l', 'o','!','\0'}; int i; for (i=0; i<7; i++) cout<<"array_ch["<<i<<"] contains: "<< array_ch[i]<<" \n"; cout<< "Put all elements together(Method I): \n"; for (i=0; i<7; i++) cout<< array_ch[i]; cout<<"\nPut all elements together(Method II): \n"; cout<<array_ch; return 0; }

ii.
#include <iostream.h> #include <string.h> main() { char array_ch[12] = {'A','n','d','a',' ','h','e','b','a', 't','!',' \0'}; int i; for (i=0; array_ch[i]; i++) cout<< array_ch[i]; return 0; }

___________________________________________________ ________________________

C++ Programming Structures (Part IV)

QF002/5/23

iii.
#include <iostream.h> #include <string.h> main() { char array_ch[3][4] = {'A','n ','d','a','h','e','b','a','t','!','!',' \0'}; int i, j; for (i=0; i<3; i++) { cout<<"\n"; for (j=0; j<4; j++) cout<< array_ch[i][j]; } return 0; }

b. The array has 8 elements by 5 elements but the code initializes 5x8. c. You wanted to write i<10 but you wrote i<=10 instead. The code will run when i == 10 and j == 8, but there is no such element as Program[10][8].
CONGRATULATIONS May success be with you always..

___________________________________________________ ________________________

Vous aimerez peut-être aussi