Anda di halaman 1dari 6

Computer Fundamentals

ARRAYS
ARRAYS - a series of simple variables grouped together under one single variable.
int Score[5];
In the computers main memory, this can be depicted as:

Score[0]
Score[4]

Score[1]

Score[2]

Score[3]

Notice that a total of 5 adjacent slots are allotted because the array is declared as size 5.
In effect, array SCORE can hold 5 different values at the same time.
There are certain rules to follow when using subscripts and they are as follows:
1
2
3
4
5

The value of the subscript must be a positive integer number.


It can be an integer constant (e.g., Score[2]), numeric variable with integer
value (e. g., Score[X] where X is a numeric variable with integer value)
The first element always has a subscript of 0.
The value of the subscript cannot exceed the size of the array minus 1. For
example, for array Score, the subscript must be between 0 and 4 only since
Score was declared as Score[5].
The subscript must be enclosed in square bracket.

Examples of Incorrect usage of subscript:


Consider the variable declaration below:
num TOTAL[12], IND
char XX[20]
1.) TOTAL [0.1]
2.) TOTAL [-1]
3.) TOTAL [14]
* Subscript must be between 0 and the size of the array minus 1 which in this case
12.

Page 1 of 6

Computer Fundamentals

2.) TOTAL {2}


* Subscript must be enclosed within square bracket. No other symbols must be
used.
3.) TOTAL [XX]
* Subscript must be either an integer constant, numeric variable with integer value
or an arithmetic expression with integer value or an arithmetic expression with
integer result. The above is a character array variable which violates the rule just
mentioned.
4.) TOTAL [2]
* Subscript must be numeric.
5.) TOTAL [5+10]
* The resulting answer of 15 exceeds the size of the array which was defined as
TOTAL is ARRAY [12]
Assigning values to an array element using assignment statement:
To assign number 10 to element number 0, the statement is:
Score[0] = 10
To assign number 20 + 30 to element number 1, the statement is:
Score[1] = 20 + 30
To assign expression X / 5 to element number 3, the statement is:
Score[3] = X / 5
To assign the content of variable X to element number 4, the statement is:
Score[4] = X
To assign the content of element number 4 to element number 3, the statement is:
Score[3] = Score[4]
To assign the result of multiplying the content of element number 4 to element number 2
and putting the result in element number 1, the statement is:
Score[1] = Score[4]

* Score[3]

Page 2 of 6

Computer Fundamentals

To assign zero to all the elements of array Score, the statement is:
ctr = 0
while (ctr<5)
begin
Score[ctr] = 0
ctr = ctr + 1
end
ctr = 0
do
begin
Score[ctr] = 0
ctr = ctr + 1
end
while (ctr<5)

Page 3 of 6

Computer Fundamentals

1.)
Create a flowchart/C++ program that would accept 10 numbers and then display
all 10 numbers together with its total.
Flowchart
Start

Declarations
num NUM[10], ctr, sum

Flowchart

ctr = 0
Output NUM[ctr]

ctr = 0
sum = 0

ctr = ctr + 1
Input NUM[ctr]
ctr = ctr + 1
sum = sum + NUM[ctr]

while
ctr <10

N
while
ctr < 10
N

Output sum

Stop

Page 4 of 6

C++ Program
#include <iostream>
Computer
Fundamentals

2.)

using namespace std;


int main()
{ int NUM[10];
int ctr=0, sum=0;
do
{
cin>> NUM[ctr];
sum = sum + NUM[ctr];
ctr = ctr + 1;
} while (ctr <10);
ctr =0;
do
{
cout<< NUM[ctr];
ctr = ctr + 1;
}
while
(ctr < 10); program that would assign numbers 8-15 to an array of 8
Create a flowchart/C++
cout<<The
total of must
all thethen
numbers
entered
= <<sum<<endl;
elements.
The program
display
the contents
of the array starting from
return
0;
the last element.
}
Flowchart

Start
ctr = 7
Declarations
num Num[8], ctr, value
Output NUM[ctr]
ctr = 0
value = 8
NUM[ctr] = value
value = value + 1
ctr = ctr + 1
#include <iostream>
using namespace std;
int main()
{ int NUM[8]; while
Y
int ctr=0, value=8;
ctr <= 7
do
{

ctr = ctr - 1

C++ Program

while
ctr >= 0

N
Stop

NUM[ctr]
= value;
A
value = value + 1;
ctr = ctr + 1;
} while (ctr <=7);
ctr=7;
do
{
cout<< NUM[ctr];
ctr = ctr 1;
} while (ctr >= 0);
return 0;
}

Page 5 of 6

Computer Fundamentals

Exercises:
1.) Create a flowchart and a C++ program that will declare an array named A with 10
elements. Prompt the user to input values into its first five cells and copy the
numbers entered into its last five cells. Finally, output the content of array A.
2.) Create a flowchart and a C++ program that will input values to array A[10] and
array B[10]. Exchange their values in reverse order and output the new content
of arrays A and B.
3.) Create a flowchart and a C++ program that will declare an array named NUM
with 10 elements. Compute its product such that all the elements in the odd
subscripts will be your multiplicand and all the elements in the even subscript will
be your multiplier. Placed the product into the array named PROD with 5
elements and finally output the content of array PROD.
4.) Create a flowchart and a C++ program that will prompt the user to input numbers
into array C[100] and the input should be terminated by a zero sentinel. Output
the contents of array C and also the highest and the lowest number entered.

Page 6 of 6

Anda mungkin juga menyukai