📖 Scenario: Imagine you are organizing a small event and need to select tasks to complete within a limited time. You want to pick tasks that give you the most value quickly. This project will help you understand how a greedy approach can work well in some cases but fail in others.
🎯 Goal: You will create a simple program in C that selects tasks based on their value and time using a greedy method. You will see when this approach gives the best result and when it does not.
📋 What You'll Learn
Create an array of tasks with fixed values and durations
Set a total available time limit
Implement a greedy selection of tasks based on value per time
Print the selected tasks and total value
💡 Why This Matters
🌍 Real World
Greedy algorithms are used in scheduling, resource allocation, and decision-making where quick, good-enough solutions are needed.
💼 Career
Understanding greedy algorithms helps in software development roles involving optimization, such as operations research, game development, and system design.
Progress0 / 4 steps
1
Create the tasks array
Create a struct called Task with int value and int duration. Then create an array called tasks with these exact entries: {value: 10, duration: 2}, {value: 5, duration: 1}, {value: 15, duration: 3}, {value: 7, duration: 2}, {value: 6, duration: 1}.
DSA C
Hint
Use typedef struct to define Task. Then create tasks array with the exact values.
2
Set the total available time
Add an integer variable called total_time and set it to 5 to represent the total time available for tasks.
DSA C
Hint
Declare int total_time and assign it the value 5.
3
Select tasks greedily by value per duration
Implement a greedy selection by creating a loop over tasks using for (int i = 0; i < 5; i++). Calculate value per duration as a float and select tasks while total duration does not exceed total_time. Keep track of selected tasks in an integer array selected initialized to 0, and update total used time and total value.
DSA C
Hint
Loop over tasks with for (int i = 0; i < 5; i++). Calculate ratio as float. Select tasks if total duration fits.
4
Print selected tasks and total value
Print the indices of selected tasks using for (int i = 0; i < 5; i++) and if (selected[i]). Then print the total value with printf("Total value: %d\n", total_value);.
DSA C
Hint
Use printf to show selected task indices and total value.
Practice
(1/5)
1. What is the main reason a greedy algorithm works correctly for some problems?
easy
A. Because it makes the best choice at each step hoping for a global optimum
B. Because it tries all possible solutions and picks the best one
C. Because it uses recursion to explore all options
D. Because it sorts the input data before processing
Solution
Step 1: Understand greedy choice property
A greedy algorithm picks the best option available at the current step without reconsidering previous choices.
Step 2: Connect greedy choice to global optimum
For some problems, making the best local choice at each step leads to the best overall solution.
Final Answer:
Because it makes the best choice at each step hoping for a global optimum -> Option A
Quick Check:
Greedy choice property = Because it makes the best choice at each step hoping for a global optimum [OK]
Hint: Greedy picks best local choice hoping for best overall [OK]
Common Mistakes:
Thinking greedy tries all solutions
Confusing greedy with recursion or backtracking
Assuming sorting alone makes greedy correct
2. Which of the following is the correct syntax to pick the next item greedily from a sorted array arr in C?
easy
A. int next = arr[--i];
B. int next = arr[i]; i++;
C. int next = arr[++i];
D. int next = arr[i]; i--;
Solution
Step 1: Understand post-increment vs pre-increment
Post-increment i++ returns current value then increments; pre-increment ++i increments then returns.
Step 2: Choose syntax that picks current item then moves forward
int next = arr[i]; i++; picks current element and then increments index for next pick.
Final Answer:
int next = arr[i]; i++; -> Option B
Quick Check:
Pick current then increment index = int next = arr[i]; i++; [OK]
Hint: Pick current element then increment index separately [OK]
Common Mistakes:
Using pre-increment which skips first element
Decrementing index instead of incrementing
Confusing post-increment inside array index
3. Consider the following C code that tries to select intervals greedily by earliest finish time:
typedef struct {
int start;
int end;
} Interval;
int main() {
Interval arr[] = {{1,4}, {3,5}, {0,6}, {5,7}, {8,9}};
int n = 5, count = 0, last_end = 0;
for (int i = 0; i < n; i++) {
if (arr[i].start >= last_end) {
count++;
last_end = arr[i].end;
}
}
printf("%d\n", count);
return 0;
}