Hello there, first time poster, many time lurker.

I'm currently working on an assignment for my CS class which has me creating a small program in which i create an array, call several functions to read intergers into the array, display the array, bur the part which is tripping me up, is to determine whether an array is sorted or not, without actually sorting the array? My instructions were not very clear on how to do this, and while my professor provided a skeleton (main) for the program (which I have filled in) I'm still at a loss on how to do this. Google hasn't been very friendly to me and neither has my book, so I'm turning to you guys for advice. Here's the code (which I've filled several requirements in already)

``````#include <iostream>
#include <ctime>
using namespace std;

const int Array_Size = 50;

void InputArray(int array[], int number);
void OutputArray(int array[], int number);
bool InAscendingOrder(int array[], int number);
bool InDescendingOrder(int array[], int number);
int MaxElement(int array[], int number);

int main()
{
int intarray[Array_Size];

InputArray(intarray, Array_Size);

cout << "Contents of the array:" << endl << "----------------------" << endl;

OutputArray(intarray, Array_Size);

if (InAscendingOrder(intarray, Array_Size))
cout << "Array is sorted in ascending order." << endl;
else if (InDescendingOrder(intarray, Array_Size))
cout << "Array is sorted in descending order." << endl;
else
cout << "Array is unsorted." << endl;

cout << "The largets number in array is " << MaxElement(intarray, Array_Size) << endl;

return 0;
}

//randomly generate array elements
void InputArray(int array[], int number)
{
int i;
srand (time(NULL));
for (i = 0; i < number; i++)
{
array[i] = rand() % 100;
}
}

//output the array
void OutputArray(int array[], int number)
{
int i;

for (i = 0; i < number; i++)
{
cout << array[i] << "\t";
}
}

//determine whether the array is in ascending order
bool  InAscendingOrder(int array[], int number)
{
//what?
}

//determine whether the array is in descending order
bool  InDescendingOrder(int array[], int number)
{
//how the hell?
}

//determine the largest element in array
int  MaxElement(int array[], int number)
{
int largest = array[0];

for(int i=1; i<number; i++)
{
if (largest < array[i])
largest = array[i];
}
return largest;

}``````
2
Contributors
3
Replies
4
Views
10 Years
Discussion Span
Last Post by vijayan121

> to determine whether an array is sorted or not
go through the array element by element from the beginning.
if the element that you currently look at is smaller(as per the sort criterion) than the one you had just looked at before this, the array is not sorted. if there are no such elements, the array is sorted.

Ok, I slept on it and took your advice + one step further. Here's my new code which compiles and seems to work. Any suggestions / optimizations?

``````#include <iostream>
#include <ctime>
using namespace std;

const int Array_Size = 50;

void InputArray(int array[], int number);
void OutputArray(int array[], int number);
bool InAscendingOrder(int array[], int number);
bool InDescendingOrder(int array[], int number);
int MaxElement(int array[], int number);

int main()
{
int intarray[Array_Size];

InputArray(intarray, Array_Size);

cout << "Contents of the array:" << endl << "----------------------" << endl;

OutputArray(intarray, Array_Size);

if (InAscendingOrder(intarray, Array_Size))
cout << "Array is sorted in ascending order." << endl;
else if (InDescendingOrder(intarray, Array_Size))
cout << "Array is sorted in descending order." << endl;
else
cout << "Array is unsorted." << endl;

cout << "The largets number in array is " << MaxElement(intarray, Array_Size) << endl;

return 0;
}

//randomly generate array elements
void InputArray(int array[], int number)
{
int i;
srand (time(NULL));
for (i = 0; i < number; i++)
{
array[i] = rand() % 100;
}
}

//output the array
void OutputArray(int array[], int number)
{
int i;

for (i = 0; i < number; i++)
{
cout << array[i] << "\t";
}
}

//determine whether the array is in ascending order
bool  InAscendingOrder(int array[], int number)
{
bool temp1 = true;

for(int j=0; j<50; j++)
{
if(array[j] > array[j+1])
{
temp1 = false;
}
}
return temp1;
}

//determine whether the array is in descending order
bool  InDescendingOrder(int array[], int number)
{
bool temp2 = true;

for(int k=0; k<50; k++)
{
if(array[k] > array[k+1])
{
temp2 = false;
}
}
return temp2;
}

//determine the largest element in array
int  MaxElement(int array[], int number)
{
int largest = array[0];

for(int i=1; i<number; i++)
{
if (largest < array[i])
largest = array[i];
}
return largest;

}``````

in functions InAscendingOrder and InDescendingOrder
a. use the size of the array passed to the function
b. make sure you do not make an 'off by one' error.
c. make them const-correct

``````//determine whether the array is in ascending order
bool  InAscendingOrder( const int array[], int number )
{
for( int i=0 ; i<(number-1) ; ++i )
if( array[i] > array[i+1] ) return false ;
return true ;
}``````

int function InputArray
a. seed the random number generator in main, not in the function (or make sure that it is seeded only once)
b. the randomness of numbers generated by the c library rand() remains poor no matter what we do. if it is being used, this could generate somewhat better random sequences

``````//randomly generate array elements
void InputArray( int array[], int number)
{
static int i = ( srand( time(0) ), 0 );
for( i=0 ; i<number; ++i )
array[i] = 1 + int( 100.0 * ( rand() / (RAND_MAX + 1.0) ) ) ;
}``````
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.