Array in C++: Erwin D. Marcelo
Array in C++: Erwin D. Marcelo
Erwin D. Marcelo
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
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.
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,};
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))