Week 2: More Control Flow and Functions

Introduction to loops (while, for)

While Loop:

  • The while loop in programming is used to repeatedly execute a block of code as long as a specified condition is true.

  • Syntax: while (condition) { code to be executed }

  • When to use: Use a while loop when you don't know in advance how many times the loop needs to run, and the loop's execution depends on a certain condition. For example, when simulating a physical system until it reaches a specific state or when reading data until the end of a file is reached.

Example:

Free fall of object from 200m above the ground:

#include <stdio.h>
#include <math.h>

int main() {
    double g = 9.8; // Acceleration due to gravity in m/s^2
    double t = 0;   // Time in seconds
    double d;       // Distance in meters
    double initialHeight = 200;
    double currentHeight;

    while(t <= 10) { // Calculate for the first 10 seconds
        d = 0.5 * g * pow(t,2); // Calculate the distance fallen
        currentHeight = initialHeight - d;
        printf("Time: %.2f s, Current height: %.2f m\n", t, currentHeight);
        t += 1; // Increment time by 1 second
    }

    return 0;
}

You will see from the above example the the current height will be negative. You should improve it! How?

  • Break when the current height is negative. Where should you put the break?

  • If the initial height is increasing, i.e. to 2000 m, how will you modify the code?

For Loop:

  • The for loop is used for iterating over a sequence of values, such as numbers, and executing a block of code for each value.

  • Syntax: for (initialization; condition; update) { code to be executed }

  • When to use: Use a for loop when you know the exact number of iterations required or when you need to iterate through a range of values. This is commonly used in physics for numerical simulations, solving differential equations, or iterating through arrays.

Example:

We will calculate the potential energy of an object mass 5 kg at different heights.

#include <stdio.h>

int main() {
    double m = 5.0; // Mass of the object in kilograms
    double g = 9.8; // Acceleration due to gravity in m/s^2
    double h;       // Height in meters
    double PE;      // Potential Energy in Joules

    printf("Potential energy for different heights of a 5 kg object:\n");
    for(h = 0; h <= 100; h += 10) { // Increase height from 0 to 100 meters in steps of 10
        PE = m * g * h; // Calculate potential energy
        printf("Height: %.1f m, Potential Energy: %.2f J.\n", h, PE);
    }

    return 0;
}

Switch statement

The switch statement is a control structure in programming used for making decisions based on the value of an expression. It provides an efficient way to choose among a large number of discrete values for the expression. The switch statement can be a powerful alternative to long chains of if-else if-else statements when dealing with multiple conditions.

Advantages of the Switch Statement:

  1. Readability: Switch statements are often more readable and concise than long if-else chains, especially when dealing with a fixed set of discrete values.

  2. Efficiency: Switch statements can be more efficient than if-else chains because the compiler can optimize them into a jump table or lookup table, resulting in faster execution.

Example:

#include <stdio.h>

int main() {
    int choice;
    double num1, num2, result;

    printf("Enter two numbers: ");
    scanf("%lf %lf", &num1, &num2);

    printf("Choose an operation:\n");
    printf("1: Addition\n2: Subtraction\n3: Multiplication\n4: Division\n");
    scanf("%d", &choice);

    switch(choice) {
        case 1:
            result = num1 + num2;
            printf("Result: %.2lf\n", result);
            break;
        case 2:
            result = num1 - num2;
            printf("Result: %.2lf\n", result);
            break;
        case 3:
            result = num1 * num2;
            printf("Result: %.2lf\n", result);
            break;
        case 4:
            if(num2 != 0) {
                result = num1 / num2;
                printf("Result: %.2lf\n", result);
            } else {
                printf("Error: Division by zero!\n");
            }
            break;
        default:
            printf("Invalid choice!\n");
    }

    return 0;
}

Loops continued (do-while, nested loops)

Do-While Loop:

