CS (151) - INTRODUCTION TO PROGRAMMING C++
Teacher: Dr. Shrestha
Fall Semester, 2000
Class hour: 5:00 - 10:00 Friday
Materials for December 1, 2000
*****************************
FUNCTIONS
Save days' work in your diskette !
DECLARING ARRAYS To reserve 12 elements for integer array c, the declaration needed
is: Likewise, Example If there are fewer initializers than elements in the array, the
remaining elements are automatically initialized to zero. Example Example PASSING ARRAYS TO FUNCTIONS To pass an array argument to a function, specify the name of the array
without any brackets. Example The following program first prints the five elements of integer array
a. Next, a and its size are passed to function
modifyArray where each of a's elements is multiplied by 2.
Then a is reprinted in main. Next, the program prints the value of a[3] and passes it to
function modifyElement which mulitplies its argument by 2 and
prints the new value. Note that when a[3] is reprinted in
main, it has not been modified because individual array elements
are passed call-by-value. Example SORTING ARRAYS Sorting data is one of the most important computing applications. The following program sorts the values of the ten element array
a into ascending order. The technique we use is called BUBBLE SORT
or the SINKING SORT because the smaller values gradually "bubble" their
way upward to the top of the array like air bubbles rising in water,
while the large values sink to the bottom of the array. The technique is
to make several passes through the array. One each pass, successive pairs
of elements are compared. If a pair is in increasing order or the values
are identical we leave the values as they are. If a pair is in decreasing
order, their values are swapped in the array. Example First the program compares a[ 0 ] to a[ 1 ], then
a[ 1 ] to a[ 2 ], then a[ 2 ] to a[ 3 ],
and so on until it completes the pass by comparing a[ 8 ] to
a[ 9]. Although there are 10 elements, only nine comparisons
are performed. Because of the way the successive comparisons are
made, a large value may move down the array many positions on a
single pass, but a small value may move up only one position. On the
first pass, the largest value is guaranteed to sink to the bottom
element of the array, a[ 9 ]. On the second pass, the second
largest value is guaranteed to sink to a[ 8 ]. On the ninth
pass, the ninth largest value sinks to a[ 1 ]. This leaves
the smallest value in a[ 0 ], so only nine passes are needed
to sort a 10-element array.
MULTIPLE-SUBSCRIPTED ARRAYS A common use of multiple-subscripted arrays is to represent tables of
values consisting of rows and columns. To identify an element, we have
to specify two subscripts: the first identifies the element's row and the
second identifies the element's column. Example The following example demonstrates initializing double-subscripted
arrays in declarations. the program declares 3 arrays, each with 2 rows
and 3 columns. The declaration of array1 provides 6 initializers
in two sublists. The first sublist initializes the first row of array to
the values 1, 2, and 3; and the second sublist initializes the second row
of the array to the values 4, 5, and 6. If the braces around each sublist
are removed from the array1 initializer list, the compiler
automatically initializes the elements of the first row foloowed by the
elements of the second row. The declaration of array2 provides 5 initializers. The
initializers are assigned to the first row then the second row. Any
elements that do not have an explicit initializer are initialized to zero
automatically. Example int *countPtr, count;
int c[ 12 ];
int b[ 100 ], x[ 25 ];
reserves 100 elements for the integer array b and 25 elements for
the integer array x
//Initializing an array
#include <iostream.h>
#include <iomanip.h>
int main()
{
int i, n[ 10 ];
for ( i = 0; i < 10; i++ )
n[ i ] = 0;
//This initializes the array.
cout << "Element" << setw( 13 )
<< "Value" << endl;
for ( i = 0; i < 10; i++ )
cout << setw( 7 ) << i
<< setw( 13 ) << n[ i ] << endl;
return 0;
}
int n[ 10 ] = { 0 };
explicitly initializes the first element to zero and implicitly
initializes the remaining nine elements to zero because there are fewer
initializers than elements in the array.
//Histogram printing program
#include <iostream.h>
#include <iomanip.h>
int main()
{
const int arraySize = 10;
int n[ arraySize ] = {19,3,15,7,11,9,13,5,17,1};
cout << "Element" << setw( 13 ) << "Value"
<< setw( 17 ) << "Histogram" << endl;
for ( int i =1; i < arraySize; i++ ) {
cout << setw( 7 ) << i << setw( 13 )
<< n[ i ] << setw( 9 );
for ( int j = 0; j < n[ i ]; j++ )
cout << '*';
cout << endl;
}
return 0;
}
For example, if array hourlyTemperatures has been declared as
int hourlyTemperatures[ 24 ];
the function call statement
modifyArray( hourlyTemperatures, 24 );
passes array hourlyTemperatures
and its size to function
modifyArray
// Passing arrays and individual array
// elements to functions
#inlcude <iostream.h>
#include <iomanip.h>
void modifyArray( int [], int );
void modifyElement( int );
int main()
{
const int arraySize = 5;
int i, a[ arraySize ] = { 0, 1, 2, 3, 4 };
cout << "Effects of passing entire array
call by reference:"
<< "\n\nThe values of the original
array are:\n";
for ( i = 0; i < arraySize; i++ )
cout << setw( 3 ) << a[ i ];
cout << endl;
// array a passed call-by-reference
modifyArray( a, arraySize );
cout << "The values of the modified array are:\n";
for ( i = 0; i < arraySize; i++ )
cout << setw( 3 ) << a[ i ];
cout << "\n\n\n"
<< "Effects of passing array element
call-by-value:"
<< "\n\nThe value of a[3] is "
<< a[3] << '\n';
modifyElement( a[ 3 ] );
cout << "The value of a[3] is "
<< a[3] ,, endl;
return 0;
}
void modifyArray( int b[], int sizeOfArray )
{
for ( int j = 0; j < sizeOfArray; j++ )
b[ j ] *= 2;
}
void modifyElement( int e )
{
cout << "Value in modifyElement is "
<< ( e *= 2 ) << endl;
}
//This program sorts an array's values into
//ascending order.
#include <iostream.h>
#include <iomanip.h>
int main()
{
const int arraySize = 10;
int a[ arraySize ] = {2,6,4,8,10,12,89,68,45,37};
int i, hold;
cout << "Data items in original order\n";
for ( i = 0; i < arraySize; i++ )
cout << setw( 4 ) << a[ i ];
for ( int pass = 0; pass < arraySize - 1; pass++ )
for ( i = 0; i < arraySize - 1; i++ ) //one pass
if ( a[ i ] > a[ i + 1 ] ) { // one comparison
hold = a[ i ]; // one swap
a[ i ] = a[ i + 1 ];
a[ i + 1 ] = hold;
}
cout << "\nData items in ascending order\n";
for ( i = 0; i < arraySize; i++ )
cout << setw( 4 ) << a[ i ];
cout << endl;
return 0;
}
//Initializing multidimensional arrays
#include <iostream.h>
void printArray( int [][ 3 ] );
int main()
{
int array1[ 2 ][ 3 ] = { {1,2,3}, {4,5,6} },
array2[ 2 ][ 3 ] = { 1, 2, 3, 4, 5 },
array3[ 2 ][ 3 ] = { { 1, 2 }, { 4 } };
cout << "Values in array1 by row are:" << endl;
printArray( array1 );
cout << "Values in array2 by row are:" << endl;
printArray( array2 );
cout << "Values in array3 by row are:" << endl;
printArray( array3 );
return 0;
}
void printArray( int a[][ 3 ] )
{
for ( int i = 0; i < 2; i++ ) {
for ( int j = 0; j < 3; j++ )
cout << a[ i ][ j ] << ' ';
cout << endl;
}
}
THE OUTPUT WILL BE:
Values in array1 by row are:
1 2 3
4 5 6
Values in array2 by row are:
1 2 3
4 5 0
Values in array3 by row are:
1 2 0
4 0 0
**********************************
Pointer variables contain memory addresses as their values. Normally, a
variable directly contains a specific value. A pointer, on the other
hand, contains an address of a variable that contains a specific value. A
variable directly references a value, and a pointer indirectly
references a value. Referencing a value through a pointer is called
indirection.
declares the variable countPtr to be of type int * (i.e., a
pointer to an integer value) and means that countPtr points to an
object of type integer. Variable count is not a pointer but an
integer. Each variable being declared as a pointer must be preceeded by
an asterisk(*). Pointers can be declared to point to objects of any data
type.
THANKS FOR STOPPING BY !
TAKE CARE !
AFTER TODAY'S CLASS IS OVER, LOOK AT THE ASSIGNMENT.