Array Revision Solution
Array Revision Solution
System.out.print(alpha[i]+" ");
count++;
if(count==5)
{
System.out.println();
count=0;
}
c. Copy 11-element array a into the first portion of array b, which contains 34 elements.
b[i]=a[i];
a)
int I;
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
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
19
31
17
e)
Answers:
4 6 8
6 8 11
8 11 13
f)
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;}
h)
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
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