Question

In: Computer Science

Write a program to prompt the user to display the following menu: Sort             Matrix                   Q

Write a program to prompt the user to display the following menu:

Sort             Matrix                   Quit

  • If the user selects ‘S’ or ‘s’, then prompt the user to ask how many numbers you wish to read. Then based on that fill out the elements of one dimensional array with integer numbers. Then sort the numbers and print the original and sorted numbers in ascending order side by side.

How many numbers: 6

Original numbers:                     Sorted numbers

34                                                                         2         

55                                                           11

2                                                              34

89                                                            55

78                                                            78

11                                                            89

Do you want to quit? Q

  • If the user selects ‘M’ or ‘m, prompt the user to read an elements of two dimensional array of 2 x 2. Then, find the transpose of matric A and then add the transposed matrix to the original matrix and save the results into matrix C. Print the elements of matrices A, A-transposed , and C.

Do you want to quit? Q

If the user enters ‘Q’ or ‘’, your program should terminate the program.

Solutions

Expert Solution

import java.util.Scanner;
public class SortMatrix
{
public static void main(String[] args)
{
   Scanner sc = new Scanner(System.in);//create object of scanner class
       char ch;
      
       while(true)
       {
       System.out.println("********Menu********");
       System.out.println("Sort (Press 'S' or 's')");
       System.out.println("Matrix (Press 'M' or 'm')");
       System.out.println("Quit (Press 'Q')");
       System.out.print("Enter Choice = ");
       ch = sc.next().charAt(0);/// get menu choice from user
       //use switch case
       switch(ch)
       {
      
       case 'S': // user enter choice in both cases small letter or capital letter
       case 's':
      
       int num,temp=0;
       System.out.print("How many numbers you wish to read = ");
       num = sc.nextInt(); //stored size of array
       //create two array
       int arr[] = new int[num];
       int arr2[] = new int[num];
      
       System.out.println("Enter numbers in array = ");
       for(int i=0;i<arr2.length;i++)
       {
       arr2[i] = sc.nextInt();//insert element in array
       }
      
       for(int i=0;i<arr2.length;i++)
       {
       arr[i] = arr2[i]; //copy arr2 in arr for print original array
       }
      
       //sort array
       for(int i=0;i<arr2.length;i++)
       {
       for(int j=i+1;j<arr2.length;j++)
       {
       if(arr2[i]>arr2[j])
       {
       temp = arr2[i];
       arr2[i] = arr2[j];
       arr2[j] = temp;
       }
       }
       }
      
       //print array in proper format
       System.out.println("Original Numbers\tSorted Numbers");
       for(int i=0;i<arr2.length;i++)
       {
       System.out.println("\t"+arr[i]+"\t\t\t"+arr2[i]);
       }
      
       break;
      
       case 'M': // user enter choice in both cases small letter or capital letter
       case 'm':
       //create three 2D array
       int matA[][] = new int[2][2];
       int trasA[][] = new int[2][2];
       int matC[][] = new int[2][2];
      
       System.out.println("Enter number in 2*2 matrix = ");
       for(int i=0;i<matA.length;i++)
       {
       for(int j=0;j<matA.length;j++)
       {
       matA[i][j] = sc.nextInt();//insert element in array first
       }
       }
       // transpose matrix
       for(int i=0;i<matA.length;i++)
       {
       for(int j=0;j<matA.length;j++)
       {
       trasA[i][j] = matA[j][i];
       }
       }
      
       //addition of Original matrix and transpose matrix stored in matc
       for(int i=0;i<matA.length;i++)
       {
       for(int j=0;j<matA.length;j++)
       {
       matC[i][j] = matA[i][j] + trasA[i][j];
       }
       }
      
       //print all three matrix
       System.out.println("Original Matrix\t\tTraspose Matrix\t\tAddition Matrix");
       for(int i=0;i<matA.length;i++)
       {
       for(int j=0;j<matA.length;j++)
       {
       System.out.print(" "+matA[i][j]+"\t");
       }
       System.out.print("\t");
      
       for(int j=0;j<trasA.length;j++)
       {
       System.out.print(" "+trasA[i][j]+"\t");
       }
       System.out.print("\t");
      
       for(int j=0;j<matC.length;j++)
       {
       System.out.print(" "+matC[i][j]+"\t");
       }
       System.out.print("\t");
      
       System.out.println("");
       }
      
       break;
      
       case 'Q':
       //exit in while loop
       System.exit(0);
       break;
      
       default:
System.out.println("Invalis Choice");
break;
       }
       }
   }
}

