Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

Arrays

An array is a collection of variables of the same type that are referred to


through a common name. A specific element in an array is accessed by an
index.
Array type:
•Single dimensional array
•Two dimensional array
•Multidimensional array

Single-Dimension Arrays
The general form for declaring a single-dimension array is
type var_name[size];
• type declares the base type of the array, which is the type of each element in
the array(ex: int, char, float, double)
• size defines how many elements the array will hold.

For example, the following statement declare a 100-element array called


balance of type double,

double balance[100]; Lec. Sahar Khalid


Accessing Array Elements
An element is accessed by indexing the array name. This is
done by placing the index of the element within square
brackets after the name of the array. For example,
balance[2] = 12.23; Balance[0]
Balance[1]
index 12.23 Balance[2]
assigns the element with index 2 in balance .
with the value 12.23. .
.
.
.
Balance[99]
In C/C++, all arrays have 0 as the index of their first element.
Therefore, when you write
char p[10];
you are declaring a character array that has ten elements, p[0]
through p[9]. Note: The index of the array is from 0 to (size-1)
Lec. Sahar Khalid
the following program loads an integer array with the squares of
numbers from 0 to 99:
#include <iostream>
using namespace std;
int main(void)
{int x[100]; /* this declares a 100-integer array */
int t;
/* load x with the squares of values 0 through 99 */
for(t=0; t<100; ++t)
x[t] = t*t;
/* display contents of x */
for(t=0; t<100; ++t)
cout<<x[t];
return 0;
}
Lec. Sahar Khalid
Single-dimension arrays are stored in contiguous memory locations in index order. For
example, the Figure shows how array appears in memory if it starts at memory
location 1000 and is declared as shown here:

char a[7];

Initializing Arrays
Initializing of array is very simple in c programming. Look at the following C code
which demonstrate the declaration and initialization of an array.

int myArray[5] = {1, 2, 3, 4, 5}; //declare and initialize the array in one statement
int studentAge[4];
studentAge[0]=14;
studentAge[1]=13;
studentAge[2]=15;
studentAge[3]=16;
Lec. Sahar Khalid
Example: the program invites the user to enter a series of six values representing sales for
each day of the week (excluding friday), and then calculates the average of these values.
#include <iostream>
using namespace std;
int main()
{
const int SIZE = 6; //size of array
double sales[SIZE]; //array of 6 variables
cout << “Enter sales for 6 days\n”;
for(int j=0; j<SIZE; j++) //put figures in array
cin >> sales[j];
double total = 0;
for(int 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;
}
• A new detail in this program is the use of a const variable for the array size and loop limits.
This variable is defined at the start of the listing:
const int SIZE = 6;
makes it easier to change the array size:Lec. Sahar Khalid
The example sets the elements of a 10-element array s to the even integers 2, 4, 6, …, 20
and prints the array in tabular format. These numbers are generated by multiplying each
successive value of the loop counter by 2 and adding 2.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{ const int arraySize = 10; // constant variable can be used to specify array size
int s[ arraySize ]; // array s has 10 elements
for ( int i = 0; i < arraySize; ++i ) // set the values
s[ i ] = 2 + 2 * i;
cout << "Element" << setw( 13 ) << "Value" << endl;
for ( int j = 0; j < arraySize; ++j ) // output contents of array s in tabular format
cout << setw( 7 ) << j << setw( 13 ) << s[ j ] << endl;
}

Lec. Sahar Khalid


Two Dimensional Array
A two-dimensional array is, essentially, an array of one-dimensional
arrays. The general form for declaring a two-dimension array is
type var_name[size1][size2];

No. of row No. of coloum


To declare a two-dimensional integer array d of size 3,4 you would write
d[0][0]
d[0][1]
int d[3][4]; d[0][2]
d[0][3]
.
d[1][0]
Index1 Index2
0..2 0..3
.
.
.
.
d[2][3]

Two-dimensional arrays are stored in a row-column matrix, where


the first index indicates the row and the second indicates the
column Lec. Sahar Khalid
consider the following two dimensional array definition
int values [3] [4] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10. 11, 12, };
This definition equivalent to
Values [0] [0] = 1 Values [0] [1] = 2 Values [0] [2] = 3 Values [0] [3] = 4 Values [1] [0] = 5
Values [1] [1] = 6 Values [1] [2] = 7 Values [1] [3] = 8 Values [2] [0] = 9 Values [2] [1] = 10
Values [2] [2] = 11 Values [2] [3] = 12

Here the first subscript stands for the row number and second one for column number.
Alternatively the above definition can be defined and initialized as
int values [3] [4] = { { 1, 2, 3, 4 } { 5, 6, 7, 8 } { 9, 10, 11, 12 } };

Here the values in first pair of braces are initialized to elements of first row, the values of
second pair of inner braces are assigned to second row and so on.

Lec. Sahar Khalid


Example:-write a program that reads a two dimensional array 4*5 and then print it in a
matrix form.

#include <iostream>
using namespace std;
int main(void)
{ int const r=4,c=5;
int i,j, num[r][c];
cout<<"enter 4*5 two dimensional array"<<endl;
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
cin>>num[i][j];
/* now print them out */
for(i=0; i<r; ++i)
{
cout<<endl;
for(j=0; j<c; ++j)
cout<<num[i][j]<<'\t';
}
return 0;
}
Lec. Sahar Khalid
Example:-write a program that reads a two dimensional array 3*4 then find the sum of
each row in the array store the sum in a one dimensional array.
#include <iostream>
using namespace std;
#define r 3
# define c 4
int main(void)
{
int i,j, num[r][c],s[r],sum;
cout<<r<<'*'<<c<<"array";
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
cin>>num[i][j];
for(i=0; i<r; ++i)
{sum=0;
for(j=0; j<c; ++j)
sum+=num[i][j];
s[i]=sum;
}
for(i=0;i<r;i++)
cout<<"sum row "<<i<<'='<<s[i];
return 0; Lec. Sahar Khalid
}
Write a C program that reads a two dimensional float array 3*4. compute the average
value of the array elements. Count how many elements are greater than the average.
#include <iostream>
using namespace std;
int main()
{ const int row=3, col=4;
float arr[row][col],cont=0,i,j;
float sum=0,avr;
cout<<"enter"<<row<<'*'<<col<<"array";
for(i=0;i<row;i++)
for(j=0;j<col;j++)
cin>>arr[i][j];
for(i=0;i<row;i++)
for(j=0;j<col;j++)
sum+=arr[i][j];
avr=sum/(row*col);
cout << "average=" << avr<<endl;
for(i=0;i<row;i++)
for(j=0;j<col;j++)
if(arr[i][j]>avr)
cont++;
cout<<"no. of elements greater than the average are"<<cont;
return 0;
} Lec. Sahar Khalid

You might also like