π
Summary
Iterators and Variables
Iterators can be used to repeat activities systematically, ensuring each item (like a card) is visited exactly once. Variables are used to maintain extra information during iteration. Unlike constants, variables can change their values during the iteration.
Generic Flowchart for Iterator with Variables
We modify the iterator flowchart to include variables to store intermediate information. The basic structure remains the same, but we need to initialize the variables before the iteration loop starts and update them using the values from the current item at each step inside the loop.
Counting Example
To count the number of cards in a pile, we use a variable named count
, initialized to 0. As we go through each card, we increment count
by 1. The flowchart shows initialization, iteration, and update steps.
Sum Example
To find the sum of values from a specific field on the cards, we use a variable named sum
, initialized to 0. We add the value of the relevant field from each card to sum
. The flowchart is similar to the counting flowchart but updates sum
with the field value from each card.
Average
To find the average value of a field from all the cards in a dataset, you need to:
- Calculate the total value of the field (sum).
- Count the number of cards (count).
- Divide the total value by the number of cards to get the average.
Initially, this process requires two passes through the cards: one to find the sum and another to find the count. However, you can optimize this by maintaining both the sum and count variables simultaneously during a single pass through the cards.
Generic Flowchart for Average
The computation of the average can be done after iterating through all the cards, rather than during the iteration. This generic flowchart can be adapted for different fields, such as physics marks or letter counts, by replacing the specific field in the flowchart.
Collecting Values in a List
Variables can store different types of data, not just numbers. For example, you can collect field values into a list. A list can contain multiple occurrences of the same element. The flowchart for collecting values into a list initializes an empty list and appends each value to the list during iteration.
Accumulator
The concept of an accumulator involves a variable that accumulates values during iteration. This pattern is seen in various scenarios, such as summing total marks or collecting a list of physics marks.
In both cases, the accumulator variable is initialized to a value representing "empty" (e.g., 0
for sum, []
for a list). The variable used in this pattern is called an accumulator variable.
Examples of accumulation include:
- Addition: Summing values.
- Appending: Collecting items into a list.
- Multiplication: Finding the product of values.
In general, an accumulator pattern can involve processing each element through a map operation followed by an accumulation operation (also known as the reduce operation).
For example, to find the total number of items purchased by all customers in a shopping bill dataset, the map operation would count the number of item rows in each bill, and the reduce operation would sum these counts to get the total number of packed items purchased.
A procedure, called as a separate statement, may not return a value but helps modularize pseudocode by handling repeated computations in different contexts. Parameters set the context, variables store returned values, and improvements benefit all uses automatically.
GA
PA:
Comments
Post a Comment