A True Canadian
Newbie
- Joined
- Aug 29, 2003
- Messages
- 2,874
- Reaction score
- 2
Hello again.
I'm curious if there is a way to determine if an array of integers is sorted or not? What I want is a piece of code that scans the integers that are inputted by the user, and then check to see whether or not they are in order, without actually sorting the numbers.
Here's what I've got so far.
IsSorted is the function that I am currently working on. It's down to the point where the information is printed out on the screen, however, I can't figure out how to only get the "Array is sorted"/"Array is not sorted" to print only once. And inserting breaks didn't work because it only tests the first two numbers. I'm also weak with for loops.
Who wants to guide me to the solution to my problem? :cheese:
I'm curious if there is a way to determine if an array of integers is sorted or not? What I want is a piece of code that scans the integers that are inputted by the user, and then check to see whether or not they are in order, without actually sorting the numbers.
Here's what I've got so far.
Code:
#include <stdio.h>
#include <ctype.h>
#define TRUE = 1
#define FALSE = 0
void InputList(int array[50], int &num);
int IsSorted(int array[50], int n);
main()
{
int a[10], num;
InputList(a, num);
IsSorted(a, num);
}
void InputList(int array[50], int &num)
{
int i;
printf("Enter the number of values in the list => ");
scanf("%d", &num);
for(i=0; i < num; ++i)
{
printf("Enter the data item => ");
scanf("%d", &array[i]);
}
}
int IsSorted(int array[], int n)
{
int i;
printf("Items in the original array\n");
for(i = 0; i < n; ++i) {
printf("%4d\n", array[i]);
}
for(i = 0; i < n; ++i) {
for(i = 0; i < n - 1; ++i) {
if (array[i] > array[i + 1]){
printf("Array is not sorted\n");
}
else
{
printf("Array is sorted\n");
}
}
}
return 0;
}
IsSorted is the function that I am currently working on. It's down to the point where the information is printed out on the screen, however, I can't figure out how to only get the "Array is sorted"/"Array is not sorted" to print only once. And inserting breaks didn't work because it only tests the first two numbers. I'm also weak with for loops.
Who wants to guide me to the solution to my problem? :cheese: