In: Computer Science
C++ PLEASE----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
In this program, you will analyze an array of 10 characters storing a gene sequence. You will be given a subsequence of 3 characters to look for within this array. If the subsequence is found, print the message:
Subsequence <XXX> found at index <i>.
Where i is the starting index of the subsequence in the array. Otherwise, print
Subsequence <XXX> not found.
The array of characters and the subsequence will be given through standard input. Read them and store them in arrays of characters of the right size. Do not use strings or vectors in this exercise.
You can assume the subsequence occurs at most once in the array.
Sample input 1:
AGTCAATGCA TCA
Output:
Subsequence TCA found at index 2.
Sample input 2:
AGTCAATGCA TAA
Output:
Subsequence TAA not found.
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
THANKS
#include<iostream>
using namespace std;
//driver function
int main(){
//character array to store gene sequence
char gene[10];
//input sequence
for(int i=0;i<10;i++){
cin>>gene[i];
}
//character array to store subsequence of 3 chars
char subs[3];
//input sequence
for(int i=0;i<3;i++){
cin>>subs[i];
}
//boolean to check if we got the answer or not
bool got=0;
//traverse thorough given sequence
for(int i=0;i<8;i++){
//starting from i check that subsequence is found or not
//flag remains 0 if we found one.
bool flag=0;
//traverse and check
for(int j=0;j<3;j++){
//if current character is unequal then set flag to 1 and
break
if(subs[j]!=gene[i+j]){
flag=1;
break;
}
}
//if we got the answer then print and set got to 1
if(!flag){
got=1;
cout<<"Subsequence
"<<subs[0]<<subs[1]<<subs[2]<<" found at
index "<<i<<".";
break;
}
}
//if we did not get the answer then print no found
if(got==0){
cout<<"Subsequence
"<<subs[0]<<subs[1]<<subs[2]<<" not
found.";
}
return 0;
}
CODE
INPUT/OUTPUT
So if you have any doubt regarding this solution please feel free to ask it int he comment section and if it is helpful then please upvote this solution, THANK YOU.