34.1k views
4 votes
#include

using namespace std;
const int SIZE = 4;
bool isSorted(const int arr[], int size);
bool isNonDecreasing(const int arr[], int size);
bool isNonIncreasing(const int arr[], int size);
void printArr(const int arr[], int size);
int main()
{
int test1[] = { 4, 7, 10, 69 };
int test2[] = { 10, 9, 7, 3 };
int test3[] = { 19, 12, 23, 7 };
int test4[] = { 5, 5, 5, 5 };
if (!isSorted(test1, SIZE))
cout << "NOT ";
cout << "SORTED" << endl;
printArr(test1, SIZE);
if (!isSorted(test2, SIZE))
cout << "NOT ";
cout << "SORTED" << endl;
printArr(test2, SIZE);
if (!isSorted(test3, SIZE))
cout << "NOT ";
cout << "SORTED" << endl;
printArr(test3, SIZE);
if (!isSorted(test4, SIZE))
cout << "NOT ";
cout << "SORTED" << endl;
printArr(test4, SIZE);
return 0;
}
bool isSorted(const int arr[], int size)
{
// TODO: This function returns true if the array is sorted. It could be
// sorted in either non-increasing (descending) or non-decreasing (ascending)
// order. If the array is not sorted, this function returns false.
// HINT: Notice that the functions isNonDecreasing and isNonIncreasing are not
// called from main. Call the isNonDecreasing and isNonIncreasing functions here.
}
bool isNonDecreasing(const int arr[], int size)
{
// TODO: Loop through the array to check whether it is sorted in
// non-decreasing (in other words, ascending) order. If the array
// is non-decreasing, return true. Otherwise, return false.
}
bool isNonIncreasing(const int arr[], int size)
{
// TODO: Loop through the array to check whether it is sorted in
// non-increasing (in other words, descending) order. If the array
// is non-increasing, return true. Otherwise, return false.
}
void printArr(const int arr[], int size)
{
for (int i = 0; i < size; i++)
cout << arr[i] << " ";
cout << endl << endl;
}

User Bresiu
by
5.6k points

1 Answer

2 votes

Output

SORTED

4 7 10 69

SORTED

10 9 7 3

NOT SORTED

19 12 23 7

SORTED

5 5 5 5

Code

//The added part is in the bottom

#include <iostream>

using namespace std;

const int SIZE = 4;

bool isSorted (const int arr[], int size);

bool isNonDecreasing (const int arr[], int size);

bool isNonIncreasing (const int arr[], int size);

void printArr (const int arr[], int size);

int

main ()

{

int test1[] = { 4, 7, 10, 69 };

int test2[] = { 10, 9, 7, 3 };

int test3[] = { 19, 12, 23, 7 };

int test4[] = { 5, 5, 5, 5 };

if (!isSorted (test1, SIZE))

cout << "NOT ";

cout << "SORTED" << endl;

printArr (test1, SIZE);

if (!isSorted (test2, SIZE))

cout << "NOT ";

cout << "SORTED" << endl;

printArr (test2, SIZE);

if (!isSorted (test3, SIZE))

cout << "NOT ";

cout << "SORTED" << endl;

printArr (test3, SIZE);

if (!isSorted (test4, SIZE))

cout << "NOT ";

cout << "SORTED" << endl;

printArr (test4, SIZE);

return 0;

}

bool

isSorted (const int arr[], int size)

{

//Added part

if (isNonDecreasing (arr, size) || isNonIncreasing (arr, size))

{

return true;

}

else

{

return false;

}

}

bool

isNonDecreasing (const int arr[], int size)

{

for (int i = 0; i < (size - 1); i++)

{

if (arr[i] > arr[i + 1]) //It compares the n value with the n-1 value and output

{

return false;

break;

}

}

return true;

}

bool

isNonIncreasing (const int arr[], int size)

{

for (int i = 0; i < (size - 1); i++)

{

if (arr[i] < arr[i + 1]) //It compares the n value with the n-1 value and output reautilization of previous function by replacing only “<”

{

return false;

break;

}

}

return true;

}

void

printArr (const int arr[], int size)

{

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

cout << arr[i] << " ";

cout << endl << endl;

}

User IFink
by
6.4k points