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
Sedaia [141]
2 years ago
14

Write a C++ program to find if a given array of integers is sorted in a descending order. The program should print "SORTED" if t

he array is sorted in a descending order and "UNSORTED" otherwise. Keep in mind that the program should print "SORTED" or "UNSORTED" only once.
Computers and Technology
1 answer:
LenKa [72]2 years ago
4 0

Answer:

The cpp program for the given scenario is as follows.

#include <iostream>

#include <iterator>

using namespace std;

int main()

{

   //boolean variable declared and initialized  

   bool sorted=true;

   //integer array declared and initialized

   int arr[] = {1, 2, 3, 4, 5};

   //integer variables declared and initialized to length of the array, arr

   int len = std::end(arr) - std::begin(arr);

       //array tested for being sorted

    for(int idx=0; idx<len-1; idx++)

    {

        if(arr[idx] < arr[idx+1])

           {

               sorted=false;

            break;

           }

    }

    //message displayed  

    if(sorted == false)

     cout<< "UNSORTED" <<endl;

 else

    cout<< "UNSORTED" <<endl;

return 0;

}

OUTPUT

UNSORTED

Explanation:

1. An integer array, arr, is declared and initialized as shown.

int arr[] = {1, 2, 3, 4, 5};

2. An integer variable, len, is declared and initialized to the size of the array created above.

int len = std::end(arr) - std::begin(arr);

3. A Boolean variable, sorted, is declared and initialized to true.

bool sorted=true;  

4. All the variables and array are declared inside main().

5. Inside for loop, the array, arr, is tested for being sorted or unsorted.  The for loop executes over an integer variable, idx, which ranges from 0 till the length of the array, arr.

6. The array is assumed to be sorted if all the elements of the array are in descending order.

7. If the elements of the array are in ascending order, the Boolean variable, sorted, is assigned the value false and for loop is exited using break keyword. The testing is done using if statement.

8. Based on the value of the Boolean variable, sorted, a message is displayed to the user.

9. The program can be tested for any size of the array and for any order of the elements, ascending or descending. The program can also be tested for array of other numeric data type including float and double.

10. All the code is written inside the main().

11. The length of the array is found using begin() and end() methods as shown previously. For this, the iterator header file is included in the program.

You might be interested in
Disadvantage of monolithic programming​
skelet666 [1.2K]

Answer:

=Tight coupling between components, as everything is in one application.

=Less reusability.

=Large code base; tough for developers and QA to understand the code and business knowledge.

=Less Scalable.

=Does not follow SRP (Single Responsibility Principle)

=More deployment and restart times.

3 0
2 years ago
After adding an image to her flyer, Danica played around to see which layout would look the best. At one point, her text was on
Norma-Jean [14]

Answer:

Position Feature

Wrap text feature

Picture tools

Explanation:

3 0
2 years ago
Read 2 more answers
1. Ce este o baza de date? (1p)
lesya [120]

Answer:

would you mind telling me what this says im still learning my spanish

Explanation:

6 0
2 years ago
Read 2 more answers
Define a method printAll() for class PetData that prints output as follows with inputs "Fluffy", 5, and 4444. Hint: Make use of
QveST [7]

Answer:

public void printAll(){  // member function petAll()

   super.printAll();  //  calls printAll() method of the superclass (base class) AnimalData using super keyword

   System.out.print(", ID: " + idNum);} // prints the ID stored in the idNum data member of the PetData class

Explanation:

Here is the complete PetData class:

public class PetData extends AnimalData {  //

private int idNum;

public void setID(int petID) {

idNum = petID;  }

// FIXME: Add printAll() member function

/* Your solution goes here */

public void printAll(){

   super.printAll();

   System.out.print(", ID: " + idNum);}  }

PetData class is a subclass which is inherited from the base class AnimalData.

This class has a private data member which is the variable idNum which is used to store the ID value.

It has a member function setID with parameter petID which is the idNum. This method basically acts as a mutator and sets the user ID.

This class has another method printAll() that uses super keyword to access the method printAll() of the base class which is AnimalData

super basically refers to the base class objects.

Here the super keyword is used with the method name of subclass PetData in order to eliminate the confusion between the method printAll() of AnimalData and PetData since both have the same method name.

In the main() method the use is prompted to enter values for userName, UserAge and UserID. Lets say user enters Fluffy as user name, 5 as user age and 4444 as userID. Then the statement userPet.printAll(); calls printAll() method of PetData class as userPet is the object of PetData class.

When this method is called, this method calls printAll() of AnimalData class which prints the Name and Age and printAll() of PetData class prints the ID. Hence the program gives the following output:

Name: Fluffy, Age: 5, ID: 4444    

8 0
3 years ago
If you want to selectively darken an area of an image you could use the
DerKrebs [107]
Create an adjustment layer and create a hue and saturation layer. You'll see sliders so adjust them to your liking. next select a layer mask and fill it will black. then select which areas you want to darken and use the wand tool. Apply a gradient mask if you want. 
5 0
2 years ago
Other questions:
  • Which of the following can be both an input device and an output device? mouse. keyboard. display screen. laser printer .
    9·1 answer
  • What file format can excel save files as
    10·1 answer
  • What is the role of programmers in an organization?
    6·1 answer
  • Sonic the Hedgehog (1991)
    14·1 answer
  • Using PowerPoint or Impressed guarantees that your presentation will do which of the following?
    6·1 answer
  • You have purchased a printer that has the capability to print in duplex mode so that users can print on both sides of a sheet of
    7·1 answer
  • look at sum of my horrible drawings lol these were last year XD let meh see sum of yalls art!! im not into drawing as much but i
    8·1 answer
  • 4. Contoso, Ltd. has a vigorous Office 365 and Azure cloud-service presence.
    8·1 answer
  • How to print something nad input on same line python
    8·1 answer
  • Basil was reading about a new attack that forces the system to abandon a higher cryptographic security mode of operation and ins
    10·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!