943,789 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 6433
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Aug 14th, 2004
0

TO CHAINSAW - or anybody else who knows C++

Expand Post »
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,&quotient,&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++
Similar Threads
Reputation Points: 12
Solved Threads: 2
Posting Whiz
Ghost is offline Offline
352 posts
since Aug 2004
Aug 14th, 2004
0

Re: pointers and dereferencing syntax

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.
C++ Syntax (Toggle Plain Text)
  1. int variable;
  2. 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)
  1. int numerator = 100;
  2. int denominator = 56;
  3. int* quotient = &q;
  4. 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)
  1. void divide(int numerator,int denominator,int* quotient, int* remainder){
  2. *quotient=numerator/denominator;
  3. // by the way you also had numerator/denominator
  4. // mixed up in your code to compute remainder
  5. *remainder=numerator%denominator;
  6. }
  7.  
  8. int main() {
  9.  
  10. int q, r;
  11. divide(100, 56, &q, &r);
  12. printf("q %d r %d", q, r);
  13. return 0;
  14. }

Hope this helps!


Ed
Reputation Points: 17
Solved Threads: 1
Junior Poster
cosi is offline Offline
153 posts
since Aug 2004
Aug 14th, 2004
0

Re: TO CHAINSAW - or anybody else who knows C++

My code was incorrect? Hee hee - preposterous! Sounds like you guys have this under control. Thanks cosi.
Reputation Points: 36
Solved Threads: 11
Posting Pro in Training
Chainsaw is offline Offline
436 posts
since Jun 2004
Aug 14th, 2004
0

Re: C++ help

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++
Reputation Points: 12
Solved Threads: 2
Posting Whiz
Ghost is offline Offline
352 posts
since Aug 2004
Aug 15th, 2004
0

Re: C++ help

Hi there,

I think you better reread my post and think over the problems before asking for help. Right off I see that question #1 you should be able to fully solve yourself. I answered that question in my post.

Have you considered taking a class?


Ed
Reputation Points: 17
Solved Threads: 1
Junior Poster
cosi is offline Offline
153 posts
since Aug 2004
Aug 15th, 2004
0

Re: TO CHAINSAW - or anybody else who knows 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
Reputation Points: 12
Solved Threads: 2
Posting Whiz
Ghost is offline Offline
352 posts
since Aug 2004
Aug 15th, 2004
0

Re: TO CHAINSAW - or anybody else who knows C++

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:
C++ Syntax (Toggle Plain Text)
  1. void minMax(int a, int b, int c, int* big, int* small){
  2. if(a<c && a<b)*small=a;
  3. else if(b<c && b<a)*small=b;
  4. else *small=c;
  5.  
  6. if(a>b && a>c)*big=a;
  7. else if(b>a && b>c)*big=b;
  8. else *big=c;
  9. }
Reputation Points: 36
Solved Threads: 11
Posting Pro in Training
Chainsaw is offline Offline
436 posts
since Jun 2004
Aug 15th, 2004
0

Re: TO CHAINSAW - or anybody else who knows C++

Thanks again. It was correct. You're really smart. I'm 10 (out of 234) questions away from getting C++ certified. If you have the time, would you mmind helping me with the other three questions above. If not, thank you for all your help.
Reputation Points: 12
Solved Threads: 2
Posting Whiz
Ghost is offline Offline
352 posts
since Aug 2004
Aug 15th, 2004
0

Re: TO CHAINSAW - or anybody else who knows 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++
Reputation Points: 12
Solved Threads: 2
Posting Whiz
Ghost is offline Offline
352 posts
since Aug 2004
Aug 16th, 2004
0

Re: TO CHAINSAW - or anybody else who knows 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.
Reputation Points: 152
Solved Threads: 39
Master Poster
Killer_Typo is offline Offline
778 posts
since Apr 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: ive got a Q
Next Thread in C++ Forum Timeline: how to start out





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC