944,099 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2258
  • C++ RSS
Dec 4th, 2006
0

help: string and random number generator Questions

Expand Post »
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

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <ctime>
  3. #include <cstdlib>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8.  
  9. int main()
  10. {
  11.  
  12.  
  13. cout << "The Program Will First Generate a DNA Sequence:" << endl;
  14.  
  15. cout << endl;
  16. {
  17. srand((unsigned)time(0));
  18. int random_integer;
  19. int lowest=1, highest=4;
  20. int range=(highest-lowest)+1;
  21. char letters;
  22. string sequence;
  23. letters = 'A','T','C','G';
  24. for(int index=0; index<20; index++)
  25. {
  26. random_integer = lowest+int(range*rand()/(RAND_MAX + 1.0));
  27. random_integer = rand() % 4;
  28. if(random_integer == 0)
  29. letters = 'A';
  30. if(random_integer == 1)
  31. letters = 'T';
  32. if(random_integer == 2)
  33. letters = 'C';
  34. if(random_integer == 3)
  35. letters = 'G';
  36. cout << letters << endl;
  37. }
  38. }
  39. {
  40. cout<< endl;
  41. int answer;
  42. int Y;
  43. int N;
  44. cout<< "Would You Like to Generate a RNA Sequence?(Y/N)"<< endl;
  45. cin>> answer;
  46. cout<< endl;
  47. {
  48. if (answer = Y)
  49. {
  50. srand((unsigned)time(0));
  51. int random_integer;
  52. int lowest=1, highest=4;
  53. int range=(highest-lowest)+1;
  54. char letters;
  55. string sequence;
  56. letters = 'A','C','G','U';
  57. for(int index=0; index<20; index++)
  58. {
  59. random_integer =lowest+int(range*rand()/(RAND_MAX + 1.0));
  60. random_integer = rand() % 4;
  61. if(random_integer == 0)
  62. letters = 'A';
  63. if(random_integer == 1)
  64. letters = 'U';
  65. if(random_integer == 2)
  66. letters = 'C';
  67. if(random_integer == 3)
  68. letters = 'G';
  69. cout << letters << endl;
  70. }
  71. }
  72. if(answer = N)
  73. {
  74. cout << "No Sequence Was Generated." << endl;
  75. }
  76.  
  77. }
  78. }
  79.  
  80. return 0;
  81. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
fafarfa is offline Offline
5 posts
since Dec 2006
Dec 4th, 2006
0

Re: help: string and random number generator Questions

Quote ...
letters = 'A','T','C','G';
That doesn't make any sense. Perhaps you wanted an array, but forgot the []?
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Dec 4th, 2006
0

Re: help: string and random number generator Questions

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
fafarfa is offline Offline
5 posts
since Dec 2006
Dec 4th, 2006
0

Re: help: string and random number generator Questions

1) You need to be saving your random letters to sequence, not changing letters. Like this:
C++ Syntax (Toggle Plain Text)
  1. for(int index=0; index<20; index++)
  2. {
  3. random_integer = lowest+int(range*rand()/(RAND_MAX + 1.0));
  4. random_integer = rand() % 4;
  5. sequence += letters[random_integer];
  6. cout << sequence << endl; // this should probably be out of the loop though
  7. }
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'.
Reputation Points: 683
Solved Threads: 53
Posting Virtuoso
Infarction is offline Offline
1,580 posts
since May 2006
Dec 4th, 2006
0

Re: help: string and random number generator Questions

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

C++ Syntax (Toggle Plain Text)
  1. string my_string = "A";
  2. 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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
fafarfa is offline Offline
5 posts
since Dec 2006
Dec 4th, 2006
0

Re: help: string and random number generator Questions

You can just index the string like an array.
C++ Syntax (Toggle Plain Text)
  1. if(mystring[i] == 'A')
  2. 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.
Reputation Points: 683
Solved Threads: 53
Posting Virtuoso
Infarction is offline Offline
1,580 posts
since May 2006
Dec 5th, 2006
0

Re: help: string and random number generator Questions

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.
Moderator
Reputation Points: 3281
Solved Threads: 895
Posting Sage
WaltP is offline Offline
7,747 posts
since May 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: undefined reference to 'main'
Next Thread in C++ Forum Timeline: ifstream





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC