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

Array Revision Solution

Uploaded by

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

Array Revision Solution

Uploaded by

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

KINGDOM OF SAUDI ARABIA Departments: CS,IS, and NW

PRINCESS NORA UNIVERSITY Course:Programming


Language(CS110D)
FACULTY OF COMPUTER AND
Class: Level 1
INFORMATION SCIENCES Year: 1436-1437 H – First Semester

Tutorial about Arrays


Q1: Write Java statements that do the following:

a. Declare an array alpha of 15 components of the type int


Int alpha[] = new int[15];
b. Print the value of the tenth component in the array alpha
System.out.print(alpha[9]);
c. Set the value of the fifth component in the array alpha to 35
alpha[4]=35;
d. Set the value of the ninth component of the array alpha to the sum of the sixth and
thirteen component of the array alpha
alpha[8]=alpha[5] +alpha[12];
e. Set the value of the fourth component of the array alpha to three time the value of the
eight component of the array alpha
alpha[3]=3*alpha[7]
f. Print the alpha so that five components per line are printed
int count =0;
for(int i=0;i<alpha.length;i++)
{

System.out.print(alpha[i]+" ");
count++;
if(count==5)
{
System.out.println();
count=0;
}

Q2: Write Java statements that do the following:

a. Initialize each of the five elements of array g to 8.


int[] g=new int[5];
for(int i=0;i<g.length;i++)
g[i]=8;
KINGDOM OF SAUDI ARABIA Departments: CS,IS, and NW
PRINCESS NORA UNIVERSITY Course:Programming
Language(CS110D)
FACULTY OF COMPUTER AND
Class: Level 1
INFORMATION SCIENCES Year: 1436-1437 H – First Semester

b. Total the 100 elements of floating-point array c. float sum =0.0f;


float sum=0.0f; for( float x : c)
for(int i=0; i<c.lenght;i++) sum=sum+x
or
sum=sum+c[i];

c. Copy 11-element array a into the first portion of array b, which contains 34 elements.

for(int i=0; i<a.lenght.i++)

b[i]=a[i];

Q3: what is the output of the following java code:

a)

int I;

int [] array1 = {1,2,3};


int [] array2 = {1,2,3};
for(i = 0; i < array1.length ; i ++)
if (array1[i] == array2[i])
System.out.println("element number " + i + " are equal ");
else
System.out.println("element number " + i + " are not equal ");

if (array1 == array2)
System.out.println(" the two array are equal ");
else
System.out.println("the two array are not equal ");

Answer:
element number 0 are equal

element number 1 are equal


b) int i;
element number 2 are equal
int [] array = {1,2,3,4,5};
the twofor
array(i
are =0 ; i<= array.length -1; i+=2)
not equal
System.out.println(array[i]);
KINGDOM OF SAUDI ARABIA Departments: CS,IS, and NW
PRINCESS NORA UNIVERSITY Course:Programming
Language(CS110D)
FACULTY OF COMPUTER AND
Class: Level 1
INFORMATION SCIENCES Year: 1436-1437 H – First Semester

BUILD SUCCESSFUL (total time: 0 seconds)

c) int i;
int [] list =new int [5];
for (i =0 ; i<5; i++)
{
list[i] = 2 * i +5;
if (i + 2 == 0)
list[i] = list[i] - 3;
}
for (i =0 ; i<5; i++)
System.out.println(list[i]);
Answer:
5

11

11
KINGDOM OF SAUDI ARABIA Departments: CS,IS, and NW
PRINCESS NORA UNIVERSITY Course:Programming
Language(CS110D)
FACULTY OF COMPUTER AND
Class: Level 1
INFORMATION SCIENCES Year: 1436-1437 H – First Semester

d) int [] list =new int [6];


list[0] =5;
for (int i =0 ; i<6; i++)
{
list[i] = i * i +5;
if (i > 2)
list[i] = 2 *list[i] - list[i-1];
}
for (int i =0 ; i<6; i++)
System.out.println(list[i]);
Answer:
6

19

31

17

e)

int[][] b = new int[3][3];


for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
b[i][j] = 2 * (i + j) + 4;
}
}

for (int i = 0; i < 3; i++) {


for (int j = 0; j < 3; j++) {
System.out.print(b[i][j] + "\t");
}
System.out.println();
}
KINGDOM OF SAUDI ARABIA Departments: CS,IS, and NW
PRINCESS NORA UNIVERSITY Course:Programming
Language(CS110D)
FACULTY OF COMPUTER AND
Class: Level 1
INFORMATION SCIENCES Year: 1436-1437 H – First Semester

Answers:

4 6 8

6 8 11

8 11 13

BUILD SUCCESSFUL (total time: 0 seconds)

f)

public static void main(String[] args)


{
int[] array = {87, 68, 94, 100, 83};
int total = 0;
for (int number : array)
total += number;
System.out.printf("Total of array elements: %d%n", total);
}

g)-
public class JavaApplication17 {
public static void main(String[] args) {
int[] array = {1, 2, 3, 4, 5};
modify1(array);
for (int value : array)
System.out.printf(" %d", value);
modify2(array[3]);
System.out.printf("array[3] after modifyElement: %d%n", array[3]);
}
public static void modify1(int x[])
{
for (int counter = 0; counter < x.length; counter++)
x[counter] *= 5;}

public static void modify2(int e)


{ e *= 2;
System.out.printf( "\nValue of element in modifyElement: %d%n", e);}
} // end class
KINGDOM OF SAUDI ARABIA Departments: CS,IS, and NW
PRINCESS NORA UNIVERSITY Course:Programming
Language(CS110D)
FACULTY OF COMPUTER AND
Class: Level 1
INFORMATION SCIENCES Year: 1436-1437 H – First Semester

h)

public static void main(String[] args)


{
int[][] array1 = {{1, 2, 3}, {4, 5, 6}};

System.out.println("Values in array1 by row are");


outputArray(array1);
}

public static void outputArray(int[][] array)


{
for (int row = 0; row < array.length; row++)
{
for (int column = 0; column < array[row].length; column++)
System.out.printf("%d ", array[row][column]);

System.out.println();
}
}
KINGDOM OF SAUDI ARABIA Departments: CS,IS, and NW
PRINCESS NORA UNIVERSITY Course:Programming
Language(CS110D)
FACULTY OF COMPUTER AND
Class: Level 1
INFORMATION SCIENCES Year: 1436-1437 H – First Semester

Q4 Write a method createArithmeticsSeq that prompts the user to input two numbers, first
and diff. The method then creates a one-dimensional array of 16 elements ordered in an arithmetic
sequence. It also output the arithmetic sequence. For example if first = 21 and diff = 5, the
arithmetic sequence is

21 26 31 36 41 46 51 56 61 66 71 76 81 86 91 96

public static void createArithmeticsSeq ()


{
int first;
int differ;
Scanner in=new Scanner(System.in);
first =in.nextInt();
differ=in.nextInt();
int[] arr=new int[16];

for(int i=0;i<arr.length;i++)
{
arr[i]=first;
first+=differ;
}

for(int i=0;i<arr.length;i++)
{
System.out.print(arr[i]+ " ");
} }

Q5: Write a method that receives array and integer x, the method should search for integer in array
and return the index of the integer in array. If not found the integer in array the method should
return -1

public static void main(String[] args)


{
int y=2;
int[] myarray={4,3,2,1};
System.out.print(Mymothod(myarray, y));
}// end of main

public static int Mymothod(int []arr, int x)


{
for(int i=0;i<arr.length;i++)
{
if (x== arr[i])
return i;}
return -1;
}

You might also like