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



Download 40.72 Kb.
Date05.08.2017
Size40.72 Kb.
#26653
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


  1. 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:

(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:




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


  1. 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.)



  1. 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.


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


  1. 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 in original order

2 6 4 8 10 12 89 68 45 37

Data items in ascending order

2 4 6 8 10 12 37 45 68 89




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


  1. 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.



Download 40.72 Kb.

Share with your friends:




The database is protected by copyright ©ininet.org 2024
send message

    Main page