Vous êtes sur la page 1sur 7

Array

An array is a special type of variable which can contain or hold one or more values of the same
data type with reference to only one variable name. In other words, the array variable has a
common name identifier and can hold many values at the same time, provided that they have
the same data type.
An array variable can be distinguished through a pair of brackets and on specified number
inside the square brackets: []. The number inside the square brackets is called index or
element.
Here is the syntax of one dimensional array:
dataType array_name[index]
Example:
int num [5];
Graphical representation
5

10

15

20

25

Here is the syntax of two dimensional array:


DataType var [row index][ column index]
Example:
int num [2][3];
Graphical representation
Column
0

Row

0
1

1
10
40

80
50

2
30
90

Example 1: Write a program using one-dimensional array that determines the highest value
among the five inputs from the keyboard and prints the difference of each value from the
highest.
Example 2: Write a program using one-dimensional array that calculates the sum and average
of the five input values from the keyboard and prints the calculated sum and average.
Example 3: Write a program using two-dimensional array that determines the even numbers
among the twelve input values from the keyboard and prints the list of these Even numbers.

Lab Activity
1. Write a program using one-dimensional array that determines the lowest value among the five
input values typed from the keyboard and prints the difference of each value from the lowest.
2. Write a program using one-dimensional array that accept five input values from the keyboard.
Then it should also accept a number to search. This number is to be searched if it is among
the five input values. If it is found, display the message "Searched number is found!",
otherwise display "Search number is lost!".
3. Write a program using two-dimensional arrays that determines the ODD numbers among the
12 input values typed from the keyboard and prints the list of these ODD numbers.
4. Write a program using two-dimensional arrays that searches a number and display the
number of times it occurs on the list of 12 input values.
5. Write a program using two-dimensional arrays that calculates the sum and average of the
twelve input values which the user would type from the keyboard and prints the calculated
sum and average.
6. Write a program using two-dimensional arrays that determines the highest and lowest of the
12 input values.
7. Write a program using two-dimensional arrays that lists the Odd numbers and Even numbers
separately in a given 12 input values.
8. Write a program using two-dimensional arrays that computes the sum of data in rows and
sum of data in columns of the 3x3 (three by three) array variable n[3[3j.
Sample input/output dialogue:
5

22

13

16

12

20

19

Example 3: Write a program using two dimensional arrays that lists the ODD numbers and
EVEN numbers separately in a given 12 input values.
Example 4: Write a program using two-dimensional array that accept twelve input values from
the keyboard. Then it should also accept a number to search. This number is to be searched if
it is among the five input values. If it is found, display the message Search number is found!
otherwise displays Search number is lost! and display the number of times it occurs on the lists
of 12 input values.
Example 1: Write a program using one-dimensional array that determines the highest value
among the five inputs from the keyboard.
Sample Input Data:
Number[0]
Number[1]
Number[2]
Number[2]
Number[1]

10
20
15
7
8

The highest value among the five inputs from the keyboard is: 20
Source Code
import java.io.*;
public class highest
{
public static void main(String[]args)throws IOException
{
BufferedReader DataIn = new BufferedReader(new InputStreamReader(System.in));
String number;
int num[] = new int[5];
int ctr;
int high;
for (ctr=0;ctr<5;ctr++)
{
System.out.print("Number" + "["+ctr+"]");
number = DataIn.readLine();
num[ctr] =Integer.parseInt(number);
}
high=0;
for(ctr=0;ctr<5;ctr++)
{

if(high<=num[ctr])
{
high=num[ctr];
}
}
System.out.println("The highest number is: "+high);
}
}

Example 2: Write a program using two dimensional arrays that determines the even numbers
among the twelve input values from the keyboard and prints the list of these Even numbers.
Sample Input data:
Number
Number
Number
Number
Number
Number
Number
Number
Number
Number
Number
Number

[0][0]
[0][1]
[0][2]
[0][3]
[1][0]
[1][1]
[1][2]
[1][3]
[2][0]
[2][1]
[2][2]
[2][3]

1
2
3
4
5
6
7
8
9
10
11
12

Lists of Even Numbers:


2

10

12

Example 3: Write a program using one dimensional array that calculates the sum and average
of the five input values from the keyboard and prints the calculated sum and average.
Example 4: Write a program using two dimensional arrays that determines the ODD numbers
among the twelve input values from the keyboard and prints the list of these ODD numbers.
Example 5: Write a program using two dimensional arrays that lists the ODD numbers and
EVEN numbers separately in a given 12 input values.

Source Code:
import java.io.*;
public class array2
{
public static void main(String[]args)throws IOException
{
BufferedReader input = new BufferedReader (new InputStreamReader(System.in));
String Snum;
int num[][] = new int[3][4];
int i,j;

for(i=0;i<=2;i++)
{
for(j=0;j<=3;j++)
{
System.out.print("Integer Number" + "[" + i + "]" + "[" + j + "]
Snum=input.readLine();
num[i][j]=Integer.parseInt(Snum);

");

}
}
System.out.println();
System.out.println();
System.out.println("\t\t\tThe numbers divisible by 2 are:");
System.out.println();
System.out.println();
for(i=0;i<=2;i++)
{
for(j=0;j<=3;j++)
{
if (num[i][j]%2==0)
{
System.out.println("Integer Number" + "[" + i + "]" + "[" + j + "] " + num[i][j]);
}
}
}
}
}

import java.io.*;
public class Array_even
{
public static void main(String[]args)throws IOException
{
BufferedReader DataIn = new BufferedReader(new InputStreamReader(System.in));
String number;
int num[][]=new int[3][4];
int eve;
int i;
int j;
for (i=0; i<3; i++)
{
for (j=0; j<4; j++)
{
System.out.print("Number [" +i+"] ["+j+"]");
number = DataIn.readLine();
num[i][j] = Integer.parseInt(number);
}
}

System.out.println("Numbers Divisible by 2 are: ");


for (i=0; i<3; i++)
{
for (j=0; j<4; j++)
{
eve = num[i][j]%2;
if (eve == 0)
{
System.out.print(num[i][j] + " ");
}
}
}
}
}

Vous aimerez peut-être aussi