Notices
Computer & Technology Related Post here for help and discussion of computing and related technology. Internet, TVs, phones, consoles, computers, tablets and any other gadgets.

C++ Loops

Thread Tools
 
Search this Thread
 
Old 26 March 2003, 01:54 PM
  #1  
Big Daz
Scooby Regular
Thread Starter
 
Big Daz's Avatar
 
Join Date: Nov 2002
Location: Bonnie Scotland
Posts: 784
Likes: 0
Received 0 Likes on 0 Posts
Post

I am looking for help in creating a program which takes a number and continuisly divides the answer by 2 until the difference is less than 0.00005.
Can any Computer experts help me ( pretty computer illiterate )?

Cheers

Big Daz
Old 26 March 2003, 01:58 PM
  #2  
Big Daz
Scooby Regular
Thread Starter
 
Big Daz's Avatar
 
Join Date: Nov 2002
Location: Bonnie Scotland
Posts: 784
Likes: 0
Received 0 Likes on 0 Posts
Post

Sorry, realised posted in wrong forum. Can a Mod move this for me please.
Ta

Big daz
Old 26 March 2003, 02:00 PM
  #3  
Dan B
Scooby Regular
 
Dan B's Avatar
 
Join Date: Feb 2002
Posts: 1,377
Likes: 0
Received 0 Likes on 0 Posts
Post

Somthing like this:

While result not equal to 0.000005 divide previous result by 2

printf: "now you just need to code it "
Old 26 March 2003, 02:01 PM
  #4  
Katana
Scooby Regular
 
Katana's Avatar
 
Join Date: Jan 2002
Location: In a house
Posts: 5,153
Likes: 0
Received 0 Likes on 0 Posts
Post

Hehe nice try. I tried to do the same by getting Andy F to do my homework too without joy.
Old 26 March 2003, 02:03 PM
  #5  
BoxerFlat4
Scooby Regular
 
BoxerFlat4's Avatar
 
Join Date: Nov 2001
Location: N Wales
Posts: 923
Received 0 Likes on 0 Posts
Post

float PointlessFunction( float Value )
{
while( Value > 0.00005 )
{
Value = Value / 2.0f;
}
return Value;
}

Tomorrow, we will be explaining polymorphism....
Old 26 March 2003, 02:13 PM
  #6  
Big Daz
Scooby Regular
Thread Starter
 
Big Daz's Avatar
 
Join Date: Nov 2002
Location: Bonnie Scotland
Posts: 784
Likes: 0
Received 0 Likes on 0 Posts
Wink

Homework, Who mentioned Homework !!
Can paint pretty pictures and calculate train fares, just can't get my head round this. Maybe better just carry on with what I get paid for.

Old 26 March 2003, 02:16 PM
  #7  
sooby
Scooby Regular
 
sooby's Avatar
 
Join Date: Jul 2001
Posts: 261
Likes: 0
Received 0 Likes on 0 Posts
Post

BoxerFlat4,

Don't you have to make that a class to make it C++?

Trending Topics

Old 26 March 2003, 02:21 PM
  #8  
BoxerFlat4
Scooby Regular
 
BoxerFlat4's Avatar
 
Join Date: Nov 2001
Location: N Wales
Posts: 923
Received 0 Likes on 0 Posts
Post

It'll compile under C++ : remember, C is just a subset of C++.

If you wanted to be clever :-

class DefineDivisionClass
{
// Class Variable
float m_Value;

// Constructor
void DefineDivisionClass( float value )
{
m_Value = value;
while( m_Value > 0.0005 )
{ m_Value = m_Value / 0.5f; }
}

// Destructor
~DefineDivisionClass ()

// Access Value variable
float GetValue( void )
{ return m_Value; }
{};

// Member functions

};

{
.
.
.
DefineDivisionClass TheClass( 2.0 );
float result = TheClass.GetResult();
.
.
.
}
Old 26 March 2003, 02:36 PM
  #9  
