Skip to main content

The Coding Process

Every problem starts with a plan, before a single line of C# is even written. This catches logic errors before syntax errors.

Right now you have 3 strategies available to you:

  • Mind Maps: what goes in, what happens, what comes out
  • Pseudocode: plain language steps
  • Flowcharts: visual decision tree

Pay attention to which one of the strategies makes the most sense for you.

Example: Calculate Pay​

We are working at Twigs and need to calculate our gross pay for the last week. Since this is Quebec, we need to factor in minimum wage is $16.60 per hour

The Mindmap​

Let's detail what inputs are needed, what you are going to do with those inputs, and what outputs are needed by the user.

ipo

This is known as the IPO Model (Input, Process, Output)

The Pseudocode​

Now that we kinda know what we are looking at, we can start giving this concept some structure

1. Define variables: hoursWorked, payRate, grossPay
2. Prompt user: "Enter number of hours worked"
3. Fetch input: hoursWorked
4. Prompt user: "Enter your pay rate"
5. Fetch input: payRate
6. Calculate: grossPay = hoursWorked * payRate
7. Output solution: "Your Gross pay is: " + grossPay

The Flowchart​

flowchart-pay

Keep in mind, while you wont have Flowgorithm available to you on an exam, it's fair game to make your decision tree on paper comme ca:

flow

The C# Program​

Now that we have the logic down, we can start thinking about the types and syntax:

double hoursWorked;
double payRate;
double grossPay;

Console.Write("Enter number of hours worked: ");
hoursWorked = double.Parse(Console.ReadLine());

Console.Write("Enter your pay rate: ");
payRate = double.Parse(Console.ReadLine());

grossPay = hoursWorked * payRate;

Console.WriteLine("Your Gross pay is: " + grossPay);
Enter number of hours worked: 40
Enter your pay rate: 17
Your Gross pay is: 680

Example: Transportation​

To cut down on parking costs, JAC has taken inspiration from your physics and is now launching students as projectiles for transportation. The projectile launcher has a launch angle in degrees and an initial velocity in meters per second (m/s)

Calculate the final velocity along the x and y axes.

The Mind Map​

It's more natural to draw this out:

ipo

Then, think about the IPO:

ipo

The Pseudocode​

Now that we kinda know what we are looking at, we can give this concept structure:

1. Define variables: initialVelocity, initialAngle, angleRadians, gravity, timeInAir, landingVelocityX, landingVelocityY
2. Prompt user: "Enter initial velocity (m/s)"
3. Fetch input: initialVelocity
4. Prompt user: "Enter launch angle (degrees)"
5. Fetch input: initialAngle
6. Assign: gravity = 9.81
7. Calculate: angleRadians = initialAngle * (PI / 180)
8. Calculate: timeInAir = (2 * initialVelocity * Sin(angleRadians)) / gravity
9. Calculate: landingVelocityX = initialVelocity * Cos(angleRadians)
10. Calculate: landingVelocityY = initialVelocity * Sin(angleRadians) - gravity * timeInAir
11. Output: "Landing Velocity X: " + landingVelocityX
12. Output: "Landing Velocity Y: " + landingVelocityY

The Flowchart​

The goal here isn't to have perfect equations necessarily, rather to better see the logic flow:

ipo

The C# Program​

double initialVelocity;
double initialAngle;
double angleRadians;
double gravity = 9.81;
double timeInAir;
double landingVelocityX;
double landingVelocityY;

Console.Write("Enter initial velocity (m/s): ");
initialVelocity = double.Parse(Console.ReadLine());

Console.Write("Enter launch angle (degrees): ");
initialAngle = double.Parse(Console.ReadLine());

angleRadians = initialAngle * (Math.PI / 180);

timeInAir = (2 * initialVelocity * Math.Sin(angleRadians)) / gravity;

landingVelocityX = initialVelocity * Math.Cos(angleRadians);
landingVelocityY = initialVelocity * Math.Sin(angleRadians) - gravity * timeInAir;

Console.WriteLine("Landing Velocity X: " + landingVelocityX);
Console.WriteLine("Landing Velocity Y: " + landingVelocityY);
Enter initial velocity (m/s): 100
Enter launch angle (degrees): 45
Landing Velocity X: 70.71067811865476
Landing Velocity Y: -70.71067811865474

⚠️ Note: No worries about knowing Math.PI, Math.Cos, and Math.Sin atm, focus more on the process here. I give you the tools as necessary.

Summary​

Notice how the program is kinda like a sandwich 🥪:

  • Variable definitions at the top 🍞
  • Operations in the middle 🧀
  • Outputs at the end 🍞