Aces High Bulletin Board

General Forums => The O' Club => Topic started by: SKurj on October 15, 2002, 04:04:41 PM

Title: A C++ question...
Post by: SKurj on October 15, 2002, 04:04:41 PM
I am currently in a C++ course..

I wrote a test today, and as part of the test I had ta make a function that would convert a length in meters(1 variable) , to both FEET and INCHES...(2 variables)

i managed to get the result errm partially right...

I don't have a copy of my solution but my answers were rounded off... which they shouldn't be...

I was stumped even as far as variable declaration went...
The length in meters i declared as double.

my solution resembled this...

double meters;
int b,c;
double a,d;

a = meters *39.37;  //(or whatever the conversion factor is)
b = a % 12;
c = a/12;

bah memory suffering.  The part I know I got wrong was that the inches should go to at least 1 decimal... my results of course were rounded.

Can any of yas offer a solution?


SKurj
Title: A C++ question...
Post by: Pei on October 15, 2002, 04:44:17 PM
The problem is that you declared
b and c as ints

let's look at
b = a % 12;

the literal 12 is an int (and the operation a% 12 is being assigned to another int (b in this case)) therefore the operation will be performed using integer arithmatic so you will get an int result (i.e. you lose the fractional part).

the same applies for c = a/12

so declare b and c to be real types (float or double) and instead of using the int literal 12 use a float or double value instead (e.g. 12.00): you'll get real arithmatic and hence a real result.

You'll then need to format the results to the correct number of significant figures.

btw just a hint: use descriptive names for your variables as it makes life easier in the long run.

also do you really need double precision? If not use float as it takes less resources.
Title: A C++ question...
Post by: funkedup on October 15, 2002, 05:47:10 PM
What Pei said.  :)
Title: A C++ question...
Post by: Chairboy on October 15, 2002, 06:08:16 PM
For descriptive variable names, consider using some hungarian notation:

For example, an integer that contains the value in meters would be iMeter or a string variable for the title might be strTitle.  It really helps because it's an easy reminder of what the scope of the variable is without making you go to your headers to check if something is a pointer to a character vs. a far pointer to a count of words, for instance.

There are lots of agreed on standards, a helpful reference might be:  

http://web.umr.edu/~cpp/common/hungarian.html

or

http://www.gregleg.com/oldHome/hungarian.html
Title: A C++ question...
Post by: koala on October 15, 2002, 06:10:17 PM
Not sure why you're using "b = a % 12", as '%' is a modulo operator.

Here's a solution:

void convert(const float meters, float &inches, float &feet)
   {
   inches = meters * 100.0 / 2.54;
   feet = inches / 12.0;
   }
Title: A C++ question...
Post by: sshh on October 15, 2002, 07:04:27 PM
Its not C++ if there are no "class" word. You should've used at least one class ! ;)
Title: A C++ question...
Post by: SKurj on October 15, 2002, 07:49:04 PM
thanks for the helps

I've had a few other suggestions as well.

lets look at what i started with and what i wanted for a result first...

i started with lets say 2.5 meters
I then basically had to convert that to
8 feet 2.4 inches

a class mate basically did this... (at least i think this is what he was sayin)

float totalinches,remainderinches
int feet

totalinches = meters * 39.37
feet = totalinches/12  (note that by usin int here it rounded down)
remainderinches = totalinches - feet

all i really needed for out put was nothin more precise than 8'2.4"

I understand about naming conventions, however we aren't being marked on that +)
I had two hours to do the test and i type lousy too +Q
errm the test consisted of more than just this little problem, this was a part of the whole.

and ssh... this function was a non-member function! +)

thanks for the input guys
i think i likely lost a mark or two for my lousy maths

SKurj
Title: A C++ question...
Post by: whgates3 on October 15, 2002, 11:52:11 PM
you should just write it in PERL or FORTRAN and only turn in a binary executable
Title: A C++ question...
Post by: StSanta on October 16, 2002, 01:03:52 AM
Heh SKurj, just wait til you have to work with libraries developed by others:

(http://home1.stofanet.dk/stsanta/bleh.jpg)

This is what I'm currently facing :D. The mysql++ API gives me so many performance warning under VC++6 it almost makes me feel GOOD about my own abilities :D
Title: A C++ question...
Post by: straffo on October 16, 2002, 02:44:25 AM
VC++ is a toy !


real man use cc and vi ;)


straffo currently in a VMS revival (berk berk .... ) :(
Title: A C++ question...
Post by: funkedup on October 16, 2002, 02:58:07 AM
VMS MUST DIE!!!
Title: A C++ question...
Post by: straffo on October 16, 2002, 03:04:35 AM
at least VMS is more funny than Tandem NSK and their ù*^$^ù*  pTAL language :rolleyes:  
and working for stock exchange guys was a bit stressing too :)

