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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
|
#include <iostream>
#include <stdio.h>
#include <iomanip>
void inputArray(int array[], int &n);
int checkArray (int array[], int start);
int determineLengthSequence(int array[], int start, int n);
using namespace std;
int main()
{
int sequence[20], count = 0, start = 1;
;
inputArray(sequence, count);
determineLengthSequence(sequence, start, count);
return 0;
}
void inputArray (int array[], int &n)
{
int numbers, i;
cout << "\n" << "Input a list of numbers (No more than 20) :";
cin >> numbers;
while (numbers != -1)
{
if ( n > 20 )
{
cout << "You have entered a more than 20 numbers." << endl;
cout << "\n\n" << "Input a list of numbers (No more than 20) :";
cin >> numbers;
}
else
{
array[n] = numbers;
n++;
cin >> numbers;
}
for (i = 0; i < n; i++ )
{
cout << setw(5) << array[i];
}
cout << endl;
}
}
int checkArray(int array[], int start) //Checks the array for a sequence
{
int k;
for (k = start; array[k] - array[k-1] == 1 && array[k] != -1; k++)
{
}
return k;
}
int determineLengthSequence(int array[], int start, int n)
{
int end = 0, currentLongest = 0, prevStart, prevEnd, numbers, k;
while (end <= 20)
{
numbers = array[n];
end = checkArray(numbers, start); // Error C2664: cannot convert parameter 1 from 'int' to 'int []'
if ( end - start > currentLongest)
{
currentLongest = end - start;
prevStart = start;
prevEnd = end;
}
cout << "Current Longest: " << currentLongest << endl;
start = end + 1;
}
cout << "Lomgest Sequence: \n";
for(k = prevStart -1; k < prevEnd; k++)
{
cout << array[k] << end;
}
cout << "Count: " << (prevEnd - prevStart) + 1 << endl;
return 0;
}
|