954,206 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

TO CHAINSAW - or anybody else who knows C++

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

Ghost
Posting Whiz
352 posts since Aug 2004
Reputation Points: 12
Solved Threads: 2
 

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:

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

cosi
Junior Poster
153 posts since Aug 2004
Reputation Points: 17
Solved Threads: 1
 

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

Chainsaw
Posting Pro in Training
436 posts since Jun 2004
Reputation Points: 36
Solved Threads: 11
 

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

Ghost
Posting Whiz
352 posts since Aug 2004
Reputation Points: 12
Solved Threads: 2
 

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

cosi
Junior Poster
153 posts since Aug 2004
Reputation Points: 17
Solved Threads: 1
 

Is the answer:

void minMax(int a, int b, int c, int* big, int* small){
if(ab && 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

Ghost
Posting Whiz
352 posts since Aug 2004
Reputation Points: 12
Solved Threads: 2
 

this part looks good:
if(avoid 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;
}

Chainsaw
Posting Pro in Training
436 posts since Jun 2004
Reputation Points: 36
Solved Threads: 11
 

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.

Ghost
Posting Whiz
352 posts since Aug 2004
Reputation Points: 12
Solved Threads: 2
 

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

Ghost
Posting Whiz
352 posts since Aug 2004
Reputation Points: 12
Solved Threads: 2
 

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.

Killer_Typo
Master Poster
781 posts since Apr 2004
Reputation Points: 152
Solved Threads: 39
 

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.


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

but i'm wrong.

Please help,
C++

cosi
Junior Poster
153 posts since Aug 2004
Reputation Points: 17
Solved Threads: 1
 

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

Ghost
Posting Whiz
352 posts since Aug 2004
Reputation Points: 12
Solved Threads: 2
 

[QUOTE=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++
[QUOTE]

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

Killer_Typo
Master Poster
781 posts since Apr 2004
Reputation Points: 152
Solved Threads: 39
 

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

Ghost
Posting Whiz
352 posts since Aug 2004
Reputation Points: 12
Solved Threads: 2
 

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" );
Chainsaw
Posting Pro in Training
436 posts since Jun 2004
Reputation Points: 36
Solved Threads: 11
 

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

cosi
Junior Poster
153 posts since Aug 2004
Reputation Points: 17
Solved Threads: 1
 

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?

Ghost
Posting Whiz
352 posts since Aug 2004
Reputation Points: 12
Solved Threads: 2
 

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?

Chainsaw
Posting Pro in Training
436 posts since Jun 2004
Reputation Points: 36
Solved Threads: 11
 

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

Ghost
Posting Whiz
352 posts since Aug 2004
Reputation Points: 12
Solved Threads: 2
 

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

Ghost
Posting Whiz
352 posts since Aug 2004
Reputation Points: 12
Solved Threads: 2
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You