Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Turing loop help
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
munt4life




PostPosted: 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
Sponsor
sponsor
Sur_real




PostPosted: 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




PostPosted: 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 Razz
copthesaint




PostPosted: 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 Smile
munt4life




PostPosted: 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 Razz
DanShadow




PostPosted: Wed Oct 13, 2010 7:04 am   Post subject: RE:Turing loop help

I never said it was Turing Wink.
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




PostPosted: 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




PostPosted: 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
Sponsor
sponsor
Sur_real




PostPosted: 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 Wink.
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 Razz
chrisbrown




PostPosted: 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




PostPosted: 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? Embarassed
Krocker




PostPosted: 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




PostPosted: 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.




PostPosted: 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.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 14 Posts ]
Jump to:   


Style:  
Search: