Creating a character sequence, assigning numerical values to letters, and summing up those number values

Hi there :)

I'm trying to input a character sequence (a bunch of letters i.e. california is awesome), assign each letter a specific number (a = 4, c = 44 etc), then sum up the total of all the letters entered (total A's + total C's etc) ...

This is not for a class or anything. It's just a learning experience. I'm trying to learn how to code...

I'm pretty much stuck... Any help is appreciated!

Last edited on
closed account (o3hC5Di1)
Hi there,

Could you please post your actual code and what it is exactly you are stuck on?
That will help us analyse the problem much better.

Thanks!

All the best,
NwN
closed account (zb0S216C)
yoyonyo wrote:
"assign each letter a specific number"

Character constants already have a value assigned to them. The value corresponds to the decimal value on the ASCII chart[1].

1
2
3
4
5
6
7
8
9
10
11
#include <string>
#include <iostream>

int main()
{
    std::string MyString("My Sample String");
    unsigned int Result(0u);

    for(std::string::iterator Iterator(MyString.begin()); Iterator < MyString.end(); ++Iterator)
        Result += (static_cast<unsigned int>(*Iterator));
}

That should do it - 100% C++.

References:
[1] http://www.asciitable.com/


Wazzak
Last edited on
Right now I'm able to display the masses I like, but unable to sum up all of the masses.. Whenever I try, I get errors: So basically I'm trying to take a DNA sequence and assign the masses that I've specified to the corresponding letters.... I would like to input the typed sequence into a character stream, assign each letter the masses specified, the sum them up to get a total mass... Any help is strongly appreciated... This is my first program so I'm sorry if it looks ridiculous! I copy pasted the iomanip coding std:: stuff and honestly I don't really know what I'm doing yet...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/* 
	Exact M.W. of ssRNA (e.g., RNA Transcript)
	
	M.W. = (An x 329.2) + (Un x 306.2) + (Cn x 305.2) + (Gn x 345.2) + 159ª
	An, Un, Cn, and Gn are the number of each respective nucleotide within the polynucleotide.
	M.W. calculated is valid at physiological pH. 
	ªAddition of "159" to the M.W. takes into account the M.W. of a 5' triphosphate.

	Exact M.W. of ssDNA (e.g., Oligonucleotides)
	
	M.W. = (An x 313.2) + (Tn x 304.2) + (Cn x 289.2) + (Gn x 329.2) + 79.0ª
	An, Tn, Cn, and Gn are the number of each respective nucleotide within the polynucleotide.
	ªAddition of "79.0" to the M.W. takes into account the 5' monophosphate left by most restriction enzymes. 
	No phosphate is present at the 5' end of strands made by primer extension. 
	
	Initially, this program will only compute for the MW for DNA.
*/
#include <iomanip>
#include <iostream>
int main()
{
	float sum = 0;
	char bases[] = "aAcCgGtT";
	float values[] = 
	{
		313.2,313.2,289.2,289.2,329.2,329.2,304.2,304.2,
    };
  char sequence[100];
  std::cout<<"Please enter a DNA sequence (up to 100 letters): ";   // Input sequence
  std::cin>> std::setw ( 100 ) >> sequence;							// Assign it to sequence
  for ( int i = 0; sequence[i] != '\0'; i++ )  						// for loop, go through sequence until terminal
{
    for ( int j = 0; bases[j] != '\0'; j++ ) 						//within that loop, assign the correct mass
{ 	
      if ( sequence[i] == bases[j] ) 				                // Assigns mass to base pair
        std::cout << values[j] <<' '; 				                //displays masses of base pairs entered	
}	
} 

std::cout <<"Fragment Mass: " << sum;
  return 0;
}
Last edited on
closed account (o3hC5Di1)
Hi there

1
2
3
4
5
6
7
8
9
10
//line 33
for ( int j = 0; bases[j] != '\0'; j++ ) 
{ 	
    //this doesn't assign anything, it's an if-expression, returning either true (1) or false (0)  
    if ( sequence[i] == bases[j] ) // Assigns mass to base pair
        std::cout<< values[j] <<' '; 				//displays masses of base pairs entered	
		sum //this doesn't do anything
//also, if you really intend the if-statement above
//you will need curly braces when you use more than one conditional instruction depending on the condition.
}	


