Please give this in assembly language. 1. Initialize an array [14, 7, 1, 17, 10]
ID: 3575422 • Letter: P
Question
Please give this in assembly language.
1. Initialize an array [14, 7, 1, 17, 10]. Then
1) Print it out. (3)
2) Use iteration to find out greatest and least numbers in this array and print them out in two lines. (4)
Desired output:
14 7 1 17 10
17
1
2. Initialize an array [1, 7, 13, 0, 11, 24, 8].
1) Load a number from the keyboard. (2)
2) If the number is odd, print out odd elements of the array. Otherwise, print out even elements. Do not forget the commas. (4)
Example output:
1, 7, 13, 11
Or:
0, 24, 8
3. Given an array [3, 1, 0, 4]
1) Initialize it from the keyboard. (1)
2) Print out these elements in ascending order. (6)
Example output:
-1 0 3 4
Explanation / Answer
Question 1:
ArrayCheck.java
public class ArrayCheck {
public static void main(String[] args) {
int a[] = {14, 7, 1, 17, 10};
int max = a[0];
int min = a[0];
for(int i=0; i<a.length; i++){
if(max < a[i]){
max = a[i];
}
if(min > a[i]){
min = a[i];
}
System.out.print(a[i]+" ");
}
System.out.println();
System.out.println(max);
System.out.println(min);
}
}
Output:
14 7 1 17 10
17
1
Question 2:
ArraySearch.java
import java.util.Scanner;
public class ArraySearch {
public static void main(String[] args) {
int a[]= {1, 7, 13, 0, 11, 24, 8};
Scanner scan = new Scanner(System.in);
System.out.println("Enter a number: ");
int n = scan.nextInt();
if(n % 2 == 0){
for(int i=0; i<a.length; i++){
if(a[i] % 2 == 0){
if(i!=a.length-1)
System.out.print(a[i]+", ");
}
}
}
else{
for(int i=0; i<a.length; i++){
if(a[i] % 2 != 0){
if(i!=a.length-1)
System.out.print(a[i]+", ");
}
}
}
}
}
Output:
Enter a number:
5
1, 7, 13, 11,
Question 3:
ArraySort.java
import java.util.Arrays;
public class ArraySort {
public static void main(String[] args) {
int a[] ={3, -1, 0, 4};
bubbleSort(a);
System.out.println(Arrays.toString(a));
}
private static void bubbleSort(int[] a) {
int n = a.length;
int temp = 0;
for(int i=0; i < n; i++){
for(int j=1; j < (n-i); j++){
if(a[j-1] > a[j]){
//swap the elements!
temp = a[j-1];
a[j-1] = a[j];
a[j] = temp;
}
}
}
}
}
output:
[-1, 0, 3, 4]