Question

In: Computer Science

C++ PLEASE---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- In this program, you will analyze an array

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

Solutions

Expert Solution

  • Below is the implementation of the problem mentioned above in C++ with code and it's output.
  • Please read the comment mentioned in the code for better understanding.
  • CODE:

#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;
}

  • INPUT/OUTPUT:
  1. AGTCAATGCA
    TCA
    Subsequence TCA found at index 2.
  2. AGTCAATGCA
    TAA
    Subsequence TAA not found.
  • Below is the screenshot of the code and it;s output for better clarity and understanding.

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.


Related Solutions

Program in C: Write a program in C that reorders the elements in an array in...
Program in C: Write a program in C that reorders the elements in an array in ascending order from least to greatest. The array is {1,4,3,2,6,5,9,8,7,10}. You must use a swap function and a main function in the code. (Hint: Use void swap and swap)
in C++ For this program, you are going to implement a stack using an array and...
in C++ For this program, you are going to implement a stack using an array and dynamic memory allocation. A stack is a special type of data structure that takes in values (in our case integers) one at a time and processes them in a special order. Specifically, a stack is what's called a first-in-last-out (FILO) data structure. That is to say, the first integer inserted into the stack is the last value to be processed. The last value in...
Please write a C++ program. Please rewrite your Array (including the operator overloading) into a template....
Please write a C++ program. Please rewrite your Array (including the operator overloading) into a template. And rewrite your main function to test your template for integer array and double array. Following is my complete code: #include <iostream> using namespace std; class Array { private: // Pointer to memory block to store integers int* data; // Maximum size of memory block int cap; // Stores number of integers in an array int num; public: // Constructor Array(int size); // Default...
c++ please Write and testa C++ main program that: declare an array arrof 6 integers Prompt...
c++ please Write and testa C++ main program that: declare an array arrof 6 integers Prompt the user for 6 integer values and store them in arr. Prompt the user for a target integer target. Search for targetin arr. If targetis found to match an element in the arr, then the program prints out a message which contains the address of the found element, otherwise, if no element found then the message “the element target not found” will be printed...
Write a complete C program that searches an element in array using pointers. Please use the...
Write a complete C program that searches an element in array using pointers. Please use the function called search to find the given number. //Function Prototype void search (int * array, int num, int size)
C++ Program: Write another program (in C++) that will allocate a local static array of integers...
C++ Program: Write another program (in C++) that will allocate a local static array of integers and then a dynamic array of integers. Are they stored next to each other? You can examine this by examining the memory addresses where they are located. As described in class, on some systems the size of a dynamic array is actually stored in the bytes previous to a dynamically allocated array. Through some experiments on your own, try to see if this is...
Write a program in C that declares the following array: int. array[] = { 1, 2,...
Write a program in C that declares the following array: int. array[] = { 1, 2, 4, 8, 16, 32 } Then write some code that accepts a number between 0 and 5 from the user and stores it in a variable called "index". Write some code that retrieves the item specified by the index, like this: int item = array[index]; Then write code that outputs the corresponding array entry based on the number the user entered. Example output: The...
Given the following array, write a program in C++ to sort the array using a selection...
Given the following array, write a program in C++ to sort the array using a selection sort and display the number of scores that are less than 500 and those greater than 500. Scores[0] = 198 Scores[3] = 85 Scores[6] = 73 Scores[9] = 989 Scores[1] = 486 Scores[4] = 216 Scores[7] = 319 Scores[2] = 651 Scores[5] = 912 Scores[8] = 846
C/ C++ Preferably 1. Write a simple program where you create an array of single byte...
C/ C++ Preferably 1. Write a simple program where you create an array of single byte characters. Make the array 100 bytes long. In C this would be an array of char. Use pointers and casting to put INTEGER (4 byte) and CHARACTER (1 byte) data into the array and pull it out. YES, an integer can be put into and retrieved from a character array. It's all binary under the hood. In some languages this is very easy (C/C++)...
Complete the given C++ program (prob1.cpp) to read an array of integers, print the array, and...
Complete the given C++ program (prob1.cpp) to read an array of integers, print the array, and then find the index of the largest element in the array. You are to write two functions, printArray() and getIndexLargest(), which are called from the main function. printArray() outputs integers to std::cout with a space in between numbers and a newline at the end. getIndexLargest () returns the index of the largest element in the array. Recall that indexes start at 0. If there...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT