15,169,085 members
1.00/5 (1 vote)
See more:
```#include <stdio.h>
#include <stdlib.h>
#include<time.h>>
int main() {

int i, next_move, night_at_home;
int position;
float probability;
night_at_home=0;
char p = '%';
time_t t;
srand((unsigned) time(&t));
for (i = 0; i <= 500; i++){
position = 0;
while(position < 10 || position <= 0){
next_move=rand() %2;
if(next_move== 0) { position-= 1;}
else { position+= 2;}

if(position <= 0 || position >= 10) {
break;
}
}
if (position >= 10) {
night_at_home += 1;
}
position = 0;

}

probability = night_at_home/500.0f;
printf("The probability of dog sleeping at home equals to - %.0f%c\n", probability*100, p);

return 0;
}```

Quote:

Assume that you`re very tired and dog leaves his lamppost on summer evenings and staggers randomly either two steps in the direction toward home or one step in the opposite direction. After taking these steps, the dog again staggers randomly two steps toward home or one step backward and does this again and again. If the pet reaches a total distance of 10 steps from the lamppost in the direction toward home, you find him and take him home. If the dog arrives back at the lamppost before reaching 10 steps in the direction toward home, he lies down and spend the night at the foot of the lamppost. Write a C program that stimulates 500 summer evenings, and calculate and print the percentage of the time your pet sleeps at home for these evenings. Accumulate the distance the dog has reached toward your home. If the distance reaches 10, stop the loop and increment the home count. If the distance reaches 0 before it reaches 10, stop the loop but do not increment the home count. Repeat this loop 500 times and find the ratio of (home count)/500.0.

What I have tried:

Just wrote a code but an issue with an answer
Posted
Updated 4 days ago
v3
CHill60 4 days ago

So what is the answer you get and what was the answer you were expecting. Given that you are using random numbers how can you project what the answer would be? You should log those numbers somewhere (e.g. output to console) so that you can check everything manually
Member 15499757 4 days ago

Sorry I`m still new to this, could you please correct my code?
CHill60 4 days ago

I can't correct anything if I don't know what is wrong with it. What do you think is wrong?
Member 15499757 4 days ago

Please, I just want you to take a look at the task and say whether the code is right, because some guys from my group got 35% so I don`t know what is the correct percentage
Richard Deeming 4 days ago

Removing the content of your question after it has been answers is extremely rude. And it will not stop your teacher from noticing your attempt to cheat on your homework.

I have rolled back your destructive edit.

## Solution 1

Your requirements are not rigorous, the program should make assumptions for handling the corner cases.
Anyway, try
C
```#include <stdio.h>
#include <stdlib.h>
#include<time.h>

int main()
{
const int Nights = 500;
int position;
double percent;
int nights_at_home  = 0;

srand((unsigned) time(NULL));

for (int i = 0; i < Nights; ++i)
{
position = 2; // this assumes the dog cannot go to position -1
do
{
position += (rand() % 2 == 0) ? 2 : -1;
if ( position >= 10) // this assumes the dog can go to position 11
++nights_at_home;
} while (position > 0 && position < 10);
}

percent = 100.0 * nights_at_home / Nights;
printf("The percent of nights of dog sleeping at home equals to - %.0f%%\n", percent);

return 0;
}```

## Solution 2

Quote:
because some guys from my group got 35% so I don`t know what is the correct percentage
There is no correct percentage. You are using random numbers so the results should be different not just for everyone in your class, but for each time you run it (the numbers are not truly random but that is not relevant here).

That is why I suggested that you output the "moves" to the console, then you can repeat them on squared paper to check your logic.

See solution 1 for the improvements that your code needs.