Author |
Message |
munt4life
|
Posted: Tue Oct 12, 2010 7:59 pm Post subject: Turing loop help |
|
|
Hey could someone help me on these, i found them on a small quiz and found them tough. Thanks!
1. Create a program that gets ten numbers from the user and computes the average of the numbers.
2. Create a program that allows the user to try to guess a number from 1 to 1000. The number to be guessed will be randomly generated by your program. After each guess tell the user if the real number is higher of lower. If they guess the right number in 10 or fewer guesses tell them they did a good job, otherwise don't be as nice. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Sur_real
|
Posted: Tue Oct 12, 2010 9:46 pm Post subject: Re: Turing loop help |
|
|
TURING!!!!!! It's been a while.
Since I don't want to provide any direct answers...
For 1. It's actually quite simple.
code: |
int count = 1;
int num = 0;
loop
{
output "enter a number";
get num;
temp = num;
num = num + temp; // Assuming you guys didn't do arrays yet but seriously arrays FTW
count++; // increment counter
} exit when count==10; // So when the program ran 10 times you exit
avg = num/10;
output "the average is" + avg;
|
For 2. A little more tricky.
code: |
int rand = createRandInt(1,1000); // Dunno the actually function but I'm sure you should've memorized it
int count = 1;
int num = 0;
loop
{
output "enter a number";
get num;
if num<rand
output num + " is less than the random value";
if num>rand
output num + " is greater than the random value";
if num==rand;
exit;
count++;
}
if count <=10;
output "Congrats, you got the number in 10 or less guesses";
else
output "it took more than 10 guesses";
|
For the second question you can also opt to stop the program after a certain number tries
*The code should be right and logical lol |
|
|
|
|
|
DanShadow
|
Posted: Tue Oct 12, 2010 11:32 pm Post subject: RE:Turing loop help |
|
|
Sur_real wrote: Since I don't want to provide any direct answers...
Lol, looks like you did his homework for him |
|
|
|
|
|
copthesaint
|
Posted: Tue Oct 12, 2010 11:54 pm Post subject: Re: Turing loop help |
|
|
Sur_real @ Tue Oct 12, 2010 wrote: TURING!!!!!! It's been a while.
Since I don't want to provide any direct answers...
........................................................................
........................................................................
*The code should be right and logical lol
Thats not turing. Good try though |
|
|
|
|
|
munt4life
|
Posted: Wed Oct 13, 2010 6:08 am Post subject: RE:Turing loop help |
|
|
I actually didnt want the answe but haha ok, that isnt turing |
|
|
|
|
|
DanShadow
|
Posted: Wed Oct 13, 2010 7:04 am Post subject: RE:Turing loop help |
|
|
I never said it was Turing .
That code/pseudo-code can be directly translated into Turing by simply interpreting what each line would be in Turing.
Really very little difference from giving him the code in Turing, still gives him the answer, lol. |
|
|
|
|
|
JamesV
|
Posted: Wed Oct 13, 2010 8:16 am Post subject: RE:Turing loop help |
|
|
Well the first program , you don't even need a loop, unless you use an array, but it can be done with simple commands easily (Even though it would be shorter using an array) |
|
|
|
|
|
DanShadow
|
Posted: Wed Oct 13, 2010 8:32 am Post subject: RE:Turing loop help |
|
|
I'd still recommend using a loop, but definitely an array would be a bit more efficient, but the end result is the same. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Sur_real
|
Posted: Wed Oct 13, 2010 12:18 pm Post subject: Re: RE:Turing loop help |
|
|
DanShadow @ Wed Oct 13, 2010 7:04 am wrote: I never said it was Turing .
That code/pseudo-code can be directly translated into Turing by simply interpreting what each line would be in Turing.
Really very little difference from giving him the code in Turing, still gives him the answer, lol.
yeah...bad pseudo-code while you're at it |
|
|
|
|
|
chrisbrown
|
Posted: Wed Oct 13, 2010 5:33 pm Post subject: Re: RE:Turing loop help |
|
|
DanShadow @ Wed Oct 13, 2010 8:32 am wrote: I'd still recommend using a loop, but definitely an array would be a bit more efficient, but the end result is the same.
An array would use 10 times more memory than is needed, and provides nothing in terms of CPU efficiency.
An explicit loop counter isn't needed either. That's what for loops are for. |
|
|
|
|
|
Sur_real
|
Posted: Wed Oct 13, 2010 7:35 pm Post subject: Re: RE:Turing loop help |
|
|
chrisbrown @ Wed Oct 13, 2010 5:33 pm wrote: DanShadow @ Wed Oct 13, 2010 8:32 am wrote: I'd still recommend using a loop, but definitely an array would be a bit more efficient, but the end result is the same.
An array would use 10 times more memory than is needed, and provides nothing in terms of CPU efficiency.
An explicit loop counter isn't needed either. That's what for loops are for.
O_o that's true...why didn't I think of that? |
|
|
|
|
|
Krocker
|
Posted: Tue Dec 07, 2010 5:41 pm Post subject: RE:Turing loop help |
|
|
srry for askin this stupid question, but what is for loop? |
|
|
|
|
|
ProgrammingFun
|
Posted: Tue Dec 07, 2010 7:44 pm Post subject: RE:Turing loop help |
|
|
A for loop is basically a counted loop in which the loop will exit after a certain number of repetitions...this is an efficient way of preventing infinite loops |
|
|
|
|
|
andrew.
|
Posted: Tue Dec 07, 2010 8:21 pm Post subject: RE:Turing loop help |
|
|
In Turing a for loop looks like this:
Turing: | for i : 1..10
put i
end for |
Basically, it's a type of loop that will loop around a certain amount of times. In the above case, I made it loop 10 times by making it count from 1 to 10 using the variable i to store the numbers. |
|
|
|
|
|
|