Tuesday, July 14, 2009

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..


What do the operators |=, ~ and >> do in C?

This better not be for a take-home exam!!!





The |= operator is actually two operators, | and =. First a bitwise OR of the left-hand argument and the right-hand argument is performed. Next, the result of the bitwise OR operation is assigned to the variable on the left-hand side. Another name for this operator is "bitwise inclusive or and assign". An example:


char a, b, c;


a = 'a'; // a == 0x61, or 01100001


b = 'b'; // b == 0x62, or 01100010


c = b;


c |= a; // after this operation, c == 01100011 (0x62, or 'c' in ASCII)





The ~ operator, in C, does a bitwise complement, or negation, of its argument. For example, if the argument is 00100100, the result of the operation will be 11011011.





The %26gt;%26gt; operator, in C, is a bitwise-shift operator, specifically bitwise shift right. The result of this operation is the left-hand arguments' bits shifted to the right the number of times specified by the right-hand value. For example:


int n = 4;


int m = n %26gt;%26gt; 2;


// After the assignment, m == 00000001 (truncating to one byte)


This has the effect of dividing by 2 the number specified by the right-hand argument times.





In C++, the ~ and %26gt;%26gt; operators gain additional meanings.

What do the operators |=, ~ and %26gt;%26gt; do in C?
The operator != is NOT-EQUAL. It is the opposite of == (EQUAL). The ~ operator (tilde) is the BINARY-NOT operator. It inverts all of the bits in a number. For example if you do ~0x00FFFF00 you'll get 0xFF0000FF. The %26gt;%26gt; operator is known as the EXTRACT operator in C++, and also known as the RIGHT-SHIFT operator in C. It shifts the number in binary to the right the number of places thats on the right of the operator, this effectively divides by 2.





For example: 32 %26gt;%26gt; 1 = 16. 32 %26gt;%26gt; 2 = 8. 2 %26gt;%26gt; 1 = 1. It is almost the opposite of doing %26lt;%26lt; (LEFT-SHIFT operator)
Reply:the first is a binary operator meaning





a |= b





or





a = a | b;





the second is used for destructors, dont use it other than that cuz its just to signify an inverse function. for example





constructor MyClass(){}


Destructor ~MyClass(){}





or bit inversion, again binary. means not 1 or not 0. but this is usually denoted by ! again meaning not or inverse. for example





true and not false = true


true %26amp;%26amp; !false = true.





and the last is a shift. can do powers quick. you should never use these, make code readable adn clear. the compiler will use these if it sees fit, its much better at programming than you.





and the last is the bunary shif operator
Reply:hahah, good answer
Reply:Use the textbook. We answer people ain't in the business of doing your take-home final for you.

buy flowers