Compare Two Strings with Array (I think)

closed account (4ybDGNh0)
kh;lkjl
Last edited on
Can you give us a sample output and your expectation the total score will be like?

+ Enter an DNA sequence : ???
+ Enter a DNA seqence for comparison : ???

The total score : ???
The thing is that you operate with variables before they have any usefull values.

The result of line 13 to 16 and 24/25 is useless because none of the variables are set to any useful value (for the debug version they might be 0, for the release version they contain any random value).

You won't get a sequence with this:
1
2
    cout << "Enter an DNA sequence: "<<endl;
    cin >> seq1; // Note that >> stops at the first whitespace 
Use getline(...) instead:

http://www.cplusplus.com/reference/string/string/getline/?kw=getline


This

if (c=='A'|| b == 'A') //match

does not check for match, actually you need and (&&):

if (c=='A' && b == 'A') //match

Notice that c and b does not have valid content due to premature use.
closed account (4ybDGNh0)
Sequence 1: ATGCTGACT
Sequence 2: CTTGAGACG
A/T score = 3*2= 6
C/G score = 3*3= 9
Non-match = 3*-2= -6
Total score = 9
closed account (4ybDGNh0)
updated code: I updated my code, but now my outputs are always AT score =2, CG score = 3, mismatch = -2 and total = 3....

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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <iostream>
#include <string>
using namespace std;

int main()
{
    string seq1; //first dna sequence
    string seq2; //string to compare to first sequence
    
    int match1, match2, match3, match4, match5 = 2;
    int mmatch1, mmatch2, mmatch3, mmatch4 = 3;
    int mismatch1, mismatch2, mismatch3, mismatch4, mismatch5, mismatch6, mismatch7, mismatch8, mismatch9 = -2;
    
    int i,j;
    

    
    cout << "Enter an DNA sequence: "<<endl;
    cin >> seq1;
    
    cout<<"Enter a DNA seqence for comparison:"<<endl;
    cin>> seq2;
    
    char c = toupper(seq1[i]);
    char b = toupper(seq2[j]);
    
    char a;
    
    for (i =0 ; i<seq1.length(); i++)
    {
        
        
        if (c=='A'&& b == 'A') //match
        {
            a+= match1;
        }
        else if (c=='A'&& b == 'T') //match
        {
            a+= match2;
        }
        else if (c=='A'&& b == 'C') //mismatch
        {
            a+= mismatch1;
        }
        else if (c=='A'&& b == 'G')//mismatch
        {
            a +=mismatch2;
        }
        else if (b=='A'&& c == 'A') //match
        {
            a+= match3;
        }
        else if (c=='T'&& b == 'A') //match
        {
            a+= match4;
        }
        else if (c=='T'&& b == 'T') //match
        {
           a+= match5;
        }
        else if (c=='T'&& b == 'G') //mismatch
        {
            a +=mismatch3;
        }
        else if (c=='G'&& b == 'G') //match
        {
            a+= mmatch1;
        }
        else if (c=='G'&& b == 'C')
        {
            a+= mmatch2;
        }
        else if (c=='G'&& b == 'A')
        {
            a +=mismatch4;
        }
        else if (c=='G'&& b == 'T')
        {
            a +=mismatch5;
        }
        else if (b=='G'&& c == 'C')
        {
            a+= mmatch3;
        }
        else if (b=='C'&& c == 'C')
        {
            a+= mmatch4;
        }
        else if (b=='A'&& c == 'C')
        {
            a +=mismatch6;
        }
        else if (b=='T'&& c == 'C')
        {
            a +=mismatch7;
        }
        else if (c =='T' && b == 'C')
        {
            a +=mismatch8;
        }
        else
        {
            
            a +=mismatch9;
        }
    }
    
    int ATscore = match1+match2+match3+match4+match5; //score for matching A/T values
    int CGscore= mmatch1+mmatch2+mmatch3+mmatch4; //score for matching C/G Values
    int misscore = mismatch1+mismatch2+mismatch3+mismatch4+mismatch5+mismatch6+mismatch7+mismatch8+mismatch9; //score for mismatches and gaps
    int totalscore = ATscore+CGscore+misscore; //total score
    
        cout<<"The A/T score is " << ATscore<<endl;
        cout<< "The C/G score is " <<CGscore<< endl;
        cout<<"The mistmatch/gap score is"<<misscore<<endl;
        cout<<"The total score is "<< totalscore<< endl;
        

    
    return 0;
}

Last edited on
This should do what you want.

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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <string>

using namespace std;

int main()
{
	std::string DNA1, DNA2;
	std::cout << "Enter an DNA sequence : "; std::cin >> DNA1;
	std::cout << "Enter a DNA seqence for comparison : "; std::cin >> DNA2;

	std::cout << std::endl;

	bool bMatched;
	int non_match = 0;
	int total_score = 0;

	for(decltype(DNA1.size()) i = 0; i < DNA1.size() && i < DNA2.size(); i++)
	{
		bMatched = false;
		switch(DNA1[i])
		{
			case 'A' : case 'T' : 
			{
				if(DNA2[i] == 'A') 
				{
					total_score += 2;
					bMatched = true;
				}
				else 
				if(DNA2[i] == 'T') 
				{
					total_score += 2;
					bMatched = true;
				}				
			}
			break;
			case 'G' : case 'C' : 
			{
				if(DNA2[i] == 'G') 
				{
					total_score += 3;
					bMatched = true;
				}
				else 
				if(DNA2[i] == 'C') 
				{
					total_score += 3;
					bMatched = true;
				}				
			}
			break;		
		}

		if(bMatched == false)
		{
			non_match -= 2;
			total_score -= 2;
		}
	}

	std::cout << "Non-match : " << non_match << std::endl;
	std::cout << "Total score : " << total_score << std::endl;

	return 0;
}


Enter an DNA sequence : ATGCTGACT
Enter a DNA seqence for comparison : CTTGAGACG

Non-match : -6
Total score : 9


http://cpp.sh/8uryt
Last edited on
For those who don't know what the assignment is like, here are the details.

mfarrelly wrote:
I am trying to write a code that you input one persons DNA then another Person's DNA and it scores it based on matches. For example if two T's (or A's) or T-A or A-T match the score is +2, if 2 C's (or G's) or GC or CG match the score is +3 and if there is not a match the score is -2. I am having trouble creating an array that will do different scores for different letters. Here is my code so far. Basically, everything is done except for the "Compare to " function.
I'd recommend you break down your code into functions to make it easier to read and follow the logic. Here's a simple way to organise your base comparison.
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
43
int compare_base(char a, char b)
{
    static const int matches = 6;
    static const string match[matches] = { 
        "AA", "AT", "TT", 
        "GG", "CC", "CG"
    };

    string base;
    // arrange alphabetically
    if (a < b)
    {
        base += a;
        base += b;
    }
    else
    {
        base += b;
        base += a;
    }

    // see if there's a match
    for (int i = 0; i < matches; i++)
    {
        // found a match
        if (base == match[i])
        {
            // A or T match is +2
            if (a == 'A' || a == 'T')
            {
                return 2;
            }
            // C or G match is +3
            else
            {
                return 3;
            }
        }
    }

    // mismatch
    return -2;
}

You use it like so:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int main()
{
    const string seq1 = "ATGCTGACT",
        seq2 = "CTTGAGACG";
    
    cout << "sequence 1 = " << seq1 << '\t' 
         << "sequence 2 = " << seq2 << '\n';
    
    int score = 0;
    for(int i = 0; i < seq1.length(); i++)
    {
        score += compare_base(seq1[i], seq2[i]);
    }
    
    cout << "score = " << score << '\n';
}


sequence 1 = ATGCTGACT	sequence 2 = CTTGAGACG
score = 9
Topic archived. No new replies allowed.