Page 1 of 1

Help - C++ Assignment Question

Posted: 12 Jul 2007, 15:21
by Arillis
Hi

Please can someone assist me with the ffg. Its a question out of my assignment

Rewrite the following piece of code, correcting all the problems found:

void CalculateValues (const in&a, float *b, int &c)
{
d = 2.04;
if (*a = 0)
*a += 1;
c = a + d;
else
b = c / d;
c = a - b + d;
}

HINT: If a=4, b=0.0 and c=5 when this function is called, their values when this function returns should be: a=4, b=0.25, and c=24.15 (6marks)

Posted: 12 Jul 2007, 15:25
by rustypup
:roll: please design my nuclear clock?

the answer is 42... do your work! - bad form, than person...

can you point out what you think the errors may be?

Posted: 12 Jul 2007, 15:34
by Arillis
Nope, please Im really struggling, I havent done any programming before. I can manage the theory part of C++ programming but I just cant get the practical part.

Ps. There was a typo:

Void CalculateValues (const in &a, float *b, int &c)

Posted: 12 Jul 2007, 15:39
by Slasher
Some time or another you will have to learn...

BTW - What year you finished school in Newcastle? Which school were you in? I matriculated in 2003... Maybe we know each other...

Posted: 12 Jul 2007, 15:41
by Hman

Code: Select all

HAI
CAN HAS STDIO?
D = 2.04;
IZ :(*A = 0:)
*A += 1;
C = A + D;p
NOWAI 
B = C / D;p
C = A - B + D;p
KTHNXBAI

Posted: 12 Jul 2007, 15:45
by Arillis
Ok, ill just go pull some more of my hair out and get back to you...

Posted: 12 Jul 2007, 15:48
by Slasher
Start with the fact that d is never defined...

I dont know C++ , cant help you...

Did you read part 2 of my post?

Posted: 12 Jul 2007, 15:48
by Hman
Sorry man, I only know LOLCODE. And BASIC, I'm gonna start C++ sometime.

Posted: 12 Jul 2007, 16:05
by rustypup
the errors are glaring for anyone who can manage the 'theory' part...

in c++:
what does '*' and '&' signify?
what happens when you iterate '*'?
how is an if/else evaluated?

really... this is like "intro to programming 101"... simply handing it to you on a plate does you no favours whatsoever... buckle down and do the work.

<for an encore, kicks some puppies into oncomming traffic and smacks ceiling cat with a wiffle bat...>

Posted: 12 Jul 2007, 16:11
by Hex_Rated
Research pointers. If you get really desperate, hand in this:

void CalculateValues( int a, float *b, float *c)
{

*b=0.25f;
*c=24.15f;

}

Posted: 12 Jul 2007, 16:23
by Arillis
Firstly, I have done zero programming, my course includes, financial management, business management, business communication, elements of programming and then C++ programming. I dont understand why they would put such a course like this together....

Anyway this is what I know so....

the & is the reference operator and can be read as 'address of'
the * is the dereference operator and can be read as 'value pointed by'

im not sure what happens when you iterate '*'

if/else is evaluated by true/false

Posted: 12 Jul 2007, 16:32
by Arillis
Thank you Hex rated

Posted: 12 Jul 2007, 16:40
by Hex_Rated
My code will work but it's not the solution you're looking for.

Posted: 12 Jul 2007, 17:07
by Judas

Code: Select all

void CalculateValues (const int &a, float *b, float &c) { // Correctly define c as a float

 float d = 2.04; // Define d before attempting to modify it

 if (a == 0) // Remove derefence (*) operator - a is a reference, not a pointer
                   // And change assignment (=) operator to equality (==) operator
  { // Wrap the block in braces
     ; // Statement was illegal - a can't be incremented because it is defined const
     c = a + d;
  } else {
   *b = c / d; // Dereference b first - otherwise you'll change the pointer
   c = a - (*b) + d; // Dereference b again
  }
}
Rusty is right, you should really learn WHY the code doesn't work - this is pretty basic programming - so if you have any questions, feel free to ask.

NOTE: I've only fixed the syntax errors - you'll still need to modify it to output the correct values.

Posted: 12 Jul 2007, 17:17
by Hex_Rated
You've corrected the syntax for him, but there's still a logical error somewhere. It doesn't evaluate to the hint.

Posted: 12 Jul 2007, 17:25
by Judas
Hex_Rated wrote:You've corrected the syntax for him, but there's still a logical error somewhere. It doesn't evaluate to the hint.
I know. I didn't want to do all the work for him. :D

Posted: 12 Jul 2007, 17:30
by Hex_Rated
Ah, I see. Didn't read your comment.

Posted: 13 Jul 2007, 14:24
by Arillis
OK guy's time to come clean. This was a test and you all have passed!!!!
Just kidding. It is actualy a friend of mine here at work that is struggeling with her c++ and I suggested to her to come to you ever so helpfull guy's here at the forums. I let her log on with my profile just to get her online as she had a deadline for her assignment. Now that she saw how helpfull you bunch are she will create her own profile. I can't code to save my live (not enough brains), just an administrator.

Thanks again!!!!!!!!
You can pm her (Milkybar)she's hot

Posted: 13 Jul 2007, 14:25
by Anthro
NOW THAT IS JUST SILLY BUSINESS

but if she's hawt.. so be it/ Jaamiiiin !!!
PM away boyz

Posted: 13 Jul 2007, 14:44
by Slasher
You know it is not nice to create a second profile just so you can get your homework assignments done?

That can NOT be at a business level with programming - How does someone get hired with such a lack of skillz?

Sneaky buggger arent you?

:lol: :lol: :lol:

Posted: 13 Jul 2007, 15:23
by Arillis
Well I don't think she can be that hot cause you need hair on your teeth to do what she is trying. She is in admin and now she is trying to learn programming for part of her course in business management or something. I told her it is not easy, but chicks don't listen.
So here is the proof, women don't know everything..lol

I suppose I sould stop nagging or I will start getting some serious chick mail.....

Posted: 01 Aug 2007, 09:26
by Milkybar
Just letting you guys know, I passed the assignment...

Posted: 01 Aug 2007, 09:59
by Judas
Milkybar wrote:Just letting you guys know, I passed the assignment...
Congrats! :bigups:

Feel free to ask if there's anything else that we can help with.