Tuesday, July 14, 2009

May I get C++ program for Genetic algorithm with binary coded single point cross over,?

May I get C++ program for Genetic algorithm with binary coded single point cross over, roulette-wheel selection operator , or any site can help me on this

May I get C++ program for Genetic algorithm with binary coded single point cross over,?
no
Reply:Yes

clematis

I need help with C++ Programming. We are using Dev C++?

I am so lost...


I am attempting to create a program that first asks to enter an


integer between 0 and 35. If the number is less than or equal to 9, the


program should output the number; otherwise it should output A for 10, B for


11, C for 12,...so on... and Z for 35. Then it tells me to use the cast operator, static_cast%26lt;char%26gt;(), for numbers %26gt;=10.





I am just lost period on how the static_cast function works...I have spent hours trying to find information...and figure it out!

I need help with C++ Programming. We are using Dev C++?
static_cast%26lt;int%26gt;("A") = 65





gives you the ASCII value of the character





Whole program:================================...





char array[25]={'A','B',.....'Z'}





cout %26lt;%26lt; "Enter number: ";


cin %26gt;%26gt; number





if(number %26lt;= 10){


cout %26lt;%26lt; number;


}


else


cout %26lt;%26lt; array[number- 10];





That is the basic program you need to write it will work just dress it up to look pretty
Reply:Okay.. So here's the thing.. Characters and integers are closely related. If you store the value "65" in a char you get the letter "A" if you store the value "65" in an integer you get the number 65. With that said, it should make a little more sense that you can cast an integer into a character and vise versa. Casting will work like this:





int myint = 65;





char ch = (char) myint; //casts myint to a character





cout %26lt;%26lt; "myint " %26lt;%26lt; myint %26lt;%26lt;"\n";


cout %26lt;%26lt; "myint casted to char" %26lt;%26lt; ch;








try that code in your program and see what happens.





The general form of casting is (%26lt;datatype%26gt;) %26lt;object%26gt;.








I'm a bit rusty with the C++ but hopefully this help.


C++ for beginners desperate for answers please help?

I really need your help please answers the questions 5 points for the best answer!





Explain the following bitwise operations in C++


a) and b) or





what is meant by the term zero indexed





what does the address operator do





list the permission values assigned to the number 0,1,2, and 4

C++ for beginners desperate for answers please help?
Q) Explain the following bitwise operations in C++





a)and





For each bit in the source and destination, "and"


results in a "1" only if *both* inputs are "1"





0 %26amp; 0 = 0


0 %26amp; 1 = 0


1 %26amp; 1 = 1


1 %26amp; 0 = 0








b) or





"or" results in a "1" if *either* inputs are "1"





0 | 0 = 0


0 | 1 = 1


1 | 1 = 1


1 | 0 = 1








Q. what is meant by the term zero indexed





It means that the first element of an array


is index "0". Some languages base their arrays


from element 1 (ie. Fortran). Others


allow the first element index to be user


specified (ie. Pascal).





Q. what does the address operator do





It returns the value of the "location" of a variable


in memory. It allows a "pointer" to refer to


a variable (by holding the variable's address).





Q. list the permission values assigned to the


number 0,1,2, and 4





I assume that this is about Unix permissions


which uses bits to specify read "r", write "w"


and execute (x).





0 = 000 ==%26gt; no read, write, or execute permission


1 = 001 ==%26gt; execute only (no read or write)


2 = 010 ==%26gt; write only, no read or exec


4 = 100 ==%26gt; read only, no write or exec
Reply:Ahhh... I'm always a bridesmaid but never a bride.. hehehe.. In any event...





1) BITWISE OPERATIONS:





The bitwise-AND operator ("%26amp;") compares each bit from the first operand to the corresponding bit in the second operand. If both bits are 1, the corresponding resulting bit is 1. Otherwise, the resulting bit is 0.


EXAMPLE:


1011


%26amp; 1101


----------


1001





The bitwise-inclusive-OR operator ("|") compares each bit from the first operand to the corresponding bit in the second operand. If either bit is 1, the corresponding resulting bit is 1. Otherwise, the corresponding resulting bit is 0.


EXAMPLE:


1011


%26amp; 1101


----------


1111





The bitwise-exclusive-OR operator("^") compares each bit of its first operand to the corresponding bit of its second operand. The result in each position is 1 if the two bits are different, and 0 if they are the same.





EXAMPLE:


1011


%26amp; 1101


----------


0110








2. ZERO INDEXED.


When you have an array, the first element is referenced in that array is the "zero" element. This is due to our ecumenically historic approach to numbers... Thanks to an Indian named Pingala and later by the Greeks, "0" (zero) is recognized in our numbering system. So if we count by ten's, the actual numbers are ZERO through 9:





C | X | I (excuse the roman numerals)


----------------


1 2 0





So in the "one's" column, we have "0" or our first non-number, number...


With ALL MY HOT AIR extinguished... here's a C++ reference using the "0" element:





int nMyIntArray[20];





nMyIntArray[0] = 12; // The first array element equals 12.


nMyIntArray[1] = 24; // The second array element equals 24.





I need a drink....





3. ADDRESS-OF OPERATOR.


The address-of operator ("%26amp;") provides the address of a operand. The address-of operator can only be applied to variables with fundamental, structure, class, or union types that are declared at the file-scope level, or to sub-scripted array references.








4. PERMISSION VALUES:


This one, you've got me stumped... Typical permissions implement a scheme with relative numeric definitions applying to the scheme design. So without knowing what system of security you're thinking of, these numbers have no apparent or inherent security rights...


C++ for beginners desperate for answers?

i really need your help badly





describe the differences between a compiler error and a link error





explain the following bitwise operations in c++


a) and b) or





what is meant by the term zero indexed





what does the address operator do





what is a UML





list the permission values assigned to the number 0,1,2, and 4

C++ for beginners desperate for answers?
You really ought to do this part yourself. With so many questions, you could have least presented us with your answers—right or wrong.





This is the easy part. If you don't truly take the time and energy to research and understand these fundamentals, and you continue with such laziness and indifference, you will most certainly get a poor grade.





What will you do when it comes time to design, write, and debug increasingly complex programs?





______________
Reply:A compiler error means an error was discovered while compilation which could be a syntax error (e.g. you forgot a semicolon) or a semantic error (e.g. you make an equality between a string and an integer).


A link error means an error was discovered while linking, consider the following code:


void Factorial(int num);


void main()


{


printf("%d", Factorial(4));


}


Here, you told the compiler that you have a function called "Factorial" and you tried to use it while you didn't specify how it works.





--------------------------------------...





Try to realize "AND" and "OR" logically, for example:


AND means that both conditions are true while "OR" means at least one of the conditions is true. By using such definition as bitwise operators:


5 AND 4 = 4


If you considered that "1" means "true" and 0 means "false" then by converting 5 and 4 to binary values:


1 0 1 (5)


1 0 0 (4)


--------


1 0 0 (4)


The most left bit in the result is the only bit which is "1" because the most left bit in both "5" and "4" is "1".


By ORing them:


1 0 1 (5)


1 0 0 (4)


-------


1 0 1 (5)


Because OR requires only one of the bits to be "0" so the first and last bit were "1".





----------------------





Zero indexed means that "0" represents the first item in a collection. For example when you want to get the value of the first element in an array you use the following way:


int x[4];


int y;


y = x[0];


Because arrays are zero indexed, so you refered to the first item by "0" not "1".





----------------------





The address operator shows where the variable is stored in the memory not the value. For example:


#include %26lt;stdio.h%26gt;


void main()


{


int x = 5;


printf("%p %d", %26amp;x, x);


}


This program will output where "x" resides in the memory and the value stored in that location.





------------------------------





UML is a modeling language which is used for designing programs, it stands for "Unified Modeling Language". It is mainly composed of some figures which represents the components of that program and the relation between them.





---------------------------------





Unfortunately I don't understand the last question, sorry.
Reply:A compile error occurs because the source code has a logic error in it. A link error occurs when you try to link you code to already compiled libraries





I never did get bitwise operators try this:


http://www.cprogramming.com/tutorial/bit...


personally i never use them.





Zero indexed (i think) means that an array (or array like data) is indexed starting at 0. So the first item would be name[0], the second item name[1] and so on. Note: There is a philosophicall diffrene from how we normally think. Exe first cow is cow 1 second 2 ect.





If its this "%26amp;" it returns the address of the variable that it is in front of instead of the value of the variable.





UML is a type of way to map out what your program is going to do. I don't use it so I would look here:


http://en.wikipedia.org/wiki/Unified_Mod...





