Setting up counter.

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2008
Posts: 13
Reputation: IrishUpstart is an unknown quantity at this point 
Solved Threads: 0
IrishUpstart IrishUpstart is offline Offline
Newbie Poster

Setting up counter.

 
0
  #1
Oct 22nd, 2008
So I need to create a set of data points for the function v*t+.5at(squared). The data points need to be every .02 seconds from 0 to 2.0 seconds. How do I set up the counter for that? Also, all of these points need to be wrote to a location so that I can try to graph them. What would be the best way to do that?

Thanks in advance
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 59
Reputation: Chaster is an unknown quantity at this point 
Solved Threads: 3
Chaster Chaster is offline Offline
Junior Poster in Training

Re: Setting up counter.

 
0
  #2
Oct 22nd, 2008
You could try a double variable, like this:
  1.  
  2. for (float c=0.0;c<2.1;c+=0.02) {
  3. ...
  4. }
Inside that for, you compute the values for your function, then you save these either in an array of type double, or (if you want them to be available later) into a text file.
Last edited by Chaster; Oct 22nd, 2008 at 10:27 am.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Setting up counter.

 
0
  #3
Oct 22nd, 2008
Never use this approach in math calculations: 0.02 is a double type approximation of the real number 0.02 so 100*0.02 != 2.0.
In that case you have 100 intervals (and 101 points):
  1. double t;
  2. int i;
  3.  
  4. for (t = 0.0,i = 0; i <= 100; ++i, t = i/50.0) {
  5. .....
  6. }
In that case you also have a ready-to-use index to place all needed values in arrays (to plot graph etc)...
To avoid time expensive conversions of i var from int type to double type you can add in the loop header auxiliary double "index" variable. As usually, no need in this optimization...
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC