
-----------------------------------
gayden
Fri Feb 20, 2015 10:12 am

Counting the number of false Boolean expressions in a loop?
-----------------------------------
Making a program where Turing generates a Rand.Int and user has to guess the number, if they get it right I want the program to tell them how many tries it took them to get the correct answer.
Here's what I have so far:

[code]var ans, times : int
const num := Rand.Int (0, 100)
times := 
put "Please guess a number between 0 and 100."
loop
    get ans
    if (ans = num) then
        put "Congratulations! It took you ", times, " attempts to guess the number!"
    elsif (ans < num) then
        put "Guess higher."
    else
        put "Guess lower."
    end if
    exit when (num = ans)
end loop
[/code]
Currently the variable 'times' has no value, so if you wish to run this you'll have to remove the declaration of times with nothing after it and the use of it in line 8.

Is there a function in turing I can use to do this?

Addittionally, I am making this one:
[code]%Program will ask user how many integers they want to be added, ask them to input the numbers, then print the sum
var nums, innum, sum : int
put "Please enter the number of integers that will be added: " ..
get nums
for count : 1 .. nums
    put "Please enter the integer #", count, ": " ..
    get innum
end for
sum := innum + 
put "The sum of the numbers is ", sum
[/code]
I think my error is similar to the first program, but I am not sure.

Any help is appreciated.

-----------------------------------
Tony
Fri Feb 20, 2015 1:39 pm

RE:Counting the number of false Boolean expressions in a loop?
-----------------------------------
A more obvious problem is that you have incomplete lines of code
[code]
times := 
[/code]
[code]
sum := innum + 
[/code].

Otherwise yes, you need to initialize variable values. Otherwise in code such as
[code]
var sum : int
sum := sum + 2
put sum
[/code]
What should it print? It can't be known. Assigning a value first:
[code]
sum := 0
[/code]
will resolve that ambiguity.

-----------------------------------
gayden
Fri Feb 20, 2015 4:34 pm

Re: RE:Counting the number of false Boolean expressions in a loop?
-----------------------------------
A more obvious problem is that you have incomplete lines of code



Currently the variable 'times' has no value, so if you wish to run this you'll have to remove the declaration of times with nothing after it and the use of it in line 8.

I know I have to give the variables values, but how do I get them to count the number of times 'sum' is written?

-----------------------------------
Tony
Fri Feb 20, 2015 5:18 pm

Re: RE:Counting the number of false Boolean expressions in a loop?
-----------------------------------
how do I get them to count the number of times 'sum' is written?
Have another counter variable.
[code]
var sum_counter : int := 0
...
put "Sum is ", sum
sum_counter += 1 % increment by one
[/code]

-----------------------------------
gayden
Tue Feb 24, 2015 9:19 am

Re: RE:Counting the number of false Boolean expressions in a loop?
-----------------------------------
how do I get them to count the number of times 'sum' is written?
Have another counter variable.

I believe I solved it.

[code]var num, sum, x : int
put "Please enter the number of integers that will be added: " ..
get num
sum := 0
for count : 1 .. num
    put "Please enter the integer #", count, ": " ..
    get x
    sum := sum + x
end for
put "The sum of your integers is", sum
[/code]
Any problems with this one?

-----------------------------------
Tony
Tue Feb 24, 2015 1:31 pm

RE:Counting the number of false Boolean expressions in a loop?
-----------------------------------
Looks good to me.
