Computer Science Canada

Counting the number of false Boolean expressions in a loop?

Author:  gayden [ Fri Feb 20, 2015 10:12 am ]
Post subject:  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

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

I think my error is similar to the first program, but I am not sure.

Any help is appreciated.

Author:  Tony [ Fri Feb 20, 2015 1:39 pm ]
Post subject:  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:

sum := innum +
.

Otherwise yes, you need to initialize variable values. Otherwise in code such as
code:

var sum : int
sum := sum + 2
put sum

What should it print? It can't be known. Assigning a value first:
code:

sum := 0

will resolve that ambiguity.

Author:  gayden [ Fri Feb 20, 2015 4:34 pm ]
Post subject:  Re: RE:Counting the number of false Boolean expressions in a loop?

Tony @ Fri Feb 20, 2015 1:39 pm wrote:
A more obvious problem is that you have incomplete lines of code
code:

times :=

code:

sum := innum +
.

Otherwise yes, you need to initialize variable values. Otherwise in code such as
code:

var sum : int
sum := sum + 2
put sum

What should it print? It can't be known. Assigning a value first:
code:

sum := 0

will resolve that ambiguity.


gayden @ Fri Feb 20, 2015 10:12 am wrote:

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?

Author:  Tony [ Fri Feb 20, 2015 5:18 pm ]
Post subject:  Re: RE:Counting the number of false Boolean expressions in a loop?

gayden @ Fri Feb 20, 2015 4:34 pm wrote:
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

Author:  gayden [ Tue Feb 24, 2015 9:19 am ]
Post subject:  Re: RE:Counting the number of false Boolean expressions in a loop?

Tony @ Fri Feb 20, 2015 5:18 pm wrote:
gayden @ Fri Feb 20, 2015 4:34 pm wrote:
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

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

Any problems with this one?

Author:  Tony [ Tue Feb 24, 2015 1:31 pm ]
Post subject:  RE:Counting the number of false Boolean expressions in a loop?

Looks good to me.


: