Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Please help to write the C code

Here is a version of a problem called the random walk. 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:

I`m not familiar with C. only with Python but my exam is in few days and I have to submit. Please help.
Posted
Updated 6-Jan-22 3:07am
Comments
jeron1 5-Jan-22 12:43pm    
So you have an exam on the C language, but you're not familiar with the C language? You got yourself a conundrum there.
0x01AA 5-Jan-22 12:45pm    
Now if you are familiar with Python then you can try to solve it first with Python. At least you have then a programatically abstration of the problem.
Of course converting the Python code to C is then the next challange...
Richard MacCutchan 5-Jan-22 12:49pm    
Explain to your teacher that you do not know C so the question is unfair.
innapivov 5-Jan-22 12:52pm    
Well, your advices are good but I can`t lose my scholarship, so if there`s a volunteer to help with a code, + to your karma
jeron1 5-Jan-22 12:55pm    
Maybe try freelancer.com?

Quote:
Well, your advices are good but I can`t lose my scholarship, so if there`s a volunteer to help with a code, + to your karma

There are several problems here:
1) You clearly have misunderstood karma. We wouldn't get good karma for doing something that would add bad karma to someone else's total - We would know we were helping you cheat and cheating doesn't add good karma at all, does it? So we'd get bad karma, you'd get bad karma, and where is the point in that?

2) If we hand you code, you don't learn anything: you learn by doing, not by looking (if you even read the code before handing it in, which I doubt) so we would be contributing to your lack of education! Argh! More bad karma!

3) If you don't learn from this exercise, then the next one is even harder to do, because that's what homework is there for: it builds on what you learned from previous exercises assuming that you fully understand it. So the next exercise is more difficult for you to do, so you are more likely to have to get someone else to do it for you, and yet more bad karma is generated for everyone involved.

4) Your scholarship is no use to you if you fail the course - which you will if you get to the end unable to actually do simple tasks like this in the final exam. And right there is even more bad karma for us!

5) You do realize that your teachers are fully aware of sites like this and do check you submissions for plagiarism - and a good way to get dismissed from your scholarship early is by cheating: handing in someone's else's work and claiming it as your own. And that really easy to spot when you have a little experience. Oh oh ... more bad karma for us!

All in all, the best thing that we can do is tell you to try and do it yourself - because that does generate good karma by doing the best thing in the long run for you!

So that's what I'm doing: telling you to try it for yourself. What's the worst that can happen?
 
Share this answer
 
v2
Start with some Learn C tutorial. It wont be to hard if you can already code in some other language, but you need to respect the rules of C like a bunch of braces and declarations.

Best is to write some functions and use struct. For some random data you can use the rand function. In the example code you are seeing annoying include syntax. It is similar to Python, but you also need sometimes to add libs to the linker.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900