| Name: _____________________ | Class: Comp 217 |
| SSN/ID: _____________________ | Section & Group: ____________ |
In this lab you will see one dimensional arrays. The objective of this lab is as follows:
NOTE: Exercises 1 and 2 will be marked in the lab.
Here is the syntax template of a one-dimensional array declaration:
DataType ArrayName [ConstIntExpression];In the syntax template, DataType describes what is stored in each component of the array. Array components may be of any type, but for now we limit our discussion to simple data types (e.g. integral and floating types). ConstIntExpression indicates the size of the array declared. That is, it specifies the number of array components in the array. It must have a value greater than 0. If the value is n, the range of the index values is 0 to n-1. For example, the declaration
int number[50];creates the number array, which has 50 components, each capable of holding one int value. In other words, the number array has a total of 50 components, all of type int.
ArrayName[ IndexExpression ]The index expression must result in an integer value. It can be of type char, short, int, long, or bool value because these are all integral types. The simplest form of index expression is a constant. For example:
number[0] specifies the 1st compnent of the number array number[1] specifies the 2nd compnent of the number array number[2] specifies the 3rd compnent of the number array number[3] specifies the 4th compnent of the number array number[4] specifies the 5th compnent of the number array . . . number[48] specifies the 2nd last component of the number array number[49] specifies the last component of the number arrayTo store values in the number array, we can do the following:
for (int i=0; i < 50; i++) {
number[i]=i; // Store a number in each array element
cout << "number[" << i << "] = " << number[i] << endl;
}
Each array element can be treated as a simple variable. Here, an integer
value is assigned to each array element, which has been declared to hold
data type int.
To use the values stored in the number array, we can treat each array element as a simple variable of data type int. For example:
for (int i=0; i < 50; i++) {
number[i]=2*number[i]; // Double the value in each array element
// and store it in the array element
cout << "number[" << i << "] = " << number[i] << endl;
}
For the number array, the valid index range is from 0 to 49. If the IndexExpression results in a value less than 0 or greater than the array_size - 1, then the index is considered to be out-of-bounds. For instance, number[50] is trying to access a memory location outside of the number array.
A variable can be initialized in its declaration. For example:
int value = 25;The value 25 is called an initializer. Similarly, an array can be initialized in its declaration. A list of initial values for array elements can be specified. They are separated with commas and enclosed within braces. For example:
int age[5] = {23, 56, 87, 92, 38};
In this declaration, age[0] is initialized to 23,
age[1] is initialized to 56, and so on. There must be at least
one initial value between braces. If too many initial values are specified,
a syntax error will occur.
Please Note: If the number of initial values is less than the array size, the remaining array elements will be initialized to zero.
In C++, the array size can be omitted when it is initialized in the declaration. For example:
int age[] = {23, 56, 87, 92, 38, 12, 15, 6, 3};
The compiler determines the size of the age array
according to how many initial values are listed. Here, the size of
the age array is 9.
// Program arrays.cpp manipulates values in an array.
#include <iostream>
using namespace std;
int main () {
const int MAX_ARRAY = 5;
int numbers[MAX_ARRAY];
int index;
int sum;
// Stored values in the array.
for (index = 0; index < MAX_ARRAY; index++)
numbers[index] = index * index;
// The values in the array are summed.
sum = 0;
for (index = 0; index < MAX_ARRAY; index++)
sum = sum + numbers[index];
cout << "Sum is " << sum << endl;
// Add code to print out the values of all the array elements
return 0;
}
|
// Program reverse.cpp reads numbers into an array
// and prints them out in reverse order.
#include <iostream>
using namespace std;
const int MAX = 10;
int main () {
int numbers[MAX];
int index;
for (index = 0; index < MAX; index++) {
cout << "Please enter an integer number: " << endl;
// FILL IN Code to store values in the numbers array
}
for (index = MAX - 1; index >= 0; index--)
// FILL IN Code to write values of the numbers array to the screen
return 0;
}
|
Hand In: This lab handout with the answers filled in attached
to a listing of your final exercises
(use the enscript command from your Programming Style Sheet to print it out:
enscript -E -G -2rj -M Letter -PECT2_PS <filename> or a2ps <filename>).