Task 1: Generate Student IDs Based on Random Major Selection
The following program generates five student IDs. Each ID starts with either 20256343 (for one major) or 20256136 (for another), followed by a four-digit number. The selection between majors is randomized.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define COUNT 5
#define MAJOR_A_MAX 80
#define MAJOR_B_MAX 35
int main() {
int i = 0;
srand((unsigned int)time(NULL));
while (i < COUNT) {
int is_major_a = rand() % 2;
int suffix;
if (is_major_a) {
suffix = rand() % MAJOR_A_MAX + 1;
printf("20256343%04d\n", suffix);
} else {
suffix = rand() % MAJOR_B_MAX + 1;
printf("20256136%04d\n", suffix);
}
i++;
}
return 0;
}
Without seeding via srand(time(NULL)), the sequence of generated numbers would be identical across runs. Removing this line results in deterministic output.
Task 2: Beverage Vending Machine Simulator
This program simulates a vending machine that allows users to select drinks, specify quantities, and receive change. The total resets after each transaction.
#include <stdio.h>
int main() {
int option, units;
float price = 0.0f, payment, refund;
while (1) {
printf("\nBeverage Menu:\n");
printf("1. Cola - ¥3/unit\n");
printf("2. Sprite - ¥3/unit\n");
printf("3. Orange Juice - ¥5/unit\n");
printf("4. Mineral Water - ¥2/unit\n");
printf("0. Exit\n");
printf("Select beverage: ");
scanf("%d", &option);
if (option == 0) break;
if (option < 1 || option > 4) {
printf("Invalid selection.\n");
continue;
}
printf("Enter quantity: ");
scanf("%d", &units);
if (units < 0) {
printf("Quantity cannot be negative.\n");
continue;
}
switch (option) {
case 1:
case 2:
price = 3 * units;
break;
case 3:
price = 5 * units;
break;
case 4:
price = 2 * units;
break;
}
printf("Insert amount: ");
scanf("%f", &payment);
refund = payment - price;
printf("Total: %.2f\n", price);
printf("Change: %.2f\n", refund);
price = 0.0f; // Reset for next transaction
}
printf("Thank you!\n");
return 0;
}
If price = 0.0f; is omitted, totals from previous purchases accumulate, leading to incorrect billing.
Task 3: Traffic Light Command Interpreter
Reads single-character commands (r, g, y) from input and responds accordingyl. Exits on EOF.
#include <stdio.h>
int main() {
char cmd;
while ((cmd = getchar()) != EOF) {
if (cmd == '\n') continue; // Skip newlines
switch (cmd) {
case 'r':
printf("stop!\n");
break;
case 'g':
printf("go go go\n");
break;
case 'y':
printf("wait a minute\n");
break;
default:
printf("something must be wrong...\n");
break;
}
}
return 0;
}
An extra getchar() was originally used to consume the newline; here, it's handled more cleanly by skipping \n.
Task 4: Daily Expense Tracker
Accumulates expenses until -1 is entered, then reports total, maximum, and minimum expenditures.
#include <stdio.h>
int main() {
double expense, total = 0.0, max_val = -1.0, min_val = 20001.0;
printf("Enter daily expenses (-1 to stop):\n");
while (scanf("%lf", &expense) == 1 && expense != -1) {
total += expense;
if (expense > max_val) max_val = expense;
if (expense < min_val) min_val = expense;
}
printf("Total spent: %.1f\n", total);
printf("Highest expense: %.1f\n", max_val);
printf("Lowest expense: %.1f\n", min_val);
return 0;
}
Initial min_val is set high to ensure any real expense will be lower.
Task 5: Triangle Classifier
Takes three side lengths and classifies the triangle as equilateral, isosceles, right-angled, or scalene—after vaildating triangle inequality.
#include <stdio.h>
int main() {
double a, b, c;
while (scanf("%lf %lf %lf", &a, &b, &c) == 3) {
if (a + b <= c || a + c <= b || b + c <= a) {
printf("Not a valid triangle\n");
} else if (a == b && b == c) {
printf("Equilateral triangle\n");
} else if (a == b || a == c || b == c) {
printf("Isosceles triangle\n");
} else if (a*a + b*b == c*c ||
a*a + c*c == b*b ||
b*b + c*c == a*a) {
printf("Right-angled triangle\n");
} else {
printf("Scalene triangle\n");
}
}
return 0;
}
Note: Floating-point comparison assumes exact equality; in practice, an epsilon tolerance may be needed.
Task 6: Lucky Day Guessing Game
Generates a random day (1–30) in November 2025. The user has three attempts to guess it.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
srand((unsigned int)time(NULL));
int lucky_day = rand() % 30 + 1;
int guess, attempts = 0;
printf("Guess your lucky day in Nov 2025 (1-30). You have 3 tries:\n");
while (attempts < 3) {
printf("Attempt %d: ", attempts + 1);
scanf("%d", &guess);
if (guess == lucky_day) {
printf("Correct! :)\n");
return 0;
} else if (guess > lucky_day) {
printf("Too late! Try an earlier date.\n");
} else {
printf("Too early! Your lucky day hasn't come yet.\n");
}
attempts++;
}
printf("Out of tries! Your lucky day was %d.\n", lucky_day);
return 0;
}
The original code incorrectly incremented x after each guess; this version tracks attempts separately for clarity.