Anda di halaman 1dari 22

Array in C++

Erwin D. Marcelo

Array is a data structure which allows a collective name


to be given to a group of elements which all have the same data type. An individual element of an array is identified by its own unique index (or subscript).

Collection of elements having the same data type Uses index to denote the location of elements Must have a size to denote maximum number of elements that array can have

Types of Array
Single-Dimension Array Multi-Dimension Array

Array Declaration
Syntax: data type array_name[size]; Example: int num[10]; array of 10 integers float grade[20]; array of 20 floating numbers char name[15]; array of 15 characters

Array Initialization
Syntax:
data_type array_name[size]= {constant_value list,};

Example: int num[5]={2, 4, 6, 8, 10}; int num[]={2, 4, 6, 8, 10} Float grade[5]={1.25,1.50,2.50,1.75,2.00}; char name[]=erwin; char name[]={e,r,w,i,n,\0};
Sample Program

Array and Input Statement


Use for statement cout<<enter five numbers: ; for(i=0;i<5;i++) cin>>num[i]; Use while statement i=0; cout<<enter five numbers: ; while(i<5) cin>>num[i];

Array and Input Statement


Use do while statement i=0; cout<<enter numbers, zero to terminate: ; do{ cin>>num[i]; i++; }while(num[i-1]!=0);
Sample Program

Array and Output Statement


Use loop statement for(i=0;i<5;i++) cout<<num[i]; while(i!=5) { cout<<num[i]; i++; }

Sample Program

Sample Program
The requirement specification for a program is: A set of positive data values (20) are available. It is required to find the average value of these values and to count the number of values that are more than 10% above the average value. Since the data values are all positive a negative value can be used as a sentinel to signal the end of data entry.

Algorithm(pseudo-code)
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. set sum to zero. set count to zero. set notgt10 to zero. enter first value. while value is positive { put value in array element with index count. add value to sum. increment count. enter a value. } average = sum/count. for index taking values 0 to count-1 if array[index] greater than 1.1*average then increment nogt10. output average, count and nogt10.

Accessing Array Elements


An array element is accessed by writing the identifier of the array followed by the subscript(index) in square brackets.

Accessing Array Elements


An array element can be used anywhere an identifier may be used. a = score[2]; aveq = (quiz[1] + quiz[2] + quiz[3])/3; score[j] = (quiz1 + quiz2)/2; score[x+1] = score[x] + 5; A value can be read into an array element directly, using cin cin >> count[i]; The element can be increased by 5, count[i] = count[i] + 5; or, using the shorthand form of the assignment count[i] += 5;

Array elements can form part of the condition for an if statement, or indeed, for any other logical expression:
if (temp[j] < 75) cout << Sorry you FAILED! << endl;

for statements are the usual means of accessing every element in an array. The first NE elements of the array temp are given values from the input stream cin.
for (i = 0; i < NE; i++) cin >> temp[i];

Sample Code
The following code finds the average temperature recorded in the first ten elements of the array. sum = 0.0; for (i = 0; i <10; i++) sum += temp[i]; av1 = sum / 10;

Sample Problem
Create an application that determine the largest and smallest number in a list of 10 integers. Input : List of 10 integers Output :Largest, Smallest number in the list

Algorithm
Accept 10 numbers For i=0 to 9 do accept num[i] Set large=num[0] Set small=num[0] Compare to the other number in the list For j = 1 to 9 do if num[j] is greater than large then large=num[j] else if num[j] is smaller than small then small=num[j] Display larger and smaller

Two-Dimensional Array

Array Declaration
Syntax: data type array_name[size1][size2][sizen]; Example: int num[10][10]; array of 100 integers
float grade[5][4]; array of 20 floating numbers

2D-array Initialization
Syntax:
data_type array_name[size1][size2][sizen]= {constant_value list,};

Example: int num[3][3]={2, 4, 6, 3,6,9,4,8, 12}; char names[3][5]={"erwin","marco","vhern"};

5x5 Multiplication Table

Sample Program

Create a program that displays a 5x5 multiplication table // assigning a value to the 2 X 2 array for r = 0 to 4 for c = 0 to 4 mt(r,c)= (r+1) * (c+1) // displaying the contents of the 2x2 array for r = 0 to 4 for c = 0 to 4 display(mt(r,c))

EXERCISES Salesman Interest


Create a program that accepts the weekly sales of 3 salesman for a month. Display the monthly sales of every salesman, the weekly sales, and the total sales for the month. Sample Screen display

Sample input screen lay-out


Enter sales of salesman 1 for week 1: 1000 Enter sales of salesman 1 for week 2: 1500 Enter sales of salesman 1 for week 3: 1000 Enter sales of salesman 1 for week 4: 2000 Enter sales of salesman 2 for week 1: 1500 Enter sales of salesman 2 for week 2: 1500 Enter sales of salesman 2 for week 3: 1700 Enter sales of salesman 2 for week 4: 2000 Enter sales of salesman 3 for week 1: 1200 Enter sales of salesman 3 for week 2: 1100 Enter sales of salesman 3 for week 3: 1000 Enter sales of salesman 3 for week 4: 1400

Sample output lay-out


Total sales of every salesman: Salesman 1: 5500 Salesman 2: 6700 Salesman 3: 4700 Total sales for the week: Week 1: 3700 Week 2: 4100 Week 3: 4700 Week 4: 5400 Total sales for the month: 16900

Anda mungkin juga menyukai