So I developed this code so that I would have a program that can randomly generate a DNA sequence.

These are my questions:
1) how can i get the random number sequence to develop in a string?
2) how can i then multiplate the string so that the "A"s are replaced by "G"s or what not?
3) In the RNA portion of it: is there another way to program the yes or no answer responses instead of using if statements?

Also if there is anyother comment on my code its appreciated

if any question needs clarifying let me know

thank you so much in advance

here is my code

#include <iostream>                                                                
#include <ctime>                                                                
#include <cstdlib>                                                                
#include <string>

using namespace std;                                                            


int main()                                                                        
{                                                                               


    cout << "The Program Will First Generate a DNA Sequence:" << endl;           

    cout << endl;                                                                
    {                                                                           
            srand((unsigned)time(0));                                            
            int random_integer;                                                    
            int lowest=1, highest=4;                                            
            int range=(highest-lowest)+1;                                        
            char letters;                                                        
            string sequence;                                                    
            letters = 'A','T','C','G';                                            
            for(int index=0; index<20; index++)                                    
            {                                                                    
                random_integer = lowest+int(range*rand()/(RAND_MAX + 1.0));       
                random_integer = rand() % 4;                                    
                if(random_integer == 0)                                           
                    letters = 'A';                                                
                if(random_integer == 1)                                            
                    letters = 'T';                                               
                if(random_integer == 2)                                           
                    letters = 'C';                                                
                if(random_integer == 3)                                            
                    letters = 'G';                                                
                cout << letters << endl;                                        
            }                                                                    
    }                                                                           
    {                                                                            
        cout<< endl;                                                           
        int answer;                                                                
        int Y;                                                                    
        int N;                                                                    
        cout<< "Would You Like to Generate a RNA Sequence?(Y/N)"<< endl;        
        cin>> answer;                                                           
        cout<< endl;                                                            
    {                                                                            
        if (answer = Y)                                                            
            {                                                                    
                srand((unsigned)time(0));                                        
                int random_integer;                                                
                int lowest=1, highest=4;                                        
                int range=(highest-lowest)+1;                                    
                char letters;                                                   
                string sequence;
                letters = 'A','C','G','U';                                        
                for(int index=0; index<20; index++)                                
                {                                                               
                    random_integer =lowest+int(range*rand()/(RAND_MAX + 1.0));   
                    random_integer = rand() % 4;                                
                    if(random_integer == 0)                                       
                        letters = 'A';                                            
                    if(random_integer == 1)                                      
                        letters = 'U';                                            
                    if(random_integer == 2)                                       
                        letters = 'C';                                           
                    if(random_integer == 3)                                       
                        letters = 'G';                                           
                    cout << letters << endl;                                    
                }                                                                
            }                                                                    
        if(answer = N)                                                           
            {                                                                   
                cout << "No Sequence Was Generated." << endl;                    
            }                                                                   

    }                                                                            
    }                                                                           

return 0;
}

letters = 'A','T','C','G';

That doesn't make any sense. Perhaps you wanted an array, but forgot the []?

That doesn't make any sense. Perhaps you wanted an array, but forgot the []?

yes i did forget my []. thank you for pointing that out

1) You need to be saving your random letters to sequence, not changing letters. Like this:

for(int index=0; index<20; index++) 
            {
               random_integer = lowest+int(range*rand()/(RAND_MAX + 1.0));       
               random_integer = rand() % 4;                                    
               sequence += letters[random_integer];
               cout << sequence << endl; // this should probably be out of the loop though
           }

2) Loop through the string, and if it has a 'A', just replace it with a 'G'.

3) with 2 options, an if statement should be fine. You could also use a switch.

BTW, with the line if(answer = Y) , you should be using if(answer == 'Y') , so that you compare (rather than assign) to the value of the character Y (rather than the non-existant variable named Y). You should also consider comparing to lowercase 'y'.

thank you for the help. making it to the sequence instead of the letters helped alot.

im still confused on the mutation part of it.

if i wanted to do an insert,
would using this be the way

string my_string = "A";
my_string insert (1,"G");

or is there an easier way to do it?? loops and string always mix me up and get me confused.

You can just index the string like an array.

if(mystring[i] == 'A')
  mystring[i] = 'G';

Note that this is done using the character values (e.g. single quotes), and can only be used when replacing a single character at a time.

It seems to me you're going about this programming stuff the hard way. Based on the code you posted, you have never compiled the program. You really should be writing it a piece at a time, testing that piece, and move on when it's working. You're going to spend a lot more time trying to get it working if you try writing it all first before compiling.

And call srand() only once at the start of the program. You don't need to call it more than once.

This article has been dead for over six months. Start a new discussion instead.