Computer Science Canada Cycle() and print() function |
Author: | tinhnho [ Mon Mar 22, 2004 1:01 pm ] |
Post subject: | Cycle() and print() function |
Hi everyone please help me this proplem,i'm really appreciate. Question: Write a cycle, and print function follow difination Quote: template<class TYPE> void cycle(TYPE& a,TYPE& b, TYPE& c) { //replace a's value by b's value and b's by c's and c's by a's }[/code] print() function which prints value a,b,c EX: print(a,b,c); Two generic function using example values of data typ char,int,double. Here what i did: [code] #include <iostream> #include <cstdlib> int main() { int i,j,e; cycle(i,j,e); print(i,j,e); system("PAUSE"); return 0; } template<class TYPE> void cycle(TYPE& a, TYPE& b, TYPE& c) { TYPE temp; //replace a's value by b's value temp = a; a = b; b = temp; //replace b's value by c's value TYPE temp1; temp1 = b; b = c; c = temp1; //replace b's value by c's value TYPE temp2; temp2 =c; c= a; a =temp2; } template<class TYPE> void print(TYPE& a, TYPE& b, TYPE& c) { cout<< a << b << c <<endl; } note: i sitll working on it,sorry if my code look messed up.Should i use this ?: [code] template <class TYPE> class triple { public: ........... private: TYPE a,b,c; }[/code] |
Author: | xtxnx [ Mon Mar 22, 2004 4:34 pm ] |
Post subject: | |
i think you should do : template<class type> class triple { } int main() { ..... return 0. } void cycle(Type& a, Type& b, Type& c) { ..... } void print(Type& a, Type& b, Type& c) { ............ } good luck |
Author: | Tony [ Mon Mar 22, 2004 5:29 pm ] | ||
Post subject: | |||
it looks like your code is wrong. You're replacing one variable with another, not switching them around. So when A becomes B, B does not become A (it becomes C) so
|
Author: | wtd [ Mon Mar 22, 2004 7:05 pm ] | ||
Post subject: | |||
You could certainly wrap it up in a class. This has one small error, but shift_right works.
|
Author: | tinhnho [ Mon Mar 22, 2004 8:40 pm ] |
Post subject: | |
thanks WTD, i changed and i compiled your program it work,and the output : 452 245 But are there anyway shorter than this?like i thought from beginning |
Author: | tinhnho [ Tue Mar 23, 2004 4:07 pm ] | ||
Post subject: | |||
Thanks wtd,unfortunatly my intructor doesn't accept that code.Here what i did,Please correct me if i was wrong,i'm appreacitate that.
note:when i compiling it,the screen is just empty,it doesn't tell me any error |
Author: | tinhnho [ Tue Mar 23, 2004 6:43 pm ] | ||
Post subject: | |||
Here is the original question: Create the print() function,that will print the values a,b,and c.Also create the cycle()function follow this difination
print() function which prints value a,b,c EX: print(a,b,c); Two generic function using example values of data typ char,int,double. |
Author: | wtd [ Tue Mar 23, 2004 7:09 pm ] | ||
Post subject: | |||
This acts a bit peculiarly when dealing with doubles, but I simplified it a bit by removing the inheritance.
|
Author: | tinhnho [ Wed Mar 24, 2004 9:34 am ] |
Post subject: | |
thanks wtd,but the assigment is required write cycle() function and print() |
Author: | tinhnho [ Wed Mar 24, 2004 11:38 am ] |
Post subject: | |
i got it,thanks wtd alot |
Author: | wtd [ Wed Mar 24, 2004 6:51 pm ] |
Post subject: | |
tinhnho wrote: thanks wtd,but the assigment is required write cycle() function and print()
That's only because your teachers are trying to teach you "C++ as a super-duper C", which is so blatantly false it makes me want to cry. And you're welcome. |
Author: | tinhnho [ Thu Mar 25, 2004 9:30 am ] |
Post subject: | |
i got it work today ,thanks wtd |