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
schepotkina [342]
3 years ago
10

Write a Java program that asks the user to enter an array of integers in the main method. The program will ask the user for the

number of integer elements to be put in the array, and then ask the user for each element of the array. The program then calls a method named isSorted() that accepts an array of integers 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, 41, 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 integer element. A one-element array is considered to be sorted.
Computers and Technology
1 answer:
Alinara [238K]3 years ago
7 0

Answer:

The program is as follows:

import java.util.Scanner;

public class MyClass {

   public static Boolean isSorted(int [] arr){

       Boolean sorted = true;

       for(int i = 0;i<arr.length;i++){

       for(int j = i;j<arr.length;j++){

           if(arr[i]>arr[j]){

               sorted = false;

               break;

           }

       }    

       }

       return sorted;

   }

   public static void main(String args[]) {

       Scanner input = new Scanner(System.in);

       int n;

       System.out.print("Length of array: ");

       n = input.nextInt();

       int[] arr = new int[n];

       System.out.println("Enter array elements:");

       for(int i = 0; i<n;i++){

           arr[i] = input.nextInt();

       }

      Boolean chk = isSorted(arr);

     System.out.print(chk);

   }

}

Explanation:

The method begins here

This line defines a method named isSorted

   public static Boolean isSorted(int [] arr){

This line declares a boolean variable and initializes it to true

       Boolean sorted = true;

The next two iterations iterate through elements of the array

       for(int i = 0;i<arr.length;i++){

       for(int j = i;j<arr.length;j++){

This compares an array element with next elements (to the right)

           if(arr[i]>arr[j]){

If the next element is smaller than the previous array element, then the array is not sorted

               sorted = false; The boolean variable is updated to false

               break; This breaks the loop

           }

       }    

       }

This returns true or false, depending on the order of the array

       return sorted;

   }

The main method begins here

public static void main(String args[]) {

       Scanner input = new Scanner(System.in);

       int n;

This prompts user for length of array

       System.out.print("Length of array: ");

This gets length of array from user

       n = input.nextInt();

This declares an array

       int[] arr = new int[n];

This prompts user for array elements

       System.out.println("Enter array elements:");

The following iteration gets the array elements

<em>        for(int i = 0; i<n;i++){</em>

<em>            arr[i] = input.nextInt();</em>

<em>        }</em>

This calls the isSorted array      

     Boolean chk = isSorted(arr);

This prints true or false, depending if the array is sorted or not

     System.out.print(chk);      

   }

You might be interested in
Hello can you please help with this if you want to thank you!
nikklg [1K]

Answer:

hardware and software is the answer

