lab 7: array

advertisement
LAB 7: ARRAY
The C++ language provides a capability that enables the user to define a set of ordered data items
known as an array. An array is a number of items arranged in some specified way - for example, in
a list or in a three-dimensional table. In computer programming languages, an array is a group of
objects with the same attributes that can be addressed individually.
LAB SHEET 7.1 : Array Basic
ESTIMATED TIME
:
2 hours
OBJECTIVE
:
To use the array data structure to represent a set of related data
items.
To initialize arrays and refer to the individual elements of arrays.
To use array of strings.
SOFTWARE
:
Microsoft Visual Studio 6 ( C++)
PROCEDURE
:
Read all the questions and write the program. Then run the
program and get the correct output.
CONCLUSION
:
Once completing the exercise, student should be able to apply
array in their program in order to represent a set of data.
DISCUSSION / RESULT / EXERCISE
a) Write the output of the following c++ code fragment.
(i)
int i;
int values[ 10 ] = { 4, 1, 1, 3, 4, 9, 9, 2, 1, 7 };
cout << "Element" << “
“ << "Value" << endl;
for ( i = 0; i < 10; i++ )
cout << i << “ “ << values[ i ] << endl;
Output:
1
(ii)
char string1[] = "How are you?";
cout << "string1 is: " << string1 << endl;
for ( int i = 0; string1[ i ] != '\0'; i++ )
cout << string1[ i ] << "_";
Output:
(iii)
const int arraySize = 10;
int a[ arraySize ] = { 4, 3, 7, 1, 13, 6, 0, 2, 9, 5 };
for ( int i = 0; i < arraySize; i++ )
{
for ( int j = 0; j < a [ i ]; j++ )
cout << "*";
cout << endl; }
Output:
2
LAB SHEET 7.2: Declare Arrays
ESTIMATED TIME
:
4 hours
OBJECTIVE
:
To declare arrays, initialize arrays and refer to the
Individual elements of arrays.
To effectively process an array using loop statements.
SOFTWARE
:
Microsoft Visual Studio 6 ( C++)
PROCEDURE
:
Read all the questions and write the program. Then run the program
and get the correct output.
CONCLUSION
:
Once completing the exercise, student should be able to understand
the array purpose and effectively implementing the array in the
program.
DISCUSSION / RESULT / EXERCISE
a) Write a program that copies each value stored in one array to the corresponding element of
another array. (for example, if the arrays are InArray and OutArray, copy InArray[0] to
OutArray[0] , then copy InArray[1] to OutArray[1], and so on.)
3
b) A common operation is to determine the minimum or the maximum value stored in an array.
Write a program that declare and initialize an array named score as follows.
int score[ 10 ] = { 4, 1, 5, 3, 4, 10, 9, 2, 1, 7 };
Find the maximum value in array score. An algorithm for finding the maximum value is as
follows:1. Assume that the first element is the largest so far and save its subscript as the subscript
of the largest so far.
2. For each array element do
If the current element is > than the largest so far then
save the subscript of the current element as the subscript of the largest so
far.
4
LAB SHEET 7.3: Sorting an Arrays
ESTIMATED TIME
:
1.5 hours
OBJECTIVE
:
Basic searching and sorting techniques.
SOFTWARE
:
Microsoft Visual Studio 6 ( C++)
PROCEDURE
:
Read all the questions and write the program. Then run the program and
get the correct output.
CONCLUSION
:
Once completing the exercise, student should be able to write a
program that could search and sorting the data in the array.
DISCUSSION / RESULT / EXERCISE
a) In the bubble sort algorithm, smaller values gradually “bubble” their way upward to the top of
the array like air bubbles rising in water, while the larger values sink to the bottom. The
bubble sort makes several passes through the array. On 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.
Write a program that sorts an array of 10 integers using bubble sort.
Data items
2 6 4 8 10
Data items
2 4 6 8 10
in
12
in
12
original order
89 68 45 37
ascending order
37 45 68 89
5
LAB SHEET 7.4: Multidimensional Arrays
ESTIMATED TIME
:
1.5 hours
OBJECTIVE
:
To declare and manipulate multidimensional arrays.
SOFTWARE
:
Microsoft Visual Studio 6 ( C++)
PROCEDURE
:
Read all the questions and write the program. Then run the
program and get the correct output.
CONCLUSION
:
Upon completing the exercise, student should be able to write a
program that require them to apply the multidimensional array.
DISCUSSION / RESULT / EXERCISE
a) Write a program to add, subtract and multiply a 2 x 2 matrix with another 2 x 2 matrix and
storing their result in yet another 2 x 2 matrix. All the matrices should be identified except the
one storing the results.
6
Download