==================================OUTPUT===============================

==Please Upvote==


Related Solutions

Write a program to prompt the user to display the following menu: Sort             Matrix                   Q
Write a program to prompt the user to display the following menu: Sort             Matrix                   Quit If the user selects ‘S’ or ‘s’, then prompt the user to ask how many numbers you wish to read. Then based on that fill out the elements of one dimensional array with integer numbers. Then sort the numbers and print the original and sorted numbers in ascending order side by side. How many numbers: 6 Original numbers:                     Sorted numbers 34                                                                         2          55                                                      ...
Write a program to prompt the user to display the following menu: Guess-Number                        Concat-names     &
Write a program to prompt the user to display the following menu: Guess-Number                        Concat-names             Quit If the user selects Guess-number, your program needs to call a user-defined function called int guess-number ( ). Use random number generator to generate a number between 1 – 100. Prompt the user to guess the generated number and print the following messages. Print the guessed number in main (): Guess a number: 76 96: Too large 10 Too small 70 Close Do you...
C++ PLEASE Write a program to prompt the user to display the following menu: Guess-Number                       ...
C++ PLEASE Write a program to prompt the user to display the following menu: Guess-Number                        Concat-names             Quit If the user selects Guess-number, your program needs to call a user-defined function called int guess-number ( ). Use random number generator to generate a number between 1 – 100. Prompt the user to guess the generated number and print the following messages. Print the guessed number in main (): Guess a number: 76 96: Too large 10 Too small 70 Close...
Write a java program that will first display the following menu: Choose one of the following...
Write a java program that will first display the following menu: Choose one of the following 1- To add 2 double integers 2- To add 2 integer numbers 3- To add 3 double numbers 4- To add 3 integer numbers After reading user’s choice, use a switch case statement to call the corresponding method Void add 1 (double n1, double n2) Void add2() Double add3 (double n1, double n2, double n3) Double add4 ()
You must prompt the user to enter a menu selection. The menu will have the following...
You must prompt the user to enter a menu selection. The menu will have the following selections (NOTE: all menu selections by the user should not be case sensitive): 1. ‘L’ – Length a. Assume that a file containing a series of names is called names.txt and exists on the computer’s disk. Read that file and display the length of each name in the file to the screen. Each name’s middle character or characters should be displayed on a line...
Write a simple airline ticket reservation program. The program should display a menu with the following...
Write a simple airline ticket reservation program. The program should display a menu with the following options: reserve a ticket, cancel a reservation, check whether a ticket is reserved for a particular person, and display the passengers. The information is maintained on an alphabetized linked list of names. In a simpler version of the program, assume that tickets are reserved for only one flight. In a fuller version, place no limit on the number of flights. Create a linked list...
We need to create basic program that will simply display a menu and allow the user...
We need to create basic program that will simply display a menu and allow the user to select one of the choices. The menu should be displayed such that each option will be numbered, as well as have one capital letter so as to indicate that either the number or the designated letter can be entered to make their choice. Once the choice is made, the sub-menu for that selection should be displayed. Colors with an odd number of letters...
Write a program names EncryptDecrypt.java that has the following menu choices: Print menu and allow user...
Write a program names EncryptDecrypt.java that has the following menu choices: Print menu and allow user to choose options. The program must have a file dialogue box for text file. Output should be based on user choices. Read in a file Print the file to the console Encrypt the file and write it to the console Write out the encrypted file to a text file Clear the data in memory Read in an encrypted file Decrypt the file Write out...
Question #11. Write a script that uses the menu function to prompt the user to select...
Question #11. Write a script that uses the menu function to prompt the user to select the current day of the week. Then use the menu function to prompt the user to select their favorite day of the week. Print a message telling the user how many days it will be until their favorite day. The output must include the current day, the favorite day, and the number of days until the favorite day. Debug your programming by running it...
IN C This assignment is to write a program that will prompt the user to enter...
IN C This assignment is to write a program that will prompt the user to enter a character, e.g., a percent sign (%), and then the number of percent signs (%) they want on a line. Your program should first read a character from the keyboard, excluding whitespaces; and then print a message indicating that the number must be in the range 1 to 79 (including both ends) if the user enters a number outside of that range. Your program...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT