Transitioning to vectors

Hi there. I need to be able to use the front and back commands in vectors in order to modify only the first and last nucleotide of my input into my array...

What would be the easiest way to make this transition? Here's my code:

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
#include <iostream>
#include <vector>
using namespace std;

int main () 
{
    float sum = 0;														//Initial sum is 0.
	char DNAbases[] = "aAcCgGtT";   									// These are uppercase and lowercase abbreviations of base pairs (DNA)
	float values[] = 
	{
		313.21,313.21,289.18,289.18,329.21,329.21,304.20,304.20, 			//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; 								
	
	std::cout << "Size of sequence: " 
	<< sequence.size() << " nucleotides" 								//Display size of sequence
	<< std::endl; 															
	
																		
	
	for ( int i = 0; sequence[i] != '\0'; i++ )    				 	    // for loop, go through sequence until terminal
	{
	for ( int j = 0; DNAbases[j] != '\0'; j++ ) 	 					//within that loop, assign the correct mass 	
		{ 	
	 if ( sequence[i] == DNAbases[j] )              				                
			{
			std::cout << DNAbases[j] << " mass:" << values[j] << ' '; 		//displays masses of base pairs entered
			sum = sum + values[j];
			}
		}	                												
		cout << "partial sum: " << sum << endl;							//displays masses of base pairs entered
	}  
		std::cout<<"Your total sum is: " << sum;   						// Output sum																	
}
You have to use a vector? Why not the string functions append() and insert()?

A string is at heart a c-string, but as I see it, it is a vector<char> too. For other string functions:

http://www.cplusplus.com/reference/string/string/

Edit: If you do need to use a vector, I do think it would be a vector<char>. In this case, it isn't so easy to switch over. You will have to change your input method and for loop conditions. You don't really want to put a null terminator, because then you wont be able to use push_back() so easy. There is no straight forward way (nothing like cout >> stringVariable) to output the contents of a vector.
Last edited on
Don't really understand what you want with that code, but if you have a vector you can use the array syntax to manipulate the first and last object in a vector.

1
2
3
4
5
6
7
8
9
10
11
12
13
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);

v[0] = 100;//first 
v[2] = 300; //last

v[v.size()-1] = 3000;//last element (for when the vector size is variable)

//if the vector size is fixed (you don't add or remove elements from it)
int size = v.size();
v[size-1] = 3;
I'm trying to be able to subtract 75 from only the first and last numerical values, which were assigned based off the letter that they typed in (a,c,g,t) ... These numbers correlate to the masses of the DNA nucleotides... The first and last nucleotides of a synthetic piece of DNA have a different mass than the rest (because it's synthetic DNA) and I'm trying to figure out how to account for that...

Essentially, I'm not trying to use pushback, I'm trying to use back etc... Pointers could presumibly do the same thing.

Maybe I'll try pointers and remain in arrays... What do you think?


Last edited on
simple answer:
1
2
if (i == 0 || i == sequence.size() - 1)
  sum -= 75;


But you aren't using strings properly. Such would be the string way:
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
#include <iostream>
#include <string>
using namespace std;

int main(void)
{
	string sequence = "Gattaca";
	string dnaBase = "aAcCgGtT";
	double values[] = { 313.21,313.21,289.18,289.18,329.21,329.21,304.20,304.20 };
	double sum = 0;
	size_t position;

	for (string::iterator iter = sequence.begin(); iter != sequence.end(); iter++)
	{
		position = dnaBase.find(*iter);
		if (position != string::npos)
		{
			cout << dnaBase.at(position) << ") Mass: " << values[position] << ' '; 
			sum += values[position];
			if (iter == sequence.begin() || iter == sequence.end() - 1)
				sum -= 75;
		}
		cout << "Parital Sum: " << sum << endl;
	}
	cout << "Total Sum: " <<  sum << endl;
	cin.get();
	return 0;
}
Last edited on
Topic archived. No new replies allowed.