| | |
TO CHAINSAW - or anybody else who knows C++
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
You replied to my post earlier. I had the C++ question on remainders. The compiler said your code was incorrect
, so I thought i would give you the orriginal question.
Here it is:
Write the definition of a function divide that takes four argumentsÂ* and returns no valueÂ*. The first two argumentsÂ* are of typeÂ* intÂ*. The last two argumentsÂ* argumentsÂ* are pointers to intÂ* and are set by the function to the quotient and remainder of dividing the first argumentÂ* by the second argumentÂ*. The function does not return a valueÂ*.
The function can be used as follows:
intÂ* numerator=42, denominator=5, quotient, remainder; divide(numerator,denominator,"ient,&remainder); /* quotient is now 8 */ /* remainder is now 2 */
I think the answer is:
void divide(int numerator,int denominator,int* quotient, int* remainder){
*(quotient=&numerator/&denominator);
*(remainder=&denominator%&numerator);
}
But, when I run i through the compiler, these three messages appear:
In function `void divide(int, int, int *, int *)':,
invalid operands `int *' and `int *' to binary `operator /',
invalid operands `int *' and `int *' to binary `operator %'
Please help :lol:
C++
, so I thought i would give you the orriginal question.Here it is:
Write the definition of a function divide that takes four argumentsÂ* and returns no valueÂ*. The first two argumentsÂ* are of typeÂ* intÂ*. The last two argumentsÂ* argumentsÂ* are pointers to intÂ* and are set by the function to the quotient and remainder of dividing the first argumentÂ* by the second argumentÂ*. The function does not return a valueÂ*.
The function can be used as follows:
intÂ* numerator=42, denominator=5, quotient, remainder; divide(numerator,denominator,"ient,&remainder); /* quotient is now 8 */ /* remainder is now 2 */
I think the answer is:
void divide(int numerator,int denominator,int* quotient, int* remainder){
*(quotient=&numerator/&denominator);
*(remainder=&denominator%&numerator);
}
But, when I run i through the compiler, these three messages appear:
In function `void divide(int, int, int *, int *)':,
invalid operands `int *' and `int *' to binary `operator /',
invalid operands `int *' and `int *' to binary `operator %'
Please help :lol:
C++
Hey Mr. C++,
You just got the semantics wrong. Remember that it is always something like this:
Always pointer to int = the address of a variable.
So the type of pointer is int*, or pointer to int.
The type of variable is int.
The type of &variable is an address of an int variable.
So when you call divide(int numerator,int denominator,int* quotient, int* remainder)
with (100, 56, &q, &r), this is like:
*ptr_variable means fetch me the variable that ptr_variable points to (a.k.a dereference operation)
So to access ints from these you can do:<br>
numerator, denominator, *quotient, *remainder.
Hope this helps!
Ed
You just got the semantics wrong. Remember that it is always something like this:
Always pointer to int = the address of a variable.
C++ Syntax (Toggle Plain Text)
int variable; int *pointer_to_int = &variable
So the type of pointer is int*, or pointer to int.
The type of variable is int.
The type of &variable is an address of an int variable.
So when you call divide(int numerator,int denominator,int* quotient, int* remainder)
with (100, 56, &q, &r), this is like:
C++ Syntax (Toggle Plain Text)
int numerator = 100; int denominator = 56; int* quotient = &q; int* remainder = &r;
*ptr_variable means fetch me the variable that ptr_variable points to (a.k.a dereference operation)
So to access ints from these you can do:<br>
numerator, denominator, *quotient, *remainder.
C++ Syntax (Toggle Plain Text)
void divide(int numerator,int denominator,int* quotient, int* remainder){ *quotient=numerator/denominator; // by the way you also had numerator/denominator // mixed up in your code to compute remainder *remainder=numerator%denominator; } int main() { int q, r; divide(100, 56, &q, &r); printf("q %d r %d", q, r); return 0; }
Hope this helps!
Ed
Thanks for your help(Ed). I really appreciate it. It was correct! Can you help me with 4 more problems. I'm getting certified and I'm having trouble with these last few questions. Thanks.
Problem 1:
Write the definition of a function minMax that has five parametersª. The first three parametersª are integersª. The last two are set by the function to the largest and smallest of the valuesª of the first three parametersª. The function does not return a valueª.
The function can be used as follows:
intª a=31, b=5, c=19 big, small; minMax(a,b,c,&big,&small); /* big is now 31 */ /* small is now 5 */
I think the answer is
void minMax(int a, int b, int c, int*big, int*small){
if(&a<&c && &a < &b){*small=&a;}
else if(&b<&c && &b < &a){*small=&b;}
else if(&c<&a && &c < &b){*small=&c;}
else if(&a>&b && &a > &c){*big=&a;}
else if(&b<&a && &b < &c){*big=&b;}
else if(&c<&a && &c < &b){*big=&c;}
}
Problem 2:
Givenª the charª *variablesª name1, name2, and name3, write a fragment of code that assignsª the largest valueª to the variableª max (assumeª all three have alreadyª been declaredª and have been assignedª valuesª).
I think the answer is
if(strcmp name1>strcmp name2 &&strcmp name1>strcmp name3)max=name1;
else if(strcmp name2>strcmp name1 && strcmp name2>strcmp name3)max=name2;
else if(strcmp name3>strcmp name1 && strcmp name3>strcmp name2)max=name3;
Problem 3:
Write a sequence of statementsª that finds the first comma in the stringª line, and assignsª to the variableª clause the portion of line up to, but not including the comma. You may assumeª that an intª variableª pos, as well as the variablesª line and clause, have alreadyª been declaredª.
I think the answer is
pos.line(",");
clause=line.substr(0,pos-1);
Problem 4:
Write a function called printStars. The function receives an intªparameterª. If the parameterª is positive, the function prints (to standard outputª) the givenª number of asterisks; otherwise the function does nothing. The function does not return a valueª. Thus, if the printStars(8) is called, ******** (8 asterisks) will be printed.
The function must not use a loop of any kind (for, while, do-while) to accomplish its job. Instead, it gets the job done by examining its parameterª, returning if the parameterª's valueª is not positive. If the parameterª is positive, it
prints a single asterisk (and no other charactersª)
then (recursively) calls itself to print the remaining asterisks.
I think the answer is
int i = 0;
void printStars(int starCount){
if(starCount>0){
if(i<starCount){
cout<<"*";
starCount;
i++;
printStars(starCount);
}
}
}
Thank you SO much,
C++
Problem 1:
Write the definition of a function minMax that has five parametersª. The first three parametersª are integersª. The last two are set by the function to the largest and smallest of the valuesª of the first three parametersª. The function does not return a valueª.
The function can be used as follows:
intª a=31, b=5, c=19 big, small; minMax(a,b,c,&big,&small); /* big is now 31 */ /* small is now 5 */
I think the answer is
void minMax(int a, int b, int c, int*big, int*small){
if(&a<&c && &a < &b){*small=&a;}
else if(&b<&c && &b < &a){*small=&b;}
else if(&c<&a && &c < &b){*small=&c;}
else if(&a>&b && &a > &c){*big=&a;}
else if(&b<&a && &b < &c){*big=&b;}
else if(&c<&a && &c < &b){*big=&c;}
}
Problem 2:
Givenª the charª *variablesª name1, name2, and name3, write a fragment of code that assignsª the largest valueª to the variableª max (assumeª all three have alreadyª been declaredª and have been assignedª valuesª).
I think the answer is
if(strcmp name1>strcmp name2 &&strcmp name1>strcmp name3)max=name1;
else if(strcmp name2>strcmp name1 && strcmp name2>strcmp name3)max=name2;
else if(strcmp name3>strcmp name1 && strcmp name3>strcmp name2)max=name3;
Problem 3:
Write a sequence of statementsª that finds the first comma in the stringª line, and assignsª to the variableª clause the portion of line up to, but not including the comma. You may assumeª that an intª variableª pos, as well as the variablesª line and clause, have alreadyª been declaredª.
I think the answer is
pos.line(",");
clause=line.substr(0,pos-1);
Problem 4:
Write a function called printStars. The function receives an intªparameterª. If the parameterª is positive, the function prints (to standard outputª) the givenª number of asterisks; otherwise the function does nothing. The function does not return a valueª. Thus, if the printStars(8) is called, ******** (8 asterisks) will be printed.
The function must not use a loop of any kind (for, while, do-while) to accomplish its job. Instead, it gets the job done by examining its parameterª, returning if the parameterª's valueª is not positive. If the parameterª is positive, it
prints a single asterisk (and no other charactersª)
then (recursively) calls itself to print the remaining asterisks.
I think the answer is
int i = 0;
void printStars(int starCount){
if(starCount>0){
if(i<starCount){
cout<<"*";
starCount;
i++;
printStars(starCount);
}
}
}
Thank you SO much,
C++
Is the answer:
void minMax(int a, int b, int c, int* big, int* small){
if(a<c && a<b)*small=a;
else if(b<c && b<a)*small=b;
else if(c<a && c<b)*small=c;
else if(a>b && a>c)*big=a;
else if(b>a && b>c)*big=b;
else if(c>a && c>b)*big=c;
}
It seems correct to me, but the compiler says:
Your function did not change the valueÂ* of
Can you please help
void minMax(int a, int b, int c, int* big, int* small){
if(a<c && a<b)*small=a;
else if(b<c && b<a)*small=b;
else if(c<a && c<b)*small=c;
else if(a>b && a>c)*big=a;
else if(b>a && b>c)*big=b;
else if(c>a && c>b)*big=c;
}
It seems correct to me, but the compiler says:
Your function did not change the valueÂ* of
Can you please help
this part looks good:
if(a<c && a<b)*small=a;
else if(b<c && b<a)*small=b;
then the next else is ok, but if a and b are not the smallest, c would HAVE to be the smallest so you could just say "else *small=c;"
Then you want to find the largest. That first 'else' gets in the way, though, because you are saying "if NONE of the params are the smallest, evaluate these expressions" and that won't ever happen. So, how about this:
if(a<c && a<b)*small=a;
else if(b<c && b<a)*small=b;
then the next else is ok, but if a and b are not the smallest, c would HAVE to be the smallest so you could just say "else *small=c;"
Then you want to find the largest. That first 'else' gets in the way, though, because you are saying "if NONE of the params are the smallest, evaluate these expressions" and that won't ever happen. So, how about this:
C++ Syntax (Toggle Plain Text)
void minMax(int a, int b, int c, int* big, int* small){ if(a<c && a<b)*small=a; else if(b<c && b<a)*small=b; else *small=c; if(a>b && a>c)*big=a; else if(b>a && b>c)*big=b; else *big=c; }
I solved Problem # 3! Disregeard. Instead, can you help with the following problem:
Write conditionalÂ* code that will produce the line of source code charÂ* *support="888-555-1234 option 2"; if the macro WINDOWS is defined, and charÂ* *support="888-555-1234 option 3"; if the macro MACOS is defined. (Note: it will never be the case that both MACOS and WINDOWS are simultaneously defined.)
I think the answer is
char*support;
if(WINDOWS)*support="888-555-1234 option 2"; *support="888-555-1234 option 3";
but i'm wrong.
Please help,
C++
Write conditionalÂ* code that will produce the line of source code charÂ* *support="888-555-1234 option 2"; if the macro WINDOWS is defined, and charÂ* *support="888-555-1234 option 3"; if the macro MACOS is defined. (Note: it will never be the case that both MACOS and WINDOWS are simultaneously defined.)
I think the answer is
char*support;
if(WINDOWS)*support="888-555-1234 option 2"; *support="888-555-1234 option 3";
but i'm wrong.
Please help,
C++
how can you expect to get C++ certified when you need help on every question, i dont mean to sound rude, but if you cant solve these problems by yourself you might need to take a class, it seems like you just coming here to get it all done by someone else. i think a class would deff help, and you would be able to do this alot more on your own.
Dont forget to spread the reputation to those that deserve!
![]() |
Similar Threads
- free() = delete or delete[]? (C++)
- Forum lurkers, introduce yourself ... !! (Community Introductions)
- Recursive prime number f(x) (C)
- passing objects as arguments (C++)
Other Threads in the C++ Forum
- Previous Thread: ive got a Q
- Next Thread: how to start out
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy desktop developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker list loop looping loops map math memory multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference rpg sorting string strings struct temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





