notes/education/software development/ECE1400/Flowcharts and Pseudocode.md

323 lines
6.3 KiB
Markdown
Raw Permalink Normal View History

2024-08-31 18:08:50 +00:00
# Banana Cake
## Instructions
2024-08-31 18:03:50 +00:00
2024-08-31 18:08:50 +00:00
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.
2024-08-31 18:03:50 +00:00
2024-08-31 20:09:50 +00:00
## Flowchart
2024-08-31 18:08:50 +00:00
```d2
2024-08-31 18:33:50 +00:00
vars: {
d2-config: {
dark-theme-id: 200
}
}
2024-08-31 19:49:50 +00:00
grid-columns: 3
2024-08-31 18:33:50 +00:00
2024-08-31 18:58:50 +00:00
begin: {
shape: oval
2024-08-31 19:39:50 +00:00
# near: top-left
2024-08-31 18:58:50 +00:00
}
2024-08-31 19:39:50 +00:00
2024-08-31 18:18:50 +00:00
begin -> add 3c flour\
2024-08-31 18:23:50 +00:00
-> add 1 1/2 tsp baking soda\
-> add 1/2 tsp cinnamon\
-> add 1/2 tsp salt\
2024-08-31 19:39:50 +00:00
-> stir\
2024-08-31 18:38:50 +00:00
-> add 3 mashed bananas\
-> add 1 tsp lemon juice\
2024-08-31 19:39:50 +00:00
-> stir again\
2024-08-31 18:38:50 +00:00
-> add 3 eggs\
2024-08-31 19:03:52 +00:00
-> add 2tsp vanilla extract\
2024-08-31 19:49:50 +00:00
-> stir a final time\
-> bake @ 350f for 50 min\
-> let cool for 1 hour\
2024-08-31 19:03:52 +00:00
-> end
2024-08-31 19:49:50 +00:00
end: {
shape: oval
# near: bottom-right
}
```
2024-08-31 19:44:50 +00:00
2024-08-31 19:49:50 +00:00
## Pseudocode
2024-08-31 19:54:50 +00:00
```c
/**************************************
* Function Title: BakeBananaCake
*
* Summary: Bake a banana cake
*
2024-08-31 19:59:50 +00:00
* 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.
2024-08-31 20:14:50 +00:00
%%
2024-08-31 20:19:50 +00:00
Assuming a step distance of 2.5 feet.
Shady path:
2024-09-01 18:42:42 +00:00
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
2024-08-31 20:49:50 +00:00
Sunny path:
2024-09-01 18:42:42 +00:00
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
2024-08-31 20:14:50 +00:00
%%
2024-08-31 20:09:50 +00:00
## Flowchart
```d2
2024-08-31 20:39:50 +00:00
vars: {
d2-config: {
dark-theme-id: 200
}
}
2024-08-31 20:09:50 +00:00
classes: {
2024-09-01 18:17:42 +00:00
turn-0: {
2024-09-01 18:22:42 +00:00
label: turn to 0 degrees
2024-09-01 18:17:42 +00:00
}
2024-09-01 18:22:42 +00:00
turn-90: {
label: turn to 90 degrees
}
turn-180: {
label: turn to 180 degrees
}
turn-270: {
label: turn to 270 degrees
}
}
2024-09-01 18:42:42 +00:00
beginning: {shape: oval}
beginning -> if
2024-09-01 18:22:42 +00:00
if: {
shape: diamond
2024-09-01 18:37:42 +00:00
label: if (shady)\n<then else>\nendif
2024-09-01 18:32:42 +00:00
2024-09-01 18:27:42 +00:00
}
2024-09-01 18:37:42 +00:00
if -> shady: {
direction: up
label: if shady path
}
2024-09-01 18:27:42 +00:00
shady {
2024-09-01 19:07:42 +00:00
grid-columns: 2
2024-09-01 18:27:42 +00:00
1.class: turn-270
1 -> 2
2024-09-01 18:42:42 +00:00
2.label: walk 112 steps
2 -> 3
3.label: turn to 225 degrees
3 -> 4
2024-09-01 19:02:42 +00:00
4.label: walk 124 steps
2024-09-01 18:42:42 +00:00
4 -> 5
5.class: turn-270
5 -> 6
2024-09-01 18:47:42 +00:00
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
2024-09-01 18:27:42 +00:00
}
2024-09-01 18:37:42 +00:00
end: {shape: oval}
if -> end
2024-09-01 19:02:42 +00:00
if -> sunny: if sunny path
2024-09-01 18:27:42 +00:00
sunny {
2024-09-01 19:07:42 +00:00
grid-columns: 2
2024-09-01 18:37:42 +00:00
1.class: turn-270
2024-09-01 18:57:42 +00:00
1 -> 2
2024-09-01 19:02:42 +00:00
2.label: walk 73 steps
2 -> 3
3.class: turn-0
3 -> 4
4.label: walk 94 steps
2024-09-01 19:07:42 +00:00
4 -> 5
2024-09-01 19:02:42 +00:00
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
2024-09-01 19:07:42 +00:00
9 -> 10
10.label: walk 80 steps
2024-08-31 20:09:50 +00:00
}
2024-09-01 19:07:42 +00:00
```
## Pseudocode
```c
/**************************************
* Function Title: WalkToTscFromEngr
*
* Summary: Walk from the west entrance of the engineering building
2024-09-01 19:12:42 +00:00
* to the southeast entrace of the taggart student center, using either
* a shady or sunny path
*
* Inputs: shady (boolean)
2024-09-01 19:07:42 +00:00
* Outputs: none
**************************************
* Pseudocode
*
* Begin
2024-09-01 19:12:42 +00:00
* 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
2024-09-01 19:17:42 +00:00
* 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
2024-09-01 19:07:42 +00:00
* End
**************************************/
2024-09-01 19:22:42 +00:00
```
# 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 |
| -- | -- |
2024-09-01 19:27:42 +00:00
| 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: {
2024-09-01 19:32:42 +00:00
shape: oval
label: beginning
}
beginning -> initialize lights
initialize lights {
2024-09-01 19:42:42 +00:00
grid-columns: 1
2024-09-01 19:32:42 +00:00
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
}
2024-09-01 19:37:42 +00:00
initialize lights -> loop
loop {
2024-09-01 19:47:42 +00:00
near: center-right
2024-09-01 19:32:42 +00:00
label: loop indefinitely
2024-09-01 19:37:42 +00:00
begin-loop: {
shape: step
2024-09-01 19:42:42 +00:00
label: begin iteration
2024-09-01 19:37:42 +00:00
}
begin-loop -> 1
1.label: wait 30 seconds
1 -> 2
2.label: toggle lights
2024-09-01 19:42:42 +00:00
end-loop: {
shape: step
label: end iteration
}
2 -> end-loop
end-loop -> begin-loop
2024-09-01 19:32:42 +00:00
}
2024-09-01 19:42:42 +00:00
loop -> end: the heat death of the universe
2024-09-01 19:32:42 +00:00
end: {
2024-09-01 19:47:42 +00:00
near: bottom-right
2024-09-01 19:32:42 +00:00
shape: oval
label: end
2024-09-01 19:27:42 +00:00
}
2024-09-01 19:52:42 +00:00
```
## Pseudocode
```c
/**************************************
2024-09-01 19:57:42 +00:00
* Function Title: RunStopLights
2024-09-01 19:52:42 +00:00
*
2024-09-01 19:57:42 +00:00
* Summary: Operate stoplights for a 4 way intersection
2024-09-01 19:52:42 +00:00
*
* Inputs: none
* Outputs: none
**************************************
* Pseudocode
*
* Begin
2024-09-01 19:57:42 +00:00
* 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
2024-09-01 19:52:42 +00:00
* End
**************************************/
2024-09-01 19:57:42 +00:00
```