Solution of exercise: Analysis of requirements specifications to identify goals
- Specs: Write a function which, given a slice of integers as parameter, finds the ones that are greater than 7 and multiple of 4 and returns their product.
- The main goal is computing a total.
- The corresponing plan requires a variable, e.g.,
product
(with the gatherer role) that must be initialized with value 1, that accumulate the product of all numbers satisfying the given condition.
- Specs: Write a program that reads 10 integers from standard input and for each one prints if it is even or odd.
- The main goal her is repeating an action
- To implement the corresponding plan, a stepper variable is needed; such variable represents how many numbers were read, must be initialized with value 0 before the loop and incremented by one for each new read number.
- Specs: Write a program that reads a sequence of words from standard input and prints the first word which begins with the character ‘a’, or “no word with ‘a’ found”
- The main goal is finding the first occurrence
- To implement the corresponding plan, a variable is needed to store the first word that begins with a; such variable must be initialized as the empty string
- Specs: Write a program that reads a sequence of integers from standard input and, for each one after the first one, prints if it is the successor of the number entered before it.
- The main goal is processing two adjacent values
- Two variables are needed, e.g.,
curr
and prev
(with the role of follower), that keep track of the number that is read at the current iteration and the number that is read at the previous iteration. Variable prev
must be initialized as the first read number and variable curr
as the second read number; the loop must iterate from the second number on.
- Specs: Write a function that receives a word as parameter and returns
true
if the word contains (at least) a double letter, false
otherwise.
- The main goal is processing two adjacent values
- No variable are needed; the word being stored in a string, it is enough to compare the
i
-th element with the i+1
-th element of the string.
- Specs: Write a program that reads a sequence of words from standard input and prints the length of the shortest word.
- The main goal is the linear search
- A variable is need, e.g.,
minLen
(with the role of most-wanted holder ) that must be initialized as the length of the first string, and that keeps storing the length of the shortest word found so far.
- Specs: Write a program that reads an integer number from the command line. The number represents the balance of a bank account. The program then reads a sequence of expenses to be charged to the bank account and prints the final balance.
- The main goal is computing a total
- A variable, e.g.,
total
, is needed with the role of a gatherer; it must be initialized as the number read from the command line and must be decreased by each of the expenses read from standard input.
- Specs: Write a program that reads a sequence of integers from standard input and prints the smallest odd number among them. The input sequence may contain both positive and negative numbers.
- The main goal is the search of an extreme value
- A variable, e.g.,
smallestOdd
is needed with the role of most-wanted holder, that keeps track of the lowest odd number found so far. To identify the case where no odd numbers are inserted, the variable can be initialized as a non-odd number, for instance 0.
- Specs: Write a program that reads a sequence of positive integers ended by -1 from standard input and prints the first number greater than 100, if there is one; otherwise it prints “no numbers greater than 100”.
- The main goal is finding the first occurrence
- Avariable, e.g.,
firstAbove100
is needed to store the first number greater that 100; such variable must be initialized as -1.
- Specs: Write a program that reads a sequence of positive integers ended by 0 from standard input, prints ’+’ every time the new value is greater than or equal to the preceding one, ‘-’ otherwise.
- The main goal is processing two adjacent values
- Two variable are needed, e.g.,
curr
and prev
(with the role of follower) that keep track of the number that is read in the current iteration and the number that was read in the precious iteration, respectively. Variable prev
must be initialized as the first read number, variable curr
as the second one.