-2

I am new in stackoverflow and i am sorry if i make mistakes.

I am starter in C language and i have one project what it needs to subtact a weight per second.For example Weight: 50kg Subtract per second 4%

i found this code

while(waitFor(1))
{
    *weight=(*weight)-(*weight)*0.4;
}

void waitFor(int secs)
{
    int retTime;
    retTime = time(0) + secs;     // Get finishing time.
    while (time(0) < retTime);    // Loop until it arrives.
}

But i dont want to wait x seconds to finish. I want faster solution. Any ideas

**Note: I want to know how much seconds need to make weight 0 Sleep command it is not working in my computer. **

For cleaning and disinfecting of the pool dropped into the water of a chemical a solid body. This solid body upon contact with water immediately begins to dissolve, losing weight equal to 4% by mass per second. If the dissolution rate of the chemical remains constant, to implement program will accept the weight of the solid body in grams and displays after how time will completely dissolve. The time is displayed in a "hours: minutes: seconds". For example, if the dissolution time is 3,740 seconds, displaying 01: 02: 20. To calculate the time you need to implement a function which accepts gram and return the three time parameters ie hours, minutes and seconds. Note that the time printed on the main function.

Michael
  • 13
  • 3

2 Answers2

1

you can use sleep(int) function in loop it will wait suspend the process up to the integer value.

  while((1) && weight > 0)
  {
 sleep(1);
*weight=(*weight)-(*weight)*0.4;
  }

it will wait for 1 seconds and subtraction will made, it will run continuously

Bhuvanesh
  • 1,269
  • 1
  • 15
  • 25
0

Edit:

To find out the number of seconds required for the weight to reach 0:

unsigned seconds = 0;
while(*weight != 0){
   *weight -= *weight * 0.04
   seconds++;
   //in case you have the patience to attend:
   sleep(1000); //in milliseconds => 1 second
}

Please note that weight is considered to be a pointer to an integer type.

Gabe
  • 961
  • 1
  • 10
  • 23
  • I want to know how much seconds need to make weight 0. Sleep is not working in my computer – Michael Dec 06 '14 at 11:20
  • Without any rounding on those calculations, the time should be infinite. For example if you start with 100KG and subtract 10% every second, after 100 seconds you still have 0,002656139889KG. (and goes on forever...) – João Neto Dec 06 '14 at 11:29
  • Look the question i write the exercise. Also tihs aswer it is not for real seconds. – Michael Dec 06 '14 at 11:31
  • @João Neto I am afraid you are wrong. My assumption implies that weight is an integer type ; which means that the value is automatically rounded to the closest integer value. – Gabe Dec 06 '14 at 11:32
  • @Gabe weight subtract every second? – Michael Dec 06 '14 at 11:34
  • @Michael Indeed. Every iteration involves a "pause" of the thread of exactly 1 second. – Gabe Dec 06 '14 at 11:35
  • Depending on the platform you're working on import unistd.h for UNIX(..) or windows.h for Windows. – Gabe Dec 06 '14 at 11:37
  • @Gabe I know - your solution is fine by me. :) I just have an issue with @Michael's comment, because I think the solution is not correct by it's own design. (`f(x) = W*(1-L)^t`) has no zeroes, where W is initial weight and L is the percentage loss). – João Neto Dec 06 '14 at 11:41
  • @Gabe same #include #include #include #include – Michael Dec 06 '14 at 11:44
  • "sleep" is actually Sleep on Windows – Gabe Dec 06 '14 at 11:48
  • Here's a nice wrapper: http://stackoverflow.com/questions/1658386/sleep-function-in-c – Gabe Dec 06 '14 at 11:48
  • ok i make it Sleep(1000) but i must again wait. If the solid body need over 2 hours to dissolve? – Michael Dec 06 '14 at 12:00
  • The Sleep sequence is of course optional. For further processing take advantage of the value of 'seconds'. – Gabe Dec 06 '14 at 12:03
  • @Gabe Hmm ok the value of seconds in loop it is millisecond? – Michael Dec 06 '14 at 12:12
  • No, it is in seconds. – Gabe Dec 06 '14 at 12:12
  • Ahh ok I spend over 4 hours for something too simple! haha but i learn more about timer thanks dude! – Michael Dec 06 '14 at 12:15
  • Please consider rating or marking my code as the solution. – Gabe Dec 06 '14 at 12:16