.

Pages: 12
in this code, I've made the code to accept only two numbers to check whether the elements in the array is in sequence or not. how can I change this code so that it can accept more than 2 number based on what the user wants..for example if the user wants to check wether five elements in the array is in sequence or not

#include <iostream>
using namespace std;


int main()
{
int data[] = {32, 30, 12, 60, 50, 5, 90, 9};
int key1,key2;

cout<<"what two subsequent element do you want to find in the array? : ";
cin>>key1>>key2;
int datasize = sizeof(data);
int numdata=datasize/sizeof(int);

for (int i = 0; i < numdata-1; i++)
{
int num = key1;
int num1 = key2;
if (data[i] == num)
{
if (data[i+1] == num1)
{
cout<<"Yes, those number is in the array subsequently"<<endl;
break;
}

else
{
cout<<"Nope, those number is not in the array subsequently"<<endl;
}
}
}
}


https://onlinegdb.com/rytYt37Od

Last edited on
You can do it with std::search
http://www.cplusplus.com/reference/algorithm/search/

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main()
{
   vector<int> data = { 32, 30, 12, 60, 50, 5, 90, 9 };
   vector<int> keys = { 12, 60, 50, 5, 90 };
   
   bool found = search( data.begin(), data.end(), keys.begin(), keys.end() ) != data.end();
   cout << "The subsequence " << ( found ? "is" : "is not" ) << " found\n";
}





If you want a home-grown version:

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
#include <iostream>
using namespace std;


bool isSubsequence( int *data, int datasize, int *keys, int keysize )
{
   if ( keysize == 0 ) return true;
   if ( datasize < keysize ) return false;
   return ( *data == *keys ) && isSubsequence( data+1, datasize-1, keys+1, keysize-1 );
}


int main()
{
   int data[] = {32, 30, 12, 60, 50, 5, 90, 9};
   int keys[] = { 12, 60, 50, 5, 90 };
   int datasize = sizeof data / sizeof data[0];
   int keysize = sizeof keys / sizeof keys[0];
   
   bool found = false;
   for ( int i = 0; i < datasize; i++ )
   {
      if ( isSubsequence( data+i, datasize-i, keys, keysize ) )
      {
         found = true;
         break;
      }
   }
   cout << "The subsequence " << ( found ? "is" : "is not" ) << " found\n";
}
Last edited on
Thank youu lasrchance for the fast reply!

cuurently I'm stuck on how to compare the two array if it has the same value and in the right sequence.

for example,when the program ask for the element that user want to search on the array data, it will store the user number and it will compare with array data to check whether the user entered the number in sequence or not

https://onlinegdb.com/ryHadGEdd

#include <iostream>
using namespace std;


int main()
{
int data[] = {32, 30, 12, 60, 50, 5, 90, 9};
int datasize = sizeof(data);
int key,key1,key2,number,ans=0;
int numdata=datasize/sizeof(int);

cout<<"what two subsequent element do you want to find in the array? : ";
cin>>key1>>key2;


for (int i = 0; i < numdata-1; i++) //need to use numdata-1 because since max index is 7 but in line 94 theres i+1 which leads to element 9 or index 8 and theres no element 9
{
int num = key1;
int num1 = key2;
if (data[i] == num)
{
if (data[i+1] == num1)
{
cout<<"Yes, those number is in the array subsequently"<<endl;
break;
}

else
{
cout<<"Nope, those number is not in the array subsequently"<<endl;
}
}
}

int *pointer = NULL;
cout<<"How Many Elements you want to find? : ";
cin>>number;

pointer = new int[number];

int temp;

for(int j=0; j<number; j++ )
{
cout<<"What are the elements? : "<<j+1<<")";
cin>>temp;

*(pointer+j)= temp;

}

cout<<"the element u have entered are"<<endl;
for(int j=0; j<number; j++ )
{
cout<<*(pointer+j)<<",";
}

delete []pointer;

return 0;
}
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
#include <iostream>

using namespace std;

int main()
{
    int data[] = {32, 30, 12, 60, 50, 5, 90, 9};
    int data_size = sizeof(data)/sizeof(int);

    // ENTER SUB-SEQUENCE
    int limit{0};
    cout << "Enter how many numbers to check in sequence? ";
    cin >> limit;
    int* sub_array = new int[limit];
    cout <<"What elements do you want to find in the array? ";
    for(int i = 0; i < limit; i++)
    {
        cin >> sub_array[i];
    }

    // FIND FIRST ELEMENT
    int index{0};
    while (index < data_size and data[index] != sub_array[0] )
    {
        index++;
    }

    // CHECK REMAINDER OF SUB-SEQUENCE
    if(index < data_size)
    {
        cout << "Matching elements found as follows: " << data[index] << ' ';
        for(int i = 1; i < limit; i++)
        {
            for(int j = index; j < data_size; j++)
            {
                if(sub_array[i] == data[j])
                {
                    cout << data[j] << ' ';
                }
            }
        }
    }

    cout << '\n';
    delete[] sub_array;

    return 0;
}