8 0
3 years ago
Read 2 more answers
Vector graphic is usually required more storage space than bit maps true or false
barxatty [35]
The answer is false
5 0
3 years ago
Dim Inventory() as Integer = {357, 126, 220}Call Stock(Inventory(2),Me.lblOutput)Sub Stock (ByVal Number As Integer, ByRef lblLa
aalyn [17]

Answer:

The answer is "Option c".

Explanation:

In the given visual basic code, an integer array "Inventory" is declared, that includes 3 elements, that are "357, 126, and 220". In the next line, a call stock function is used, in the function, the array is passed as a parameter and in array parameter, the index value that is "2" is passed. This function use label to print array index value, that is "220". and other options are incorrect that can be described as follows:

  • Option a and Option b both are wrong because array indexing always starts with 0 and ends with n-1, and in this array, it will take index value 2, which is not equal to 357 and 126.

6 0
3 years ago
Given two complex numbers, find the sum of the complex numbers using operator overloading.Write an operator overloading function
Inessa05 [86]

Answer:

I am writing the program in C++ programming language.  

#include<iostream>  // to use input output functions

using namespace std;   // to identify objects like cin cout

class ProblemSolution {  //class name

private:  

// private data members that only be accessed within ProblemSolution class

int real, imag;

 // private variables for real and imaginary part of complex numbers

public:  

// constructor ProblemSolution() to initialize values of real and imaginary numbers to 0. r is for real part and i for imaginary part of complex number

ProblemSolution(int r = 0, int i =0) {  

real = r; imag = i; }  

/* print() method that displays real and imaginary part in output of the sum of complex numbers */

void print(){  

//prints real and imaginary part of complex number with a space between //them

cout<<real<<" "<<imag;}  

// computes the sum of complex numbers using operator overloading

ProblemSolution operator + (ProblemSolution const &P){  //pass by ref

          ProblemSolution sum;  // object of ProblemSolution

          sum.real = real + P.real;  // adds the real part of the  complex nos.

          sum.imag = imag + P.imag;  //adds imaginary parts of  complex nos.

//returns the resulting object

          return sum;       }  //returns the sum of complex numbers

};   //end of the class ProblemSolution

int main(){    //start of the main() function body

int real,imag;  //declare variables for real and imaginary part of complex nos

//reads values of real and imaginary part of first input complex no.1

cin>>real>>imag;  

//creates object problemSolution1 for first complex number

ProblemSolution problemSolution1(real, imag);  //creates object

//reads values of real and imaginary part of first input complex no.2

cin>>real>>imag;

//creates object problemSolution2 for second complex number

ProblemSolution problemSolution2(real,imag);

//creates object problemSolution2 to store the addition of two complex nos.

ProblemSolution problemSolution3 = problemSolution1 + problemSolution2;

problemSolution3.print();} //calls print() method to display the result of the //sum with real and imaginary part of the sum displayed with a space

Explanation:

The program is well explained in the comments mentioned with each statement of the program. The program has a class named ProblemSolution which has two data members real and imag to hold the values for the real and imaginary parts of the complex number. A default constructor ProblemSolution() which initializes an the objects for complex numbers 1 and 2 automatically when they are created.

ProblemSolution operator + (ProblemSolution const &P) is the operator overloading function. This performs the overloading of a binary operator + operating on two operands. This is used here to add two complex numbers.  In order to use a binary operator one of the operands should be passed as argument to the operator function. Here one argument const &P is passed. This is call by reference. Object sum is created to add the two complex numbers with real and imaginary parts and then the resulting object which is the sum of these two complex numbers is returned.  

In the main() method, three objects of type ProblemSolution are created and user is prompted to enter real and imaginary parts for two complex numbers. These are stored in objects problemSolution1 and problemSolution2.  Then statement ProblemSolution problemSolution3 = problemSolution1 + problemSolution2;  creates another object problemSolution3 to hold the result of the addition. When this statement is executed it invokes the operator function ProblemSolution operator + (ProblemSolution const &P). This function returns the resultant complex number (object) to main() function and print() function is called which is used to display the output of the addition.

4 0
4 years ago
HELP!! 20 POINTS!!
Svetlanka [38]

Signal processing deals with the analysis and operation of a. mechanical signals b. electrical signals c. biological signals. These can be  b. digital c. analog which vary across a range of values or b. digital with only two possible values.

Explanation:

  • In signal processing, a signal is a function that conveys information about a phenomenon. In electronics and telecommunications, it refers to any time varying voltage, current or electromagnetic wave that carries information.
  • A signal may also be defined as an observable change in a quality such as quantity...
  • A signal can be audio, video, speech, image, sonar and radar-related
  • Signal processing deals with the analysis and operation of mechanical signals, electrical signals and  biological signals.
  • The value of the signal is an electric potential which is voltage is also a signal.
  • The term analog signal usually refers to electrical signals. Analog signals may also be other mediums such as mechanical, pneumatic or hydraulic.
  • An analog signal signifies a continuous signal that keeps changes with a time period. A digital signal signifies a discrete signal that carries binary data and has discrete values. Analog signals are continuous sine waves. Digital signal is square waves

7 0
3 years ago
Other questions:
  • Which boolean operator (and, not, or or) gives you the least results? *?
    6·1 answer
  • write a simple assembly code using 8088 microprocessor instruction set to add two numbers ? please help me if you know :(​
    11·1 answer
  • You don't have policies that force settings for the look of users' computer desktops. Each user's chosen desktop settings are ap
    15·2 answers
  • Imagine that you have access to a class named MyCircle that has void setRadius(double r) and double getRadius() methods. Write a
    14·1 answer
  • The question of ________ arises when considering the way in which online marketers gather consumers’ information over the Intern
    6·1 answer
  • Please help me!!!!!!!!
    5·2 answers
  • You are adding more features to a linear regression model and hope they will improve your model. If you add an important feature
    7·1 answer
  • Explain the following terms <br><br>copyleft:<br><br>creative Commons:<br><br>GNU/GPL:​
    8·1 answer
  • Please help it’s timed
    11·1 answer
  • The value that decides whether something falls into one category or another is called a ____
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!