assigning values to the array position while using if else condition

Hi All,

I am trying to make cpp code in which it is reading a file like :
1
2
3
4
5
6
1       A
2       I
3       V
4       I
5       A
6       I

and the code will create a 2D matrix, of 3X3 dimensions. In this code, it is actually counting the number of times any character is a neighbour of other (i.e,in pair). The characters which is above the paired one should come in row, and the below character should come in column, and every time the pair repeats the frequency should increase by one.

The output should be like:
1
2
3
4
	A	V	I
A	0	0	2
V	0	0	1
I	1	1	0


The code which I am trying is something like this:
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
#include<cstdio>

#include<cstdlib>

#include<cstring>

#include<iostream>

#include<math.h>

int main(int argc,char *argv[])

{

  FILE *fp;

 struct pdb{int resnum;
 char residue[3];
 };
struct pdb seq[10000];

  char store[100];
int cnt=0,totatom,ALA[3],VAL[3],ILE[3];
  double distance;

int freq=0;

fp=fopen(argv[1],"r");

while(fgets(store,100,fp)!=NULL)

  {

    sscanf(store,"%d %s",&seq[cnt].resnum,seq[cnt].residue);

    cnt++;	

   }

   totatom=cnt;
for(int u=0;u<3;u++){ ALA[u];VAL[u];ILE[u];}
   
	   for (int i=0;i<(totatom-1);i++)

	   {

	      for(int j=0;j<totatom;j++)

	      {

		  if(seq[j].resnum = seq[i].resnum+1)

		  {
                   if(strcmp(seq[i].residue,"A")==0)
		     {
			      if(strcmp(seq[j].residue,"A")==0)
			      { freq=freq+1;
				u=0;
				ALA[u]=freq;}
				else if (strcmp(seq[j].residue,"V")==0)
			       {freq=freq+1;
				u=1;
				ALA[u]=freq;}
				else if (strcmp(seq[j].residue,"I")==0)
			       {freq=freq+1;
				u=2;
				ALA[u]=freq;}
		     }

               
                 if(strcmp(seq[i].residue,"V")==0)
               {
                              if(strcmp(seq[j].residue,"A")==0)
                              { freq=freq+1;
                                u=0;
                                VAL[u]=freq;}
                       		else if (strcmp(seq[j].residue,"V")==0)
                               {freq=freq+1;
                                u=1;
                                VAL[u]=freq;}
                       		else if (strcmp(seq[j].residue,"I")==0)
                               {freq=freq+1;
    			        u=2;
                                VAL[u]=freq;}
		     }
		    if(strcmp(seq[i].residue,"I")==0)
                     {
                              if(strcmp(seq[j].residue,"A")==0)
                              { freq=freq+1;
                                u=0;
                                ILE[u]=freq;}
                                else if (strcmp(seq[j].residue,"V")==0)
                               {freq=freq+1;
                                u=1;
                                ILE[u]=freq;}
                                else if (strcmp(seq[j].residue,"I")==0)
                               {freq=freq+1;
                                   u=2;
                              ILE[u]=freq;}
			}
                  }
	      }
for(int k=0;k<3;k++)
 
printf("%-8d%-8d%-8d%\n",ALA[k],VAL[k],ILE[k]);

}
}


I am not getting the result as required... Can anyone please help me to figure out the problem?
Last edited on
You have hardcoded three residues into your conditionals. That is boring, and will not cope with input that has more of them.


Rather than figuring out errors in your code, let me propose an approach:
1. Read the sequence. (You basically do that.)
2. Make a list of unique characters. (Sort and then strip duplicates.)
3. The size of the list defines the size of the square 2D array. (Dynamic allocation, initialize with 0.)
4. Convert the list into std::map; a lookup table where char key returns corresponding index.
In your 3x3 array the map would contain: {{'A', 0}, {'V', 1}, {'I', 2}}
(std::map is C++, but your code looks more like C.)

5. The Real Deal
5a. Store index of first residue char of sequence as 'prev'
5b. Loop the rest. For each residue 'curr', do increment: ++array[prev][curr]. Update prev=curr for next iteration.

You sequence AIVIAI would map to indices 021202. The loop would thus increment cells:
0. 02
1. 21
2. 12
3. 20
4. 02

6. Print array with nested loop and do include column and row headers.


[EDIT] Oh, you do read residues into char residue[3]; as C-string and then compare that who-knows-how-long string with single-letter C-string? I hope that you have single-letter codes in the input, and if you do, then there is a == for chars.
Last edited on
I am not very expert in c and cpp. But i wrote the code with the help of previous examples. So, I couldnot understand most of things you wrote. Can you tell me the easiest way to get that.
Copy-paste "programming" (without comprehension) does no good. The "easiest way" is to work hard; to read more (reference documentation, tutorials, about algorithms, data structures, ...) and think.
Topic archived. No new replies allowed.