how to map one value to another in C

Mapping one value to another in C

In C, you can map one value to another using various techniques, such as using conditional statements or lookup tables. Here are a few examples:

  1. Using conditional statements:
int mapValue(int input) {
    int output;

    if (input == 1) {
        output = 10;
    } else if (input == 2) {
        output = 20;
    } else if (input == 3) {
        output = 30;
    } else {
        output = 0; // default value if input doesn't match any condition
    }

    return output;
}
  1. Using a switch statement:
int mapValue(int input) {
    int output;

    switch (input) {
        case 1:
            output = 10;
            break;
        case 2:
            output = 20;
            break;
        case 3:
            output = 30;
            break;
        default:
            output = 0; // default value if input doesn't match any case
            break;
    }

    return output;
}
  1. Using a lookup table:
int mapValue(int input) {
    int output;

    int lookupTable[] = {0, 10, 20, 30}; // map input values to output values

    if (input >= 0 && input <= 3) {
        output = lookupTable[input];
    } else {
        output = 0; // default value if input is out of range
    }

    return output;
}

These are just a few examples of how you can map one value to another in C. The specific approach you choose will depend on your requirements and the complexity of the mapping you need to implement.