Push back values in a struct

Hi guys, I'm currently working on a project for my intro programming class.
I currently have a global struct defined as so:

1
2
3
4
5
6
7
struct GeneSequence
{
	char nuc1;
	char nuc2;
	char nuc3;
	string aAcid;
};


Where nuc1-nuc3 represented a user entered genetic code, ex: AAG, UUU, etc. I also have an array of structs so that users can enter as many sequences as they please.

What I would like to do is add an option where you are able to "mutate" the genetic code by selecting a sequence, remove a letter, and push all of the other letters back 1 space in the struct array. For example:

Original sequence:
list[0].nuc1 = 'A';
list[0].nuc2 = 'U';
list[0].nuc3 = 'A';

list[1].nuc1 = 'G';
list[1].nuc2 = 'G';
list[1].nuc3 = 'G';

I want to delete the value of list[0].nuc3, shift all the values of list[1] back 1, so the end product looks like this:

list[0].nuc1 = 'A';
list[0].nuc2 = 'U';
list[0].nuc3 = 'G';

list[1].nuc1 = 'G';
list[1].nuc2 = 'G';
list[1].nuc3 = '-';

I'm very new with structs and programming in general, I want to know if what I'm proposing is possible, and a step in the right direction if it is. I'd really appreciate any help, thanks a lot :)
Last edited on
This could be done with some difficulty, but I suspect that there may be a more appropriate representation for your data. For example, you might want to change nuc1-3 to char nuc[3];

What should happen to the aAcid member when you mutate the genetic code? The answer to this question might help determine how the data should be structured.
I forgot to mention about the aAcid :)

I currently have a long if else statement, part of which looks like this:

1
2
3
4
if((tmp.nuc1 == 'U')&&(tmp.nuc2 == 'U')&&(tmp.nuc3 == 'U'||'C'))
	{
		tmp.aAcid = "PHE-";
	}


else if(...)

I have a menu function where it displays
cout << list[i].aAcid

With luck, if I am able to delete the value as per above, it will change the resulting aAcid string value.
Last edited on
I'd structure this differently. If you can move the nuc's around and the Acid is a function of a string of 3, I'd just make one large collection nucs to store them. If you use an array or vector you'll get fast random access but inserting or deleting in the middle will be expensive (because if you insert or delete the i'th element then you have to move all the elements after the i'th up or down in the array).

If you use a linked list then inserting/deleting in the middle is faster but you don't get random access and it will take up more space because of the links.

I'd then create a function to determine the Acid. It would take something that gets you the first element (an index if they're stored in an array or a pointer to the element if they are in a list). Then it would figure out the Acid string based on the next 3 values. Note that this would be a lot easier if you created a table that mapped 3 nuc values to an Acid string an then just looked up the right entry in the table.

Hope this helps.
(tmp.nuc3 == 'U'||'C')
should be
(tmp.nuc3 == 'U'||tmp.nuc3 =='C')
Topic archived. No new replies allowed.