Introduction to Problem Solving

introduction to Problem Solving

Problem solving in C programming refers to identifying a task or requirement and writing a logical, structured program to achieve a correct and efficient solution. It involves understanding the problem, planning a solution, coding it in C, testing, and improving it.


🔁 Trial & Error Method

The Trial and Error technique involves writing and testing different code versions until the desired output is achieved. It’s commonly used by beginners or when logic isn't yet clear.

Example: Trying different loop conditions to print a pattern until it appears correctly.

// Trying different logic to print a right-angled triangle
for(int i = 1; i <= 5; i++) {
    for(int j = 1; j <= i; j++) {
        printf("*");
    }
    printf("\n");
}

Here, the logic might be adjusted multiple times before arriving at the final correct output.


💡 Brainstorming

Brainstorming involves thinking of various ways to approach a problem before writing any code. It is especially useful when the problem can be solved in more than one way (e.g., using arrays, functions, or recursion).

Example: To find the largest element in an array, one might consider using:

  • A loop-based approach
  • Sorting and picking the last element
  • A recursive comparison

This helps in selecting the most efficient or suitable method based on requirements like time, space, or readability.


🧩 Divide & Conquer

Divide and Conquer is a powerful technique where a large problem is divided into smaller sub-problems, solved individually, and then combined to get the final result.

Example: Merge Sort algorithm in C.

// Basic structure of divide and conquer using recursion
void mergeSort(int arr[], int left, int right) {
    if (left < right) {
        int mid = (left + right) / 2;

        mergeSort(arr, left, mid);
        mergeSort(arr, mid + 1, right);

        // Merge the sorted halves
        merge(arr, left, mid, right);
    }
}

Here, the array is divided into halves recursively until each part is sorted, then merged back together.


Define the Problem

Defining the problem means understanding exactly what needs to be done. In C programming, this includes identifying the input, desired output, and any specific constraints or rules.

For example, if the task is to check whether a number is prime, then:

  • Input: A number from the user
  • Output: A message stating whether the number is prime or not

🧠 Analyze the Problem

Analyzing the problem involves breaking it down into smaller parts and identifying the logic needed. You may look for patterns, conditions, and flow of the program.

In our prime number example, analysis may lead to the following logic:

  • A number less than or equal to 1 is not prime.
  • Check divisibility from 2 to square root of the number.
  • If divisible, it's not prime; else, it is.

🔍 Explore Possible Solutions

Exploring solutions means thinking of different algorithms or methods to solve the problem. In C, this could mean choosing between:

  • Using a simple loop
  • Creating a function for reusability
  • Using optimization like breaking the loop early

Once options are considered, you choose the one that is most efficient and write the code.

Comments