Enter how many numbers to check in sequence? 3
What elements do you want to find in the array? 12 60 50
Matching elements found as follows: 12 60 50 
Program ended with exit code: 0


Note: By counting up the number of matches a clear statement can be made about whether the whole sub-string matches.
Thank you so much againtry, may i know how to make the condition to check if the number entered by the user is following the sequence from the array data.

https://onlinegdb.com/BJLrcX4Ou

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
#include <iostream>

using namespace std;

int main()
{
    int data[] = {32, 30, 12, 60, 50, 5, 90, 9};
    int data_size = sizeof(data)/sizeof(int);

    // ENTER SUB-SEQUENCE
    int limit{0};
    cout << "Enter how many numbers to check in sequence? ";
    cin >> limit;
    int* sub_array = new int[limit];
    cout <<"What elements do you want to find in the array? ";
    for(int i = 0; i < limit; i++)
    {
        cin >> sub_array[i];
    }

    // FIND FIRST ELEMENT
    int index{0};
    while (index < data_size and data[index] != sub_array[0] )
    {
        index++;
    }

    // CHECK REMAINDER OF SUB-SEQUENCE
    if(index < data_size)
    {
        cout << "Matching elements found as follows: " << data[index] << ' ';
        for(int i = 1; i < limit; i++)
        {
            for(int j = index; j < data_size; j++)
            {
                if(sub_array[i] == data[j])
                {
                    cout << data[j] << ' ';
                }
            }
        }
    }

    cout << '\n';
    delete[] sub_array;

    return 0;
}
Last edited on
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
#include <iostream>
using namespace std;


bool isSubarrayInSequence( int *data, int datasize, int *keys, int keysize )
{
   if ( keysize == 0 ) return true;
   if ( datasize < keysize ) return false;
   for ( int i = 0; i < datasize; i++ ) if ( data[i] == keys[0] ) return isSubarrayInSequence( data + i + 1, datasize - i - 1, keys + 1, keysize - 1 );
   return false;
}


int main()
{
   int data[] = {32, 30, 12, 60, 50, 5, 90, 9};
   int datasize = sizeof data / sizeof data[0];

   int limit;
   cout << "Enter how many numbers to check in sequence: ";   cin >> limit;
   int* sub_array = new int[limit];
   cout << "Enter the " << limit << " elements: ";
   for( int i = 0; i < limit; i++ ) cin >> sub_array[i];
   
   bool found = isSubarrayInSequence( data, datasize, sub_array, limit );
   cout << "The subarray " << ( found ? "is" : "is not" ) << " found in sequence\n";

   delete [] sub_array;
}


Enter how many numbers to check in sequence: 3
Enter the 3 elements: 30 60 90
The subarray is found in sequence


Enter how many numbers to check in sequence: 3
Enter the 3 elements: 30 90 60
The subarray is not found in sequence
may i know how to make the condition to check if the number entered by the user is following the sequence from the array data
If I understand you correctly you need to simply adapt what you already have.

1 enter a number
2 find its index
loop
3 increment index
4 enter number
5 find index
6 goto loop

If this is what you mean then you don’t need the sub_array etc, it is a simple adaptation of the single number version key1 but no key2 which can also be improved by adding a search function to avoid repetition.

Thank youu so much last chance and againtry! u guys helped me a lot!
can someone explain to me line 9,10 and 26 please.. i don't really understand the concept

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
#include <iostream>
using namespace std;


bool isSubarrayInSequence( int *data, int datasize, int *keys, int keysize )
{
   if ( keysize == 0 ) return true;
   if ( datasize < keysize ) return false;
   for ( int i = 0; i < datasize; i++ ) if ( data[i] == keys[0] ) return isSubarrayInSequence( data + i + 1, datasize - i - 1, keys + 1, keysize - 1 );
   return false;
}


int main()
{
   int data[] = {32, 30, 12, 60, 50, 5, 90, 9};
   int datasize = sizeof data / sizeof data[0];

   int limit;
   cout << "Enter how many numbers to check in sequence: ";   cin >> limit;
   int* sub_array = new int[limit];
   cout << "Enter the " << limit << " elements: ";
   for( int i = 0; i < limit; i++ ) cin >> sub_array[i];
   
   bool found = isSubarrayInSequence( data, datasize, sub_array, limit );
   cout << "The subarray " << ( found ? "is" : "is not" ) << " found in sequence\n";

   delete [] sub_array;
}
Last edited on
Line 9 loops through the data array trying to find the first element of keys. If it does, then it can continue to search forward for the next lement (hence keys +1) from the next element in data (pointed to by data+i+1). As the call is recursive, line 7 signifies success once you find the last element of keys.

