PL Labexer4 Sacasas Elizalde

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Sacasas, Elizalde

ITC C202 - 201DA

Prelims Lab Exercise 4: Arrays

PROBLEM #: 1

Write a JAVA program that will input 10 scores and output the Highest and Lowest score.

REQUIREMENTS:

Input/s: 10 scores (e.g. 33, 38, 50, 42, 45, 23, 18, 39, 21, 43)

Output/s: Only the highest and lowest score (e.g. H = 50, L = 18)

DESIGN: Algorithm (Pseudocode):

Step 1: Intialize low, high, array []


Step 2: Read num of limit
Step 3: Loop enter score
Step 4: If num is reached
Display the lowest and highest score
Step 5: End

SOURCE CODE:

import java.util.Arrays;
import java.util.Scanner;

public class Prelim_LabExer4_1 {

public static void main (String[] args) {

int stud1, stud2, stud3, stud4, stud5, stud6, stud7, stud8, stud9, stud10;
int max = 0;
int count = 0;
int[] userInput = new int[10];

Scanner keyboard = new Scanner(System.in);

System.out.println("ENTER STUDENT SCORES");


System.out.print("\n");
System.out.print("Student #1: ");
stud1 = keyboard.nextInt();
userInput[0] = stud1;

System.out.print("Student #2: ");


stud2 = keyboard.nextInt();
userInput[1] = stud2;

System.out.print("Student #3: ");


stud3 = keyboard.nextInt();
userInput[2] = stud3;

System.out.print("Student #4: ");


stud4 = keyboard.nextInt();
userInput[3] = stud4;

System.out.print("Student #5: ");


stud5 = keyboard.nextInt();
userInput[4] = stud5;

System.out.print("Student #6: ");


stud6 = keyboard.nextInt();
userInput[5] = stud6;

System.out.print("Student #7: ");


stud7 = keyboard.nextInt();
userInput[6] = stud7;

System.out.print("Student #8: ");


stud8 = keyboard.nextInt();
userInput[7] = stud8;

System.out.print("Student #9: ");


stud9 = keyboard.nextInt();
userInput[8] = stud9;

System.out.print("Student #10: ");


stud10 = keyboard.nextInt();
userInput[9] = stud10;

keyboard.close();

for (int s = 0; s < userInput.length; s++) {


count = count + userInput[s];
}
System.out.println("\n");

Arrays.sort(userInput);
max = userInput[userInput.length - 1];
System.out.println("LOWEST SCORE: " + userInput[0]);

System.out.println("HIGHEST SCORE: " + max);


}
}

SCREENSHOTS:

PROBLEM #: 2

Write a JAVA program that will generate the given


output. A program that prints histogram.
REQUIREMENTS:

Input/s: 

Output/s: E.G.

Elements Value Histogram

0 10 **********

1 3 ***

2 6 ******

3 18 ******************

4 11 ***********

5 1 *

DESIGN: Algorithm (Pseudocode)

Step 1: Initialize array[] = {10, 3, 6, 18, 11, 1}


Step 2: String output = element, value, histogram
Step 3: Is count = arr reached?
Step 4: If no,
Process array length and count array
Step 5: If yes,
Output the given value of an array
Step 6: End

SOURCE CODE:

public class Main {

public static void main(String[] args) {


int array[] = {10, 3, 6, 18, 11, 1};

String output = "Element \tValue \t\tHistogram";

for ( int count = 0; count < array.length; count++ ) {

output += "\n" + count + "\t\t" + array[ count ] + "\t\t";

for ( int asterisk = 0; asterisk < array[ count ]; asterisk++ ) {

output += "* ";


}
}
System.out.println(output);
}
}

SCREENSHOTS:

PROBLEM #: 3

Input 5 integer values for arrayOne and 5 integer values for arrayTwo. Write a JAVA program that would
merges two ordered list of integers into a single ordered list object of integers. Merge the two arrays and
sort.
REQUIREMENTS:

Input/s: arr1 = {5, 9, 3, 0, 2} arr2 = {7, 1, 8, 6, 4} (or any 5 integer)

Output/s: Array One: [5, 9, 3, 0, 2] Array Two: [7, 1, 8, 6, 4]

Merged Array: [5, 9, 3, 0, 2, 7, 1, 8, 6, 4] Sorted Array: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

DESIGN: Algorithm (Pseudocode)

Step 1: Initialize arr1 = {5, 9, 3, 0, 2} arr2 = {7, 1, 8, 6, 4}

Step 2: Merge and sort arrays

Step 3: Process arr3 = arr1+arr2

Step 4: Use quick sort, array copy

Step 5: Output array one, array two, merged array and sorted array

Step 6: End

SOURCE CODE:

import java.util.Arrays;

public class Main {

public static void main(String[] args) {

int[] arr1 = {5, 9, 3, 0, 2};


int[] arr2 = {7, 1, 8, 6, 4};

int ar1 = arr1.length, ar2 = arr2.length;


int ar3 = ar1 + ar2;

int[] arr3 = new int[ar3];

System.arraycopy(arr1, 0, arr3, 0, ar1);


System.arraycopy(arr2, 0, arr3, ar1, ar2);

System.out.println("Array One: " +Arrays.toString(arr1));


System.out.println("Array Two: " +Arrays.toString(arr2));
System.out.println("\nMerged Array: " +Arrays.toString(arr3));

Arrays.sort(arr3);
System.out.println("Sorted Array: " +Arrays.toString(arr3));

}
}

SCREENSHOTS:

You might also like