Find Number of Equal Consecutive Numbers
Author |
Message |
CtrlAltDelicious
|
Posted: Tue Jun 06, 2017 8:29 am Post subject: Find Number of Equal Consecutive Numbers |
|
|
What is it you are trying to achieve?
Write a program that prompts the user for a sequence of integers, using zero as the end of input. The program should count the number of times that two consecutive values are equal. For example, if the inputs is 3 6 7 7 4 4 4 6 0 then the program should determine that there are three cases in which consecutive values are equal. (use “if” statement inside “while” loop)
Describe what you have tried to solve this problem
Have a previous number integer that will later be used in an "if" statement, to determine if the current & last number entered by the user are equal.
Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
var integer1 : int
var integer2 : int
var counter : int
counter := 0
var previous : int
get integer1
loop
get integer2
exit when integer2 = 0
if integer1 = integer2 then
counter := counter + 1
end if
previous := integer2
if previous = integer2 then
counter := counter + 1
end if
previous := integer2
end loop
% Display Final result
put ""
put "There is/are ", counter, " case(s) in which the numbers you've " ..
put "entered are equal."
Please specify what version of Turing you are using
Turing 4.1.1 |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Insectoid
|
Posted: Tue Jun 06, 2017 9:42 am Post subject: RE:Find Number of Equal Consecutive Numbers |
|
|
What part are you having trouble with? |
|
|
|
|
|
CtrlAltDelicious
|
Posted: Tue Jun 06, 2017 7:41 pm Post subject: Re: Find Number of Equal Consecutive Numbers |
|
|
So far, I've developed a rough idea on how I should use the variables (previous integer & current integer), but don't actually know how to use them properly in a loop, if that makes sense. |
|
|
|
|
|
Insectoid
|
Posted: Wed Jun 07, 2017 4:14 pm Post subject: RE:Find Number of Equal Consecutive Numbers |
|
|
You're on the right track. Here's a quick hint: you can do away with your previous variable and use your integer1 variable for both jobs. |
|
|
|
|
|
CtrlAltDelicious
|
Posted: Fri Jun 16, 2017 8:49 am Post subject: Re: Find Number of Equal Consecutive Numbers |
|
|
Sorry, could you clarify on what you meant by both integers being able to do? |
|
|
|
|
|
CtrlAltDelicious
|
Posted: Tue Jun 20, 2017 7:42 am Post subject: Re: Find Number of Equal Consecutive Numbers |
|
|
I'm stuck on how I should use the previous integer variable.. |
|
|
|
|
|
Insectoid
|
Posted: Tue Jun 20, 2017 1:14 pm Post subject: RE:Find Number of Equal Consecutive Numbers |
|
|
You're only ever checking two numbers against each other, so you only need two variables, right? One for the first number and one for the second. When another number is entered, the first can be discarded, the second becomes the first, and the new number is now the second. Now you can compare them exactly the same way you did the first two numbers! Repeat until there are no more numbers. |
|
|
|
|
|
|
|