I am creating an integer array through user input and now how can I find if the array contains a number certain times consecutively?
#include <iostream>
using namespace std;
int main()
{
int size;
int *ptr;
cout << “Enter the size of your array: “;
cin >> size;
ptr = new int[size];
cout << “Enter the values you want to store: “;
for (int i = 0; i < size; i++)
{
cin >> ptr[i];
}
cout << “The array: “;
for(int i = 0; i < size; i++)
{
cout << ptr[i] << ” “;
}
cout << “– ” << (size) << ” characters” << endl;
return 0;
}
after takinf input just check like this:
for (int i = 0; i< size-1; i++) {
if ((ptr[i+1] == ptr[i]+1) || (ptr[i+1] == ptr[i]-1)) {
}
else
{
printf("Array Dont Have CONSECUTIVE elementsn");
return 0;
}
}
printf("Array Has CONSECUTIVE ELEMENTSn");
}