If you get to line 10 then you were unable to find the first element of keys, so you return false.

Line 26 contains a ternary operation which writes "is" if found is true and "is not" otherwise.

If I hadn't been so tired I would have combined lines 8 and 9.
Last edited on
1
2
#include <iostream>
using namespace std;int main(){int d[]={32,30,12,60,50,5,90,9};int s=sizeof(d)/sizeof(int);int l{0};cout<<"Enter how many numbers to check in sequence? ";cin>>l;int* a=new int[l];cout<<"What elements do you want to find in the array? "; for(int i=0;i<l; i++)cin>>a[i];int n{0};while(n<s and d[n]!=a[0])n++;if(n<s){cout<<"Matching elements found as follows: "<<d[n]<<' ';for(int i=1;i<l;i++){for(int j=n;j<s;j++){if(a[i]==d[j])cout<<d[j]<<' ';}}}cout<<'\n';delete[]a;return 0;}


Enter how many numbers to check in sequence? 3
What elements do you want to find in the array? 30 60 90
Matching elements found as follows: 30 60 90 
Program ended with exit code: 0


It just dawned on me that the idea of the absolute minimum of lines probably is a throwback to Hollerith card punching machine and white coat days where a big mainframe had 4k of RAM.
Last edited on
lastchance
I see.. then what is the function of line 7 and 8?

btw, is it possible to use if else statement instead of bool?
Last edited on
Thank youuuu
againtry
Here's another method using strings which is simpler:
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
#include <iostream>
#include <string>

using namespace std;

int main()
{
    int data[] = {32, 30, 12, 60, 50, 5, 90, 9};
    int data_size = sizeof(data)/sizeof(int);

    // GENERATE MAIN SEQUENCE
    string data_str{};
    for(int i = 0; i < data_size; i++)
        data_str += to_string(data[i]) + ' '; // <-- EDIT

    // ENTER SUB-SEQUENCE STRING
    string sub{};
    int limit{0};
    cout << "Enter how many numbers to check in sequence? ";
    cin >> limit;

    int temp{}; // <-- EDIT
    cout <<"What elements do you want to find in the array? ";
    for(int i = 0; i < limit; i++)
    {
        cin >> temp;
        sub += to_string(temp) + ' '; // <-- EDIT
    }
    cout << sub << '\n';

    // SUB_SEQUENCE IS IN MAIN SEQUENCE?

    if( data_str.find(sub) < data_str.length() ) // <-- EDIT
    {
        cout << "Found\n";
    }
    else
    {
        cout << "Not found\n";
    }
    
    return 0;
}



Enter how many numbers to check in sequence? 3
What elements do you want to find in the array? 60 50 90
Not found
Program ended with exit code: 0

Enter how many numbers to check in sequence? 3
What elements do you want to find in the array? 23 01 26
23126
Not found
Program ended with exit code: 0

EDIT: fixed
Found?
below
Last edited on
What about:


Enter how many numbers to check in sequence? 3
What elements do you want to find in the array? 23 01 26
Found


Found?

Perhaps:

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
include <iostream>
#include <string>
#include <iterator>

int main()
{
	const int data[] {32, 30, 12, 60, 50, 5, 90, 9};

	// GENERATE MAIN SEQUENCE
	std::string data_str;

	for (const auto& n : data)
		data_str += std::to_string(n) + " ";

	// ENTER SUB-SEQUENCE STRING
	std::string sub, temp;
	int limit {};

	std::cout << "Enter how many numbers to check in sequence? ";
	std::cin >> limit;

	std::cout << "What elements do you want to find in the array? ";

	for (int i = 0; i < limit; i++) {
		std::cin >> temp;
		sub += temp + " ";
	}

	// SUB_SEQUENCE IS IN MAIN SEQUENCE?
	if (data_str.find(sub) == std::string::npos)
		std::cout << "Not ";

		std::cout << "Found\n";
}

Last edited on
Can someone let me know what value does data in line 9 in isSubarrayInSequence holds?
The one with the formula data + i + 1.

same goes to keys and keysize (keys + 1, keysize - 1) in there,what value actually its holds?
.

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
#include <iostream>
using namespace std;


