CS101L Manual 8rr
CS101L Manual 8rr
Arrays
Objectives:
➢
Array definition
➢
When to use Array
➢
Array declaration
➢
Array Initialization
➢
Accessing Array elements
➢
Copying Arrays
Outcomes:
➢
Students should be able to understand and use arrays.
Instead of declaring individual variables, such as number0, number1, ..., and number99, you declare
one array variable such as numbers and use numbers[0], numbers[1], and ..., numbers[99] to represent
individual variables. A specific element in an array is accessed by an index.
All arrays consist of contiguous memory locations. The lowest address corresponds to the first
element and the highest address to the last element.
It is quite simple. The variable age is created at line (5) as int. A value is assigned to it. Finally, age is
printed to the screen.
age
20
110
Example 2
#include <iostream>
using namespace std;
int main()
{
int age[4]; //declaration of Array
age[0]=23; //initialization of Array elements
age[1]=34;
age[2]=65;
age[3]=74;
return 0;
}
On line (5), an array of 4 int is created. Values are assigned to each variable in the array on line (6)
through line (9). In memory these are contiguous set of locations shown in following figure.
age [0] age [1] age [2] age [3]
23 34 65 74
Example 3
#include <iostream>
using namespace std;
int main()
{
int age[4]; //array ‘age’ of 4 ints
for(int j=0; j<4; j++) //get 4 ages
{
cout << “Enter an age: “;
cin >> age[j]; //access array element
}
111
8.4 Initialization of array?
It is like a variable, an array can be initialized. To initialize an array, we provide initializing values
which are enclosed within curly braces in the declaration and placed following an equals sign after the
array name. Here is an example of initializing an integer array.
int age [4] = {23,34,65,74};
age [0] age [1] age [2] age[3]
23 34 65 74
Now how to initialize all the values in array to 0? It can be done by the following statement;
int age [4] = {0};
age [0] age [1] age [2] age [3]
0 0 0 0
For example, to store the value 75 in the third element of age, we could write the following statement:
age [2] = 75; //note: array index start with 0 in c.
And, for example, to store the value of the third element of age to a variable called a, we could write:
int a = age [2];
112
Here’s another example of an array at work. This one, SALES, invites the user to enter a series of six
values representing widget sales for each day of the week (excluding Sunday), and then calculates the
average of these values. We use an array of type double so that monetary values can be entered.
Example 4
#include <iostream>
using namespace std;
int main()
{
const int SIZE = 6; //size of array double
sales[SIZE]; //array of 6 variables cout <<
“Enter widget sales for 6 days\n”;
for(int j=0; j<SIZE; j++) //put figures in
array cin >> sales[j];
double total = 0;
for(j=0; j<SIZE; j++) //read figures from
array total += sales[j]; //to find total
double average = total / SIZE; // find
average cout << “Average = “ << average <<
endl; return 0;
}
113
int i=0;
age[0]=23;
age[1]=34;
age[2]=65;
age[3]=74;
for (;i<4;i++)
same_age[i]=age[i];
for (i=0;i<4;i++)
cout<<same_age[i];
return 0;
}
In the above program two arrays are created: age and same_age. Each element of the age array is
assigned a value. Then, in order to copy the four elements in age into the same_age array, we must do
it element by element. We have used for loop to access every element of array note that for loop takes
value from 0 to 3.
Note: Like printing arrays, there is no single statement in the language that says "copy an entire array
into another array". The array elements must be copied individually. Thus If we want to perform any
action on an array, we must repeatedly perform that action on each element in the array.
114
{
{7, 6},
{3, 4},
{5, 3},
{2, 3}
},
{
{8, 9},
{7, 2},
{3, 4},
{5, 1}
}
};
cout<<"\narr[0][0][0] = "<<arr[0][0][0]<<"\n";
cout<<"\narr[0][2][1] = "<<arr[0][2][1]<<"\n";
cout<<"\narr[2][3][1] = "<<arr[2][3][1]<<"\n";
return 0;
}
int main()
{
string line;
int i;
cout << "Enter any string :: ";
cin>>line;
cout << "\nThe Original String is :: " << line<<endl;
int len = line.size();
for(i=0;i<len;++i)
{
if (!((line[i]>='a' && line[i]<='z') || (line[i]>='A' && line[i]<='Z')))
{
line[i]='\0';
}
}
cout << "\nAfter Removing Characters, String is :: " << line<<endl;
return 0;
}
115