BMS 201
Arrays and
Functions
ARRAYS IN C++
In other sciences and especially mathematics
an array an arrangement of a number of items
into an ordered matrix i.e. into rows and
columns which has the respective positions
described using subscripts. In computer
programming, an array is a collection of
memory locations that are related by the fact
that they all have the same name and type
DECLARATION OF ARRAYS
The general format of declaring an array is as follows:
array-type array-name number-of-elements;
Example 1: int BMS201Marks [45];
The array’s name is BMS201Marks and the array
contains 45 elements of type integer.
In order to refer to a particular element in the array, the
array name is specified followed by the position
number (expressed as a subscript) of the element in the
array. The array elements are counted from zero (0),
thus elements of an array having n elements are
counted as follows: 0, 1, 2, 3… (n-1).
Example 2:
// This program uses an integer array
#include<iostream.h>
int main ( )
{
int arrayExample[10];
int i;
for (i=0; i<10; i++)
{
cout<< “Value for arrayExample[“<<i<<”]: ” ;
cin>>arrayExample[i];
}
for(i=0; i<10; i++)
cout<<i<<“:”<<arrayExample[i]<<endl;
return 0;
}
Explanation:
The array is declared in the statement int
arrayExample[10]; The array
stores ten integer values
The first for loop counts the array
elements from 0 through 9.
The statement cout<< “Value for
arrayExample[“<<i<<”]: ” ; prompts
the user to enter .an integer value
The values entered are stored in their
respective positions within the array as shown in figure(i) below:
0
1
2
3
4
5
6
7
8
9
Display of integers as entered by the user
The second for loop outputs the integer values entered by the user. The output is:
0:1
1:2
2:3
3:4
4:5
5:6
6:7
7:8
8:9
9:10
INITIALIZATION OF ARRAYS
An array can be initialized by specifying the
type, name, subscript, assignment operator,
opening brace, elements (separated by
commas), closing brace, and semi-colon.
Example 3
int studentMarks [7] = {75, 60, 80, 68, 78,
82, 71};
This array has seven elements of integer
type.
MULTIDIMENSIONAL ARRAYS
Arrays can have more than one dimensions and
each dimension is represented as a subscript in the
array. The multi-dimensional arrays can also be
initialized.
Example 4
int MyArray[6][4];
This array reserves a memory space for 6 rows and
4 columns, which hold data of integer type. The
rows are numbered 0,1,2,3,4,5 whereas the
columns are numbered 0,1,2,3. The first four
elements to be entered go into the row(0), and the
last four elements go into row(5).
Example 5
// This program demonstrates how a multidimensional array is declared
#include<iostream.h>
int main ( )
{
int arrayExample[6][4] ={{1,2,3,4}, {1,2,3,4}, {1,2,3,4}, {1,2,3,4},
{1,2,3,4}, {1,2,3,4}};
for (int i=0; i<6; i++)
{
for (int j=0; j<4; j++)
{
cout<<"arrayExample ["<<i<<"]["<<j<<"]: "<<arrayExample[i]
[j]<<endl;
cin.get ( )
}
}
return 0;
}
Explanation:
The initialized arrayExample array has 24 integer
values
The first for loop helps to read through the different
rows whereas the second
for loop helps in reading through the different columns
The cin.get ( ) command is a function call which reads
in input and expects the user to hit the return key. This
command keeps the window from closing because the
program is not done yet because it waits for the hitting
of the enter key. Inclusion of that line gives time to the
programmer to see running of the program.
The output
1, 2, 3, 4
1, 2, 3, 4
1, 2, 3, 4
1, 2, 3, 4
1, 2, 3, 4
1, 2, 3, 4
REVISION EXERCISE
By using a suitable C++ program segment,
show how you can:-
Enable the entry of the elements of a 5 x 3
matrix from the keyboard
Display the elements of the 5 x 3 matrix on a
computer screen
Demonstrate how you can initialize a 3 x 2
array
FUNCTIONS IN C++ LANGUAGE
A function is a block of code that performs a service.
Every C++ program has at least one function, namely;
the main function. This is the function that is
automatically called when a C++ program starts.
The Operating System (OS) installed in the computer
invokes the main () function to start a C++ program
whereas other functions are called from the main ()
function or from one another under the control of the
main () function. A C++ program is executed line by line
in the order it appears in the source code, until a function
is called. Then the program branches off to execute the
function. When the function completes the execution, it
returns control to the next line in the calling function.
Functions either return a value or void (i.e. return
nothing). For instance, a function that;
Adds two integers might return the sum, and thus would
be defined as returning an integer value
Prints a message has nothing to return, hence it would be
declared void.
A well written function performs a specific task only and
then returns. It is advisable to break the complicated
tasks into multiple functions, so that each can be called
in turn. This makes the code easier to understand and
maintain.
A well written function performs a specific task only
and then returns. It is advisable to break the
complicated tasks into multiple functions, so that each
can be called in turn. This makes the code easier to
understand and maintain.
DECLARATION AND DEFINITION OF
FUNCTIONS
A function must first be declared and defined before use.
This provides the function’s return type, name, and
parameters to the compiler. The declaration of a function is
referred to as its prototype. The function prototype is a
statement, hence it ends with a semi-colon
Functions consist of a header and a body. The header
consists of the return type, function name and the
parameters to that function. A parameter is a declaration of
what type of value will be passed in. The parameters to a
function allow values to be passed into the function.
int Product (int x, int y);
The actual value passed in by the calling function is
known as argument. The function prototype and
function definition must agree exactly about the return
type, name, and parameter list.
Example 1: Demonstration of a Function Declaration and Definition
// Demonstration about the declaration and definition of a function
#include<iostream.h>
// Declaration of a function prototype
int FindCost(int quantity, int price); this is a function prototype
int main ( )
{
int quantityOfItems;
int priceOfItems;
int costOfItems;
// Entry of values
cout<<“ Enter the quantity of items”;
cin>> quantityOfItems;
cout<<“ Enter the price of items”;
cin>> priceOfItems;
costOfItems = FindCost (quantityOfItems, priceOfItems); a function header
cout<< “\nThe total cost of all items is ”;
cout<< costOfItems;
return 0;
}
// Definition of a function !!
int FindCost(int q, int p) this is a function definition
{
return q*p;
}
In order to return a value from a function, the keyword
return is written followed by the value to be returned. The
following are return statements are all legal:
return 5;
return (y > 12);
return (MyFunction());
The value in the second return statement will be zero if y is
not greater than 12. The value returned in this case is the
value of the expression (i.e. false or true) but not the value
of y. When the return keyword is executed, the expression
following return is returned as the value of the function.