Robot Simulation

This repository contains a simple C program that demonstrates the use of structures to represent a robot with a name, speed, and position, and update the position with respect to time.

Source File:

 1#include <stdio.h>
 2
 3// Define a struct to represent the Robot
 4typedef struct {
 5    char name[20];
 6    int position;
 7    int speed;
 8} Robot;
 9
10// Function to update the state of the robot
11Robot update_robot_state(Robot robot, int time) {
12    // Update the robot's position based on its speed and time
13    robot.position += robot.speed * time;
14    return robot;
15}
16
17// Function to print the current state of the robot
18void print_robot_state(Robot robot) {
19    printf("Robot: %s\n", robot.name);
20    printf("Position: %d\n", robot.position);
21    printf("Speed: %d\n", robot.speed);
22    printf("-------------------------\n");
23}
24
25int main() {
26    // Create a robot instance
27    Robot myRobot = {"Robo1", 0, 5};
28
29    // Array representing time intervals
30    int time_intervals[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
31
32    // Loop through each time interval and update the robot's state
33    for(int i = 0; i < 10; i++) {
34        printf("Time Interval: %d\n", time_intervals[i]);
35        myRobot = update_robot_state(myRobot, time_intervals[i]);
36        print_robot_state(myRobot);
37    }
38
39    return 0;
40}

Explanation

This program demonstrates how to use structures and functions in C to simulate a robot moving over time. Let’s break down each component:

  1. Struct Definition:

typedef struct {
    char name[20];
    int position;
    int speed;
} Robot;
  • This struct defines the properties of a robot:
    • name: A string to store the robot’s name.

    • position: An integer representing the current position of the robot on a 1D line.

    • speed: An integer representing how many units the robot moves per unit time.

  1. Update Function

// Function to update the state of the robot
Robot update_robot_state(Robot robot, int time) {
    // Update the robot's position based on its speed and time
    robot.position += robot.speed * time;
    return robot;
}
  • This function takes a Robot structure and a time interval.

  • It updates the robot’s position by multiplying its speed with the time interval.

  • The function returns the updated Robot.

  • Note: The robot is passed by value, and the updated structure is reassigned in the main function.

  1. Print Function

// Function to print the current state of the robot
void print_robot_state(Robot robot) {
    printf("Robot: %s\n", robot.name);
    printf("Position: %d\n", robot.position);
    printf("Speed: %d\n", robot.speed);
    printf("-------------------------\n");
}
  • Prints the robot’s current name, position, and speed in a readable format.

  1. Main Function

int main() {
    // Create a robot instance
    Robot myRobot = {"Robo1", 0, 5};

    // Array representing time intervals
    int time_intervals[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

    // Loop through each time interval and update the robot's state
    for(int i = 0; i < 10; i++) {
        printf("Time Interval: %d\n", time_intervals[i]);
        myRobot = update_robot_state(myRobot, time_intervals[i]);
        print_robot_state(myRobot);
    }

    return 0;
}
  • A robot named “Robo1” is created with a starting position of 0 and speed of 5.

  • The time_intervals array represents how long the robot moves during each iteration.

  • A loop iterates through the time intervals:
    • It prints the current time step.

    • It updates the robot’s position using the update_robot_state function.

    • It then prints the robot’s updated state using print_robot_state.