Unfortunately I have no clue what your program is supposed to be doing (I'm a complete science ignoramus), so I can't really tell you the exact solution for this.

Anyway, if you want to assign the value of base[j] to sequence[i], you will need to use sequence[i] = base[j];

If you want to add something to your sum, you would do sum += <value here>;

Hope that helps.

All the best,
NwN
Last edited on
I found a partial sum code that hypothetically can do what I want if I can figure out how to use it...

Basically, I'm trying to put these two codes together...

So far, it correctly inputs the values and displays them, but now I added a partial sum code which I would like to apply to the values that I've assigned to the letters that were typed in... i.e. run this program and type in aaaaa ....

you'll see the values displayed... Now I just want to partial sum those values :)

Thanks!

--J

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// partial_sum example
#include <iostream>
#include <functional>
#include <numeric>
#include <iomanip>

using namespace std;

int myop (int x, int y) {return x+y+1;}

int main () 
    {
float sum = 0;
	char bases[] = "aAcCgGtT";
	float values[] = 
	{
		313.2,313.2,289.2,289.2,329.2,329.2,304.2,304.2,        //these are the masses being assigned to the corresponding letters above
    };
  char sequence[100];
  std::cout<<"Please enter a DNA sequence (up to 100 letters): ";   // Input sequence
  std::cin>> std::setw ( 100 ) >> sequence;							// Assign it to sequence
  
	for ( int i = 0; sequence[i] != '\0'; i++ )  {  						// for loop, go through sequence until terminal
	for ( int j = 0; bases[j] != '\0'; j++ ) 	 {					 	
      if ( sequence[i] == bases[j] ) 				               
        std::cout << values[j] <<' '; 			 }	                //displays masses of base pairs entered	
        cout << endl; 							 }  
 

  
  int val[] = {1,2,3,4,5};             // I copy pasted this part in, just need to modify it... 
  int result[5];

  partial_sum (val, val+5, result);
  cout << "using default partial_sum: ";
  for (int i=0; i<5; i++) cout << result[i] << ' ';					
  cout << endl;
																	
}
Last edited on
My problem is, this partial sum code is for a total of 5 values in the array... My my amount of values is unknown. It could be 5, it could be 20... So I need a way to leave that group variable while still being able to tell it to terminate at the end etc....

so int val[] = {this needs to be the number values from the input sequence?}
int result [this needs to correspond to the amount of inputs];

partial_sum (val, val+total inputs, result)

etc...
total of 5 values in the array... My my amount of values is unknown


This is when you either look into dynamic memory, or vectors (much easier/safer to deal with). Vectors are basically arrays that will resize automatically when they need to, so you can stick 1000 values in there if you want.

http://www.cplusplus.com/reference/stl/vector/
So, I can input letters into a vector, have those letters be converted to specific masses that I specify, then call those numbers to be added in partial sum?

sounds like I need to start over... lol
What about strings? Can I input into a string and call from that string instead? Is that possible to use strings as a dynamic input/output?>

You could probably do with stringstream, but I wouldn't recommend it. It would be ugly, and vectors would be easier. Vectors are, for all intents and purposes, the same as using an array. But it's just flexible. Your code wouldn't have to change a whole, but using stringstream would mean a lot of changing the code.
I tried reading the vectors tutorial but couldn't understand it...

Is there any way that you could help me with the transition?

This is where I'm at right now:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// yo
#include <iostream>
#include <functional>
#include <numeric>
#include <iomanip>

using namespace std;

int main () 
{
    float sum = 0;
	char bases[] = "aAcCgGtT";   // These are uppercase and lowercase abbreviations of base pairs (DNA)
	float values[] = 
	{
		313.2,313.2,289.2,289.2,329.2,329.2,304.2,304.2, //These are the assigned masses to corresponding base pairs above
    };
	string sequence;
	std::cout<<"Please enter a DNA sequence (up to 100 letters): ";     // Input sequence
	std::cin >> sequence;												// Assign it to sequence
  
	for ( int i = 0; sequence[i] != '\0'; i++ )  {  						// for loop, go through sequence until terminal
	for ( int j = 0; bases[j] != '\0'; j++ ) 	 {					//within that loop, assign the correct mass 	
      if ( sequence[i] == bases[j] )              				                
        std::cout << bases[j] << ":" << values[j] << ' '; 			 }	                //displays masses of base pairs entered	
		cout << endl;							 }  
 
 std::cout<<"Your total sum is: " << sum;   // Output sum

  

																	
}
Here is how you add some random input into a vector

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <vector>
#include <iostream>

int main(){
std::vector<int> vec;
int input;

while(std::cin >> input)
{
     vec.push_back(input);  //push_back() basically just sticks the element of its argument at the next available space in the vector, and if it is full (or reached a predetermined size) it will resize.
}

//You can access elements of a vector just like arrays
std::cout << vec[3]; //3 is just an arbitrary number.

}

There isn't a whole lot to using vectors, one you get the hang of them, you should look at the iterator class and that will help with actually iterating through vectors. But the time being, this works just fine.
 
awesome. I'll give it a shot and see how it works out. Thanks!
No problem, just let us know if you have any more issues.
I would probably go with a std::map<string,int> to hold the base pairs and their values. Then just iterate over the string.
I would probably go with a std::map<string,int>


Definitely the cleaner route to go, yet could be more confusing to a beginner.
Shouldn't be much if any more confusing then using a vector. Here is how I would do it (there is no input validation in this example). Also note I used a map<char, float> since it makes more sense in this situation.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <map>
#include <string>

int main()
{
	std::map<char,float> m;

	m.insert(std::pair<char,float>('A',313.2));
	m.insert(std::pair<char,float>('C',289.2));
	m.insert(std::pair<char,float>('G',329.2));
	m.insert(std::pair<char,float>('T',304.2));

	std::string input;
	getline(std::cin,input);

	float sum = 0;
	for(int i = 0; i< input.length();++i)
	{
		sum+= m[toupper(input[i])];
	}
	std::cout<<sum<<std::endl;
	return 0;
}
Last edited on
Topic archived. No new replies allowed.