Laboratory 7 - Single-D Array PDF
Laboratory 7 - Single-D Array PDF
Laboratory Exercise # 7
Single-Dimension Array
Student Name
(LN, FN MI)
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:
Materials:
1 computer set
C ++ IDE
Introduction:
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].
As you can with simple variable, you can use either an assignment statement
or the extraction operator to enter data into an array element.
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.
Syntax:
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
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:
Laboratory Task:
Develop a program that will sort the elements of a single dimension array.
Program Requirements:
Sorting Arrays
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)
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)