notes/education/software development/ECE1400/Flowcharts and Pseudocode.md
2024-09-01 13:57:42 -06:00

323 lines
6.3 KiB
Markdown
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Banana Cake
## Instructions
1. Assume an action set of {add \<ingredient>, stir, bake, cool}. Draw a flowchart, using the paradigm discussed in class, to show the process of baking a banana cake. Use Google to find a typical list of ingredients.
2. Represent the flowchart from problem 1 as pseudocode.
## Flowchart
```d2
vars: {
d2-config: {
dark-theme-id: 200
}
}
grid-columns: 3
begin: {
shape: oval
# near: top-left
}
begin -> add 3c flour\
-> add 1 1/2 tsp baking soda\
-> add 1/2 tsp cinnamon\
-> add 1/2 tsp salt\
-> stir\
-> add 3 mashed bananas\
-> add 1 tsp lemon juice\
-> stir again\
-> add 3 eggs\
-> add 2tsp vanilla extract\
-> stir a final time\
-> bake @ 350f for 50 min\
-> let cool for 1 hour\
-> end
end: {
shape: oval
# near: bottom-right
}
```
## Pseudocode
```c
/**************************************
* Function Title: BakeBananaCake
*
* Summary: Bake a banana cake
*
* Inputs: none
* Outputs: none
**************************************
* Pseudocode
*
* Begin
* Add 3c flour
* Add 1 1/2 tps baking soda
* Add 1/2 tsp cinnamon
* Add 1/2 tsp salt
* Stir
* Add 3 mashed bananas
* Add 1 tsp lemon juice
* Stir
* Add 3 eggs
* Add 2tsp vanilla extract
* Stir a final time
* Bake @ 350F for 50 min
* Cool for 1 hour
* End
**************************************/
```
# Walking
## Instructions
3. Assume an action set of {walk \<value> steps, turn to \<value> degrees}. Draw a flowchart showing a shady path and a sunny path to get from the west doors of the Engineering Building to the south-east doors of the TSC. Pick a meaningful “if” condition to select one of the two paths. Use real-world data in your design.
4. Represent the flowchart from problem 3 as pseudocode.
%%
Assuming a step distance of 2.5 feet.
Shady path:
1. Turn to 270 degrees
2. Walk 112 steps
3. Turn to 225 degrees
4. Walk 124 steps
5. Turn to 270 degrees
6. Walk 361 steps
7. Turn to 0 degrees
8. Walk 176 steps
9. Turn to 270 degrees
10. Walk 62 steps
Sunny path:
1. Turn to 270 degrees
2. Walk 73 steps
3. Turn to 0 degrees
4. Walk 94 steps
5. Turn to 275 degrees
6. Walk 467 steps
7. Turn to 180 degrees
8. Walk 86 steps
9. Turn to 270 degrees
10. Walk 80 steps
%%
## Flowchart
```d2
vars: {
d2-config: {
dark-theme-id: 200
}
}
classes: {
turn-0: {
label: turn to 0 degrees
}
turn-90: {
label: turn to 90 degrees
}
turn-180: {
label: turn to 180 degrees
}
turn-270: {
label: turn to 270 degrees
}
}
beginning: {shape: oval}
beginning -> if
if: {
shape: diamond
label: if (shady)\n<then else>\nendif
}
if -> shady: {
direction: up
label: if shady path
}
shady {
grid-columns: 2
1.class: turn-270
1 -> 2
2.label: walk 112 steps
2 -> 3
3.label: turn to 225 degrees
3 -> 4
4.label: walk 124 steps
4 -> 5
5.class: turn-270
5 -> 6
6.label: walk 361 steps
6 -> 7
7.class: turn-0
7 -> 8
8.label: walk 176 steps
8 -> 9
9.class: turn-270
9 -> 10
10.label: walk 62 steps
}
end: {shape: oval}
if -> end
if -> sunny: if sunny path
sunny {
grid-columns: 2
1.class: turn-270
1 -> 2
2.label: walk 73 steps
2 -> 3
3.class: turn-0
3 -> 4
4.label: walk 94 steps
4 -> 5
5.label: turn to 275 degrees
5 -> 6
6.label: walk 361 steps
6 -> 7
7.class: turn-0
7 -> 8
8.label: walk 176 steps
8 -> 9
9.label: turn to 270 degrees
9 -> 10
10.label: walk 80 steps
}
```
## Pseudocode
```c
/**************************************
* Function Title: WalkToTscFromEngr
*
* Summary: Walk from the west entrance of the engineering building
* to the southeast entrace of the taggart student center, using either
* a shady or sunny path
*
* Inputs: shady (boolean)
* Outputs: none
**************************************
* Pseudocode
*
* Begin
* If (shady) then
* Turn to 270 degrees
* Walk 112 steps
* Turn to 225 degrees
* Walk 124 steps
* Turn to 270 degrees
* Walk 361 steps
* Turn to 0 degrees
* Walk 176 steps
* Turn to 270 degrees
* Walk 62 steps
* Else
* Turn to 270 degrees
* Walk 73 steps
* Turn to 0 degrees
* Walk 94 steps
* Turn to 275 degrees
* Walk 467 steps
* Turn to 180 degrees
* Walk 86 steps
* Turn to 270 degrees
* Walk 80 steps
* EndIf
* End
**************************************/
```
# 4 Way Intersection
## Instructions
5. Develop a flowchart that describes the behavior of a set of traffic lights that control a 4-way intersection. Assume the light can either be red or green.  Define an appropriate action set that accounts for the time the light has been in the current state.
6. Represent the flowchart from problem 5 as pseudocode.
## Action Set
| Action name | Description |
| -- | -- |
| Set \[north, east, south, west] light to \[red, green] | Set the specified light to either red or green |
| Toggle lights | Change the color of all 4 lights to the color they were not |
| Wait \[number of seconds] seconds | Pause for \[number of seconds] seconds before continuing to the next instruction |
## Flowchart
```d2
vars: {
d2-config: {
dark-theme-id: 200
}
}
classes: {
toggle-lights: {
label: Toggle lights
}
}
beginning: {
shape: oval
label: beginning
}
beginning -> initialize lights
initialize lights {
grid-columns: 1
1.label: set the north light to red
1 -> 2
2.label: set the south light to red
2 -> 3
3.label: set the east light to green
3 -> 4
4.label: set the west light to green
}
initialize lights -> loop
loop {
near: center-right
label: loop indefinitely
begin-loop: {
shape: step
label: begin iteration
}
begin-loop -> 1
1.label: wait 30 seconds
1 -> 2
2.label: toggle lights
end-loop: {
shape: step
label: end iteration
}
2 -> end-loop
end-loop -> begin-loop
}
loop -> end: the heat death of the universe
end: {
near: bottom-right
shape: oval
label: end
}
```
## Pseudocode
```c
/**************************************
* Function Title: RunStopLights
*
* Summary: Operate stoplights for a 4 way intersection
*
* Inputs: none
* Outputs: none
**************************************
* Pseudocode
*
* Begin
* Set the north light to red
* Set the south light to red
* Set the east light to green
* Set the west light to green
* Loop indefinitely
* Wait 30 seconds
* Toggle lights
* EndLoop
* End
**************************************/
```