Need help on C Programming
Imagine a turtle crawling around aimlessly so that it turns to the left or to the right (by 90 degrees) and then walks forward 25 units exactly 100 times. Create a flowchart or write pseudocode for accomplishing this dizzying task.
@jango_IN_DTOWN
Have you worked this one out?
I have to use these for choosing the random movement of going left or right: #include <stdio.h> #include <stdlib.h> #include <time.h> int main(void) { srand(time(NULL)); /* Seed the random number generator */ `int answer = rand() % 100 + 1; /* Randomly select a number */` this can be changed
Nope. I am confused on how I can make the computer choose randomly whether to go left/right.
you dont have to create a program, it said to create flowchart or pseudocode
But I was thinking of assigning left a number and right another number. For example left = 0 and right = 1. Then I can perhaps use: int answer = rand() % 100 + 1; / to order to randomly select a number
Well in another part it says for me to implement the flowchart or pseudocode by writing it out as a C program
It will be better if you post this question in computer science portion, I will be studying string functions in a later course. Right now I am not acquainted with it properly. I know those portions of C in which numerical methods are involved in mathematics
I am honestly not so sure about string functions myself. My teacher just said to use that whenever we're asking the computer to choose something random.
For example, let's say we have `int n` Can I say `int n = rand() % 2;` I learned that when choosing the random numbers n will subtract 1. Therefore, by choosing 2 and subtracting 1 I will get the number 1. And I designated that left = 0 and right = 1
ok, so when you declare int n, it will takes integer values only and since you operate modulus by 2, it will have answers 0 or 1, nothing else. This part is correct
OK
So can we start creating the program?
But how do you show the turtle moves 25 units 100 times, I am not familiar with this stuff
I guess with while loop?
we can use this perhaps? while (time=100) { turtle_forward(25); }
Your program worked?
I am still writing it. It's almost done
Good
How do I move a file from one directory to the other?
I accidentally wrote my program one directory "experiments" but it's supposed to be in another one named "turtle"
GO in the directory you wrote, find the file name. It will have the extension .CPP. Just copy and paste it
in the required directory
OK did it !
paste the program code here..
I accidentally deleted while compiling it!
you type ctrl and v at once
it will be back
It's not working
undo option not working?
Nope!
then you have to work it out again...
Yeah I guess so. Just give me like 5 minutes plz
ok sure.:)
#include <stdio.h> #include <stdlib.h> #include <time.h> #include "turtle.h" int main(void) { int times; srand(time(NULL)); while (times<=100) { int left_or_right = rand() % 2; if (left_or_right==1) { turtle_turn_left(90); } else { turtle_turn_right(90); } turtle_forward(200); times++; } turtle_save_bmp("drawing.bmp"); turtle_cleanup(); return 0; }
When I try to run it Preview says that the file cannot be opened
#include<turtle.h>
they say it needs to be #include "turtle.h"
wait a bit, I dont know about drawing.. Which book are you reading?
What do you mean by what book I am reading?
I mean, reference book, which you follow to learn C++
It's an elective class I am taking at my school
So you dont read from any other refenrence book? /i am not acquainted with drawing in C++
This is supposed to be only C
Yeah, C or C++ you may use same software package, only a few commands are different
OK thank you for helping though :)
Join our real-time social learning platform and learn together with your friends!