Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 5th, 2005, 10:11 AM   #1
Easter Bunny
Programmer
 
Easter Bunny's Avatar
 
Join Date: Mar 2005
Location: different places. constantly on the run.
Posts: 57
Rep Power: 4 Easter Bunny is on a distinguished road
um, rounding? anyone?

g'day, i'm currently studying through correspondence and one of the questions for an assignment has to do with changing pounds to kilo's, or yards to meters or something. that's fine, i can do that, but then it say round up to 2 decimals. i searched the site and quickly googled it, but didn't find anything that made sense. i looked in the books they gave me + the book i got from my mom's library, but that didn't help, because i couldn't find anything.

is there something in c++ that lets you set a standard "look" for your numbers like java's DecimalFormat? or do i have to stay up all night and drink lots of (strong) coffee and make my own function thingy that will look at the numbers after the "." and ceil() or floor() or something like that?

oh, i'm very new new to c++. started studying this year, so you can't laugh at me and tell all your friends that there's actually a dude who doesn't know how to implement rounding in c++.

thanks for the help(in advance of course)and for those who help me, you should pop round sometime and we can have a braai(bbq in english)and talk about integers, strings, etc. it'll be fun.Ü
__________________
There's got to be more to life than being really, really
ridiculously good looking
Easter Bunny is offline   Reply With Quote
Old Apr 5th, 2005, 11:20 AM   #2
spydoor
Programmer
 
Join Date: Feb 2005
Posts: 64
Rep Power: 4 spydoor is on a distinguished road
cout << fixed << setprecision(2) << value;
spydoor is offline   Reply With Quote
Old Apr 5th, 2005, 5:26 PM   #3
tempest
Programming Guru
 
tempest's Avatar
 
Join Date: Oct 2004
Posts: 1,041
Rep Power: 5 tempest is on a distinguished road
Send a message via ICQ to tempest Send a message via AIM to tempest Send a message via Yahoo to tempest
float round(float val, int prec) {
  	val *= (10^prec);
  	val = (int)val;
  	return val/(float)prec;
  }
__________________


Last edited by tempest; Apr 5th, 2005 at 5:44 PM.
tempest is offline   Reply With Quote
Old May 3rd, 2005, 10:20 AM   #4
Easter Bunny
Programmer
 
Easter Bunny's Avatar
 
Join Date: Mar 2005
Location: different places. constantly on the run.
Posts: 57
Rep Power: 4 Easter Bunny is on a distinguished road
here you go, this is what i've got. it works on some numbers, but not on all. i end up being out by 0.000000000000...00001 sometimes. sat for hours trying to work something out, but not getting there.

the Temp = ceil( f ); part works for the first part of the assignment's test data, but on the second i'm over by 0.000001, then it rounds up and then i'm over by 0.01 or something. stole the recursive function from another post here(thanks dude! ). if i take out the ceil( f ) bit, then on the first bit of data i'm under by 0.01 or so.

i'm close to having a hernia.

int exponent( int Num, int Exp )
{
    if ( Exp == 0 )
        return 1;
    return ( Num * ( exponent( Num, Exp-1 ) ) );
}

float rounding( float f, int Exp )
{
    int Temp;
    int Div = exponent( 10, Exp );
    f *= Div;
    
    Temp = ceil( f );
    
    f = (int)Temp;
    f /= Div;
    return f;
}

thanks for the help(in advance of course).
__________________
There's got to be more to life than being really, really
ridiculously good looking
Easter Bunny is offline   Reply With Quote
Old May 3rd, 2005, 10:21 AM   #5
Easter Bunny
Programmer
 
Easter Bunny's Avatar
 
Join Date: Mar 2005
Location: different places. constantly on the run.
Posts: 57
Rep Power: 4 Easter Bunny is on a distinguished road
when i use
cout << fixed << setprecision(2) << value;

do i have to include something special, because it doesn't work when i compile.
__________________
There's got to be more to life than being really, really
ridiculously good looking
Easter Bunny is offline   Reply With Quote
Old May 3rd, 2005, 10:41 AM   #6
BaroN NighT
Programmer
 
BaroN NighT's Avatar
 
Join Date: Mar 2005
Location: Lexington, Ky
Posts: 58
Rep Power: 4 BaroN NighT is on a distinguished road
Yes you do :

#include <iomanip>

