Académique Documents
Professionnel Documents
Culture Documents
Lecture Note
The program uses five variables with five different identifiers at three different places. Try to solve the
same problem with a hundred numbers (I will not do that if I have to use 100 variables with 100
identifiers).
A loop statement is not used in this program because data have to be kept after it is being read. The
process for these numbers is done after all numbers have been obtained from the user. Using a loop
will allow the use of only one variable with one identifier. When the next loop is executed, the previous
number or value will be replaced with a new number.
On the other hand, a loop statement is in favor if an identifier is used to represent all numbers. The
variable, which uses an identifier for a group of memory locations, is known as an array. The following
example program uses an array to display the five numbers in a reverse order.
void main()
{ int no[5], i;
cout<<"Enter 5 numbers : ";
for(i=0; i<5; i++)
{
cin >> no[i];
}
cout<<"The numbers in the reverse order is : "
for(i=4; i>=0; i--)
{
cout << no[i] <<" ";
}
}
1
CSC138
Lecture Note
The program uses an array of five elements name no. Elements can also be referred to as location.
The numbers that the user inputs are inserted or stored into elements of an array one by one in the
first loop structure.
Each element of the array is referred to using an index. The value of index changes at every loop, but
the name of the array does not changed. After the execution of a loop, the value i is increased or
decreased. It reflects the element, which will be referred at the following loop.
Try to solve the problem using one hundred numbers. (Tips: only 3 statements in program, which will
be replaced).
dataType arraName[intExp];
where intExp is any constant expression that evaluates to a positive integer. Also, intExp
specifies the number of components or elements in the array.
For example, the statement:
float number[5];
declares an array marks of five components. Each component is of type float. The components
are number[0], number[1], number[2], number[3], and number[4]. The following
diagram illustrates the array number.
number[0]
number[1]
number[2]
number[3]
number[4]
When declaring an array, a programmer has to ensure that the size of an array is enough to process
the required data.
A constant value may be used in declaration of an array as the following example.
CSC138
Lecture Note
arraName[indexExp]
where indexExp, called the index, is any expression whose value is a non negative integer. The
index value specifies the position of the component in the array. In C++, the array index starts at 0.
Assume the following array is used.
list[1]
32
list[2]
45
list[3]
67
list[4]
77
list[5]
98
An index (subscript) is used to access the value of elements in an array. list[0] is read as list
sub 0 or list index 0. list[0] refers to the value of the first item in the list or the first element in
the list or the first member in the list or the first location in the list.
The eight element is referred as list[7], not list[8] because index start from 0. list[8]
does not exist.
Sample statements, which are used to manipulate the element of this array:
Statements
Actions
Display the value of list sub 2, which
is 45.
Replace the value in list[3] with 65.
Store the sum of list sub 0 and list
sub 1 to variable sum
Add value of list sub 2 to sum
Store the sum of list sub 2 and list
sub 1 to list sub 4.
Display all value of elements in list
cout<< list[2];
list[3] = 65;
sum = list[0] + list[1];
sum = sum + list[2];
list[4] = list[2] + list[1];
for(i=0; i<6; i++)
cout<<list[i] << ;
CSC138
Lecture Note
double sales[10];
int ind;
double highestSale, sum, average;
The first statement declares an array sales of 10 components, with each component being of type
double. The meaning of the other statements is clear.
a) Initializing an array: The following loop initialize every component of the array sales to 0.0.
CSC138
Lecture Note
c) Printing an array: The following loop outputs the array sales. For simplicity, we assume that
the output goes to the screen.
sum = 0;
for(ind = 0; ind < size; ind++)
sum = sum + number[ind];
average = sum / size;
cout<< Sum of all numbers : <<sum<<endl;
cout<< The average is : <<average<<endl;
b) Highest element in the array:
highest = 0;
for(ind = 0; ind < size; ind++)
{
if(number[ind] > highest)
highest = number[ind];
}
cout<< The highest value in array is : <<highest<<endl;
OR
highest = number[0];
for(ind = 1; ind < size; ind++)
{
if(number[ind] > highest)
highest = number[ind];
}
cout<< The highest value in array is : <<highest<<endl;
CSC138
Lecture Note
lowest = 9999;
for(ind = 0; ind < size; ind++)
{
if(number[ind] < lowest)
lowest = number[ind];
}
cout<< The lowest value in array is : <<lowest<<endl;
OR
lowest = number[0];
for(ind = 1; ind < size; ind++)
{
if(number[ind] < lowest)
lowest = number[ind];
}
cout<< The lowest value in array is : <<lowest<<endl;
d) Count the Occurrence: For example, count the number of odd numbers in the array.
countOdd = 0;
for(ind = 0; ind < size; ind++)
{
if(number[ind] % 2 == 1 )
countOdd = countOdd + 1; //or countOdd++
}
cout<< The number of odd numbers : <<countOdd<<endl;
e) Searching (Linear/Sequential Search)
Lets start with the problem statement for searching:
Given a value X, return the index of X in the array, if such X exists. Otherwise, return
NOT_FOUND (-1). We assume there are no duplicate entries in the array.
There are two possible outcomes in searching: Either we locate an X or we dont. We will call
the first a successful search and the latter an unsuccessful search. Figure below illustrates the
successful and unsuccessful searches. An obvious as this may sound, it is critical to
differentiate the two because it is possible for one searching algorithm to perform superbly for
successful searches, but very poorly for unsuccessful searches. When we analyze the
performance of a searching algorithm, we normally derive two separate performances, one for
a successful search and another for an unsuccessful search.
Unsuccessful search:
Successful search:
search(12) -- 4
number
0
23
17
90
12
44
38
84
77
CSC138
Lecture Note
In a linear search we search the array from the first position to the last position in a linear
progression. The linear search is also called a sequential search. The linear search algorithm
can be expressed as:
int loc = 0;
int searchValue;
cout<< Enter the search value : ;
cin>>searchValue;
while (loc < size && number[loc] != searchValue)
{
loc++;
}
if(loc == size)
cout<< Sorry! The value not found. ;
else
cout<< The value stored at index : <<loc;
OR
f)
CSC138
Lecture Note
23
17
90
12
44
38
84
77
90
12
44
38
84
77
23
90
12
44
38
84
77
23
90
12
44
38
84
77
Exchange
0
17
23
2
5
Exchange
0
17
1
5
0
17
1
5
Exchange
0
17
1
5
23
12
90
44
38
84
77
38
84
77
84
77
Exchange
0
17
1
5
23
12
44
5
90
Exchange
0
17
1
5
23
12
44
38
90
Exchange
0
17
1
5
23
12
44
38
84
90
77
Exchange
0
17
1
5
23
12
44
38
84
77
90
CSC138
Lecture Note
Using these properties, we can express the bubbleSort in the following algorithm:
int bottom, temp;
bool exchange = true;
bottom = size 2;
while(exchange)
{
exchange = false;
for(ind = 0; ind <= bottom; ind++)
{
if(number[ind] > number[ind+1])
{
temp = number[ind];
number[ind] = number[ind+1];
number[ind+1] = temp;
exchange = true;
}
}
bottom--;
}
for (ind = 0; ind < size; ind++)
cout << number[ind] << " ";
OR
for(int j = 1; j < size; j++)
{
for(int k = 0; k < (size-j); k++)
{
if (number[k] > number[k+1])
{
temp = number[k];
number[k] = number[k+1];
number[k+1] = temp;
}
}
}
for (ind = 0; ind < size; ind++)
cout << number[ind] << " ";
CSC138
Lecture Note
int theArray[20];
it would be enough to write a call like this:
funcArrayAsParam(theArray);
Sometimes, the number of elements in the array might be less than the size of the array. For example,
the number of elements in an array storing student data might increase or decrease as students drops
or add courses. In such situations, we want to process only the components of the array that holds
actual data. To write a function to process such arrays, in addition to declaring an array as a formal
parameter, we declare another formal parameter specifying the number of elements in the array, as in
the following function:
10
CSC138
Lecture Note
In the case above, the first parameter (int list[]) accepts any array whose elements are of type
int, whatever its length. For that reason, we have included a second parameter that tells the function
the length of each array that we pass to its first parameter. This allows the for loop that prints out the
array to know the range to iterate in the array passed, without going out of range.
In certain situation we may need to pass only one or two elements of an array to a function, not the
entire array. Although entire arrays are passed by reference, individual array elements are passed by
value or by reference exactly as simple variables are. To pass an element of an array to a function,
use the subscripted name of the array element as an argument (actual parameter) in the function call
and a variable without subscript as simple variable for formal parameter in the function definition
header.
Consider the following function definition:
void compare(int value1, int value2)
{
if(value1 > value2)
cout<< Highest : <<value1<<endl;
else
cout<< Highest : <<value2<<endl;
}
For example we have an array as follows:
int theArray[10] = {4,7,2,9,12,6,19,32,11,1};
In order to pass the third and fourth elements of that array, we can write a call for the above function
like this:
compare(theArray[2], theArray[3]);
When we pass individual array elements by value to a function, the content of the original array will
remain. If we want the changes made in the function effect the original array elements, we have to use
reference parameter as simple variable. The following example of function definition shows how to use
reference parameter.
void swap(int &value1, int &value2)
{
int temp;
if(value1 > value2)
{
temp = value1;
value1 = value2;
value2 = temp
}
}
We can make a function call statement as follows:
swap(theArray[i], theArray[i+1]);
11