You replied to my post earlier. I had the C++ question on remainders. The compiler said your code was incorrect :twisted: , 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++

Recommended Answers

All 28 Replies

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.

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:

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.

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

My code was incorrect? Hee hee - preposterous! Sounds like you guys have this under control. Thanks cosi.

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

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

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

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:

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;
}

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.

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

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.

Hi C++,

You cannot use a char* pointer for which you have not allocated space. char* support contains a junk value unless you do a) or b).

You can either
a) not use a pointer. do char[255] support;
b) use malloc or new. char* support; support = new char[255];
if you do option b), then you must remember to do delete support; when you are done with it.

You shouldn't be doing *support = "....."
use the strcpy from C.

#include <strings.h>  // for the strcpy..

#define WINDOWS_LINE "888-555-1234 option 2"
#define MAC_LINE 888-555-1234 option 3"

// .... in your code
// REMEMBER to do option a) or b) like I said
#ifdef WINDOWS
   strcpy(support, WINDOWS_LINE);
#else
   strcpy(support, MAC_LINE);
#end

C++, which certification are you going for? I strongly advise you to take a class in C++. Getting the answers from this forum alone won't help you pass.


Ed

char*support;
if(WINDOWS)*support="888-555-1234 option 2"; *support="888-555-1234 option 3";

but i'm wrong.

Please help,
C++

I've got through 226 of 234 questions of certification on my own. These last eight are VERY difficult. When I tried to run your code through the compiler, the compiler says I need to use char type and that your code declaresª support twice or acts unpredictably when neither macro is defined. Can you please help me mith this and the other ?s posted on: TO CHAINSAW or anybody else who knows C++.

Thanks,
C++

P.S. Is the correct code for the problem:

#include <strings.h>
#define WINDOWS ("888-555-1234 option 2")
#define MACOS ("888-555-1234 option 3")


char* support;
*support = new char[255];
#ifdef WINDOWS{
   strcpy(*support, WINDOWS);
   delete[] *support;
}
#else{
   strcpy(*support, MACOS);
   delete[] *support;
}
#end

Oh yeah... I solved problems #1, 3, and 4.

I've got through 226 of 234 questions of certification on my own. These last eight are VERY difficult. When I tried to run your code through the compiler, the compiler says I need to use char type and that your code declaresª support twice or acts unpredictably when neither macro is defined. Can you please help me mith this and the other ?s posted on: TO CHAINSAW or anybody else who knows C++.

Thanks,
C++


ahhh i see im sorry, i misunderstood you, thought you had only done the first ten and were getting help for each. not that you only had ten left :-p

It's okay - but could you help me with the questions I have?

cosi's version of #ifdef WINDOWS looks better than yours; WINDOWS is a pre-processor directive defined outside your code, so you'd say something like

const char *support = NULL; // const char because we'll point to things we can't change.
#ifdef WINDOWS
    support = "888-555-1234 option 2";  // point to the constant string
#endif
#ifdef MACOS
    support = "888-555-1234 option 3";
#endif
    if (support)
        printf( "%s\n", support );  // one of the options was taken
    else
        printf( "Neither WINDOWS nor MACOS were defined!\n" );

No. Your code is wrong. I thought I taught you how pointers and dereferencing work. *sigh* I guess I won't lose my cool over your fishing for answers. Do heed our advice; All these questions should come easy to those seeking certification. Could you please give a background of your experience with C++? I still think you need to take a class and get some real project experience with the language.

Anyways, I cooked up the solution below. Study it and understand what each line means. You should be able to reproduce the same functionality without any references if you are looking to get certification.


Ed

#include <stdio.h>
#include <strings.h>

#define WINDOWS
#define WL "hi"
#define ML "hi2"

void test() {
        char *support = new char[255];
#ifdef WINDOWS
        strcpy(support, WL);
#elif MAC
        strcpy(support, ML);
#endif

        printf("%s", support);
        delete support;
}