The do-while loop is another type of loop in programming, similar to the while loop. It is used for repetitive execution of a block of code as long as a specified condition remains true. However, unlike the while loop, the do-while loop guarantees that the block of code is executed at least once, even if the condition is false initially. The condition is checked after the code block execution.

Syntax of the do-while loop:

do {
    // Code to be executed
} while (condition);
  • The code block is executed at least once before checking the condition.

  • If the condition is true, the loop continues; otherwise, it terminates.

Nested Loops:

Nested loops are loops placed inside other loops. They allow for more complex iterations and are particularly useful when dealing with multidimensional data or when simulating systems with multiple components or dimensions.

Example:

To calculate and print a table of projectile distances for various angles and initial velocities.

#include <stdio.h>
#include <math.h>

#define PI 3.14159265358979323846
#define g 9.8 // Acceleration due to gravity in m/s^2

int main() {
    int velocity, angle;
    double distance, angleRad;

    printf("Projectile Distance Table:\n");
    for(velocity = 10; velocity <= 50; velocity += 10) { // Outer loop for velocity
        printf("\nVelocity = %d m/s:\n", velocity);
        for(angle = 10; angle <= 80; angle += 10) { // Inner loop for angle
            angleRad = angle * PI / 180; // Convert angle to radians
            distance = (velocity * velocity * sin(2 * angleRad)) / g; // Calculate distance
            printf("Angle: %d degrees, Distance: %.2f meters\n", angle, distance);
        }
    }

    return 0;
}

Functions and function prototypes

Functions as Reusable Blocks of Code:

In programming, a function is a self-contained, reusable block of code that performs a specific task or set of tasks. Functions are a fundamental concept in computer programming because they help make code modular, organized, and easier to understand and maintain. By breaking down a program into smaller functions, each responsible for a particular task, you can write code that is more readable, testable, and maintainable.

Functions have several advantages:

  1. Modularity: Functions allow you to break complex problems into smaller, more manageable parts. Each function focuses on a specific task, making the code easier to design, implement, and maintain.

  2. Reusability: Once a function is defined, you can reuse it in different parts of your program or even in other programs. This promotes code reuse and reduces duplication.

  3. Abstraction: Functions abstract away the implementation details of a task. When you call a function, you don't need to know how it works internally; you only need to understand its interface (the inputs and outputs).

Function Prototypes:

In C programming, a function prototype is a declaration of a function that provides the compiler with essential information about the function's name, return type, and parameters before the function's actual definition. Function prototypes serve several important purposes:

  1. Compiler Checking: Function prototypes inform the compiler about the existence and signature (return type and parameter types) of functions used in the program. This enables the compiler to check that function calls match the declared signatures.

  2. Order Independence: Function prototypes allow you to call functions in your code before defining them. This is useful when functions call each other, as long as the prototypes are declared before the calls.

  3. Documentation: Prototypes also serve as a form of documentation, providing other developers (and your future self) with information about the functions used in the code.

Declaring and Defining Functions:

Here's how you declare and define functions in C:

Declaration (Function Prototype):

return_type function_name(parameter_type1, parameter_type2, ...);
  • return_type is the type of data the function will return (e.g., int, double, void).

  • function_name is the name of the function.

  • parameter_type1, parameter_type2, etc., are the types of parameters the function accepts.

Definition:

return_type function_name(parameter_type1 parameter_name1, parameter_type2 parameter_name2, ...) {
    // Function code
    // ...
    return result; // Return statement (if applicable)
}
  • return_type must match the type of value the function returns.

  • parameter_name1, parameter_name2, etc., are the names of the function's parameters.

Here's an example of a simple C function that calculates the sum of two integers:

#include <stdio.h>

// Function prototype (declaration)
int add(int a, int b);

int main() {
    int num1 = 5;
    int num2 = 7;
    int sum = add(num1, num2); // Function call

    printf("Sum: %d\n", sum);
    return 0;
}

// Function definition
int add(int a, int b) {
    return a + b;
}

Function parameters and return values

Functions in programming can take parameters (also known as arguments) as inputs and return values as outputs. Parameters allow you to pass data to a function so that it can perform operations on that data, and return values allow functions to communicate their results back to the caller. The proper use of data types and their consistency is crucial for ensuring that functions work correctly and produce meaningful results.

Parameters:

  • Parameters are variables or values that you provide to a function when calling it. They allow functions to receive data that they can work with.

  • Parameters are defined within the parentheses of a function's prototype and definition.

  • Data types of parameters should match the expected data types in the function.

Return Values:

  • Return values are values that a function sends back to the caller.

  • Functions can return a single value of a specific data type.

  • The return value can be assigned to a variable or used directly in expressions.

Example: Calculate Kinetic Energy

#include <stdio.h>

// Function prototype
double calculateKineticEnergy(double mass, double velocity);

int main() {
    double mass = 5.0; // kilograms
    double velocity = 10.0; // meters per second

    // Call the function and store the result in energy
    double energy = calculateKineticEnergy(mass, velocity);

    printf("Kinetic Energy: %.2lf joules\n", energy);
    return 0;
}

// Function definition
double calculateKineticEnergy(double mass, double velocity) {
    // Calculate kinetic energy: KE = 0.5 * mass * velocity^2
    double kineticEnergy = 0.5 * mass * velocity * velocity;
    return kineticEnergy;
}

Example: Solve Quadratic Equation

#include <stdio.h>
#include <math.h>

// Function prototype
int solveQuadraticEquation(double a, double b, double c, double *root1, double *root2);

int main() {
    double a = 1.0;
    double b = -3.0;
    double c = 2.0;
    double root1, root2;

    int numRoots = solveQuadraticEquation(a, b, c, &root1, &root2);

    if (numRoots == 2) {
        printf("Root 1: %.2lf\n", root1);
        printf("Root 2: %.2lf\n", root2);
    } else if (numRoots == 1) {
        printf("Root: %.2lf\n", root1);
    } else {
        printf("No real roots\n");
    }

    return 0;
}

// Function definition
int solveQuadraticEquation(double a, double b, double c, double *root1, double *root2) {
    double discriminant = b * b - 4 * a * c;

    if (discriminant > 0) {
        *root1 = (-b + sqrt(discriminant)) / (2 * a);
        *root2 = (-b - sqrt(discriminant)) / (2 * a);
        return 2; // Two real roots
    } else if (discriminant == 0) {
        *root1 = -b / (2 * a);
        return 1; // One real root
    } else {
        return 0; // No real roots
    }
}

Void Functions (Functions with No Return Value):

A void function is a function that does not return any value. It is typically used for functions that perform a task or operation but do not need to return a result.

#include <stdio.h>

void printMessage() {
    printf("Hello, world!\n");
}

void swap(int *a, int *b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}

int main() {
    int x = 5;
    int y = 10;
    
    printMessage();

    printf("Before swap: x = %d, y = %d\n", x, y);

    swap(&x, &y); // Call the swap function to swap x and y

    printf("After swap: x = %d, y = %d\n", x, y);

    return 0;
}

Classwork - Week 2

Classwork-1: Separating digits in an integer

Write a program that inputs one five-digit number, separates the number into its individual digits and displays the digits separated from one another by three spaces each. For example, if the user types in 10300, the program should display

1   0   3   0   0

Homework - Week 2

Homework-1: C progrma for projectile motion calculation

Write a C program to simulate the motion of a projectile under gravity. Your program should calculate the time of flight, maximum height, and range of the projectile for various initial velocities and launch angles. Use if-else statements, a prototype function, and loops in your code. Your program should ask for user inputs including initial velocity (in m/s) and launch angle (in degrees).

The problem is not difficult. Think carefully and write a good program!

Homework-2: Improve your calculator

Improve your calculator program by

  1. Instead of performing 4 basic calculations, your program also ask for operator to calculate.

  2. As physics student, your program should display an answer with proper significant figures.

Last updated