Professional Documents
Culture Documents
Arrays 1
Arrays 1
Arrays are used to store multiple values in a single variable, instead of declaring separate
variables for each value.
String[] cars;
We have now declared a variable that holds an array of strings. To insert values to it, we can
use an array literal - place the values in a comma-separated list, inside curly braces:
Volvo
10
Vertical Implementation:
Sample Output:
Mazda
40
Accessing Arrays Using For Loop Statements
for(int ctr = 0;
ctr < 4; ctr = ctr + 1) // the counting starts at 0 or zero because of index
System.out.println(cars[ctr]);
for(int ctr = 0; ctr < 4; ctr = ctr + 1) // the counting starts at 0 or zero because of index
System.out.println(myNum[ctr]); // for Vertical Implementation
for(int ctr = 0; ctr < 4; ctr = ctr + 1) // the counting starts at 0 or zero because of index
System.out.print(cars2[ctr]+” ”); // Vertical and Horizontal implementation have the same
// For Loop Statements in different Arrays.
}
}
Sample Output:
Volvo
BMW
Ford
Mazda
10
20
30
40
1. Create a program that initialized String Array which have the following values:
cars [] = {"Volvo", "BMW", "Ford", "Mazda"}; then display output of Array.
Algorithm:
Initialize: cars [] = {"Volvo", "BMW", "Ford", "Mazda"};
Output: cars[0], cars[1], cars[2], cars[3];
Program Design
Start
cars [] = {"Volvo",
"BMW", "Ford",
"Mazda"};
Print(cars[0])
Print(cars[1])
Print(cars[2])
Print(cars[3])
Stop
Program Coding
Program Testing and Debugging
Sample Output:
Problem Definition
2. Create a program that initialized String Array which have the following values:
cars [] = {"Volvo", "BMW", "Ford", "Mazda"}; then display output of Array,
using Pre-condition Approach Loop
Algorithm:
Initialize: cars [] = {"Volvo", "BMW", "Ford", "Mazda"};
Process: i = 0, i = i + 1
Loop: i < 4
Output: cars[i]
Program Design
Start
A
A
cars [] = {"Volvo",
"BMW", "Ford",
"Mazda"};
i=0
False
i<4 Stop
True
Print(cars[i])
Loop
i=i+1
Program coding
3. Create a program that accept how many elements can user put on Array of Car brand. Print the elements in Car Array.
Algorithm:
Input: i, c, cars[i];
Print: cars[i]
Program Design
Start
i = 0, c = 0;
Input(c)
i=0
False
i<c
True
i=0
Input(cars[i])
Loop False
i=i+1 i<c
True
Print(cars[i]) Stop
Loop
i=i+1
Program Coding
Program Testing and Debugging