int main() {
        test();
}

#include <strings.h>
#define WINDOWS ("888-555-1234 option 2")
#define MACOS ("888-555-1234 option 3")


char* support;
*support = new char[255];
#ifdef WINDOWS{
strcpy(*support, WINDOWS);
delete[] *support;
}
#else{
strcpy(*support, MACOS);
delete[] *support;
}
#end

Oh yeah... I solved problems #1, 3, and 4.

I got the same error message as before. The message is:

Your code declares support twice or acts unpredictably when neither macro is defined.

an you please help?

C++, you posed the question as:

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


so I guess the literal interpretation of that is:

#ifdef WINDOWS
    char* support = "888-555-1234 option 2";
#endig
#ifdef MACOS
    char* support = "888-555-1234 option 3";
#endif

Though I still prefer my suggestion for a couple of reasons; (1) the char* may as well be a const char*, though that's not what the question asked, and (2) what if neither WINDOWS nor MACOS were defined? The question doesn't say, but I think in a real program you'd have to deal with that.

COSI - were you saying my code was wrong? What was wrong with it? You said something about pointers and dereferencing; can you enlighten me?

It's correct - with a minor error. You forgot to delete support after using it. I couldn't have figured it out without your help. I only have 4 questions left for my certification!!!

Can you help me with the following problem too?

Define a macro MAX that accepts two parameters and expands to the value of the larger of the two (you'll need the conditional operator for this).

I think the answer is:

#define MAX(x,y) (
if(x>y)delete(y)
else if((y)>(x))delete(x)
)


Thanks,
C++ :)

Sorry, here's a better font:

#define MAX(x,y) (
if(x>y)delete(y)
else if((y)>(x))delete(x)
)

Chainsaw your code is good. I think you are a fine engineer in the field.

I meant Mr. C++'s code is wrong. He uses * and & with no idea what those operators mean. I've explained it to him more than once in different questions with the same mistakes.

Mr. C++, please don't say that the code people help you with is wrong. At best we spare a few moments of our time to stamp the solution out. For all I care I could be simply giving you pseudocode that doesn't compile in a compiler at all. The code is meant to illustrate a concept. It is your job to understand and learn the concepts. You should be able to rewrite the code you find here on your own.


Ed

C++, you posed the question as:
COSI - were you saying my code was wrong? What was wrong with it? You said something about pointers and dereferencing; can you enlighten me?

Ok... I still have the stomach to answer your question. This time at least it's a different mistake ;-)

#define's can only be multilined if you do the \
at the end of the line

#define MAX(x,y) \
    if(x>y)delete(y) \
    else if((y)>(x))delete(x)

This is in contrast to inline functions that can be multilined without the \

inline int max(x,y) {
   if (x>=y)
     return x;
   else return y;
}

You asked about the conditional?
Here's how to use a conditional:
boolean-condition ? statement-to-excute-if-true : statement-to-execute-if-false;

This time, try to come up with the code to do it on your own. Won't spoon feed you this time.


Ed

Sorry, here's a better font:

#define MAX(x,y) (
if(x>y)delete(y)
else if((y)>(x))delete(x)
)

C++ why are you deleteing x or y? You said:
"Define a macro MAX that accepts two parametersª and expands to the valueª of the largerª of the two (you'll need the conditionalª operatorª for this). "

where does it say you want to delete x or y? And, when you "expand to a value" it isn't an if statement, thats why they say you'll need a conditional.

By the way, what certification IS this? Sounds like a class C++ test.

It is Turingscraft C++ certification.

Ah... It all makes sense now. I never did have much faith in certification programs. Best certification is demonstrated project experience. :-)

It is Turingscraft C++ certification.

If this is truly a C++ test, why the focus on so much C style code?

Good question - and that's why I need your help. I know C++, but not C.

Hi everybody,

thanls for all your help. I just completed the C++ certification.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.