TO CHAINSAW - or anybody else who knows C++

Reply

Join Date: Aug 2004
Posts: 350
Reputation: Ghost is an unknown quantity at this point 
Solved Threads: 2
Ghost's Avatar
Ghost Ghost is offline Offline
Posting Whiz

TO CHAINSAW - or anybody else who knows C++

 
0
  #1
Aug 14th, 2004
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++
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 153
Reputation: cosi is an unknown quantity at this point 
Solved Threads: 1
cosi's Avatar
cosi cosi is offline Offline
Junior Poster

Re: pointers and dereferencing syntax

 
0
  #2
Aug 14th, 2004
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.
  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:
  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.


  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
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 436
Reputation: Chainsaw is an unknown quantity at this point 
Solved Threads: 10
Chainsaw's Avatar
Chainsaw Chainsaw is offline Offline
Unprevaricator

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

 
0
  #3
Aug 14th, 2004
My code was incorrect? Hee hee - preposterous! Sounds like you guys have this under control. Thanks cosi.
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 350
Reputation: Ghost is an unknown quantity at this point 
Solved Threads: 2
Ghost's Avatar
Ghost Ghost is offline Offline
Posting Whiz

Re: C++ help

 
0
  #4
Aug 14th, 2004
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++
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 153
Reputation: cosi is an unknown quantity at this point 
Solved Threads: 1
cosi's Avatar
cosi cosi is offline Offline
Junior Poster

Re: C++ help

 
0
  #5
Aug 15th, 2004
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 350
Reputation: Ghost is an unknown quantity at this point 
Solved Threads: 2
Ghost's Avatar
Ghost Ghost is offline Offline
Posting Whiz

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

 
0
  #6
Aug 15th, 2004
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 436
Reputation: Chainsaw is an unknown quantity at this point 
Solved Threads: 10
Chainsaw's Avatar
Chainsaw Chainsaw is offline Offline
Unprevaricator

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

 
0
  #7
Aug 15th, 2004
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:
  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. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 350
Reputation: Ghost is an unknown quantity at this point 
Solved Threads: 2
Ghost's Avatar
Ghost Ghost is offline Offline
Posting Whiz

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

 
0
  #8
Aug 15th, 2004
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 350
Reputation: Ghost is an unknown quantity at this point 
Solved Threads: 2
Ghost's Avatar
Ghost Ghost is offline Offline
Posting Whiz

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

 
0
  #9
Aug 15th, 2004
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++
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 759
Reputation: Killer_Typo will become famous soon enough Killer_Typo will become famous soon enough 
Solved Threads: 35
Sponsor
Killer_Typo's Avatar
Killer_Typo Killer_Typo is offline Offline
Master Poster

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

 
0
  #10
Aug 16th, 2004
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!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC