Task 1
#include <stdio.h> #include <stdlib.h> #include <time.h> #define N 5 #define N1 80 #define N2 35 int main() { int counter; int major_code, number; srand(time(NULL)); counter = 0; while(counter < N) { major_code = rand() % 2; if(major_code) { number = rand() % N1 + 1; printf("20256343%04d\n", number); } else { number = rand() % N2 + 1; printf("20256136%04d\n", number); } counter++; } return 0; }
Result: Each run produces different output.
- Removing
srand(time(NULL));results in the same output each time, suggesting that this line initializes the random seed. - This program randomly generates student IDs.
Task 2
#include <stdio.h> int main() { int selection, quantity; float total = 0, paid, change; while (1) { printf("\nVending Machine Menu:\n"); printf("1. Coke - 3 yuan per bottle\n"); printf("2. Sprite - 3 yuan per bottle\n"); printf("3. Orange Juice - 5 yuan per bottle\n"); printf("4. Mineral Water - 2 yuan per bottle\n"); printf("0. Exit\n"); printf("Enter drink number: "); scanf("%d", &selection); if (selection == 0) break; if (selection < 1 || selection > 4) { printf("Invalid drink number, please try again.\n"); continue; } printf("Enter quantity: "); scanf("%d", &quantity); if (quantity < 0) { printf("Quantity cannot be negative, please try again.\n"); continue; } if(selection == 1 || selection == 2) total += 3 * quantity; else if(selection == 3) total += 5 * quantity; else total += 2 * quantity; printf("Enter amount: "); scanf("%f", &paid); change = paid - total; printf("Total price: %.2f yuan\n", total); printf("Change: %.2f yuan\n", change); total = 0; } printf("Thank you for shopping, come again!\n"); return 0; }
Output:
- Without
total = 0;, previous total is not reset and continues to next iteration. continueends current loop iteration and starts the next one.
Task 3
#include <stdio.h> int main() { char color; while (scanf("%c", &color) != EOF) { if (color == 'r') printf("stop!\n"); else if (color == 'g') printf("go go go\n"); else if (color == 'y') printf("wait a minute\n"); else printf("something must be wrong...\n"); getchar(); } return 0; }
Task 4
#include <stdio.h> int main() { double expense; double sum = 0.0; double max=-1, min=-1; printf("Enter today's expenses, enter -1 to stop:\n"); while (1) { scanf("%lf", &expense); if (max == -1 && min == -1) { max = expense; min = expense; } if (expense == -1) break; sum += expense; if (expense > max) max = expense; if (expense < min) min = expense; } printf("Today's total expenses: %.1f\n", sum); printf("Today's highest expense: %.1f\n", max); printf("Today's lowest expense: %.1f\n", min); return 0; }
Output:
Task 5
#include <stdio.h>
int main() { int a, b, c; while (scanf("%d%d%d", &a, &b, &c) != EOF) { if (a + b <= c || a + c <= b || b + c <= a) { printf("Cannot form a triangle\n"); continue; } if (a == b && b == c) { printf("Equilateral triangle\n"); } else if (a == b || b == c || a == 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("Normal triangle\n"); } return 0; }
Task 6
#include <stdio.h> #include <stdlib.h> #include <time.h>
int main(void) { int day, guess; srand((unsigned int)time(NULL)); day = rand() % 30 + 1; printf("Start, you have three chances, guess (1~30): "); int i; for (i = 0; i < 3; i++) { scanf("%d", &guess); if (guess == day) { printf("Wow, you guessed it!\n"); break; } else if (guess < day) { printf("Your guess is too early, your lucky day hasn't come yet\n"); } else { printf("You're guess is too late, your lucky day is earlier\n"); } } if(i==3) printf("You've used all chance. By the way, your lucky day is %d\n",day);
return 0;
}