CSC 202 Session 6
CSC 202 Session 6
ARRAYS
When multiple data items with common
characteristics (such as the same name and data type)
are to be processed then Arrays could be used.
For instance, a set of numerical data: x1 + x2 + … xn can
be processed with an array
Each individual data item of an array can be referred to
by specifying the array name followed by the
subscripts, with each subscript enclosed in square
brackets . e.g. X[0], x[1], x[2]…x[n-1] etc.
The number of subscripts of an array determines the
dimensionality of the array e.g. :
One dimensional as in x[i]
Two dimensional as in x[i][j]
Three dimensional as in x[i][j][k]
DEFINING AN ARRAY
The syntax for defining an array is:
Data_type array_name [array size]
Examples are:
int age[50];
char name[25] ;
float result[25];
Values can be assigned to arrays from the declaration
point as in:
int age[10] = (15, 27, 23, 14, 22, 34, 27,2 8, 29, 31);
age[0] = 15; digits[1] = 27; digits[2] = 23 ….
int main ()
{
int n, Grade[50], count ;
cout << "How many numbers have you?"<< endl ;
cin >> n;
for (count = 0; count < n; ++count)
{
cout << "supply your value "<< count + 1<<endl;
cin >> Grade[count] ;
}
for (count = 0; count < n; ++count)
cout << "your values are "<< count + 1 <<":"<< Grade[count] <<endl;
}
SORTING AN ARRAY <<
#include <iostream>
#include <iomanip>
using namespace std; int main(void)
{ int a[10], i=0, j=0, n, t;
cout<< "\n Enter the no. of elements: "<<endl;
cin >> n;
//A. DETERMINING THE NUMBER OF ELEMENTS IN THE ARRAY
for (i = 0; i <n; i++) {
cout<<"\n Enter element : " << (i+1)<< endl;
cin >> a[i]; }
//B. SORTING THE ARRAY
for (j=0 ; j<n; ++ j) {
for (i=j+1 ; i<n ;++ i) {
if (a[j] > a[i]) {
t = a[j];
a[j] = a[i ];
a[i] = t; } }}
//C. DISPLAYING CONTENTS IN ASCENDING ORDER
cout<< "\n Ascending order: "<< "\n";
for (i=0 ; i<n ; ++i) {
cout<< "element" << i+1 << setw(4) << a[i] << "\n"; }
//D. DISPLAYING CONTENTS IN DESCENDING ORDER
cout<< "\n \nDescending order: " << "\n";
for (i=n ; i>0 ; i--) {
cout<< "element" << i+1 << setw (4) << a[i-1] << "\n"; }
return 0; }
MULTIDIMENSIONAL ARRAYS
A multidimensional array is an array with two or more
dimensions.
It could be of two dimensions as in A[m][n], three
dimensions as in A[m][n][0] etc
Multidimensional arrays are defined in a similar way as
one-dimensional arrays
data-type array[expression_1][expression_2] . . .
[ expression_n];
A m x n two-dimensional array can be referred to as a
table of values with m rows and n columns
A three-dimensional array can be visualized as a set of
tables
MULTIDIMENSIONAL ARRAYS
For example: float scores [50][50];
double records[100][66][255];
Elements of a multidimensional arrays are assigned on
a row by row basis
For a two-dimensional array, the elements of the first
row will be assigned, followed by the elements of the
second row, and so on.
Int values[3][4] = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
The natural order in which the initial values are
assigned can be altered by forming groups of initial
values enclosed within braces
However, the number of values within each pair of
braces cannot exceed the defined row size.
MULTIDIMENSIONAL ARRAYS
If the number of rows or columns defined is more
than the initial values assigned then the
remaining rows and columns will be filled with
zero as in:
int values[3][4] = {
{a, b, c},
{d, e, f},
{g, h, i},
},
int values[3][4] = {1, 2, 3, 4, 5, 6,7 , 8 };
LOADING A TWO-DIMENSIONAL ARRAY
Values can be read into Array M x N by first reading values into
the first row, then the next row until all the rows are filled up
#include <stdio.h>
int a[10][10], rows, cols, i,j;
main()
{
cout<<"How many number of rows ? \n";
cin<< rows;
cout<<"How many number of columns ? \n";
cin <<&cols;
for (i = 0; i < rows; i++)
{
cout<<"supply your values for row : \n \n“<< i;
for (j = 0; j < cols; j++)
{
cout<<" Column : “<< j;
cin <<a[i][j];
} }}
DISPLAYING THE CONTENTS OF A TWO-
DIMENSIONAL ARRAY
// function declaration
void readFirstMatrix();
void printFirstMatrix();
void readSecondMatrix();
void printSecondMatrix();
void sumMatrix();
void subtractMatrix();