0% found this document useful (0 votes)
130 views9 pages

Laboratory 7 - Single-D Array PDF

The document provides instructions for a laboratory exercise on sorting elements of a single-dimension array. Students are asked to develop a program that inputs elements into an array, sorts the array in ascending and descending order, and identifies the highest and lowest elements. The program output displays the sorted array in both orders and prints the highest and lowest values. Students are then asked questions to test their understanding of arrays and sorting algorithms.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
130 views9 pages

Laboratory 7 - Single-D Array PDF

The document provides instructions for a laboratory exercise on sorting elements of a single-dimension array. Students are asked to develop a program that inputs elements into an array, sorts the array in ascending and descending order, and identifies the highest and lowest elements. The program output displays the sorted array in both orders and prints the highest and lowest values. Students are then asked questions to test their understanding of arrays and sorting algorithms.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

The UNIVERSITY of MINDANAO

College of Engineering Education

Programming Logic and Design

Laboratory Exercise # 7

Single-Dimension Array

Student Name
(LN, FN MI)

Laboratory Rm No. Subject Code

Subject Teacher

Date Submitted

Score
Laboratory Exercise # 7

Single-Dimension Array
Objective:

At the conclusion of this laboratory exercise, the student should be able to:

1. input elements to an array with the use of a looping structure


2. design and implement an algorithm that sorts elements of an array in
ascending and descending order
3. design and implement an algorithm that can identify the highest and
smallest elements of an array

Materials:

1 computer set

C ++ IDE

Introduction:

An array is a collection of variables of the same type and can be referred to


by a common name. An example of which is the scores for the quizzes of students.
The use of array offers a convenient way of creating list of related variables. There
can only be one declaration for the list of values related to each other giving us the
advantage of not declaring multiple variable names.

There are different dimensions to be used in array. It may have from one to
several dimensions but the commonly used are the one-dimensional array and two-
dimensional array. A one-dimensional array is coded as:
variable_type name[size];

The type refers to the data type for the elements making up the array. Names
refers to the array name similar to that of the variable name. Additional feature is
the size of the array. The size determines the number of elements that the array
can hold. The size also determines the index of the individual element. Example
declaration of an array can be:

int varone[10];

Given the declaration above we can say that the array holds 10 elements.
Each element of an array is accessed through its index. The index is used to describe
the position of an element within the given array. It starts with zero index indicating
the first element. From the given we can say that varone has index values from 0
to 9. Accessing a particular index is coded as varone[0] which is the first element of
the array. Varone has elements from varone[0] through varone[9].

Entering Data into a One-Dimensional Array

As you can with simple variable, you can use either an assignment statement
or the extraction operator to enter data into an array element.

How to use assignment statement to assign data to a one-dimensional array?

Syntax

arrayname[subscript] = expression;

Example 1:
char letters[3]; //declares a three element array
letter[1] = ‘Y’; //assigns the letter Y to the second element
// in the letter array

Example 2:

int numbers[6];
int x;
for(x = 1; x <= 6; x++
numbers[x – 1] = pow(x,2); // end of loop
// assigns the squares of the numbers from 1 through 6 to the
six-element
// numbers array

Example 3:

int x, increase = 0;
int numbers[6];
cout << “enter increase amount : “;
cin >> increase;
for( x = 0; x < 6; x++)
numbers[x] = numbers + increase; // end of loop

Assigns to each element in the six-element numbers array, the sum of the
element’s current value plus the value stored in the increase variable.

How to use the Extraction Operator to store data in a One-Dimensional Array

Syntax:

cin >> arrayName[subscript];

Example 1:

char letters[5];
cin >> letters[0]; // stores the user’s entry in the first element
// in the letters array
Example 2:

int sub;
int sales[4];
for (sub = 0; sub < 4; sub++)
{
cout << “ Enter the sales for Region “;
cout << sub + 1 << ”: “;
cin >> sales[sub];
} // end of for loop
//stores the user’s entries in the four-element sales array

Displaying the contents of a one-dimensional array

To display the contents of an array, you need to access each of its elements.
You do this using a loop along with a counter variable that keeps track of each
subscript in the array.

Example 1:

int x = 0;
char letters[3];
x = 0;
while (x < 3)
{
cout << letters[x] << endl;
x++;
} // end of while loop
// displays the contents of the three element letters array

Example 2:

int sub, int sales[4];


for(sub = 0; sub < 4; sub++)
{
cout << “Sales for Region “ << sub + 1 << “: $”;
cout << sales[sub] << endl;
} // end of for loop
// displays the contents of the four-element sales array

Laboratory Task:

Develop a program that will sort the elements of a single dimension array.

Program Requirements:

1. Declare a single dimension array of size n


2. Input n elements to a single dimension array
3. Sort the elements of an array from highest to lowest and vice versa
4. Also, the program will identify and output the highest and lowest integer.

Sorting Arrays

Number of items to sort: 10


Enter elements
10 0 5 13 -10 20 -89 101 96 3
Lowest -> Highest
-89 -10 0 3 5 10 13 20 96 101
Highest -> Lowest
101 96 20 13 10 5 3 0 -10 -89
Highest = 101 Lowest = -8

Figure 6.1 Sample Output of Sorting inputs


Questions:

1. What is the algorithm used to sort elements of array in ascending and


descending order?
Answer: ______________________________________________________
_____________________________________________________________
_____________________________________________________________
_____________________________________________________________
_____________________________________________________________
_____________________________________________________________

2. How many integer arrays were used in this program?


Answer: ______________________________________________________
_____________________________________________________________
_____________________________________________________________
_____________________________________________________________
_____________________________________________________________
_____________________________________________________________

3. Can this program be done with only 1 integer array being used?
Explain your answer.
Answer: ______________________________________________________
_____________________________________________________________
_____________________________________________________________
_____________________________________________________________
_____________________________________________________________
_____________________________________________________________
4. Explain how was the program able to determine the highest and lowest
element.
Answer: ______________________________________________________
_____________________________________________________________
_____________________________________________________________
_____________________________________________________________
_____________________________________________________________
_____________________________________________________________

Observations:

___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
__________________________________________________________
__________________________________________________________
Scoring Rubric
Ratings
Parameters
3 2 1

Source code contains the Source code lacks Source code is not
Specifications complete details to run necessary details to enough to run the
(30%) the program correctly. run the program program correctly.
(30) correctly.
(20) (10)

Syntax Source code contains no Source code contains Source code contains
(30%) syntax error. 1 to 5 syntax errors. more than 5 syntax
(20) errors.
(30) (10)

Source code is well Minor issues such as Major issues are


Readability organized and easy to variable naming, causing the codes to
(10%) understand. variable utilization, be not readable.
etc. are observed. (6)
(10) (3)

Screen Output Source code is well Source code allows Source code does not
(10%) organized and easy to the required screen meet the required
understand. output to be displayed screen output to be
correctly with 1-3 displayed correctly.
errors found.
(6) (3)
(10)

The documentation is The documentation The documentation


Documentation well written and clearly lacks some didn’t satisfy all the
(20%) explained all the information and some questions that were
questions given in the mistakes are found in given in the laboratory
laboratory exercise. the questions. exercise.
(14)
(20) (7)

You might also like