help: string and random number generator Questions

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Dec 2006
Posts: 5
Reputation: fafarfa is an unknown quantity at this point 
Solved Threads: 0
fafarfa fafarfa is offline Offline
Newbie Poster

help: string and random number generator Questions

 
0
  #1
Dec 4th, 2006
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

  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. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: help: string and random number generator Questions

 
0
  #2
Dec 4th, 2006
letters = 'A','T','C','G';
That doesn't make any sense. Perhaps you wanted an array, but forgot the []?
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 5
Reputation: fafarfa is an unknown quantity at this point 
Solved Threads: 0
fafarfa fafarfa is offline Offline
Newbie Poster

Re: help: string and random number generator Questions

 
0
  #3
Dec 4th, 2006
Originally Posted by joeprogrammer View Post
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
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 1,580
Reputation: Infarction has a spectacular aura about Infarction has a spectacular aura about Infarction has a spectacular aura about 
Solved Threads: 52
Infarction's Avatar
Infarction Infarction is offline Offline
Battle Programmer

Re: help: string and random number generator Questions

 
0
  #4
Dec 4th, 2006
1) You need to be saving your random letters to sequence, not changing letters. Like this:
  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'.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 5
Reputation: fafarfa is an unknown quantity at this point 
Solved Threads: 0
fafarfa fafarfa is offline Offline
Newbie Poster

Re: help: string and random number generator Questions

 
0
  #5
Dec 4th, 2006
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

  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.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 1,580
Reputation: Infarction has a spectacular aura about Infarction has a spectacular aura about Infarction has a spectacular aura about 
Solved Threads: 52
Infarction's Avatar
Infarction Infarction is offline Offline
Battle Programmer

Re: help: string and random number generator Questions

 
0
  #6
Dec 4th, 2006
You can just index the string like an array.
  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.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,131
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 283
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: help: string and random number generator Questions

 
0
  #7
Dec 5th, 2006
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.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 2040 | Replies: 6
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC