0% found this document useful (0 votes)
4 views

CS101L Manual 8rr

rr

Uploaded by

Saadia Asghar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

CS101L Manual 8rr

rr

Uploaded by

Saadia Asghar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Lab# 08

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.

8.1 What is Array?


Array is a data structure in C++, which stores a fixed size sequential collection of elements of the
same type. An array is used to store a collection of data. It is often useful to think of an array as a
collection of variables of the same type.

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.

8.2 When to use array?


Let's start by looking at listing 1 where a single variable is used to store a person's
age. Example 1
#include <iostream>
using namespace std;
int main()
109
{
int age;
age=23;
cout<< age;
return 0;
}

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

2390(its address in memory)


Now let's keep track of 4 ages instead of just one. We could create 4 separate variables, but creating 4
separate variables is not a good approach. (If you are using 4 separate variables for it, then consider
keeping track of 1000 ages instead of just 4). Rather than using 4 separate variables, we'll use an array
because it is quite easy to handle one variable instead of 1000.

8.3 Declaration of array?


To declare an array in C++, the programmer specifies the type of the elements and the number of
elements required by an array as follows:
type arrayName [ arraySize ];
This is called a single-dimension array. The arraySize must be an integer constant greater than zero
and type can be any valid C++ data type. For example, to declare a 4-element array called age of type
int, use this statement:
int age[4];

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
}

for(j=0; j<4; j++) //display 4 ages


cout << “You entered “ << age[j] <<
endl; return 0;
}

Here’s a sample interaction with the program in example 3

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

8.5 Accessing elements of array?


In any point of a program in which an array is visible, we can access the value of any of its elements
individually as if it was a normal variable, thus being able to both read and modify its value.
array_name [index];
Following the previous examples in which “age” had 4 elements and each of those elements was of
type int, the name which we can use to refer to each element is the following:
age [0] age [1] age[2] age [3]

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;
}

Here’s some sample interaction with sales:

8.6 Copying arrays


Suppose that after filling our 4 element array with values, we need to copy that array to another array
of 4 int.
Example 5
#include <iostream>
using namespace std;
int main()
{
int age[4];
int same_age[4];

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.

8.7 Dealing with characters using arrays


You can also store characters and other type data (float etc.) in the arrays. Just declare it as we’ve done
in the case with int. There is no difference in dealing with characters except you’ve to enclose the value
in a single quote.
Char ar [3];
ar [0] = ’a’; ar [1] = ’b’…..

8.8 Practice problems


8.8.1 Practice problem 1:

Write a C++ Program for Three Dimensional Array using arrays


#include<iostream>
using namespace std;
int main()
{
int arr[3][4][2] = {
{
{2, 4},
{7, 8},
{3, 4},
{5, 6}
},

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;
}

8.8.2 Practice problem 2:


C++ program to remove characters in string except alphabets
#include <iostream>
using namespace std;

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

You might also like