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
Page 1 of 6
Computer Fundamentals
* 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.)
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