Big Daz
Scooby Regular
Thread Starter
 
Big Daz's Avatar
 
Join Date: Nov 2002
Location: Bonnie Scotland
Posts: 784
Likes: 0
Received 0 Likes on 0 Posts
Red face

Ooooohhh what have I started here !! (feel head getting ready to explode)
Old 26 March 2003, 02:59 PM
  #10  
Dan B
Scooby Regular
 
Dan B's Avatar
 
Join Date: Feb 2002
Posts: 1,377
Likes: 0
Received 0 Likes on 0 Posts
Post

Use a spreadsheet 10 times quicker...
Old 26 March 2003, 04:14 PM
  #11  
MrDeference
Scooby Regular
 
MrDeference's Avatar
 
Join Date: Mar 2002
Posts: 337
Likes: 0
Received 0 Likes on 0 Posts
Post

static int CMyClass::GetIteratedAmount(int x)
{
int count = 0;
while ((20000 * x) > (2<<count))
{
count ++;
}
return count;
}
Old 26 March 2003, 04:49 PM
  #12  
Rob Walker
Scooby Regular
 
Rob Walker's Avatar
 
Join Date: Nov 1999
Location: Stockport
Posts: 474
Likes: 0
Received 0 Likes on 0 Posts
Post

spot the bug time

// Constructor
void DefineDivisionClass( float value )
{
m_Value = value;
while( m_Value > 0.0005 )
{ m_Value = m_Value * 0.5f; } // should be * 0.5 rather than / 0.5
}
Old 26 March 2003, 04:59 PM
  #13  
MorayMackenzie
Scooby Senior
 
MorayMackenzie's Avatar
 
Join Date: Jun 1999
Posts: 3,410
Likes: 0
Received 0 Likes on 0 Posts
Post

Re: "It'll compile under C++ : remember, C is just a subset of C++."

Hum.

C arrived first, by many years.

C++ is mostly a superset of C, mostly.


Moray

(thinks: " for ( ; value > 0.00005 ; value/=2.0F ){}; " )
Old 26 March 2003, 05:18 PM
  #14  
MrDeference
Scooby Regular
 
MrDeference's Avatar
 
Join Date: Mar 2002
Posts: 337
Likes: 0
Received 0 Likes on 0 Posts
Post

Since we are bug fixing...
As long as the number is a whole number between 1 and 214747 mine's the quickest.
If the number has to be a decimal or out of those bounds change the parameter to a double and it's still quickest.
:P

[EDIT] Actually it dawns on me that I am solving a different problem to everyone else PMSL.
Before I go back to sleep is the final value important or did you just want the count?

[Edited by MrDeference - 3/26/2003 5:28:22 PM]
Old 26 March 2003, 05:24 PM
  #15  
stevencotton
Scooby Regular
 
stevencotton's Avatar
 
Join Date: Jan 2001
Location: behind twin turbos
Posts: 2,710
Likes: 0
Received 1 Like on 1 Post
Post

It'd be even faster without the inital "2<<0" surely
Old 26 March 2003, 05:29 PM
  #16  
MrDeference
Scooby Regular
 
MrDeference's Avatar
 
Join Date: Mar 2002
Posts: 337
Likes: 0
Received 0 Likes on 0 Posts
Post

Well, yeah.
You know I have been sat here looking at it, and it's only algebra. Something that could be solved with a one hit equation. No iteration, nothing.
Can I work out what it is? Nope.
Related Topics
Thread
Thread Starter
Forum
Replies
Last Post
Mister:E
Subaru Parts
2
24 September 2015 01:37 PM
Scooby Snax
Computer & Technology Related
8
20 May 2002 02:50 PM
chopper
Computer & Technology Related
5
27 February 2002 04:58 PM
Crispin
Computer & Technology Related
1
12 February 2002 12:16 PM
paulmon
Non Scooby Related
2
16 November 2001 05:15 PM



Quick Reply: C++ Loops



All times are GMT +1. The time now is 02:28 AM.