Honestly I have no clue Good luck.
Reply:If you're talking about UNIX permissions, this site (http://www.perlfect.com/articles/chmod.s... should help.


C++ input question?

so here is the thing I have a program broken up into classes with there appropriate implementation files.





This problem im getting is that the in the main menu I created when the user inputs there choice from the menu ive done it like this:





basically its





cin %26gt;%26gt; choice;





switch(choice)


{


case 1:





break;





}





and so on.





But once they choice is made there is a conflict because the choice is taken in by the cin %26gt;%26gt; operator and the first thing taken in after the choice is made is using the getline because it needs to be able to take spaces. So the question is this....





Is there another way I can do my menu so that it doesnt use cin? I tried doing getline but it doesnt work with switch or if statements for some reason.





Since getline is from C not c++ is there an equevalent that will work for my situation???





thanks for reading!

C++ input question?
Q


But once they choice is made there is a conflict because the choice is taken in by the cin %26gt;%26gt; operator and the first thing taken in after the choice is made is using the getline


End Q


See http://www.daniweb.com/tutorials/tutoria... .





Q


Is there another way I can do my menu so that it doesnt use cin? I tried doing getline but it doesnt work with switch or if statements for some reason.


End Q


Let me guess, choice Is an integer? Because if you take a look at getline (http://www.cplusplus.com/reference/strin... ), you’ll see that it requires a string. There’s a C string version and then there’s a C++ string version. Either one you can convert to an integer for using in a switch statement. But you need to go the conversion route with getline.





Q


Since getline is from C not c++ is there an equevalent that will work for my situation???


End Q


Getline is from C++ not C. What references are you using? Get better ones.
Reply:Before the line


cin%26gt;%26gt;choice,


add the line


fflush(stdin);


There are chances that this may work out.

columbine

PLEASE HELP ME WITH THIS: using multiple if-else and ternary operator...?

make a c program that will ask the user to enter a number. the program should display "POSITIVE ODD","NEGATIVE ODD","POSITIVE EVEN","NEGATIVE EVEN"... depends on the number....


please use multiple if-else condition...





please!!!!! i just need it for our midterm....





also using ternary operator...











example output:


enter a number: 56


POSITIVE EVEN!





please help me with this peeps...





hope u understand...


thanks!!

PLEASE HELP ME WITH THIS: using multiple if-else and ternary operator...?
Using CONDITIONAL (TERNARY) operator:





#include %26lt;iostream.h%26gt;





void main(){


signed int num;





cout %26lt;%26lt; "Enter Number" %26lt;%26lt; endl;


cin %26gt;%26gt; num;


cout %26lt;%26lt; ((num %26gt; 0) ? "POSITIVE " : "NEGATIVE ");


cout %26lt;%26lt; ((num %26amp; 1) ? "ODD" : "EVEN");


}





--------------------------------------...





Using IF - ELSE statements:





#include %26lt;iostream.h%26gt;





void main(){


signed int num;





cout %26lt;%26lt; "Enter Number" %26lt;%26lt; endl;


cin %26gt;%26gt; num;


if(num %26gt; 0)


{


cout %26lt;%26lt; "POSITIVE ";


}


else


{


cout %26lt;%26lt; "NEGATIVE ";


}


if(num %26amp; 1)


{


cout %26lt;%26lt; "ODD";


}


else


{


cout %26lt;%26lt; "EVEN";


}


}








OUPUT:





Enter Number


32


POSITIVE EVEN





Enter Number


-33


NEGATIVE ODD
Reply:not know c, but if you can translate from basic...


cls


do


input "enter a number";num


a=int(num/2)


b=num-a


if a=b then a$="even" else a$="odd"


If num%26lt;0 then c$="negative"


if num%26gt;0 then c$="positive"


Print"number "num is "c$;" and";a$


loop until inkey$%26lt;%26gt;""
Reply:#include %26lt;iostream.h%26gt;





main() {


int num;


cout%26lt;%26lt;"Enter a number: "


cin%26gt;%26gt;num;


if(num %26gt;= 0) {


if(num%2 != 0) {


cout%26lt;%26lt;"POSITIVE ODD";


}


else {


cout%26lt;%26lt;"POSITIVE EVEN";


}


}


else {


if(num%2 != 0) {


cout%26lt;%26lt;"NEGETIVE ODD";


}


else {


cout%26lt;%26lt;"NEGETIVE EVEN";


}


}


}





Thats the basic structure, although you probably won't be able to copy/paste it and have it work. The guy above me doesn't use the mod operator (the % sign), so his code is slightly more complicated than mine. I think mine is what your teacher is looking for. You should really read a book or ask the teacher if you have further problems. One thing I've learned from going to Yahoo Answers for support on programming bugs is that they aren't as helpful as the teacher/professor.





Happy Programming.


Beginner C++ question?

Hi, I'm just learning how to use C++ and I'm having some problems with Objects and plugging variables from one class into another. Right now I'm designing a simple calculator program with various classes, each one associated with seperate operators, example Addition class, subtraction, etc. The driver class(Calculator in this case) will accept 2 values from the user and let him/her decide which operator to perform. If the user chooses to add them together then int Value1 and int Value2 should be forwarded to the Addition class which simply returns the two values after adding them together. My only problem is I'm not entirely sure how to do this, in Java I'm assuming I would say something along these lines. Addition new = Addition(Value1, Value2);. then just, return Addition;





Any ideas?

Beginner C++ question?
You could put the operands into the constructor and instantiate a new Addition each time (as in your example), or reuse a single Addition instance via method setOperands( int left, int right ), or methods setOperandLeft( int value ) setOperandRight( int value ).





In either case, to get the result you could have a method


int getResult()


which would generate the reult and return it.





example usage:


Addition newAddition = new Addition(Value1, Value2);


int result = additionInstance.getResult();


free(newAddition);


return result;


*or, under the reuse scenario:*


additionInstance.setOperands( value1, value2 );


display(additionInstance. getResult())





To really get into the spirit of things, Addition should be a subclass of Operation, or even of BinaryOperation which would be a subclass of Operation. The superClass would fully define the setOperands() methods and would include an abstract of the getResult() method. This would allow you to manipulate Operations in a general manner without knowing which operation was involved in every step.





Java would follow the same layout. Since free() is unnecessary, the first example usage shorthens to:


Addition newAddition = new Addition(Value1, Value2);


return additionInstance.getResult();





Hope this helps
Reply:C and C++ work with "pointers" and references (just C++)...so for your case you MUST make sure you pass your "int" values as "%26amp;Value1" and "%26amp;Value2" and set your param for the function with *Value1 and *Value2 .... or just make your function param as "%26amp;Value1" and "%26amp;Value2" and when the variables get passed in they will be passed by reference.





See this example: http://publib.boulder.ibm.com/infocenter...


Can someone help me on this? the genes a, b, c in the following table are from the lac operon of E. coli. one?

represents the repressor (i); another the operator (o); and the third, the structural gene for B. galactose (z). study the data and decide which gene (o,i, or z) each of the three is, and explain briefly in each case how you decided.


Genotype of Bacterium Activity of the z Gene


- Inducer +Inducer


a- b+ c+ + +


a+ b+ c- + +


a+ b- c+ - -


a+ b- c+/ a- b+ c- + +


a+ b+ c+/ a- b- c- - +


a+ b+ c-/a- b- c+ - +


a- b+ c+/a+ b- c- + +

Can someone help me on this? the genes a, b, c in the following table are from the lac operon of E. coli. one?
a- b+ c+ + +


a+ b+ c- + +


a+ b- c+ - -


Some questions related to c++?

i had been asked two questions by my teacher which i couldn't answer:


1.what is conditional operator (which i know) and COMPARE IT WITH OTHER SIMILAR CONSTRUCTION IN C++


2.HOW MANY DATA BYTES ARE USED TO STORE A USER DEFINED DATA TYPE ie.for STRUCTURES

Some questions related to c++?
You know a conditional operator is something like if () {} else {}





For #1., the teacher wants you to compare that to something like while {}, or for () {}.





For #2, if you make your own data type, like a structure, the size in bytes will be the size of all the components added together. So if you define a type called tINT32 that is really just an int,





#define tINT32 int





then it will be the same size as an int.





If you define a struct that has two ints in it, then it will be the size of an int * 2. If your compiler gives 32 bytes for an int, the struct will be 64 bytes.





If it has two ints and a 6 element array of chars, then the size will be (2 * 32) + (6*8)

carnation

C++ programing this is part of my study guide and im not sure if my answers are right could you help me out?

1.The arguments in a function call must match the arguments in


the function's header


a)only when the function call is in a different file from the function body.