then you can use setprecision(n). n is the number of decimal places you want the program to output.
BaroN NighT is offline   Reply With Quote
Old May 6th, 2005, 7:11 AM   #7
Easter Bunny
Programmer
 
Easter Bunny's Avatar
 
Join Date: Mar 2005
Location: different places. constantly on the run.
Posts: 57
Rep Power: 4 Easter Bunny is on a distinguished road
ok, so i included iomanip, but it still says "'setprecision' undeclared (first use this function)".

i'd rather ask a question and look stupid, than be stupid forever, so here goes...

cout << fixed << setprecision(2) << value;

what would it look like in my program? this:

cout << fixed << setprecision(2) << Arr[i];

Arr[i] is an array of floats.

i do feel slightly stupid now, but i'm sure it's not gonna last for long... and think of all the other people you're helping who are too affraid to ask and blah blah blah...

thanks.
__________________
There's got to be more to life than being really, really
ridiculously good looking
Easter Bunny is offline   Reply With Quote
Old May 6th, 2005, 7:56 AM   #8
spydoor
Programmer
 
Join Date: Feb 2005
Posts: 64
Rep Power: 4 spydoor is on a distinguished road
use namespace std , or fully qualify
#include <iostream>
#include <iomanip>
using namespace std; // if you don't do this you have to fully qualify cout, fixed, setprecision .....

int main () {
  double f =3.14159;
  cout << fixed << setprecision (2) << f << endl; 
  std::cout << std::fixed << std::setprecision (4) << f << std::endl;
  return 0;
}
spydoor is offline   Reply With Quote
Old May 6th, 2005, 8:38 AM   #9
Easter Bunny
Programmer
 
Easter Bunny's Avatar
 
Join Date: Mar 2005
Location: different places. constantly on the run.
Posts: 57
Rep Power: 4 Easter Bunny is on a distinguished road
this is the whole thing so far. first two functions i spent hours on, but it doesn't work 100%.

//assignment 3 --> question 1a
#include <iostream>
#inclide <iomanip>
using namespace std;

int exponent( int Num, int Exp )
{
    if ( Exp == 0 )
        return 1;
    return ( Num * ( exponent( Num, Exp-1 ) ) );
}

float rounding( float f, int Exp )
{
    int Temp;
    int Div = exponent( 10, Exp );
    f *= Div;
    //this worked for the first half of the test data, but second half it's out by a bit
//    Temp = ceil( f );
    
    f = (int)Temp;
    f /= Div;
    return f;
}

void getInput( float Arr[] )
{
    for ( int i = 0 ; i < 20 ; i++ )
    {
        cin >> Arr[i];
    }
}

void getDifference( float Arr1[], float Arr2[], float Arr3[] )
{
    for ( int i = 0 ; i < 20 ; i++ )
    {
        Arr3[i] = Arr2[i] - Arr1[i];
    }
}

void displayResults( string Message, float Arr[], int Round )
{
    cout << "The differences in " << Message << " : " << endl << endl << endl;
    
    for ( int i = 0 ; i < 20 ; i++ )
    {
        if ( Arr[i] < 0 )
        {
        //    cout << i << " -> " << " down" << endl;
            cout << fixed << setprecision(2) << -Arr[i];
        }
        else
        {
            if ( Arr[i] > 0 )
            {
                //this is my attempts at the rounding thing with those functions
                cout << i << " -> " << rounding( Arr[i], Round ) << " up" << endl;
            }
            else
            {
                cout << i << " -> stayed the same" << endl;
            }
        }
    }
    
    cout << endl;
}

int main()
{
    float Info2004[20], Info2005[20], Difference[20];
    string Message;
    
    cout << "Enter April 2004 lengths : " << endl;
    getInput( Info2004 );
    
    cout << "Enter April 2005 lengths : " << endl;
    getInput( Info2005 );
    
    getDifference( Info2004, Info2005, Difference );
    Message = "weights";
    displayResults( Message, Difference, 2 );

//*******************

    cout << "Enter April 2004 weights : " << endl;
    getInput( Info2004 );
    
    cout << "Enter April 2005 weights : " << endl;
    getInput( Info2005 );
    
    getDifference( Info2004, Info2005, Difference );
    Message = "lengths";
    displayResults( Message, Difference, 1 );
    
    system( "PAUSE" );
    return 0;
}

__________________
There's got to be more to life than being really, really
ridiculously good looking
Easter Bunny is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:51 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC