easy
0 views

Function printColor – CTS Pattern

Fix the logical errors in a switch-case function to correctly map numbers to color names and handle invalid inputs.

Understand the Problem

Problem Statement

You are required to fix all the logical errors in the given code. The function printColor should print a string representing the name of a color based on the input number.

For example, if the input number is:

  • 1, it should print "Red"
  • 2, it should print "Yellow"
  • 3, it should print "Blue"
  • 4, it should print "Green"

For any other input value, the function should print "No Color".

The function accepts one argument num (integer) representing the input number.

The current implementation compiles successfully but fails to return the desired result for some test cases due to incorrect implementation.

Your task is to fix the code so that it passes all test cases.

Constraints

  • The input number num can be any integer value
  • The function should handle negative numbers, zero, and positive numbers
  • For values other than 1, 2, 3, 4, the function must print "No Color"
  • The function should not print any additional text or formatting

Examples

Example 1
Input
1
Output
Red
Explanation

When the input number is 1, the function should print "Red" as specified in the case 1 block.

Example 2
Input
3
Output
Blue
Explanation

When the input number is 3, the function should print "Blue" as specified in the case 3 block.

Example 3
Input
5
Output
No Color
Explanation

When the input number is 5 (or any value other than 1, 2, 3, 4), the function should print "No Color" as specified in the default case.

Example 4
Input
0
Output
No Color
Explanation

When the input number is 0, the function should print "No Color" as 0 is not one of the specified cases (1, 2, 3, 4).

Solution

#include <stdio.h>

void printColor(int num)
{
    switch(num)
    {
    case 1:
        printf("Red\n");
        break;
    case 2:
        printf("Yellow\n");
        break;
    case 3:
        printf("Blue\n");
        break;
    case 4:
        printf("Green\n");
        break;
    default:
        printf("No Color\n");
        break;
    }
}

// Example usage and testing
int main() {
    // Test cases
    printColor(1);  // Should print: Red
    printColor(2);  // Should print: Yellow
    printColor(3);  // Should print: Blue
    printColor(4);  // Should print: Green
    printColor(5);  // Should print: No Color
    printColor(0);  // Should print: No Color
    printColor(-1); // Should print: No Color
    
    return 0;
}
Time:O(1) - The switch-case operation executes in constant time regardless of input size
Space:O(1) - The function uses only a constant amount of memory for the input parameter and no additional data structures
Approach:

The C solution uses a standard switch-case structure to map integer values to color names. The key fix was adding \n escape sequences to each printf statement to ensure proper line breaks in the output.

The switch statement evaluates the input num and executes the corresponding case block. Each case prints the appropriate color name followed by a newline character.

The default case handles all values that don't match cases 1, 2, 3, or 4, printing "No Color" with a newline.

The main function demonstrates the solution with various test cases to verify correct behavior.

Visual Explanation

Loading diagram...