b)in number, order, and variable name.


c)in number and order only.


d)in number, order, and type.


e)They don't have to match the header, only the function prototype.


2.To use I/O redirection to read from a file you need to:


a.declare a file variable in your program and openit


b.use the full path name with cin (a:\file1.dat.cin %26gt;%26gt;)


c.run your program from dos and add the file to be read from with the correct operator ( myprog %26lt; file1.dat)


d.any of the above will work


3.If you try to explicitly open an ifstream or ofstream file, how can you tell if the file opened successfully?


a.use the fail() function (method)


b.a return code of 1 indicates success


c.a pointer to a buffer that will receive data will be null if it fails


d.there is no way to tell other than to try to do input of output


e.a return code of 0 for success

C++ programing this is part of my study guide and im not sure if my answers are right could you help me out?
1-d


2-b


3-c





Please select best answer if all are correct


Another c++ problem.?

how to create the equivalent of a four-function calculator?the program should ask the user to enter a number, an operator, and another number. and then it should carry out the specified arithmetical operation: adding, subtracting, multiplying or dividing the two numbers.


waa..i'm really not good in c++. somebody please help!


well, the output is suppose to look something like this:


enter first number, operator, second number: 10 / 3


answer = 3.333333


do another (y/n)? y


enter first number, operator, second number: 12 + 100


answer = 112


do another (y/n)?n


that's about it..do help..

Another c++ problem.?
just take an F
Reply:Well, one way that you can do this is to,





read in the line of input from the user,


read in the three various values from the string,


determine the operator from the second input,


then you will be able to determine the answer.
Reply:[sum]


Print "Enter first number, operator, second number: ";


input a, b$, c


answer = a b$ c


print "answer is ";answer


print


input "do another (y/n)?;choice$


if choice$ = "y" then [sum] else end
Reply:#include%26lt;iostream.h%26gt;


#include%26lt;conio.h%26gt;


void main()


{


cout%26lt;%26lt;"Four function Calculator";


char *p;


double num1, num2;


num1=num2=0.0;


char op, continue;


do{


cout%26lt;%26lt;"enter first number, operator, second number: "


cin%26gt;%26gt;p;





while(p[i]!="+" || p[i]!="-" || p[i]!="*" || p[i]!="/")


num1=(num1*10 )+ p[i];


op = p[i];


i++;


while(p[i]!='\0')


num2=(num2*10 )+ p[i];





// now we have got all info


switch(op)


{


case '+':


cout%26lt;%26lt;"answer = "%26lt;%26lt;num1+num2;


break;


case '-':


cout%26lt;%26lt;"answer = "%26lt;%26lt;num1-num2;


break;


case '*':


cout%26lt;%26lt;"answer = "%26lt;%26lt;num1*num2;


break;


case '/':


cout%26lt;%26lt;"answer = "%26lt;%26lt;num1/num2;


break;


default:


cout%26lt;%26lt;"Invalid Operator";


break;


}


cout%26lt;%26lt;"do another (y/n)?";


cin%26gt;%26gt;continue;


}while(continue=='y' || continue=='Y')


}














// please mail me if u find any issues


PLease provide a short explanation with each answer of my C++ program....?

Assuming Point is a class type with a public copy constructor, iden¬tify each use of the copy constructor in this program fragment:


Point global;


Point foo_bar(Point arg)


