Exercise 1: Understanding Random Seed Initialization
The fololwing program demonstrates how to generate random student identification numbers using the C standard library's random number generation functions.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define STUDENT_COUNT 5
#define MAX_ID_SUFFIX 80
#define ALT_ID_SUFFIX 35
int main(void) {
int iteration;
int department_code, id_suffix;
srand((unsigned int)time(NULL));
iteration = 0;
while (iteration < STUDENT_COUNT) {
department_code = rand() % 2;
if (department_code) {
id_suffix = rand() % MAX_ID_SUFFIX + 1;
printf("20256343%04d\n", id_suffix);
} else {
id_suffix = rand() % ALT_ID_SUFFIX + 1;
printf("20256136%04d\n", id_suffix);
}
iteration++;
}
return 0;
}
Output Behavior: Each execution produces different results due to the time-based seed initialization.
Experiment: Removing the srand(time(NULL)) statement causes the program to produce identical output across multiple runs. This confirms that the seeding function establishes the starting point for the pseudo-random sequence, enabling variability between executions.
Practical Application: This program simulates random selection of student records from two different academic departments.
Exercise 2: Automated Vending Machine Simulation
This exercise implements an interactive beverage vending system using nested loops and conditional logic to process customer orders.
#include <stdio.h>
int main(void) {
int product_choice, purchase_quantity;
float running_total = 0.0f, payment_received, transaction_change;
while (1) {
printf("\n--- Beverage Vending Machine ---\n");
printf("1. Cola - $3.00/bottle\n");
printf("2. Sprite - $3.00/bottle\n");
printf("3. Orange Juice - $5.00/bottle\n");
printf("4. Mineral Water - $2.00/bottle\n");
printf("0. Exit\n");
printf("Select product: ");
if (scanf("%d", &product_choice) != 1) {
break;
}
if (product_choice == 0) {
break;
}
if (product_choice < 1 || product_choice > 4) {
printf("Invalid selection. Please choose 1-4.\n");
continue;
}
printf("Enter quantity: ");
scanf("%d", &purchase_quantity);
if (purchase_quantity < 0) {
printf("Quantity cannot be negative. Try again.\n");
continue;
}
switch (product_choice) {
case 1:
case 2:
running_total += 3.0f * purchase_quantity;
break;
case 3:
running_total += 5.0f * purchase_quantity;
break;
case 4:
running_total += 2.0f * purchase_quantity;
break;
}
printf("Amount due: $%.2f\n", running_total);
printf("Enter payment: ");
scanf("%f", &payment_received);
transaction_change = payment_received - running_total;
printf("Transaction total: $%.2f\n", running_total);
printf("Change returned: $%.2f\n", transaction_change);
running_total = 0.0f;
}
printf("Thank you for your purchase. Goodbye!\n");
return 0;
}
Key Observations:
When the running_total = 0.0f; reset is omitted, the accumulation persists across transactions, causing incorrect calculations in subsequent purchases. The continue statement skips the remaining code in the current iteration and proceeds to the next loop cycle, allowing for input validation without terminating the program.
Exercise 3: Traffic Signal Response System
This program processes single-character input to simulate traffic light control commands.
#include <stdio.h>
int main(void) {
char signal;
while (scanf("%c", &signal) != EOF) {
switch (signal) {
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;
}
getchar();
}
return 0;
}
The getchar() call after the switch statement consumes the newline character remaining in the input buffer, preventing unintended processing during the next iteration.
Exercise 4: Daily Expense Tracker
This application monitors daily spending by tracking accumulated expenses, identifying maximum and minimum transactions.
#include <stdio.h>
int main(void) {
double expense_amount;
double total_expense = 0.0;
double highest = -1.0, lowest = -1.0;
printf("Enter daily expenses (enter -1 to finish):\n");
while (1) {
scanf("%lf", &expense_amount);
if (expense_amount == -1.0) {
break;
}
if (highest == -1.0 && lowest == -1.0) {
highest = expense_amount;
lowest = expense_amount;
}
total_expense += expense_amount;
if (expense_amount > highest) {
highest = expense_amount;
}
if (expense_amount < lowest) {
lowest = expense_amount;
}
}
printf("Total expenses: %.1f\n", total_expense);
printf("Highest single expense: %.1f\n", highest);
printf("Lowest single expense: %.1f\n", lowest);
return 0;
}
The initialization checks ensure proper handling of edge cases, particularly when tracking maximum and minimum values throughout the expense tracking session.
Exercise 5: Geometric Triangle Classification
This program analyzes three side lengths to classify geometric triangle properties using conditional logic and the Pythagorean theorem.
#include <stdio.h>
int main(void) {
int side_a, side_b, side_c;
while (scanf("%d%d%d", &side_a, &side_b, &side_c) == 3) {
if (side_a + side_b <= side_c ||
side_a + side_c <= side_b ||
side_b + side_c <= side_a) {
printf("Cannot form a triangle\n");
continue;
}
if (side_a == side_b && side_b == side_c) {
printf("Equilateral triangle\n");
} else if (side_a == side_b || side_b == side_c || side_a == side_c) {
printf("Isosceles triangle\n");
} else if (side_a * side_a + side_b * side_b == side_c * side_c ||
side_a * side_a + side_c * side_c == side_b * side_b ||
side_b * side_b + side_c * side_c == side_a * side_a) {
printf("Right triangle\n");
} else {
printf("Scalene triangle\n");
}
}
return 0;
}
The triangle inequality check precedes all other classifications, ensuring invalid input is handled before attempting geometric analysis.
Exercise 6: Lucky Number Guessing Game
This interactive game challenges users to guess a randomly generated number within a limited number of attempts.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void) {
int target_number, user_guess;
srand((unsigned int)time(NULL));
target_number = rand() % 30 + 1;
printf("Game started! You have 3 chances to guess (1-30): ");
int attempt;
for (attempt = 0; attempt < 3; attempt++) {
scanf("%d", &user_guess);
if (user_guess == target_number) {
printf("Congratulations! You got it :)\n");
break;
} else if (user_guess < target_number) {
printf("Too early! Your lucky day hasn't arrived yet\n");
} else {
printf("Too late! Your lucky day is ahead\n");
}
}
if (attempt == 3) {
printf("Out of attempts. Secret value: %d\n", target_number);
}
return 0;
}
The loop counter variable is used post-execution to determine whether the player exhausted all attempts, enabling conditional display of the hidden number as a reveal mechanism.