Question

In: Computer Science

[after §3.23 easy] Swapping : The following pseudocode describes a simple algorithm which swaps the values...

  1. [after §3.23 easy] Swapping :

The following pseudocode describes a simple algorithm which swaps the values in two variables, x and y:

1 print “Enter initial value of x: ”

2 input x

3 print “Enter initial value of y: ”

4 input y

5 set temp to x

6 set x to y

7 set y to temp

8 print “x = ” x“, y = ” y

Write a Java Program that implements this algorithm.

Solutions

Expert Solution

program:

import java.util.*;


public class Main
{
   public static void main(String[] args)
{
int x,y,temp; //variable declaration
  
Scanner s = new Scanner(System.in); //to read input from user we create this object s
  
  
System.out.println("Enter initial value of x "); //prompt user to enter x value
x = s.nextInt(); //read x value from user

System.out.println("Enter initial value of y "); //prompt user to enter y value
y = s.nextInt(); //read y value from user

//swapping values
  
temp=x;
x=y;
y=temp;

//after sapping
  
System.out.println("After swapping: x= "+x +" y=" + y);
System.out.println( );

   }
}

output:


Related Solutions

Implement the following pseudocode in Python Consider the following pseudocode for a sorting algorithm: STOOGESORT(A[0 ......
Implement the following pseudocode in Python Consider the following pseudocode for a sorting algorithm: STOOGESORT(A[0 ... n - 1]) if (n = 2) and A[0] > A[1] swap A[0] and A[1] else if n > 2 m = ceiling(2n/3) STOOGESORT(A[0 ... m - 1]) STOOGESORT(A[n - m ... n - 1]) STOOGESORT(A[0 ... m - 1])
Consider the following pseudocode for insertion-sort algorithm. The algorithm sorts an arbitrary array A[0..n − 1]...
Consider the following pseudocode for insertion-sort algorithm. The algorithm sorts an arbitrary array A[0..n − 1] of n elements. void ISORT (dtype A[ ], int n) { int i, j; for i = 1 to n – 1 {     //Insert A[i] into the sorted part A[0..i – 1]     j = i;     while (j > 0 and A[j] < A[j – 1]) {         SWAP (A[j], A[j – 1]);         j = j – 1 }     }...
Given two unsorted arrays of integers. a) Write a pseudocode algorithm which will output only the...
Given two unsorted arrays of integers. a) Write a pseudocode algorithm which will output only the integers not common to both arrays. When writing pseudocode, consider that no implementation for data structures or algorithms exist. b) Implement your algorithm in Modern C++
Very easy: Write an EBNF rule that describes the following for statement in Java for (...
Very easy: Write an EBNF rule that describes the following for statement in Java for ( counter = 1; counter <= 10; counter ++) { sum=sum+1; } Then test your EBNF rule in Java as a recursive-descent subprogram
Write an algorithm (flowchart OR Pseudocode) for the following problem Take input character from the user...
Write an algorithm (flowchart OR Pseudocode) for the following problem Take input character from the user unless he enters '$'. Thereafter display how may vowels were entered by the user. Also display the number of each vowel ('a', 'e', 'i', 'o' and 'u') separately. For example if the user enters B a b e c o o d i u g o a l $ Then we have the output below: #A=2 #E=1 #I=1 #O=3 #U=2
Write an algorithm (flowchart OR Pseudocode) for the following problems: Take ten different numbers as input...
Write an algorithm (flowchart OR Pseudocode) for the following problems: Take ten different numbers as input and display the sum of there squares (SS). Example: let the inputs are: 3, 0, -1, 0, 9, -5, 6, 0, 2, 1 then 157 is displayed, because: SS=32+02+(-1)2+02+92+(-5)2+62+02+22+12=157 Take input character from the user unless he enters '$'. Thereafter display how may vowels were entered by the user. Also display the number of each vowel ('a', 'e', 'i', 'o' and 'u') separately. For...
Chapter 2, Business P2.32: The following pseudocode describes how a bookstore computes the price of an...
Chapter 2, Business P2.32: The following pseudocode describes how a bookstore computes the price of an order from the total price and the number of the books that were ordered. Ask user for the total book price and the number of books. Compute the tax (7.5 percent of the total book price). Compute the shipping charge ($2 per book). The price of the order is the sum of the total book price, the tax, and the shipping charge. Print the...
19.       Which of the following terms describes the act of stealing cash revenue after it has...
19.       Which of the following terms describes the act of stealing cash revenue after it has been recorded in the accounting system?             A) cash larceny             B) register disbursement             C) less cash scheme             D) skimming 20.          Diana, a forensic accountant, obtained the financial statements of Ryer Inc. from its accountant. She noted that Ryer generated a profit of $152.7 million in the previous year. This is an example of             A) third-party data.             B) quantitative data....
1. Which sorting algorithm can be improved with the following simple optimization? During each pass, you...
1. Which sorting algorithm can be improved with the following simple optimization? During each pass, you keep track of whether a swap was made. At the end of a pass, if no swaps were made, you can assume the list is sorted. a. Insertion sort b. Selection sort c. Bubble Sort d. None of the above 2. If you want to use Java's built-in Collections.sort() method to sort an ArrayList, what do you have to ensure about the type of...
Fractional Knapsack Problem Algorithm which best describes the tightest range of the number of items with...
Fractional Knapsack Problem Algorithm which best describes the tightest range of the number of items with only fractional inclusion (i.e. not entirely included or excluded) in the knapsack? (Let n denote the number of items for possible inclusion.) A) At least 0 items and at most n items B) At least 1 items and at most n items C) Exactly n items D) At least 0 items and at most n-1 items E) At least 1 items and at most...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT