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
What refrigerant has been approved for new household refrigerators and freezers
Alexxx [7]
Bsbsnsnsnsn jajwjemoeododhdhdw whajsjshtzzvsssnxbba. 3
8 0
3 years ago
Aubrey is on a Windows machine. She wants to back up her Halloween pictures on an external hard drive. Which of the following ta
Leya [2.2K]

Answer:

no clu3 lol

Explanation:

I'm stupid hahaha

8 0
2 years ago
What are the data types in access​
Julli [10]

Answer:

Data type in Microsoft Access Database

Text  

Memo  

Byte  

Integer

Long  

Single  

Double  

Currency  

AutoNumber  

Date/Time  

Yes/No  

Ole Object  

Hyperlink  

Lookup Wizard

Explanation:

4 0
3 years ago
The exception of MS Access 2007 and above files is _ and older version are _ files ​
ohaa [14]

Answer:

Never heard of that kind of software, but I think it's an old file.

4 0
2 years ago
Given the value x=false ,y=5 and z=1 what will be the value of F=(4%2)+2*Y +6/2 +(z&amp;&amp;x)?
Bezzdna [24]
<h2>Answer:</h2>

F = 13

<h2>Explanation:</h2>

Given:

x = false

y = 5

z = 1

F = (4%2)+2*Y +6/2 +(z&&x)

We solve this arithmetic using the order of precedence:

<em>i. Solve the brackets first</em>

=> (4 % 2)

This means 4 modulus 2. This is the result of the remainder when 4 is divided by 2. Since there is no remainder when 4 is divided by 2, then

4 % 2 = 0

=> (z && x)

This means (1 && false). This is the result of using the AND operator. Remember that && means AND operator. This will return false (or 0) if one or both operands are false. It will return true (or 1) if both operands are true.

In this case since the right operand is a false, the result will be 0. i.e

(z && x) = (1 && false) = 0

<em>ii. Solve either the multiplication or division next whichever one comes first.</em>

=> 2 * y

This means the product of 2 and y ( = 5). This will give;

2 * y = 2 * 5 = 10

=> 6 / 2

This means the quotient of 6 and 2. This will give;

6 / 2 = 3

<em>iii. Now solve the addition by first substituting the values calculated earlier back into F.</em>

F = (4%2)+2*Y +6/2 +(z&&x)

F = 0 + 10 + 3 + 0

F = 13

Therefore, the value of F is 13

5 0
2 years ago
Other questions:
  • Your company is developing a new marketing campaign and wants to know which customers have never placed an order. You will need
    10·1 answer
  • How can you best utilize CSS for Web Development?
    6·1 answer
  • SI TENGO: ¿R = 255, G0 =, B = 0, QUE COLOR OBTENGO?
    14·1 answer
  • Discuss what they need to consider if they wish to use their devices when they are away from home?
    6·1 answer
  • Which one of the following is not the name of a 20th century school composition
    14·2 answers
  • A byte is made up of 8 bits (binary digits). You have a programming language that uses one byte to represent characters and are
    5·1 answer
  • When compared to traditional desktop customers, why are mobile phone users much more likely to book a room or airline reservatio
    12·1 answer
  • The ___________ method returns the length of an array.
    11·2 answers
  • An e-commerce client is moving from on-premise, legacy systems to a cloud-based platform. During the transition, the client is a
    6·1 answer
  • . prevalence of coronary artery disease in patients with isolated aortic valve stenosis. br heart j. 1984;51:121–4.
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!