{


Point local = arg;


Point *heap = new Point(global);


*heap = local;


Point pa[ 4 ] = { local, *heap };


return *heap;





______________________________...





Exercise 13.10: Define an Employee class that contains the employee's name and a unique employee identifier. Give the class a default constructor and a constructor that takes a string representing the employee's name. If the class needs a copy construc¬tor or assignment operator, implement those functions as well.





______________________________...








Among the fundamental operations a pointer supports are dereference and arrow. We can give our class these operations as follows:


class ScreenPtr { public:


// constructor and copy control members as before


Screen %26amp;operator*() { return *ptr-%26gt;sp; }


Screen *operator-%26gt;() { return ptr-%26gt;sp; }


const Screen %26amp;operator*() const { return *ptr-%26gt;sp; } const Screen *operator-%26gt;() const { return ptr-%26gt;sp; } private:


ScrPtr *ptr; // points to use-counted ScrPtrclass








Exercise 14.20: In our sketch for the ScreenPtr class, we declared but did not define the assignment operator. Implement the ScreenPtr assignment operator.





______________________________...





Exercise 14.21: Define a class that holds a pointer to a ScreenPtr. Define the over¬loaded arrow operator for that class.


______________________________...








Exercise 15.4: A library has different kinds of materials that it lends out—books, CDs, DVDs, and so forth. Each of the different kinds of lending material has different check-in, check-out, and overdue rules. The following class defines a base class that we might use for this application. Identify which functions are likely to be defined as virtual and which, if any, are likely to be common among all lending materials. (Note: we assume that LibMember is a class representing a customer of the library, and Date is a class representing a calendar day of a particular year.)


class Library { public:


bool check_out(const LibMemberk);


bool check_in (const LibMember%26amp;);


bool is_late(const Date%26amp; today);


double apply_fine();


ostream%26amp; print(ostream%26amp; = cout);


Date due_date() const;


Date date_borrowed() const;


string title 0 const;


const LibMember%26amp; member() const;





______________________________...








Exercise 15.8: Given the following classes, explain each print function:


struct base {


string name () { return basenatne; }


virtual void print(ostream %26amp;os) { os « basename; } private:


string basename;


};


struct derived {


void printO { print (ostream %26amp;os) ; os « " " « mem; } private:


int mem;


};





If there is a problem in this code, how would you fix it?





______________________________...








Exercise 15.13: Given the following classes, list all the ways a member function in Cl might access the static members of ConcreteBase. List all the ways an object of type C2 might access those members.


struct ConcreteBase {


static std::size_t object_count(); protected:


static std::size_t obj_count;


};


struct Cl : public ConcreteBase


{ /* . . .*/};


struct C2 : public ConcreteBase


{ /* ...*/};





______________________________...





Exercise 15.25: Assume Derived inherits from Base and that Base defines each of the following functions as virtual. Assuming Derived intends to define its own ver¬sion of the virtual, determine which declarations in Derived are in error and specify what's wrong.


(a)Base* Base::copy(Base*);


Base* Derived::copy(Derived*);


(b)Base* Base::copy(Base*) ;


Derived* Derived::copy(Base*) ;


(c)ostreamk Base::print(int, ostream%26amp;=cout);


ostream%26amp; Derived::print(int, ostream%26amp;);


(d)void Base::eval() const;


void Derived::eval();

PLease provide a short explanation with each answer of my C++ program....?
And what makes you think that people here have enough free time to do your homework, when all you could do was to copy and paste it here?


Can a C++ reference hold a reference to a child of it's class?

For C++ gurus.





Consider the following code snippets:





ostream %26amp; outfile=cout; // assign stdout to a reference of ostream





Then I may want to do something like this:


ofstream* alternate_op=new ofstream("my_output.txt");


outfile=(ostream %26amp;) *alternate_op;





Now outfile points to my alternate output file instead of stdout. Fine. In MS Visual C++ 6.0 this works out just the way I want it. But other compilers (such as MingW for Windows) complain with messages like:





741 C:\Dev-Cpp\include\c++\3.4.2\bits\ios_ba... `std::ios_base%26amp; std::ios_base::operator=(const std::ios_base%26amp;)' is private





So, is my solution the best one? Or is there a better way? I want to be able to dynamically use either stdout (cout) or my own o/p file stream along with operator%26lt;%26lt; to format output without a lot of fooling around about which one I'm really using. And I'd like my solution to be as portable between compilers as possible.

Can a C++ reference hold a reference to a child of it's class?
Biting my tongue and not taking an obligatory swipe at VC.





Assuming you can, just change the reference to a pointer and you are good to go.








int main ()


{


ostream* outfile = %26amp;cout; // assign stdout to a POINTER to ostream





*outfile %26lt;%26lt; "Hello\n";





ofstream* alternate_op=new ofstream("my_output.txt");





outfile = alternate_op;





*outfile %26lt;%26lt; "World" %26lt;%26lt; endl;





}
Reply:You could run into a dangling pointer problem.
Reply:In C++ you can't reassign a reference. I don't know why VC6 allows you to do it, but it shouldn't. When you assign to a reference, you actually assign to the value being referenced. In this case your code is trying to change the value of cout.

pansy

PLEASE: I Need To Write This C++ Program ..?

Please Guys, Can You Tell Me How Exactly I Can Write The Source For This Program:





Write a Program that prompts the user to input an integer between 0 and 35. If the number is less than or equal to 9, the program should output the number; otherwise, it should output A for 10, B for 11, C for 12, . . ., and Z for 35. (Hint: Use the cast operator, static_cast%26lt;char%26gt; ( ), for numbers%26gt;=10.

PLEASE: I Need To Write This C++ Program ..?
#include%26lt;iostream.h%26gt;


main()


{


int n,t;


t=55;


cout%26lt;%26lt;"enter a number between 1 and 35";


cin%26gt;%26gt;n;


if((n%26gt;0)%26amp;%26amp;(n%26lt;10))


cout%26lt;%26lt;n;


else if((n%26gt;10)%26amp;%26amp;((n%26lt;36))


cout%26lt;%26lt;(char)(n+t);


else


cout%26lt;%26lt;"enter valid number";


return 0;


}


Why cant i use tehse in c++ programming?

why cant i use == or != operators for floating point numbers in c++ programmes?

Why cant i use tehse in c++ programming?
Very good question. To understsnd the answer, you need to know how the float numbers are saved inside the computesr. The float numbers are saved as two different numbers, fraction and power. e.g. 0.05 will be first translated as 5 * 10^-2. And now the numbers 5 and -2 will be saved together. Clearly comparing two float numbers takes comparing these the fraction and the power part. Normally its not the problem with the power, but the fraction part. The computaion of this can make it different. So 5.0 and 5.00 could be different. Infact 5.0 and 4.9999 could be same.
Reply:because floating point are approximated upto certain decimal points, so == does not make sense rather use abs(f1-f2) %26lt; a small number


Question for an experienced Ham Radio Operator?

I am a ham radio operator myself and i have a question. Does is matter what set of the phonetic alphabet you use? Like the military one (Alpha = a, Bravo = b, Charlie = c, Delta = d) or the Law Enforcement one (Adam = a, Boy = b, Charles = c David = d). I use the military one, but i'm starting to get used to the Law Enforcement one seeing as i'm going to college soon to become an officer...

Question for an experienced Ham Radio Operator?
No it doesn't matter as long as the meaning is clear and others can understand your phonetics. The older generation of Hams (think old timers) use the form that the police use as that was the first version that the military used. Newer Hams usually use the current military phonetics.





All in all, just use what is easiest for you!





73,


Ke5bkd





Oh, and good luck on your way to being an officer!! Hope to make it someday myself!
Reply:right, it shouldn't really make much difference which to use so long as the phonetics are understood - uh except that internationally hammers take to the military version more n i believe that's one way of bridging the gaps or coming-through or meeting halfway
Reply:You should use the "military one". Actually it is the ITU phonetics. The reason is that it is internationally understood, even by folks who don't speak English well.
Reply:As a Radio Ham Operator for 50 years, it is fairly standard to use the 'military' phonetics on the air. However, any phonetic is better than none!! Some folks just cannot accept change. I generally use whatever comes to mind. If someone doesn't like it, they can go back in their cave!! (Airlines also still use the military type also. Towers, ATIS, etc.) It's refreshing to see that 'English' is kind of the Universal language on CW around the world. (I don't even get that in my local stores!!)


73.. N1QQ


Name 4 operators which can be used in C++ but not in C.?

I found a page for you.





It looks like any of the following:


scope resolution (::), global (::), constructor (()), new (new), delete (delete), throw (throw), or member selection (-%26gt; or .)





Another page I found also had insertion (%26lt;%26lt;) and extraction (%26gt;%26gt;).





Hope that helps!

Name 4 operators which can be used in C++ but not in C.?
All operators that refer to class members are C++ only! In C a class is undefined!
Reply::: , .* , -%26gt;* ,##

floral centerpieces

Boolean algebra, what is a "unary" operator '?

Formally, a Boolean algebra is a mathematical system consisting of a set of elements, B={0,1}, together with two binary operations, (denoted by the symbols + and •) There is also a unary operator ′.





I understand the laws and the operations ( and, or, not). I just need help understanding this unary ' thing.





For instance : Z=( A ' • B ' ) '+ C ' ) '





Also : Z = ( A • B ' ) ' + B ) • C





- I thought it was the "not" symbol until i saw these. Please help me understand.

Boolean algebra, what is a "unary" operator '?
"Unary" just means "operates on one thing only." Negation ("-x") is a unary operator that you often see in standard math.





"Not" is an example of a unary operator. It looks to me as if they mean + (or), * (and), and ' (not) for their operations.... though the parentheses are not balanced in your examples.





Z=( A ' • B ' ) '+ C ' ) '





I would read that as:





Z = not{ not[ not(a) AND not(b) ] or not(c) }





PS - You can't just "ignore the extras." All of those "nots" have meaning.





For example, "not[ not(a) AND not(b) ]" is shown in this table, along with intermediate calculated values:





a, b, a', b', (a' and b'), (a' and b')':


T, T, F, F, F, T


T, F, F, T, F, T


F, T, T, F, F, T


F, F, T, T, T, F





Note that "not( not(a) AND not(b) )" is exactly the same as "a OR b" (false only when a and b are both false).





Personally, I would simplify:





Z = ( ( A ' • B ' ) '+ C ' ) '





... as:





Z = ( (A + B)' • C )
Reply:A unary operator acts on one element (negation or "not" in Boolean algebra)


A binary operator acts on two elements (like + or *)
Reply:You are correct ' is the symbol being used for NOT.





AND (*) and OR (+) are binary operators because they work with TWO things [operands] e.g. T * F gives F





NOT (') is a unary operator because it only needs ONE operand e.g. T' gives F





-------------





Unary operators usually take precedence over binary operators. So with A * B' , B' is evaluated first.





With the problems like you gave as examples, its best to work from the inside of the parenthesis out e.g. in ( A • B ' ) ' + B ) • C do the A*B' first ... another example...


    (F' * T)' -%26gt; (T * T)' -%26gt; (T)' -%26gt; F





OR use some of the properties of Boolean algebra to rearrange things


    e.g. [distributing the not]    (A * B)' = A' + B'


    also (A * B')' = A' + B since B'' = B


Please give me short note of my C++ program...?

Problems are taken from C++ Primer book





-Ensure you can complete prior to accepting and emailing me.





-If you do not provide your price to do this I will not respond…





-I require this completed in 1 week.





Exercise 13.3: Assuming Point is a class type with a public copy constructor, iden¬tify each use of the copy constructor in this program fragment:


Point global;


Point foo_bar(Point arg)


{


Point local = arg;


Point *heap = new Point(global);


*heap = local;


Point pa[ 4 ] = { local, *heap };


return *heap;





______________________________________...





Exercise 13.10: Define an Employee class that contains the employee's name and a unique employee identifier. Give the class a default constructor and a constructor that takes a string representing the employee's name. If the class needs a copy construc¬tor or assignment operator, implement those functions as well.





______________________________________...








Among the fundamental operations a pointer supports are dereference and arrow. We can give our class these operations as follows:


class ScreenPtr { public:


// constructor and copy control members as before


Screen %26amp;operator*() { return *ptr-%26gt;sp; }


Screen *operator-%26gt;() { return ptr-%26gt;sp; }


const Screen %26amp;operator*() const { return *ptr-%26gt;sp; } const Screen *operator-%26gt;() const { return ptr-%26gt;sp; } private:


ScrPtr *ptr; // points to use-counted ScrPtrclass








Exercise 14.20: In our sketch for the ScreenPtr class, we declared but did not define the assignment operator. Implement the ScreenPtr assignment operator.





______________________________________...





Exercise 14.21: Define a class that holds a pointer to a ScreenPtr. Define the over¬loaded arrow operator for that class.


______________________________________...








Exercise 15.4: A library has different kinds of materials that it lends out—books, CDs, DVDs, and so forth. Each of the different kinds of lending material has different check-in, check-out, and overdue rules. The following class defines a base class that we might use for this application. Identify which functions are likely to be defined as virtual and which, if any, are likely to be common among all lending materials. (Note: we assume that LibMember is a class representing a customer of the library, and Date is a class representing a calendar day of a particular year.)


class Library { public:


bool check_out(const LibMemberk);


bool check_in (const LibMember%26amp;);


bool is_late(const Date%26amp; today);


double apply_fine();


ostream%26amp; print(ostream%26amp; = cout);


Date due_date() const;


Date date_borrowed() const;


string title 0 const;


const LibMember%26amp; member() const;





______________________________________...








Exercise 15.8: Given the following classes, explain each print function:


struct base {


string name () { return basenatne; }


virtual void print(ostream %26amp;os) { os « basename; } private:


string basename;


};


struct derived {


void printO { print (ostream %26amp;os) ; os « " " « mem; } private:


int mem;


};





If there is a problem in this code, how would you fix it?





______________________________________...








Exercise 15.13: Given the following classes, list all the ways a member function in Cl might access the static members of ConcreteBase. List all the ways an object of type C2 might access those members.


struct ConcreteBase {


static std::size_t object_count(); protected:


static std::size_t obj_count;


};


struct Cl : public ConcreteBase


{ /* . . .*/};


struct C2 : public ConcreteBase


{ /* ...*/};





______________________________________...





Exercise 15.25: Assume Derived inherits from Base and that Base defines each of the following functions as virtual. Assuming Derived intends to define its own ver¬sion of the virtual, determine which declarations in Derived are in error and specify what's wrong.


(a)Base* Base::copy(Base*);


Base* Derived::copy(Derived*);


(b)Base* Base::copy(Base*) ;


Derived* Derived::copy(Base*) ;


(c)ostreamk Base::print(int, ostream%26amp;=cout);


ostream%26amp; Derived::print(int, ostream%26amp;);


(d)void Base::eval() const;


void Derived::eval();

Please give me short note of my C++ program...?
This kind of stuff, where you pay for someone who codes for you would be best accomplished at http://www.rentacoder.com or http://www.guru.com
Reply:And that is a fact since it goes against community guidelines and rules and will be *REPORTED*.
Reply:looks good to me


911 operator?

Did any one c the news about the kid that called 911. He told them that his mother passed out and is lying on the floor. The operator assumed it was prank and hung up. 3 hours later the boy called claiming his mom is dead and they still assumed it was a prank til they sent a patrol car to check it out. Did this piss off anyone else? I mean the kid was 5 years old and he had did the right thing and had to watch his mom die right before his eyes.

911 operator?
wow that is screwed up. I think every call should be taking very seriously
Reply:I hope the 911 operator lost his or her job. This poor little boy tried to do the right thing, and now he has to grow up without a mom. Yes, it totally pisses me off!
Reply:that is truly messed up poor kid
Reply:This happens, unfortunately, every once in a while. They are incompetent operators and hopefully are fired from their jobs when they do this. This happened several years ago when 911 was first up and running. A guy called to say that his mom was having a heart attack. The operator didn't believe him. The guy called back, and of course, was upset, so the lady told him he was being rude and to calm down, then hung up again. When he called back again, infuriated and panicked, the operator got her supervisor on the phone and he told the man to stop being so rude. The woman died after about 45 minutes of suffering.


Things have mostly improved since then, but every once in an unfortunate and sad while, an insensitive nut gets a hired at a job that requires sensitivity and a little common sense. There was the emergency room situation in California, where a woman throwing up blood for hours in the emergency wating room, died of a perforated bowel, virtually ignored by doctors and nurses, though people kept trying to get her help.


And right here in my city, a young woman died when she was ignored my paramedics who failed to check her pulse at the scene of an accident. They "assumed" she was dead, because of the nature of her injury, and left her for hours, unattended. Even the police officer on the scene said he believed she was alive and asked paramedics to check on her. They said they didn't need to, bc they knew she was dead. She arrived at the hospital alive, but died the next day.


Yes, it's very sad and angering.
Reply:I did not hear of this specific case, but have heard of others like that. 9-1-1 prank calls are very common, more common than people realize and sometimes it hard to determine what is a legitimate call and what is not. It is also not uncommon for pranksters to call to pull the police away from a certain area so that they can commit crimes or whatever. That being said, the operators are trained to tell the difference between a prank call and a legitimate call. If she would have talked with the child a bit more, she probably would have realized that he was young and that the call was legitimate. Very sad.
Reply:Too bad people are stupid, right?
Reply:I HEARD ABOUT IT 3 DAYS AGO AND IT WAS NOT A 9-1-1, HE HAD(ACCORDINGLY) DIAL THE AMBULANCE COMPANY AND ASKED FOR ASSISTANCE. THEY THOUGHT IT WAS A PRANK CALL AND A RECALL RAISE SUSPICION AND THEY ASKED THE POLICE TO CHECK IT OUT.


911 IS A HIGH PRIORITY LINE. ALL CALL ARE LOGGED FOR A 72 HOURS PERIOD AND REVIEWED CONSTANTLY BY A LINE SUPERVISOR.


I HIGHLY DOUBT THE CALL WENT TO 911 SINCE THEY HAVE TO RESPOND-PRANK OR NOT?


C or C++ code?

a) How can create the equivalents of a four-function calculator. The program should request the user to enter a number, an operator, and another number. (Use floating point). It should then carry out the specified arithmetical operation: adding, subtracting, multiplying, or dividing the two numbers. Use switch case statement to select the operation. Finally display the result.


When it finishes the calculation, the program should ask if the user wants to do another calculation. The response can be ‘y’ or ‘n’.


Sample Output


Some sample interaction with the program might look like this:


Enter first number, operator, second number: 10/3


Answer: 3.33333


Do another (y/n)?y


Enter first number, operator, second number: 12+100


Answer: 112


Do another (y/n)?n





b) Write C++ code to replace two variables A and B with each other.


Given A=2, B=1, you need to swap the values of A and B without using any third variable.





Sample Output


A =2


B=1


After swapping


A=1


B=2

C or C++ code?
a)


#include %26lt;iostream%26gt;


using std;


#define f(n,o) float n (float x, float y) {return x o y;}


f(p,+);f(m,-);f(t,*);f(d,/); static float (*fs)(float x, float y)[] = {p,m,t,d};


static char *os="+-*/";


int main(){do{


cout%26lt;%26lt;"Enter: ";float x,y;char c;cin%26gt;%26gt;x%26gt;%26gt;c%26gt;%26gt;y;


cout%26lt;%26lt; "Result: "%26lt;%26lt; fs[strchr(os, c) - os](x,y)%26lt;%26lt; endl%26lt;%26lt;"more? ";cin%26gt;%26gt;c;}while(c=='y');}








b)


a+=b;a-=b=a-b;





happy?
Reply:a = b + a;


b = a - b;


a = a - b;





or





a = a + b - (b = a)
Reply:b) I dont think is posible without another variable





a)


int a ,b


string operator


string yesno="y"


int answer








dowhile(yesyno=="y")


{


enter a


enter operator


enter b








switch(operator)


//your case %26amp; operation on 2 numbers things go in here








print answer


print do another


input yesno


}


dont recal much c++ synatx but thats it

wedding florist

Need help creating c++ set class?

I'm creating a new class for c++ based on Set theory. I was given Set.h and have to create Set.cpp. I need help with the cardinality function. I'm uncertain how to do it. Here is the program:





#include %26lt;iostream%26gt;


#include "Set.h"





// Constructor, sets _cardinality to zero


Set::Set(){


_cardinality=0;


}





// returns true if its argument is an element of the set


bool Set::isElement(const int num) const{


for(int i=0; i%26lt;_cardinality; i++){


if(_element[i]==num){


return true;


}


else{


return false;


}


}


}





// adds the argument to the set


void Set::add( int num){


for(int i=0; i%26lt; _cardinality; i++){


if(_element[i]==num){


return;


}


else{


_element[_cardinality]=num;


_cardinality++;


i++;


}


}


}





// removes the element from the set


void Set::remove(int num){


for(int i=0; i%26lt; _cardinality; i++){


if(_element[i]!=num){


return;


}


else if(_cardinality==1){


clear();


}


else{


_element[i]=_element[i+1];


_cardinality--;


}


}


}





// resets cardinality to zero (effectively emptying the set)


void Set::clear(){


for(int i=0; i%26lt;_cardinality-1; i++){


_element[i]=0;


}


}





// returns the cardinality of the set


unsigned int Set::cardinality()const {


int count=0;


count=


return count;


}





// prints out the contents of the set to the terminal, all elements,


// comma separated list, no line breaks


void Set::print() const {


for(int i=0; i%26lt; _cardinality; i++){


cout %26lt;%26lt; _element[i] %26lt;%26lt;", ";


}


cout %26lt;%26lt; endl;





}





// returns a set object representing the intersection


// of its two arguments


Set operator*( const Set%26amp; a, const Set%26amp; b)


{


int n=0;


Set rv;


for( int i = 0; i %26lt; a.cardinality(); i++ ){


for(int j=0; j%26lt; b.cardinality(); j++){


if(a._element[i]==b._element[j]){


rv._element[n]=a._element[i];


n++;


}


}


}


rv.print();


}





// returns a set object representing the union


// of its two arguments


Set operator+( const Set%26amp; a, const Set%26amp; b)


{


Set u, x;


int sz_u=0, sz_x=0, sz_d=0;


//compute union


for(int i=0; i%26lt;sz_u; ++i) {


u._element[i] = a._element[i];


++sz_u;


}


int sz_u_old = sz_u;


for(int i=0; i%26lt;sz_x; ++i) {


bool found=false;


for(int j=0; j%26lt;sz_u_old; ++j) {


if (b._element[i] == u._element[j]) {


found = true;


break;


}


}


if (!found) {


u._element[sz_u] = b._element[i];


++sz_u;


}


}


for(int i=0; i%26lt;sz_u; ++i) {


cout %26lt;%26lt; u._element[i] %26lt;%26lt; ", ";


}


cout %26lt;%26lt; endl;


}





// prints out the contents of the set to the terminal, all elements,


// comma separated list, no line breaks


ostream%26amp; operator%26lt;%26lt;(ostream%26amp; output, const Set%26amp; a){


for(int i=0; i%26lt;a.cardinality(); i++){


output %26lt;%26lt; a._element[i] %26lt;%26lt; ", ";


}


}





// adds second argument to the set in the first argument


void operator+=(Set%26amp; a, const int rhs){


Set num;


for(int i=0; i%26lt;a.cardinality(); i++){


num._element[i]=a._element[i]+rhs;


}


num.print();


}





// removes second argument from the set in the first argument, returns


// true if an element was actually removed, otherwise returns false


bool operator-=(Set%26amp; a, const int rhs)


{


Set num;


for(int i=0; i%26lt;a.cardinality(); i++){


num._element[i]=a._element[i]-rhs;


if(num._element[i]==0){


num.remove(num._element[i]);


return true;


}


else{


return false;


}


}


num.print();


}





// returns true if the left side is a subset of the right


bool operator%26lt;(const Set%26amp; rhs, const Set%26amp; num)


{


for(int i=0; i%26lt;num.cardinality(); i++){


for(int j=0; j%26lt;num.cardinality(); j++){


if(num.isElement(rhs._element[j])==true)...


return true;


}


else{


return false;


}


}


}


}





// returns true if the left side is an element of the right


bool operator%26lt;(const int%26amp; rhs, const Set%26amp; num)


{


for(int i=0; i%26lt;num.cardinality(); i++){


if(num.isElement(rhs)==true){


return true;


}


else{


return false;


}


}


}





// returns true if the right side is a subset of the left


bool operator%26gt;(const Set%26amp; rhs, const Set%26amp; num)


{


for(int i=0; i%26lt;rhs.cardinality(); i++){


for(int j=0; j%26lt;num.cardinality(); j++){


if(rhs.isElement(num._element[j])==true)...


return true;


}


else{


return false;


}


}


}


}





// returns true if the right side is an element of the left


bool operator%26gt;(const Set%26amp; num, const int%26amp; rhs)


{


for(int i=0; i%26lt;num.cardinality(); i++){


if(num.isElement(rhs)==true){


return true;


}


else{


return false;


}


}


}

Need help creating c++ set class?
If the add( ) operation increments _cardinality, and your remove( ) operation decrements _cardinality and packs the element array, and clear() sets _cardinality to zero, all the cardinality( ) operation needs to do is return the value of the _cardinality attribute.





It looks like you're trying to pack the array in remove( ), but I don't think your logic there is right. E.g., if the first element in the set is not the one to be removed, you return? You need to test that function and make sure it works.





Your add( ) operation increments the loop counter in the 'for' statement and also inside the loop, which looks suspicious. I think add( ) can be as simple as this:





if (isElement(num) == false) {


element[_cardinality++] = num;


}





remove( ) should also use isElement( ) to determine if the specified value is in the set. In fact, it would be convenient for remove( ) if isElement( ) were to return the index of the requested value, if present in the set, or -1 if not. Then, if the value is found, remove( ) can start at the returned index, and pack the array from there.





I assume the Set class definition you were given declared the elements attribute as a simple array of int. Does it have a fixed, max size? If so, you need to be careful in add( ) not to exceed the bounds of element[ ]. It would be nice if element was a vector%26lt;int%26gt;. If not, it should be an int*, dynamically allocated to some initial, max size, with the size possibly provided to a Constructor. Then if any of your operations need a bigger element array, you can reallocate. Perhaps to simplify this assignment, the Set definition you were given just declared element[ ] to be a fixed size. In that case, add( ) would just have to reject a request to add past the bounds of the array.





You have a really good start on the implementation of this class. Clean up your logic a bit, and do some thorough testing, and I'm sure it'll turn out well.


C++ question?

Please tell me in C++ how does return *this; relates to returning a reference in assignment operator overloading





I am confused because this is a pointer to the current object and the return value of the function is ClassName %26amp; which is a reference. so how can ClassName%26amp; be the return type when we are returning using return *this; statement.





sample code:





MyClass%26amp; MyClass::operator=(const MyClass %26amp;rhs) {


... // Do the assignment operation!





return *this; // Return a reference to myself.


}

C++ question?
well a reference is like a pointer whose address you can't set so *this returns a MyClass Object and c++ can automatically make a MyClass reference from an objec.t


Static cast in C++?

guys how can I use The static cast to write this C++ program:


Write a Program that prompts the user to input an integer between 0 and 35. If the number is less than or equal to 9, the program should output the number; otherwise, it should output A for 10, B for 11, C for 12, . . ., and Z for 35. (Hint: Use the cast operator, static_cast%26lt;char%26gt; ( ), for numbers%26gt;=10.

Static cast in C++?
The static_cast can be used to take a data type (like integer, float, etc) and cast it (aka convert it) to another data type like making an integer into a character like you need for this example.





The trick here is that the integer 35 isn't the ascii number for Z, so you will have to do a bit of addition before converting.





For example... if you look at an ascii chart you will notice that the capital "Z" is number 90.





static_cast%26lt;char%26gt;(90) will then give you the capital Z. So what we need to do is take the difference between 90 and 35... which is 55 and add it to every number the user types in greater than or equal to 10, then convert it.





So if the user entered.... 11, we would add 55 and get 66 then go static_cast%26lt;char%26gt;(66) to get a character "B". We then print out the character to screen.





As a side note, lower case letters start at 97 for "a" and go to 122 for "z" so that offset will need to be figured out if you want to use lower case.





I will let you figure that one out.





Hope this helps you out. Enjoy!
Reply:Please make few modifications to support your needs. The lines are broken.





To contact me mail me at m_gopi_m@yahoo.co.in.





I hope this is the program you are expecting.





//program for static casting.





#include%26lt;iostream.h%26gt;


#include%26lt;stdio.h%26gt;


#include%26lt;conio.h%26gt;





char static_cast(int);





void main()


{


int num;


char a;





clrscr();


cout%26lt;%26lt;"\n Enter a number: ";


cin%26gt;%26gt;num;





if(num%26gt;=0 %26amp;%26amp; num%26lt;=9)


{


cout%26lt;%26lt;"\n\n The given number: "%26lt;%26lt;num;


cout%26lt;%26lt;"\n\n The corresponding character: "%26lt;%26lt;num;


}


else if(num%26gt;9 %26amp;%26amp; num%26lt;36)


{


a=static_cast(num);


cout%26lt;%26lt;"\n\n The given number: "%26lt;%26lt;num;


cout%26lt;%26lt;"\n The corresponding character: "%26lt;%26lt;a;


}


else


cout%26lt;%26lt;"\n\n Cannot predict the value.";


getch();


}





char static_cast(int num)


{


int first='A'; //static casting is invoked here.


char a;


num-=10;


a=first+num;


return(a);


}


Need help fixing the errors in this Template C++Program. I'm lost.Seems to be the same error repeatedly.Thanks

#include %26lt;iostream%26gt;


using namespace std;





template %26lt;class T%26gt;


class Complex


{


friend ostream %26amp;operator%26lt;%26lt;( ostream %26amp;, const Complex %26amp; );


friend istream %26amp;operator%26gt;%26gt;( istream %26amp;, Complex %26amp; );


public:


Complex(const T = 0.0, const T = 0.0 ); // constructor


T%26amp; operator+( const Complex%26amp; ) const; // addition


T%26amp; operator-( const Complex%26amp; ) const; // subtraction


T%26amp; operator*( const Complex%26amp; ) const; // multiplication


T%26amp; operator=( const Complex%26amp; ); // assignment


T%26amp; operator==( const Complex%26amp; ) const;


T%26amp; operator!=( const Complex%26amp; ) const;


// T%26amp; operator[](double);





private:


double real; // real part


double imaginary; // imaginary part


};








// Constructor


template %26lt;class T%26gt;


Complex%26lt;T%26gt;::Complex(T r, T i)


{


T real = r;


T imaginary = i;


}





// Overloaded addition operator





template %26lt;class T%26gt;


T %26amp;Complex%26lt;T%26gt;::operator+(const Complex %26amp;operand2) const


{


Complex sum;





sum.real = real + operand2.real;


sum.imaginary = imaginary + operand2.imaginary;





return sum;


}


// Overloaded subtraction operator





template %26lt;class T%26gt;


T %26amp;Complex%26lt;T%26gt;::operator-(const Complex %26amp;operand2) const


{


Complex diff;





diff.real = real - operand2.real;


diff.imaginary = imaginary - operand2.imaginary;


return diff;


}





// Overloaded multiplication operator


template %26lt;class T%26gt;


T %26amp;Complex %26lt;T%26gt;::operator*(const Complex %26amp;operand2 ) const


{


Complex times;





times.real = real * operand2.real + imaginary * operand2.imaginary;


times.imaginary = real * operand2.imaginary + imaginary * operand2.real;


return times;


}





// Overloaded = operator


template %26lt;class T%26gt;


T %26amp;Complex%26lt;T%26gt;::operator=(const Complex %26amp;right )


{


real = right.real;


imaginary = right.imaginary;


return *this; // enables concatenation


}





template %26lt;class T%26gt;


T %26amp;Complex%26lt;T%26gt;::operator==( const Complex %26amp;right ) const


{ return right.real == real %26amp;%26amp; right.imaginary == imaginary ? true : false; }





template %26lt;class T%26gt;


T %26amp;Complex%26lt;T%26gt;::operator!=( const Complex %26amp;right ) const


{ return !( *this == right ); }





template %26lt;class T%26gt;


ostream%26amp; operator%26lt;%26lt;( ostream %26amp;output, const Complex%26lt;T%26gt; %26amp;complex )


{


output %26lt;%26lt; complex.real %26lt;%26lt; " + " %26lt;%26lt; complex.imaginary %26lt;%26lt; 'i';


return output;


}





template %26lt;class T%26gt;


istream%26amp; operator%26gt;%26gt;( istream %26amp;input, Complex%26lt;T%26gt; %26amp;complex )


{


input %26gt;%26gt; complex.real;


input.ignore( 3 ); // skip spaces and +


input %26gt;%26gt; complex.imaginary;


input.ignore( 2 );





return input;


}








//Use the following main function to test your program


int main()


{


double a=4.3, b = 8.2, c = 3.3, d = 1.1;


Complex%26lt;double%26gt; x, y(a,b), z(c,d), k;





cout %26lt;%26lt; "Enter a complex number in the form: a + bi\n? ";


cin %26gt;%26gt; k;


cout %26lt;%26lt; "x: " %26lt;%26lt; x %26lt;%26lt; "\ny: " %26lt;%26lt; y %26lt;%26lt; "\nz: " %26lt;%26lt; z %26lt;%26lt; "\nk: "


%26lt;%26lt; k %26lt;%26lt; '\n';


(x = y + z);


cout %26lt;%26lt; "\nx = y + z:\n" %26lt;%26lt; x %26lt;%26lt; " = " %26lt;%26lt; y %26lt;%26lt; " + " %26lt;%26lt; z %26lt;%26lt; '\n';


(x = y - z);


cout %26lt;%26lt; "\nx = y - z:\n" %26lt;%26lt; x %26lt;%26lt; " = " %26lt;%26lt; y %26lt;%26lt; " - " %26lt;%26lt; z %26lt;%26lt; '\n';


(x = y * z);


cout %26lt;%26lt; "\nx = y * z:\n" %26lt;%26lt; x %26lt;%26lt; " = " %26lt;%26lt; y %26lt;%26lt; " * " %26lt;%26lt; z %26lt;%26lt; "\n\n";


if ( x != k )


cout %26lt;%26lt; x %26lt;%26lt; " != " %26lt;%26lt; k %26lt;%26lt; '\n';


cout %26lt;%26lt; '\n';


x = k;


if ( x == k )


cout %26lt;%26lt; x %26lt;%26lt; " == " %26lt;%26lt; k %26lt;%26lt; '\n';





int e=4, f=8, g=3, h=2;


Complex%26lt;int%26gt; l, m( e,f), n(g,h ), p;





cout %26lt;%26lt; "Enter a complex number in the form: a + bi\n? ";


cin %26gt;%26gt; p;


cout %26lt;%26lt; "x: " %26lt;%26lt; l %26lt;%26lt; "\ny: " %26lt;%26lt; m %26lt;%26lt; "\nz: " %26lt;%26lt; n %26lt;%26lt; "\nk: "


%26lt;%26lt; p %26lt;%26lt; '\n';


(l = m + n);


cout %26lt;%26lt; "\nx = y + z:\n" %26lt;%26lt; l %26lt;%26lt; " = " %26lt;%26lt; m %26lt;%26lt; " + " %26lt;%26lt; n %26lt;%26lt; '\n';


(l = m - n);


cout %26lt;%26lt; "\nx = y - z:\n" %26lt;%26lt; l %26lt;%26lt; " = " %26lt;%26lt; m %26lt;%26lt; " - " %26lt;%26lt; n %26lt;%26lt; '\n';


(l = m * n);


cout %26lt;%26lt; "\nx = y * z:\n" %26lt;%26lt; l %26lt;%26lt; " = " %26lt;%26lt; m %26lt;%26lt; " * " %26lt;%26lt; n %26lt;%26lt; "\n\n";


if ( l != p )





cout %26lt;%26lt; l %26lt;%26lt; " != " %26lt;%26lt; p %26lt;%26lt; '\n';


cout %26lt;%26lt; '\n';


l = p;


if ( l == p )


cout %26lt;%26lt; l %26lt;%26lt; " == " %26lt;%26lt; p %26lt;%26lt; '\n';








return 0;


}

Need help fixing the errors in this Template C++Program. I'm lost.Seems to be the same error repeatedly.Thanks
Haven't compiled your code, but looking at the definitions:





-Your +,-,* operators should return Complex%26lt;T%26gt;, not T - when you add two Complex%26lt;T%26gt;, you want to get a Complex%26lt;T%26gt; object and not a T object back.





-Your ==,!= operators should return bool, as these are logical operators.





-Your private members should be of type T - otherwise, when you assign them, you are implicitly saying that T is a specific type.





-In your constructor, you are redeclaring your members; use something like





Complex( T r, T i ) : real( r ), imaginary( i ) { }





to initialize them.





-In your operator implementations, you need to set a type for your return object. You'll need to declare them as Complex%26lt;T%26gt; objects, not just Complex.
Reply:It might help to know what the error message is./

local florist

Java Conditional Operator Problem PLS help?

Hi I have a problem that I cant solve. We have a school exercise in Java abt conditional operator (?:) We were asked to code the following:





a=1


b=2


c=3


highest number is = 3





I was able to code this correctly after a LONG time, and with some help. Now I am trying to do more practice and is trying to code the following, and it seems like I am stuck in deep mud again. Pls help me. I am trying to code:





a=1


b=2


c=3


d=4


highest number is = 4





pls help me

Java Conditional Operator Problem PLS help?
just remember, all the ?: operator is a short hand for if-then-else





or in this case multiple if then elseif's(don't know the exact java syntax)





but would look something like


(a%26gt;b)?((a%26gt;c)?((a%26gt;d)?print a : print d):((c%26gt;d)? print c : print d)...





if you got the above part all this last part is one more check at each level of the if then elseif all you doing is writing as you comparing a to the other 3 first... if true in all cases a is highest if false in last case d is highest... if false before then you need to check that variable with the rest down the line gave you if c%26gt;a case all you need to do is finish with if b%26gt;a using paranthesis and even adding lines might help for readability


C++ syntax help?

i'm trying to write a matrix class for c++, but i'm a bit rusty.


I have the following operators defined in my main program file but i'd like to include them in my header and implementation file instead. my syntax is a bit rusty but I think it should look something like this:





template%26lt;class itemType%26gt;


apmatrix%26lt;itemType%26gt; operator+(const apmatrix%26lt;itemType%26gt;%26amp; a, const apmatrix%26lt;itemType%26gt;%26amp; b)


{


//declare a matrix temp of type T to store the result and return this matrix


apmatrix%26lt;int%26gt; temp;


temp.resize(a.numrows(),b.numcols());


for(int i = 0; i %26lt; a.numrows(); i++)


for(int j = 0; j %26lt; a.numcols(); j++)


temp[i][j] = a[i][j] + b[i][j];


return temp;


}


I need to know how to change this to make it work, and what to put in my header.





the apmatrix class already has the operator = defined, but when i tried copying the similar format for my + operator in my header file i got this error:

C++ syntax help?
the plus operator is a binary not a tertiary operator - so it only takes one argument. in other words it's an operation between the current class and another parameter, so it wouldn't work with 3 things to operate on (this, a and b). Try this instead-








template%26lt;class T%26gt;


class m {





m%26lt;T%26gt; operator+(const m%26lt;T%26gt;%26amp; b) const


{


// use "(*this)" instead of "a"


// the function is const because it doesn't alter "this"


}





};


C for beginners problem....supposed to determine output of relational operators?

My homework asks "What is the output of the following?" and i'm thinking something is horribly wrong with this code or they skipped this in class.





the only thing vaguely tickling my brain is the true/false (1 or 0) thing. but if that's so, i didn't realize you could assign 1 or 0 to integers using true / false tests.





int main()


{


int a = 3, b = -1, c = 0, i, j, k, n;


i = a || b || c;


j = a %26amp;%26amp; b %26amp;%26amp; c;


k = a || b %26amp;%26amp; c;


n = a %26amp;%26amp; !(b || c);


printf("\ni = %d, j = %d, k = $d, n = %d\n", i, j, k, n);


}

C for beginners problem....supposed to determine output of relational operators?
Actually, this code prints out 1, 0, "$d", and 1 (k's value after "n ="), because you have "k = $d" instead of "%d". The output is:





i = 1, j = 0, k = $d, n = 1





Unless that's your typo, maybe you are being taught the value of matching up printf arguments. :-)





If you meant "%d", it's:





i = 1, j = 0, k = 1, n = 0
Reply:C treats anything other than 0 as true even negative values as true.


so if you write anything like i=2%26amp;%26amp;5 it sets value of i to 1.


so i=1 bcoz a and b are true, but j=0 boz of c which is zero means false.


%26amp;%26amp; operator has higher precedence than || so it is true again and hence k=1.


not operator has highest precedence. b||c=true as b is non zero


!(b||c) is false which when anded with a gives false setting n to 0


hence


your result is


i=1


j=0


k=1


n=0
Reply:C has no real concept of true and false. 0 is false and everything else is true. As a matter of fact and (%26amp;%26amp;) is an operator that says if either operand is 0 then return 0 otherwise return 1. Or (||) is an operator that says if either operand is not zero then return 1 else return 0.





This is the concept your teacher is testing you on with this question.
Reply:i = true


j, k, n = false





I think, just by looking at it.





Now, integers can be booleans; %26lt;= 0 is false (example, 0 and -1 are false, anything 0 or below)


And a true integer is anything above 0.


Sample C++ code?

whats the best website to find sample programs of C++ code. I need to know how to do operator overloading, and i need to know about classes. the book is very confusing, and doesn't show code of how these things are correctly used in a program.

Sample C++ code?
Hello


You are looking to use this code / learn. There are two websites.





http://www.cplusplus.com


http://www.codeproject.com

floral deliveries

Change program (using MODULUS OPERATOR)?

Well, it turns out. I have to use the MODULUS OPERATOR, and the math didn't work for Microsoft Visual C++ 6.0 anyways. It worked on Dev C++, but at school we use Microsoft.





Here's what I have so far for the program, all I need is the math for the nickels and pennies. The quarters and dimes seem to be working.





#include %26lt;iostream.h%26gt;





main()


{


int quarters;


int dimes;


int nickels;


int pennies;


int number;





cout %26lt;%26lt; "Enter a number. " %26lt;%26lt; '\n';


cin %26gt;%26gt; number;





quarters = number / 25;


dimes = number % 25 / 10;


nickels = I need help here


pennies = I need help here





cout %26lt;%26lt; "Quarters - " %26lt;%26lt; quarters %26lt;%26lt; '\n';


cout %26lt;%26lt; "Dimes - " %26lt;%26lt; dimes %26lt;%26lt; '\n';


cout %26lt;%26lt; "Nickels - " %26lt;%26lt; nickels %26lt;%26lt; '\n';


cout %26lt;%26lt; "Pennies - " %26lt;%26lt; pennies %26lt;%26lt; '\n';


cout %26lt;%26lt; '\n


return 0;


}

Change program (using MODULUS OPERATOR)?
Microsoft requires using namespace, replace the following line of code:








#include %26lt;iostream.h%26gt;





with this one:





#include %26lt;iostream%26gt;


using namespace std;


Compilation error and warnings in Visual C++ 6.0?

When I compile the following codes:





#include %26lt;iostream%26gt;


using namespace std;


#define pi 3.14;





void main ()


{





float radius(2),area;





area=pi*radius*radius;


}





I get the follow messages:


--------------------Configuration: Cpp1 - Win32 Debug--------------------


Compiling...


Cpp1.cpp


C:\VC++\Cpp1.cpp(12) : warning C4305: '=' : truncation from 'const double' to 'float'


C:\VC++\Cpp1.cpp(12) : error C2100: illegal indirection


C:\VC++\Cpp1.cpp(12) : warning C4552: '*' : operator has no effect; expected operator with side-effect


Error executing cl.exe.





Cpp1.exe - 1 error(s), 2 warning(s)





However, when I write the same thing but chage the position of 'pi' as follows, I don't get any error nor warning:





#include %26lt;iostream%26gt;


using namespace std;


#define pi 3.14;





void main ()


{





float radius(2),area;





area=radius*radius*pi; //position of pi changed here


}





How come? Please Help!

Compilation error and warnings in Visual C++ 6.0?
This is because you wrote


#define pi 3.14;


The preprocessor will replace any "pi" with "3.14;".


You just need to write


#define pi 3.14


without ";"


What is wrong with my program? [help in c programming]?

#include %26lt;stdio.h%26gt;


int main(void)





{


float x,y, result;


char cop1;





printf("Choose among the following operators:\n+,-,*,/", cop1);


scanf("%c", %26amp;cop1);





if (cop1=='/')


{


printf("Enter Dividend\n", x);


scanf("%f",%26amp;x);


printf("Enter Divisor\n", y);


scanf("%f", %26amp;y);


printf("result=%f\n",x/y);


}


else if (cop1=='+')


{


printf("Enter 1st addend\n", x);


scanf("%f",%26amp;x);


printf("Enter 2nd addend\n", y);


scanf("%f", %26amp;y);


printf("sum=%f\n",x+y);


}


{


else if (cop1=='-');


printf("enter minuend\n", x);


scanf("%f",%26amp;x);


printf("enter subtrahend",y);


scanf("%f",%26amp;y);


printf("difference=%f", x-y);


}


{


else if (cop=='*');


printf("enter 1st factor\n", x);


scanf("%f",%26amp;x);


printf("enter 2nd factor\n",y);


scanf("%f",%26amp;y);


printf("product is=%f\n",x*y);


}


else


printf("Wrong!\n");





return=0;


}





calculator.c: In function 'main':


calculator.c:28: error: parse error before 'else'


calculator.c:36: error: parse error before 'else'


calculator.c:43: error: parse error before 'else'

What is wrong with my program? [help in c programming]?
Here are the reasons why it won't currently compile (some of which you haven't found yet):





[[ 1 ]] You have opening braces in the wrong places:


{


else if (cop=='*');


They should be right after the else-if clause, not before.





[[ 2 ]] You have a semicolon after some else-ifs:


else if (cop=='*');


Take them out.





[[ 3 ]] The last else-if says "if (cop=='*')." It should be cop1, not cop.





[[ 4 ]] Finally, at the very end, you have "return=0;" Leave out the "=". It's just "return 0;"





Fix those errors and your program seems to work correctly. Nice job.
Reply:First of all, you have an opening brackets "{" right before else as well as inconsistent if{}else{} statements.


I'd suggest you to utilize indentation so you would see where your clauses begin and end.
Reply:Hi!





Seeing your various printf statements doesn't allow me to believe that you could make you a small mistake in the beginning. You could program very well.





Errors:


1. Your first printf statement. The quote doesn't include the variable name or instead remove if not needed.





2. Misplaced curly brackets. You have done well in your first two if..else blocks.





3. No semi-colon after if condition.





4. return 0;





Advise:


You can better improve your program if you would have used printf statements for once in the beginning i.e. before the if..else blocks instead of placing them in each block.





Sorry but you have to look for own small mistakes.


Enjoying programming at your level best. Try harder and harder, if unsuccessful then make a request. Goodluck!





Gagan..