Vector <int> checking to see if numbers are in sequential order

Been studying all day and my brain is completely mush.

If I have a vector that is sorted in size order with 5 numbers 5,6,7,8,9

How can I cout a simple message to say "Numbers are in order" , if the numbers are 10,11,13,14,15 I will get a cout message "Numbers are not in order". (Just need 5 numbers to be +1 each element in a row)

I am sure its incredibly simple but I am so tired I literally cant think :(
I don't know if it's the best solution but, you can iterate through the vector 1 time and if there is not a swap?..the "Numbers are in order" else if there are at least 1 number greater or less than the current vector position then "Numbers are not in order"

I have an example of Bubble sorting using an array, when there are no swap stops to iterate through the array.

i think that may help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//BubbleSort.cpp
//Program that sorts an array of 10 elements.
#include <iostream>
using std::cout;
using std::endl;

void BubbleSort(int array[],int size); //function prototype
void printArray(const int array[],int size); //function prototype



int main(){

const int SIZE=15;
int myArray[SIZE]={1,3,5,2,6,4,7,10,9,8,15,12,14,11,13}; 
cout<<"\nUnsorted array"<<endl;
printArray(myArray,SIZE);
cout<<"\nSorting the array"<<endl;
BubbleSort(myArray,SIZE);


return 0; //indicates success
}//end main

void BubbleSort(int array[],int size){
bool swap=false;
int aux=size;
	for(int times=0;times<size-1;times++){
		for(int i=0;i<aux;i++){
			if(array[i+1]<array[i]){
				int aux;
				aux=array[i];
				array[i]=array[i+1];
				array[i+1]=aux;
				swap=true;	
			}//end if		
		}//end inner for
	aux--;
	if(swap){
		swap=false;
		printArray(array,size);
	}else{
		break;
	}//end if...else
	}//end outer for	
}//end function bubble sort

void printArray(const int array[],int size){
	for(int index=0;index<size;index++)
		cout<<array[index]<<' ';
cout<<endl;
}//end function printArray 


If I understand correctly, you have numbers in order if your vector contains a,a+1,a+2,...
Then I would simply transform my vector v by subtracting v[0] and it's index
1
2
3
4
5
6
7
8
9
10
11
12
vector<int> v;
.............. initialize v................

int v0=v[0]
for(int i=0;i<v.size();i++)
    v[i]-=(v0+i);

vector<int> zeros(v.size(),0);
if (v==zeros) 
   cout<<"order";
else
   cout<<"not order";
Oh no, you don't have to subtract anything, just compare previousPosition<nextPosition or previousPosition>nextPosition -depends what you expect greater than or less- if there are at least 1 condition true, the "Numbers are not in order"

for example:

//pseudocode
if(previousPosition>nextPosition)
//Numbers are not in order
Thanks guys, ats15 works great and makes very simple sense , 10 hours of messing around with code and hitting the books means I cant figure anything out right now :) , cheers!
Topic archived. No new replies allowed.