| | |
help: string and random number generator Questions
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Dec 2006
Posts: 5
Reputation:
Solved Threads: 0
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
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
C++ Syntax (Toggle Plain Text)
#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; }
1) You need to be saving your random letters to sequence, not changing letters. Like this:
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
C++ Syntax (Toggle Plain Text)
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 }
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'. •
•
Join Date: Dec 2006
Posts: 5
Reputation:
Solved Threads: 0
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
or is there an easier way to do it?? loops and string always mix me up and get me confused.
im still confused on the mutation part of it.
if i wanted to do an insert,
would using this be the way
C++ Syntax (Toggle Plain Text)
string my_string = "A"; my_string insert (1,"G");
You can just index the string like an array.
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.
C++ Syntax (Toggle Plain Text)
if(mystring[i] == 'A') mystring[i] = 'G';
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
And call
srand() only once at the start of the program. You don't need to call it more than once. The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
![]() |
Similar Threads
- Random number generator's (C++)
- Realy weird random number generator bug (C++)
- Random number generator (Java)
- Random number generator (Java)
- Random number generator (Java)
- random number generator (C++)
Other Threads in the C++ Forum
- Previous Thread: undefined reference to 'main'
- Next Thread: ifstream
Views: 2040 | Replies: 6
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamiccharacterarray encryption error file format forms fstream function functions game givemetehcodez graph homeworkhelp iamthwee ifstream input int java lib lines list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sort sorting spoonfeeding string strings struct temperature template templates text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






