Selection and Iteration exercise

Task: Create a solution which accepts 10 temperatures in Celsius and outputs “it is hot” if the temperature is greater than 30 degrees, otherwise output “it is cold”

Solution

See flowgorithm files here:

https://drive.google.com/drive/folders/1lvuR4sNPoV8ZfWN_GPL1DnZewjNm3bES?usp=sharing

Flowchart

Source: https://upload.wikimedia.org/wikipedia/commons/3/3a/Looping_and_Selection_Exercise.png

Pseudocode

Start
    // Task: Create a solution which accepts 10 temperatures in Celsius and outputs "it is hot" if the temperature is greater than 30 degrees, otherwise output "it is cold"
    // 
    // Start Declaring Variables
    // End Declaring Variables
    // Start Variable Initialization
    // 
    counter = 1
    maximum = 10
    currentTemperature = -999.99
    
    // End Variable Initialization
    // 
    loop while counter <= maximum
        output "Please enter temperature value #" + counter
        input currentTemperature
        if currentTemperature > 30 then
            output "it is hot"
        else
            output "it is cold"
        end If
        counter = counter + 1
    end while
    output "Ending program"
end 

© 2022  Vedesh Kungebeharry. All rights reserved. 

2022 Jan P02 IT CSEC Q3a

Start
    // Initialization of variables....
    price = -999.99
    totalPrice = -999.99
    discountAmount = -999.99
    discountRate = 0.25
    quantity = -999
    
    // Prompt the user for input....
    output "Input Price"
    input price
    output "Input quantity"
    input quantity
    totalPrice = price * quantity
    
    // Determine the discount....
    if totalPrice > 1800 then
        discountAmount = totalPrice * discountRate
    else
        discountAmount = 0.00
    end If
    
    // output results....
    output "the Discount is " + discountAmount
Stop

Alternate solution

Note: In this solution , our selection statement does not contain an else branch, since the discountAmount is initialized to 0.

Start
    // Initialization of variables....
    price = -999.99
    totalPrice = -999.99
    
    // here, discount is set to 0.
    discountAmount = 0
    discountRate = 0.25
    quantity = -999
    
    // Prompt the user for input....
    output "Input Price"
    input price
    output "Input quantity"
    input quantity
    totalPrice = price * quantity
    
    // Determine the discount....
    if totalPrice > 1800 then
        discountAmount = totalPrice * discountRate
    else
        
        // because the discount was initialized to 0, we don't need an else section for our selection construct.
    end If
    
    // output results....
    output "the Discount is " + discountAmount
Stop

Additional resources

See flowcharts and solution in flowgorithm here:

https://drive.google.com/drive/folders/1pLQq5N6fqzs7wlFdUaehraZxkhGv2oke?usp=sharing

© 2022  Vedesh Kungebeharry. All rights reserved. 

The purpose of an algorithm

Algorithms are really intended as a blueprint for communication to programmers.

Example: Finding the area of a circle

Problem Definition: A solution is needed to find the area of a circle given a radius.

Solution as a flowchart

Solution in pseudocode:

START 
//variable declaration
float radius
float area
float pi  = 3.14

//input
print "Please enter a radius"
input radius

//processing
area = pi * radius * radius


//output
print "The area is : ", area

END

Actual program code in C:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    float radius, area;
    const float pi = 3.14;
    //input
    printf ("Please enter a radius\n");
    scanf("%f", &radius);

    //processing
    area = pi *radius *radius;
    printf ("\nThe area is : %f", area);



    return 0;
}

Notice how similar the pseudocode is to the actual coded solution.

© 2022  Vedesh Kungebeharry. All rights reserved. 

SBA Marking – Spreadsheets

Marks were assigned on the 9th March 2022.

Videos below.

Group 1

https://youtu.be/QBVQRvTstO0

Group 2

https://youtu.be/u-CoLNecmg4

Group 3

https://youtu.be/TCAk5DQGSgk

Group 4

https://youtu.be/27PNckjqvOY

Group 5

https://youtu.be/Yt_kWC6fLgY

Note

Video feedback for group 6 and 7 was not posted due to an ongoing investigation into the authenticity of the submissions.

© 2022  Vedesh Kungebeharry. All rights reserved.