0% found this document useful (0 votes)
18 views

Array Tutorial

Uploaded by

d84nvrnh4f
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Array Tutorial

Uploaded by

d84nvrnh4f
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Technical programming 1

Array tutorials

Practices. These are done on the whiteboard


P1. Write Java statements that do the following:
a) Declare an array numArray of 15 elements of type int.
b) Output the value of the tenth element of the array alpha.
c) Set the value of the fifth element of the array alpha to 35.
d) Set the value of the ninth element of the array alpha to the sum of the sixth
and thirteenth elements of the array alpha.
answer.

a //Declare an array numArray of 15 elements of type int


int[] numArray = new int[15];

b//Output the value of the tenth element of the array alpha.


System.out.println(alpha[9]);

c//Set the value of the fifth element of the array alpha to 35.
alpha[4] = 35;

d//Set the value of the ninth element of the array alpha to the sum of
//the sixth and thirteenth elements of the array alpha
alpha[8] = alpha[5] + alpha[12];

P2 Write a statement that declares a string array initialized with the following
strings:
"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" and
"Saturday".
// Declare a string array and initialize
String[] days = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",
"Friday" , "Saturday"};

Exercises: These are done on the computer


Exercise 1
Write a java code to declare and initialize an array called List of type integer that can store 5 elements
namely 2, 5, 16, 17 and 18. The program must display on the screen only those elements that are even.
Hint Use %.

Solution
/**
* This program displays even values
* of array.
*/
public class EvenArray
{
public static void main(String[] args)
{
int[] list = { 2, 5, 16, 18 };

// Display all even elements of array


System.out.println("The even numbers of array:");

for (int i = 0; i < list.length; i++)


{
if (list[i] % 2 == 0)
{
System.out.print(list[i] + " ");
}
}

System.out.println();
}
}

Exercise 2 largest number


Write a java code for a program that can store 10 integers and calculate and
display on the screen the largest of those numbers.

Solution
/**
* This program find the largest value
* in an array
*/
Import java.
import java.util.Scanner;
public class PassArray
{

public static void main(String[] args)


{
Scanner console = new Scanner(System.in);

final int SIZE = 4; // Size of the array


// Create an array.
int[] list = new int[SIZE];

// Fill array with values

for (int i = 0; i < list.length; i++)


{

System.out.println("Enter a number ");

list[i] = console.nextInt();
}

int max = 0; // hold index number of largest number

// Finding largest value.


for (int i = 0; i < list.length; i++)
{

if (list[i] > list[max])

{
max = i;

}
System.out.println();

// Show largest value of array

System.out.println("Largest value is " + list[max]);

}
}

Exercise 3 sum and average number

Write a program that creates an array of 10 elements size representing


employee salaries. Your program should prompt the user to input numbers in
array and then display the sum and the average salary of the 10 employees.
Solution

import java.util.Scanner;
public class ArraySum
{
public static void main(String[] args)
{

// Create an array to hold numbers.


;

Scanner console = new Scanner(System.in);


System.out.println("Enter the numbers of employees ");
SIZE = console.nextInt();
int[] numbers = new int[SIZE];
// Get employees' salary.
for (int i = 0; i < SIZE; i++)
{
numbers[i] = console.nextInt();
}

int sum = 0;

// Calculate the sum.


for (int i = 0; i < SIZE; i++)
{
sum += numbers[i];
}

System.out.println("Sum of numbers: " + sum);


}
}

You might also like