Help - C++ Assignment Question

Get help on programming - C++, Java, Delphi, etc.
Post Reply
Arillis
Registered User
Posts: 106
Joined: 19 May 2004, 02:00
Location: Newcastle,KZN

Help - C++ Assignment Question

Post 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)
User avatar
rustypup
Registered User
Posts: 8872
Joined: 13 Dec 2004, 02:00
Location: nullus pixius demonica
Contact:

Post 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?
Most people would sooner die than think; in fact, they do so - Bertrand Russel
Arillis
Registered User
Posts: 106
Joined: 19 May 2004, 02:00
Location: Newcastle,KZN

Post 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)
Slasher
Registered User
Posts: 7525
Joined: 23 Aug 2003, 02:00
Location: 5th rock from the sun.

Post 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...
My BF2142 Stats:
Image


Slasher : Former member of www.PCFormat.co.za
I have reached the end of my near 5 year forum life. Farewell good days...

slasher (at) webmail (dot) co (dot) za
Hman
Registered User
Posts: 28520
Joined: 06 Oct 2003, 02:00
Processor: Intel i5 650
Motherboard: Asus P7H55-M LX
Graphics card: Gigabyte 7850 2GB OC
Memory: 8GB Kingston DDR3
Location: In my skin
Contact:

Post 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
"Every thinking man is a drinking man."


Member of the Barberton Tigers
Arillis
Registered User
Posts: 106
Joined: 19 May 2004, 02:00
Location: Newcastle,KZN

Post by Arillis »

Ok, ill just go pull some more of my hair out and get back to you...
Slasher
Registered User
Posts: 7525
Joined: 23 Aug 2003, 02:00
Location: 5th rock from the sun.

Post 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?
My BF2142 Stats:
Image


Slasher : Former member of www.PCFormat.co.za
I have reached the end of my near 5 year forum life. Farewell good days...

slasher (at) webmail (dot) co (dot) za
Hman
Registered User
Posts: 28520
Joined: 06 Oct 2003, 02:00
Processor: Intel i5 650
Motherboard: Asus P7H55-M LX
Graphics card: Gigabyte 7850 2GB OC
Memory: 8GB Kingston DDR3
Location: In my skin
Contact:

Post by Hman »

Sorry man, I only know LOLCODE. And BASIC, I'm gonna start C++ sometime.
"Every thinking man is a drinking man."


Member of the Barberton Tigers
User avatar
rustypup
Registered User
Posts: 8872
Joined: 13 Dec 2004, 02:00
Location: nullus pixius demonica
Contact:

Post 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...>
Last edited by rustypup on 12 Jul 2007, 16:21, edited 1 time in total.
Most people would sooner die than think; in fact, they do so - Bertrand Russel
Hex_Rated
Registered User
Posts: 3679
Joined: 19 Jan 2006, 02:00
Contact:

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

}
DFI LanParty X48 LT-2TR
Intel Q9450 @ 3.2Ghz
Dell 24" 2408WFP | Phillips 37" 1080p
Sapphire HD4870 X2 2GB
4GB Corsair DDR-2 1066 | Thermalrite 120 Ultra Extreme | G9 Mouse | G15 Keyboard
Vista Ultimate x64
Arillis
Registered User
Posts: 106
Joined: 19 May 2004, 02:00
Location: Newcastle,KZN

Post 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
Arillis
Registered User
Posts: 106
Joined: 19 May 2004, 02:00
Location: Newcastle,KZN

Post by Arillis »

Thank you Hex rated
Hex_Rated
Registered User
Posts: 3679
Joined: 19 Jan 2006, 02:00
Contact:

Post by Hex_Rated »

My code will work but it's not the solution you're looking for.
DFI LanParty X48 LT-2TR
Intel Q9450 @ 3.2Ghz
Dell 24" 2408WFP | Phillips 37" 1080p
Sapphire HD4870 X2 2GB
4GB Corsair DDR-2 1066 | Thermalrite 120 Ultra Extreme | G9 Mouse | G15 Keyboard
Vista Ultimate x64
Judas
Registered User
Posts: 2118
Joined: 17 Oct 2006, 02:00
Location: Stellenbosch
Contact:

Post 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.
'One will rarely err if extreme actions be ascribed to vanity, ordinary actions to habit, and mean actions to fear.'
- Friedrich Nietzsche

'Do not argue with Judas, nube, that would be foolish!'
- D3PART3D
Hex_Rated
Registered User
Posts: 3679
Joined: 19 Jan 2006, 02:00
Contact:

Post 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.
DFI LanParty X48 LT-2TR
Intel Q9450 @ 3.2Ghz
Dell 24" 2408WFP | Phillips 37" 1080p
Sapphire HD4870 X2 2GB
4GB Corsair DDR-2 1066 | Thermalrite 120 Ultra Extreme | G9 Mouse | G15 Keyboard
Vista Ultimate x64
Judas
Registered User
Posts: 2118
Joined: 17 Oct 2006, 02:00
Location: Stellenbosch
Contact:

Post 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
'One will rarely err if extreme actions be ascribed to vanity, ordinary actions to habit, and mean actions to fear.'
- Friedrich Nietzsche

'Do not argue with Judas, nube, that would be foolish!'
- D3PART3D
Hex_Rated
Registered User
Posts: 3679
Joined: 19 Jan 2006, 02:00
Contact:

Post by Hex_Rated »

Ah, I see. Didn't read your comment.
DFI LanParty X48 LT-2TR
Intel Q9450 @ 3.2Ghz
Dell 24" 2408WFP | Phillips 37" 1080p
Sapphire HD4870 X2 2GB
4GB Corsair DDR-2 1066 | Thermalrite 120 Ultra Extreme | G9 Mouse | G15 Keyboard
Vista Ultimate x64
Arillis
Registered User
Posts: 106
Joined: 19 May 2004, 02:00
Location: Newcastle,KZN

Post 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
Anthro
Moderator Emeritus
Posts: 5547
Joined: 21 Dec 2002, 02:00
Processor: i7 3770k
Motherboard: ASUS P8P67-Pro
Graphics card: 2xNvidia GTX670
Memory: 16 GB Gskill Sniper
Location: In SQL Space inserting 'null' on purpose
Contact:

Post by Anthro »

NOW THAT IS JUST SILLY BUSINESS

but if she's hawt.. so be it/ Jaamiiiin !!!
PM away boyz
Temporary Absence
Slasher
Registered User
Posts: 7525
Joined: 23 Aug 2003, 02:00
Location: 5th rock from the sun.

Post 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:
My BF2142 Stats:
Image


Slasher : Former member of www.PCFormat.co.za
I have reached the end of my near 5 year forum life. Farewell good days...

slasher (at) webmail (dot) co (dot) za
Arillis
Registered User
Posts: 106
Joined: 19 May 2004, 02:00
Location: Newcastle,KZN

Post 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.....
Milkybar
Registered User
Posts: 1
Joined: 13 Jul 2007, 02:00

Post by Milkybar »

Just letting you guys know, I passed the assignment...
Judas
Registered User
Posts: 2118
Joined: 17 Oct 2006, 02:00
Location: Stellenbosch
Contact:

Post 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.
'One will rarely err if extreme actions be ascribed to vanity, ordinary actions to habit, and mean actions to fear.'
- Friedrich Nietzsche

'Do not argue with Judas, nube, that would be foolish!'
- D3PART3D
Post Reply