bool isSubarrayInSequence( int *data, int datasize, int *keys, int keysize )
{
   if ( keysize == 0 ) return true;
   if ( datasize < keysize ) return false;
   for ( int i = 0; i < datasize; i++ ) if ( data[i] == keys[0] ) return isSubarrayInSequence( data + i + 1, datasize - i - 1, keys + 1, keysize - 1 );
   return false;
}


int main()
{
   int data[] = {32, 30, 12, 60, 50, 5, 90, 9};
   int datasize = sizeof data / sizeof data[0];

   int limit;
   cout << "Enter how many numbers to check in sequence: ";   cin >> limit;
   int* sub_array = new int[limit];
   cout << "Enter the " << limit << " elements: ";
   for( int i = 0; i < limit; i++ ) cin >> sub_array[i];
   
   bool found = isSubarrayInSequence( data, datasize, sub_array, limit );
   cout << "The subarray " << ( found ? "is" : "is not" ) << " found in sequence\n";

   delete [] sub_array;
}
faati wrote:
The one with the formula data + i + 1.
same goes to keys and keysize (keys + 1, keysize - 1) in there,what value actually its holds?

data+i+1 will be a pointer (type int *) to the i+1 indexed element of data sent to that function. It is where you will start looking for the next element from keys (pointed to by keys+1). datasize-i-1 and keysize-1 will be the number of elements left in the respective arrays once you start again at these positions. This program assumes that you want the searched-for elements in the correct order, but not necessarily consecutively.


@faati, you still have NOT said whether you want this subarray to be in contiguous positions (i.e. successive elements in the data array), or whether you want them simply to be in the right order.

If you want them to be in contiguous positions, then you can just use std::search, as in the second code below - also my first reply to your topic.

You can, of course, use vector<int> or any other suitable container instead of your dynamic array.



For order correct, but not necessarily in contiguous positions:
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
#include <iostream>
using namespace std;

bool isSubSeq( int *data, int datasize, int *keys, int keysize )
{
   if ( keysize == 0 ) return true;
   for ( int i = 0; i <= datasize - keysize; i++ )
   {
      if ( data[i] == keys[0] ) return isSubSeq( data+i+1, datasize-i-1, keys+1, keysize-1 );
   }
   return false;
}


int main()
{
   int data[] = {32, 30, 12, 60, 50, 5, 90, 9};
   int datasize = sizeof data / sizeof data[0];

   int limit;
   cout << "Enter how many numbers to check in sequence: ";   cin >> limit;
   int* sub_array = new int[limit];
   cout << "Enter the " << limit << " elements: ";
   for( int i = 0; i < limit; i++ ) cin >> sub_array[i];
   
   bool found = isSubSeq( data, datasize, sub_array, limit );
   cout << "The subarray " << ( found ? "is" : "is not" ) << " found in sequential order\n";

   delete [] sub_array;
}




For a subarray in contiguous positions:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
   int data[] = {32, 30, 12, 60, 50, 5, 90, 9};
   int datasize = sizeof data / sizeof data[0];

   int limit;
   cout << "Enter how many numbers to check in sequence: ";   cin >> limit;
   int* sub_array = new int[limit];
   cout << "Enter the " << limit << " elements: ";
   for( int i = 0; i < limit; i++ ) cin >> sub_array[i];
   
   bool found = search( data, data + datasize, sub_array, sub_array + limit ) != data + datasize;
   cout << "The subarray " << ( found ? "is" : "is not" ) << " found in contiguous sequence\n";

   delete [] sub_array;
}

Last edited on
i see,so youre saying that value for data in that is the index value right? not the element value right?. thank you so much @lastchance.. and yeah I only want to it to be in right order.Not necessarily contiguous.
Thank youu to @againtry and @seeplus also. i learnt a lot with all your code..since I'm a student and still a junior in programaming
I only want to it to be in right order.Not necessarily contiguous.


Consider:

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
#include <iostream>
#include <iterator>

int main()
{
	const int data[] {32, 30, 12, 60, 50, 5, 90, 9};

	// ENTER SUB-SEQUENCE
	size_t limit {};

	std::cout << "Enter how many numbers to check in sequence? ";
	std::cin >> limit;

	auto sub_array {new int[limit] {}};

	std::cout << "What elements do you want to find in the array? ";
	for (size_t i = 0; i < limit; ++i)
		std::cin >> sub_array[i];

	auto itr {std::cbegin(data)};

	for (size_t i = 0; i < limit; ++i)
		if (itr = std::find(itr, std::cend(data), sub_array[i]); itr == std::cend(data))
			break;

	if (itr != std::cend(data))
		std::cout << "Sequence found\n";
	else
		std::cout << "Sequence not found\n";
}

Pages: 12