*problems/questions.

Started by
13 comments, last by Talonius 19 years, 7 months ago
Sorry I didn't see this earlier charles you know my aim wnxdurfinator so if you need more help i'll help. I know a lot more c/c++ than i do web programming :)
Favorite Quotes:Gandalf: You cannot pass!|Smeagol: We don't need you!|Sloth: Hey you guys!|
Advertisement
/*convets user inputted DNA to in the end, convert to final tRNA.*///include important headers.#include <iostream>using namespace std;#include <conio.h>char entire_dna[400]; //limit entry to 400char entire_mrna[400];char entire_trna[400];int dna_array_bounds=0;int max_dab;//recieve char//return a charchar dna_input(){	char singledna;	cin >> singledna;	return(singledna);}void copy(const char *src, char *dest) //array1 is the copy source, 2 is copy to be made{	for(int x=0;x<max_dab;++x)	{		dest[x]=src[x];	}}//function to check that code is workingvoid checkdna(){	for(int i=0;i<max_dab;++i)		cout << entire_dna << " ";}void checkmrna(){	for(int i=0;i<max_dab;++i)		cout << entire_mrna << " ";}void checktrna(){	for(int i=0;i<max_dab;++i)		cout << entire_trna << " ";}int main(){	cout << "Enter values of a,g,t,c or d anything else\n\n";	//recieve DNA values from user	while(1)	{		//firststep: writes the DNA;		char userinput=dna_input();		if(userinput!='d' && dna_array_bounds<400)		{			//invalid entry check			if(userinput=='a' || userinput=='g' || userinput=='t' || userinput=='c')			{				entire_dna[dna_array_bounds]=userinput;				//increment to read everything.				++dna_array_bounds;			}		}		else			break;	}	max_dab=dna_array_bounds; //sets how many dna "stuff" are set	copy(entire_dna, entire_mrna);	//We recieved the DNA input and now we need to	//chnage it to mRNA	//Conversion: A-U,T-A,C-G	for(int j=0;j<max_dab;++j)	{		//need to dereference the pointer.		char mrna=entire_mrna[j];		switch(mrna)		{		case 'a': entire_mrna[j]='u';			break;		case 't': entire_mrna[j]='a';			break;		case 'c': entire_mrna[j]='g';			break;		case 'g': entire_mrna[j]='c';			break;		}	}	copy(entire_mrna, entire_trna);//repeat everything	//chnage it to tRNA	//Conversion: A-U,T-A,C-G and this time we have possible Us (instead of possible t's	//because of the helix model) in the beginning translation, so U-A needs to be checked too	for(int y=0;y<max_dab;++y)	{		//need to dereference the pointer.		char trna=entire_trna[y];		switch(trna)		{		case 'a': entire_trna[y]='u';			break;		case 'u': entire_trna[y]='a';			break;		case 'c': entire_trna[y]='g';			break;		case 'g': entire_trna[y]='c';			break;		}	}	cout << "DNA\n";	checkdna();	cout << "\n\nmRNA\n";	checkmrna();	cout << "\n\ntRNA\n";	checktrna();	getch();	return(0);}


What I don't understand is, within the copy() prototype, how is the actual values of the array being manipulated. The parameters with * derenferencing is the only way to make the program work (correctly) without errors but I see no passing by reference which is required when sending values through a function...n'est ce pas?
The "type calculus" here really is simpler than you're trying to make it. Notice how the solution mostly involved *removing* *'s and &'s.

Passing by (non-const) reference is needed to modify *the parameter*. You're not modifying the parameter; your parameter is a pointer, and you modify the *pointed-at data*. Which is perfectly kosher, since your arrays are arrays of non-constant char's.

When you have dest[x] = src[x] inside your copy() function, it's equivalent to *(dest + x) = *(src + x). This is the (rough) equivalence between pointers and arrays. Yes they are different, but the differences are usually just pedantic (unless you need to play tricks with sizeof).

But now that that's out of the way, I feel I must scold you for your evil, evil mixing of C++ with old-style C. This stuff really is quite a bit easier with std::string. It would handle the copying for you, without the need for an arbitrary limit on data length.
Quote:Original post by Zahlman
The "type calculus" here really is simpler than you're trying to make it. Notice how the solution mostly involved *removing* *'s and &'s.

Passing by (non-const) reference is needed to modify *the parameter*. You're not modifying the parameter; your parameter is a pointer, and you modify the *pointed-at data*. Which is perfectly kosher, since your arrays are arrays of non-constant char's.

When you have dest[x] = src[x] inside your copy() function, it's equivalent to *(dest + x) = *(src + x). This is the (rough) equivalence between pointers and arrays. Yes they are different, but the differences are usually just pedantic (unless you need to play tricks with sizeof).

But now that that's out of the way, I feel I must scold you for your evil, evil mixing of C++ with old-style C. This stuff really is quite a bit easier with std::string. It would handle the copying for you, without the need for an arbitrary limit on data length.


Could you eleborate on the evils?
Mostly bad style. As well, using old school C can easily bypass some of the safety mechanisms that C++ builds in to protect you from mistakes. C didn't consider those mistakes. And, believe it or not, learning C++ fully shows you that the really complicated stuff in C can be made a *lot easier*.

(References come to mind right off the top of my head.)
..what we do will echo throughout eternity..

This topic is closed to new replies.

Advertisement