Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,121 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,764 people online right now. Registration is fast and FREE... Join Now!




My answer vs my books answer...

 
Reply to this topicStart new topic

My answer vs my books answer..., Is there any advantage to one or the other?

Guydin
7 Jun, 2007 - 09:08 PM
Post #1

New D.I.C Head
*

Joined: 5 Jun, 2007
Posts: 9


My Contributions
Ok, I'm doing the exercises at the end of a chapter. One of the questions is this:

Assuming there are 7.481 gallons in a cubic foot, write a program that asks the user to enter a number of gallons, and then displays the equivalent in cubic feet.

This was my solution to the problem (and it works) is:

CODE
//gallon.cpp
//tests my knowlege, converts gallons to cubic feet

#include <iostream>
#include <conio.h>
using namespace std;

int main()    {
    double gallon;

    cout << "Enter the number of gallons: ";
    cin >> gallon;
    gallon /= 7.481;
    cout << "Volume in cubic feet is: " << gallon;
    getch();
    return 0;
}



My book did it a little different. It used 2 separate variables. Here it is:

CODE
#include <iostream>
#include <conio.h>
using namespace std;

int main()    {
    float gallons, cufeet;

    cout << "\nEnter quantity in gallons: ";
    cin >> gallons;
    cufeet = gallons / 7.481;
    cout << "Equivalent in cubic feet is " << cufeet << endl;
    getch();
    return 0;
}


(I didn't compile this version, so sorry for any errors.) I was wondering, is there any inherent advantage to one over the other? Just wanted to ask and make sure my way wasn't a 'bad' way to do it, and I was also curious if the way I did it might be superior in some situations. Thanks!

Edit: Oh, I forgot to ask. I was also wondering if there's a way to use the /= arithmetic operator to divide the other way if you catch my drift. Switch from x/y to y/x.

This post has been edited by Guydin: 7 Jun, 2007 - 09:26 PM
User is offlineProfile CardPM
+Quote Post

RautRupali
RE: My Answer Vs My Books Answer...
7 Jun, 2007 - 09:19 PM
Post #2

New D.I.C Head
*

Joined: 7 Jun, 2007
Posts: 44


My Contributions


Hi,

Your way of coding is nice as u r using only one variable..It saves memory...

Thanks.
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: My Answer Vs My Books Answer...
7 Jun, 2007 - 09:54 PM
Post #3

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,858



Thanked: 49 times
Dream Kudos: 550
My Contributions
I agree with RautRuali, however there is something to be said for using the two variables. In the end of your program you have a variable named gallons which does not contain any gallon information rather now it is cubic centimeters... This can be confusing when reading over your code.

But this can be solved by giving the variable a better name, and adding comments. So, although you code is slightly less readable, it is probably more elegant. -- There is little to no semantic difference. I like your code better.
User is offlineProfile CardPM
+Quote Post

no2pencil
RE: My Answer Vs My Books Answer...
8 Jun, 2007 - 03:20 AM
Post #4

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 6,442



Thanked: 66 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
CODE

gallon /= 7.481;

is short for ...
CODE

cufeet = gallon / 7.481;


The only reason that I can think of using the 2nd method is if you need to refer to the original value of gallon. Rather than just manipulate the value into a new variable, you overwrote the variables value. Granted, if you no longer need it's existing value, then as noted by NickDMax, you are just using less variables & the books code is using more overhead to do the same leg work.
User is online!Profile CardPM
+Quote Post

Topher84
RE: My Answer Vs My Books Answer...
8 Jun, 2007 - 05:25 AM
Post #5

D.I.C Head
Group Icon

Joined: 4 Jun, 2007
Posts: 232


Dream Kudos: 25
My Contributions
The only real "problem," if you want to call it that is the "re-usability" of the code. As in if you ever need to use gallons for a seperate calculation within the same program you would have to prompt the user for that value again. Both code examples are 100% working but as i've found out with A LOT of professors working doesn't always mean the best (or even correct for that matter). As a personal opinion I prefer the books answer overall. If that is all the program is going to do then yes your code would take less memory and therefore be more efficient but if you had to do something else then your code would then require additional memory if you ever needed to use gallons again because it is now a different value.


User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 10:09PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month