Write the pseudocode for an application that will:
a) Prompt a user for three numbers.
b) Store the values entered by a user in an array as they are provided by the user.
c) Ask a user if they would like to search for a value:
If the user wishes to search for a value, the user needs to be prompted for a value to search for. If the value is found, a notification needs to be provided to the user.
d) Use a for loop to cycle through the arrays to calculate and display the total of the values stored in the array
Pseudocode
Start
Declare array numbers
For i=0 to 3 step 1
Prompt a user for a number
Get a number from a keyboard
Add a number to the array numbers
end
Display message: "Would like to search for a value?y/n"
Get the answer from a keyboard
if answer = y then
Display a message: "Enter a value to search for"
Get a value from a keyboard
isFound=false
For i=0 to 3 step 1
if value=numbers(i) then
Display a message:"The number is found"
isFound=true
Exit loop
end if
end
if isFound =false then
Display a message: "The number is not found."
end if
end
Declare variable totalValues=0
For i=0 to 3 step 1
totalValues=totalValues+numbers(i)
end
Display a message: "The total of the values stored in the array is: "+totalValues
Stop
JAVA CODE:
import java.util.Random;
import java.util.Scanner;
public class Q190207 {
/**
* The start point of the program
* @param args
*/
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
try {
int []numbers=new int[3];
//a) Prompt a user for three numbers.
for(int i=0;i<3;i++) {
System.out.print("Enter number "+(i+1)+": ");
//b) Store the values entered by a user in an array as they are provided by the user.
numbers[i]=keyboard.nextInt();
}
//c) Ask a user if they would like to search for a value:
keyboard.nextLine();
System.out.print("Would like to search for a value?y/n: ");
String answer=keyboard.nextLine();
if(answer.compareToIgnoreCase("y")==0) {
//If the user wishes to search for a value, the user needs to be prompted for a value to search for.
System.out.print("Enter a value to search for: ");
//b) Store the values entered by a user in an array as they are provided by the user.
int value=keyboard.nextInt();
//If the value is found, a notification needs to be provided to the user.
boolean isFound=false;
for(int i=0;i<3;i++) {
if(value==numbers[i]) {
System.out.println("\nThe number is found\n");
isFound=true;
break;
}
}
if(!isFound) {
System.out.println("\nThe number is not found.\n");
}
}
//d) Use a for loop to cycle through the arrays to calculate and display the total of the values stored in the array
int totalValues=0;
for(int i=0;i<3;i++) {
totalValues+=numbers[i];
}
System.out.println("\nThe total of the values stored in the array is: "+totalValues+"\n");
} catch (Exception e) {
// Display the error message:
System.out.print("Incorrect value");
}
// close Scanner
keyboard.close();
}
}
Comments
Leave a comment