1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
telo118 [61]
3 years ago
9

Write a Java program HW2.java that asks the user to enter an array of integers in the main method. The program should prompt the

user for the number of elements in the array and then the elements of the array. The program should then call a method named isSorted that accepts an array of and returns true if the list is in sorted (increasing) order and false otherwise. For example, if arrays named arr1 and arr2 store [10, 20, 30, 30, 56] and [2, 5, 3, 12, 10] respectively, the calls isSorted(arr1) and isSorted(arr2) should return true and false respectively. Assume the array has at least one element. A one-element array is considered to be sorted.
Computers and Technology
1 answer:
tino4ka555 [31]3 years ago
7 0

Answer:

The java program for the given scenario is given below.

This program can be saved as HW2.java.

import java.util.Scanner;

public class HW2 {

   public static void main(String args[]) {

      HW2 ob = new HW2();  

     Scanner sc = new Scanner(System.in);

     int len;      

     System.out.println( "Enter the number of elements to be entered in the array");

     len = sc.nextInt();

     int[] arr = new int[len];

     System.out.println( "Enter the elements in the array");

     for( int k=0; k<len; k++ )

     {

         arr[k] = sc.nextInt();

     }

     boolean sort = ob.isSorted(arr, len);

     System.out.println( "The elements of the array are sorted: " + sort );

   }    

   public boolean isSorted( int a[], int l )

   {

       int sort = 0;

       if( l==1 )

           return true;

       else

       {        

           for( int k=1; k<l; k++ )

           {

               if(a[k-1] < a[k])

                   continue;

               else

               {

                   sort = sort + 1;

                   break;

               }

           }

           if(sort == 0)

               return true;

           else

               return false;

       }

   }

}

OUTPUT

Enter the number of elements to be entered in the array

3

Enter the elements in the array

1

2

1

The elements of the array are sorted: false

Explanation:

1. First, integer variable, len, is declared for length of array. User input is taken for this variable.

2. Next, integer array is declared having the size, len. The user is prompted to enter the equivalent number of values for the array.

3. These values are directly stored in the array.

4. Next, the function isSorted() is called.

5. This method uses an integer variable sort which is declared and initialized to 0.

6. Inside for loop, all elements of the array are tested for ascending order. If the 1st and 2nd elements are sorted, the loop will continue else the variable sort is incremented by 1 and the loop is terminated.

7. Other values of the variable sort indicates array is not sorted.

8. A Boolean variable, sort, is declared to store the result returned by isSorted method.

9. Lastly, the message is displayed on the output whether the array is sorted in ascending order or not.

You might be interested in
Obtain a file name from the user, which will contain data pertaining to a 2D array Create a file for each of the following: aver
cricket20 [7]

Answer:

see explaination

Explanation:

#include <iostream>

#include <fstream>

#include <iomanip>

using namespace std;

int main()

{

string filename, file1, file2, file3, file4;

cout << "Enter the filename : ";

getline(cin, filename);

int row, col;

ifstream ifile;

ifile.open(filename.c_str());

if(!ifile)

{

cout << "File does not exist." << endl;

}

else

{

ifile >> row >> col;

float mat[row][col], diag[row][col];

for(int i=0; i<row; i++)

{

for(int j=0; j<col; j++)

ifile >> mat[i][j];

}

cout << "Enter the filename to save averages : ";

getline(cin, file1);

ofstream avgFile(file1.c_str(), std::fstream::in | std::fstream::out | std::fstream::app);

float t_avg = 0, avg = 0;

for(int i=0; i<row; i++)

{

avg = 0;

for(int j=0; j<col; j++)

{

avg += mat[i][j];

}

t_avg += avg;

avg = avg/col;

avgFile << std::fixed << std::setprecision(1) << "Row " << i+1 << " average: " << avg << endl;

}

t_avg = t_avg / (row*col);

avgFile << std::fixed << std::setprecision(1) << "Total average: " << t_avg << endl;

cout << "Enter the filename to store reverse matrix : ";

getline(cin, file2);

ofstream revFile(file2.c_str(), std::fstream::in | std::fstream::out | std::fstream::app);

for(int i=0; i<row; i++)

{

for(int j=col-1; j>=0; j--)

{

revFile << std::fixed << std::setprecision(1) << mat[i][j] << " ";

}

revFile << endl;

}

cout << "Enter the filename to store flipped matrix : ";

getline(cin, file3);

ofstream flipFile(file3.c_str(), std::fstream::in | std::fstream::out | std::fstream::app);

for(int i=row-1; i>=0; i--)

{

for(int j=0; j<col; j++)

{

flipFile << std::fixed << std::setprecision(1) << mat[i][j] << " ";

}

flipFile << endl;

}

cout << "Enter the filename to store diagonal matrix : ";

getline(cin, file4);

ofstream diagFile(file4.c_str(), std::fstream::in | std::fstream::out | std::fstream::app);

for(int i=0; i<row; i++)

{

for(int j=0; j<col; j++)

{

diag[j][i] = mat[i][j];

}

}

for(int i=0; i<col; i++)

{

for(int j=0; j<row; j++)

{

diagFile << std::fixed << std::setprecision(1) << diag[i][j] << " ";

}

diagFile << endl;

}

}

return 0;

}

7 0
3 years ago
.When an argument is passed ______________, the called method can access and modify the caller’s original data directly.
wel

Answer:

c.using keyword ref

Explanation:

In C# when we have pass any variable by reference we use the ref keyword. When we pass a variable by reference in the function the changes done in the function are directly happening on the original variables.

When a variable is  passed by value a copy of these variable is created in the function and the operation is done on those copies.No changes can be seen on the original variables.

Hence the answer is option c.

6 0
3 years ago
Q13. On which option do you click to
fredd [130]

Answer:

The answer is "Home cells Or Home Tab".

Explanation:

The Excel Home Tab is also known as the home cell, it also is used to execute the standard commands like bold, highlight, copy/paste. It also uses templates in a worksheet for cells, which is used to Insert and Delete Cells, and the wrong cell can be defined as follows:

  • In choice Formulas cell or Tab it is used to add the formula, that's why it is wrong.
  • On the choice page cell or Tab is used to view the data, that's why it is wrong.
5 0
3 years ago
What is the main theme of The hundred dresses​
iogann1982 [59]

Explanation:

The main theme of The hundred dresses is about the act of forgiving someone's mistake. i hope so

5 0
3 years ago
Read 2 more answers
Digital cameras always create great photographs. <br>True <br>False
Pavel [41]

True..they do take great photography..

8 0
3 years ago
Read 2 more answers
Other questions:
  • What is heat sink?what is its use?If it is not in the system what will happen?
    12·1 answer
  • Prior to the release of a movie, a film studio launches a website that allows people to play online games featuring actors in th
    15·2 answers
  • A spreadsheet is a software program for storing, managing, and retrieving information. true or false?
    15·2 answers
  • Which group on the Home Ribbon contains the line spacing attribute?
    9·2 answers
  • What is an effective way to assess user requests for additional features and functions
    10·1 answer
  • To generate a report with exact results based on specific criteria it is best to base the report on a(n) ____________________ cr
    6·1 answer
  • Consider a planet where everyone belongs to a family of six, every family lives in its own house, each house has a unique addres
    8·1 answer
  • Function of C:\&gt;DIR*.doc/p​
    5·1 answer
  • What is the output of this program?
    10·2 answers
  • What is office course<br> how to learn office course
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!