I noticed that not everyone know Tamdem NSK (afaik there is less than 0.00001% person in the world knowing that beast )
here is a description I ripped on another site

Fully redundant hardware - paired CPUs, mirrored disks, dual power supplies, Operating System support to transparently restart jobs from failed CPUs to it's paired CPU.
// straffo : yep I love process playing ping pong to death :D
Let me explain you how it work  :
Process 1 start on CPU 0 and launch his backup process 2 on CPU 1
from time to time process 2 is synchronized aka all the context of process 1 is send to process 2
Now imagine that just the instant before the sync. between PS we have all the context to have an abnormal end of PS1...

it end like that :
PS1 crash PS2 take over restart a backup process (don't forget we are non-stop) synchronise with is new backup (and so making sure it would crash latter :D) and crash ...

And now  : play it again Sam :) I love NSK :p

It make programming fault funny ... I even seen the Paris stop exchange stopped 1/2 day because of that



There is the Kernel, called NSK (for Non-Stop Kernel).
// straffo : sometime it's Non Startable Kernel too ... I never seen a Tandem stop ... but sometime I've seen Tamdem unable to boot :p

There are at least two user operating environments. The old traditional one is called Guardian, and it's shell interpreter is called TACL (pronounced "tackle" as in "bait").
// straffo : TACL is fun ! like Rexx for you IBM oldies
The newer one is called OSS (Open System Services), and it an admirable approximation of DEC Unix (which makes sense, since Compaq did buy Digital Equipment back in the mid-1990s.
// straffo : approximation is rigth on spot :)
Fixed-hierarchical filesystem - in other words, every filename has exactly three (no more, no less) components, each up to 8 characters long:
disk volumne (usually prefixed with a '$' sign)
subvolume (eight characters)
filename (eight characters)
These three components have a '.' period between themm. So, a valid filename might be $DISK01.DATA01.CUSTOMER. There are no extensions.

// straffo : you feel the love
// straffo :  especially when porting a U*x program to Tandem :D


There is a numeric file type associated with each file, and a particular program cannot read any file it wants to. In addition to a somewhat-more-complex-than-Unix permission scheme, each program is only allowed to open certain kinds of filetypes. This prevents text editors from opening binary files, but is totally against the Unix way of doing things.
// straffo : root is called super.super on NSK (or 255,255)
// straffo : one fun fact is  : saving data on NSK don't exist ... all buffer are saved !

Title: A C++ question...
Post by: funkedup on October 16, 2002, 03:16:56 AM
I'm glad to say I don't know what pTAL is.  :)

But learning VMS was a waste of time for me.  I learned VMS before I even knew what Unix was.  :D
Title: A C++ question...
Post by: AcId on October 16, 2002, 08:12:21 AM
Straffo!!!

lol

VI - The best editor known to man, only rival is ed. ;)

vi stands for Very Intuitive :D

VMS - What a bucket of bolts that is, I work at Hewlett-Compaqard (classic compaq) can't beleive folks still use VMS.
Title: A C++ question...
Post by: straffo on October 16, 2002, 08:26:57 AM
I was not aware of any still running microvax
I used one when I was a beginner some 10 year ago :)

yes it's the 1985 version  :eek: :eek: (http://www.montagar.com/dfwcug/VMS_HTML/timeline/photos/microvax2.jpg)

But as it still work the customer decided to keep it :rolleyes:
Title: A C++ question...
Post by: AcId on October 16, 2002, 08:40:57 AM
we have quite a few customers who still use it, It's mainly running AlphaServers now. Matter of fact I have it installed on one of these in my lab right now externaly booting on a Fibre attached storage array. :cool:

ES40
Title: A C++ question...
Post by: straffo on October 16, 2002, 08:51:12 AM
I bet it will be sci fi for my customer :)

Today I recieved an email ... he saw that and said 'you got a new fax' :D
Title: A C++ question...
Post by: StSanta on October 16, 2002, 10:15:58 AM
LOL, you guys are truly oldtimers.

I am waiting for one of you to start talking about what a squeak it is when the hole card punching machine breaks down :D

Had a prof that went on and on about his experiences with them :D
Title: A C++ question...
Post by: H. Godwineson on October 16, 2002, 10:18:56 AM
You guys are SICK!  Get an abacus!

Shuckins
Title: A C++ question...
Post by: whgates3 on October 16, 2002, 02:51:28 PM
Mi computer r0o|z:
(http://cray.com/images/systems/cray1.gif)
Title: A C++ question...
Post by: john9001 on October 16, 2002, 03:15:03 PM
im' teaching myself c++ , to day i lernt what  // means or was it \\...?? darn
Title: A C++ question...
Post by: SKurj on October 16, 2002, 03:46:49 PM
In our C+ class we are on windows boxes.. no cc or vi there, altho in unix class i am starting to like vi +)


SKurj