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;
}