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])
The following is the pseudocode of algorithm that searches a sorted array of n items by...
The following is the pseudocode of algorithm that searches a sorted array of n items by dividing it into three sub arrays of almost n/3 items (7pt). Hint: refer Mergesort algorithm) Input: Sorted array of Keys(A) indexed from 1 to n. Output: Location, the Location of K in array A (hint: return 0 if K is no in A) index location3 (int A[1…n ], index L, index H) { index m1, m2; if (L > H) return 0; else   ...
Implement (provide pseudocode) the 'A la Russe' algorithm which does not use arrays.
Implement (provide pseudocode) the 'A la Russe' algorithm which does not use arrays.
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 }     }...
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
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++
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...
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....
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT