Lec array
Lec array
Array
An array is used to
store a collection of
data, but it is often
more useful to think of
an array as a
collection of variables
of the same type.
Represented as a
group of consecutive
memory locations
(sequentially stored).
Defining array
4
Examples
Int A[10]
An array of ten integers
A[0], A[1], …, A[9]
double B[20]
An array of twenty long floating point numbers
B[0], B[1], …, B[19]
Element
Storing array elements
7
Three ways to enter elements (values) in array:
1.Initialize arrays in the variable declaration statement
Example: double temperature [5]= {12.3,7.5,65,72.1,87.5};
2.Read input values into the array from the keyboard or
from a file and then store them in array using loop.
Example
Array elements are commonly used in loops
int size=10; int a[size],index;
for(index =0; index<=size-1; index++) { cin>>a[index]; }
To access all elements of an array a for loop is used, for loop will start
from index 0 to the last element in the array which has an index of array
size-1
When looping through an array, the array index should never go below
0 and always be less than the total number of elements in the array
(size –1).
Storing array elements
8
Three ways to enter elements (values) in array:
3.Use assignment statements.
Example: declaration: int score [6];
score[0]=49;
score[1]=75;
score[2] = 65;
score[3] = 90;
score[4]=77;
score[5]=70;
Accessing array elements
9
To refer to an element, specify
Array name
Position number
Format:
Arrayname [position number]
No
Why?
15
Example on comparing array.
#include<iostream>
using namespace std;
int main(){ Compare 2 arrays
int firstArray[] = { 2, 4, 6, 8, 10 };
content
int secondArray[] = { 2, 4, 6, 8, 10 };
bool arraysEqual = true;
int size2=5, size1=5, i=0;
// First determine whether the arrays are same size.
if (size1 != size2)
arraysEqual= false;
// Next determine whether the elements contain same data.
while ( arraysEqual ==true && i< 5)
{
if (firstArray [i] != secondArray[i]){
arraysEqual= false; }
i++;
}
if (arraysEqual==true)
cout <<"The arrays are equal .";
else
cout <<"The arrays are not equal .";
return 0;
}
Two dimension array
16
A two-dimensional array is an array of arrays.
Table, with rows and columns .
index
Two dimension array
17
Initializing Two-Dimensional Arrays
Multi dimensioned arrays may be initialized by
specifying bracketed values for each row. Following is
an array with 3 rows and each row have 4 columns.
Example:
int a[3][4] = {
{0, 1, 2, 3} , /* initializers for row indexed by 0 */
{4, 5, 6, 7} , /* initializers for row indexed by 1 */
{8, 9, 10, 11} /* initializers for row indexed by 2 */
};
The nested braces, which indicate the intended row, are
optional. The following initialization is equivalent to
previous example:
int a[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11};
Two dimension array
18
Initializing Arrays
Programs that process two-dimensional arrays can do so
with nested loops.
To fill the scores array:
int scores[3][4];
for (int row = 0; row < 3; row++)
{
for (int col = 0; col < 4; col++)
{
cout<<"Enter a score: ";
cin>>scores[row][col];
}
}
Two dimension array
19
To print the scores array:
#include <iostream>
using namespace std;
int main () {
// an array with 5 rows and 2 columns.
int a[5][2] = { {0,0}, {1,2}, {2,4}, {3,6},{4,8}};
cout << "a[" << i << "][" << j << "]: ";
cout << a[i][j]<< endl;
}
return 0;
}
21
Exercise
Write a program that reads the scores of 20 students, and
print:
The Maximum value of elements stored in an array.
The Average value of elements stored in an array.
The Minimum value of elements stored in an array.
22
Outline of course
Contents going to be covered during the course:
Problem Solving
Flow chart, pseudo code, and Algorithm
Starting writing first C++ program
Input and Output
Conditions
Loops
Array
Function
Pointers
String
OOP
23