Thirteen problems, from beginner to advanced, where you write your own functions. In most of them the main function that reads input and prints the result is already written, so all you write is the body of the requested function. Press "Check" to run your code with real gcc on several inputs and see whether the output is correct.
Note: Run and Check send your code to Wandbox, a free external compiler service, and run it with gcc.
int max3(int a, int b, int c) that takes three integers and returns the largest of them. The main function that reads the input and prints the result is provided. The output is "Max: value"."Run" executes your code with the input above. "Check" tests it against 5 prepared inputs.
a in a variable m, then compare with b and then c: "if it is larger than m, replace m". Finally, return m; sends the value back to the caller.#include <stdio.h> int max3(int a, int b, int c) { int m = a; if (b > m) m = b; if (c > m) m = c; return m; } int main(void) { int a, b, c; scanf("%d %d %d", &a, &b, &c); printf("Max: %d\n", max3(a, b, c)); return 0; }
return; it takes the place of max3(a, b, c) in the caller. Calling printf inside the function does not make a return value. If you start the maximum at 0, the function returns 0 when all the numbers are negative (the 4th test).void print_time(int sec) that takes a number of seconds and prints it on one line in the form "Hh Mm Ss" (0 ≤ sec ≤ 1000000). It returns no value (void). The main function is provided."Run" executes your code with the input above. "Check" tests it against 5 prepared inputs.
sec / 3600, minutes are sec % 3600 / 60, and seconds are sec % 60. The printing is also done inside the function.#include <stdio.h> void print_time(int sec) { int h = sec / 3600; int m = sec % 3600 / 60; int s = sec % 60; printf("%dh %dm %ds\n", h, m, s); } int main(void) { int sec; scanf("%d", &sec); print_time(sec); return 0; }
void "returns no value and just does a job (here, printing)". You do not need to write return; when execution reaches the end of the function, it goes back to the caller. If you compute minutes as sec / 60, you get the total minutes including the hours (61 minutes for 3661 seconds).int is_leap(int year) that returns 1 if the year is a leap year and 0 otherwise (1 ≤ year ≤ 9999). A leap year is "a year divisible by 4, except years divisible by 100, but years divisible by 400 are leap years". The main function is provided."Run" executes your code with the input above. "Check" tests it against 5 prepared inputs.
(year % 4 == 0 && year % 100 != 0) || year % 400 == 0. In C, the result of a comparison or logical operation is 1 (true) or 0 (false), so you can return this expression directly.#include <stdio.h> int is_leap(int year) { return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0; } int main(void) { int year; scanf("%d", &year); if (is_leap(year)) { printf("%d is a leap year\n", year); } else { printf("%d is not a leap year\n", year); } return 0; }
is_xxx that returns 1 for true and 0 for false, code like if (is_leap(year)) reads like a sentence. Checking only "divisible by 4 means leap year" gets 1900 wrong.double c_to_f(double c) that takes a temperature c in Celsius and returns it in Fahrenheit (c × 9 ÷ 5 + 32). The main function is provided and prints the results to one decimal place."Run" executes your code with the input above. "Check" tests it against 5 prepared inputs.
double, so you can return the result of the calculation directly, as in return c * 9.0 / 5.0 + 32.0;.#include <stdio.h> double c_to_f(double c) { return c * 9.0 / 5.0 + 32.0; } int main(void) { double c; scanf("%lf", &c); printf("%.1f C = %.1f F\n", c, c_to_f(c)); return 0; }
c * 9 / 5 is computed correctly because c is a double, but if you write 9 / 5 * c, 9 / 5 is evaluated first as integer division and becomes 1, so the result is off. Also note that if the return type is int, the fractional part is cut off.int reverse_num(int n) that takes an integer n (0 ≤ n ≤ 99999999) and returns the number with its digits in reverse order (e.g. 1230 → 321). The main function is provided and prints the original n after calling the function."Run" executes your code with the input above. "Check" tests it against 5 prepared inputs.
r at 0 and repeat "r = r * 10 + n % 10; → n /= 10;" until n becomes 0. It is fine to modify n inside the function.#include <stdio.h> int reverse_num(int n) { int r = 0; while (n > 0) { r = r * 10 + n % 10; n /= 10; } return r; } int main(void) { int n; scanf("%d", &n); int r = reverse_num(n); printf("%d reversed is %d\n", n, r); return 0; }
n the function receives is a copy of the caller's n, so even if the function counts it down to 0, the n in main does not change and the original number can still be printed. Conversely, a function cannot report a result to the caller by modifying its parameter, so the result is returned with return.int sum_array(const int a[], int n) that takes an int array with n elements and returns the sum, and a function int max_array(const int a[], int n) that returns the maximum. The main function is provided: it first reads n (1 ≤ n ≤ 100), then n integers (absolute value at most 10000), and prints the sum, the maximum and the average (to two decimal places)."Run" executes your code with the input above. "Check" tests it against 4 prepared inputs.
const int a[] and receive the number of elements in a separate parameter n. For the maximum, treat a[0] as the tentative maximum and compare it with a[1] onward.#include <stdio.h> int sum_array(const int a[], int n) { int sum = 0; for (int i = 0; i < n; i++) { sum += a[i]; } return sum; } int max_array(const int a[], int n) { int max = a[0]; for (int i = 1; i < n; i++) { if (a[i] > max) max = a[i]; } return max; } int main(void) { int a[100]; int n; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &a[i]); } int sum = sum_array(a, n); printf("Sum: %d\n", sum); printf("Max: %d\n", max_array(a, n)); printf("Average: %.2f\n", (double)sum / n); return 0; }
sizeof(a) inside the function is not the size of the whole array). Adding const says "this function does not modify the array". If you start the maximum at 0, you get it wrong when all the numbers are negative (the 3rd test).void reverse_array(int a[], int n) that reverses the order of an int array with n elements in place. The main function is provided: it reads n (1 ≤ n ≤ 100) and n integers, calls the function, and prints the array separated by spaces."Run" executes your code with the input above. "Check" tests it against 5 prepared inputs.
a[i] with the back element a[n - 1 - i], repeating up to the middle (i < n / 2). Use a temporary variable tmp for the swap.#include <stdio.h> void reverse_array(int a[], int n) { for (int i = 0; i < n / 2; i++) { int tmp = a[i]; a[i] = a[n - 1 - i]; a[n - 1 - i] = tmp; } } int main(void) { int a[100]; int n; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &a[i]); } reverse_array(a, n); for (int i = 0; i < n; i++) { printf("%d ", a[i]); } printf("\n"); return 0; }
a[i] inside the function changes the array in main. If the loop runs all the way to i < n, the elements already swapped are swapped again and end up back where they started.int sum_divisors(int n) that returns the sum of the divisors of n excluding n itself. In this problem the function definition goes after main, so a prototype declaration is placed at the top. The main function is provided: it prints that n is a perfect number if the sum of divisors equals n, an abundant number if it is larger than n, and a deficient number if it is smaller."Run" executes your code with the input above. "Check" tests it against 6 prepared inputs.
i from 1 to n / 2, add it if n % i == 0 (divisors other than n itself are never larger than n/2). When n = 1 the loop never runs and the sum is 0.#include <stdio.h> int sum_divisors(int n); // prototype declaration int main(void) { int n; scanf("%d", &n); int s = sum_divisors(n); printf("Sum of divisors: %d\n", s); if (s == n) { printf("%d is a perfect number\n", n); } else if (s > n) { printf("%d is an abundant number\n", n); } else { printf("%d is a deficient number\n", n); } return 0; } int sum_divisors(int n) { int sum = 0; for (int i = 1; i <= n / 2; i++) { if (n % i == 0) sum += i; } return sum; }
; at the end of the declaration.char grade(int score) that takes a score (0–100) and returns a grade letter: A for 90 or more, B for 80 or more, C for 70 or more, D for 60 or more, and F below that. The main function is provided: it reads the number of students n (1 ≤ n ≤ 50) and their n scores, and prints "score pts: grade" for each student on its own line."Run" executes your code with the input above. "Check" tests it against 4 prepared inputs.
if (score >= 90) return 'A';, and return as soon as one matches. A character is written in single quotes, like 'A'.#include <stdio.h> char grade(int score) { if (score >= 90) return 'A'; if (score >= 80) return 'B'; if (score >= 70) return 'C'; if (score >= 60) return 'D'; return 'F'; } int main(void) { int n; scanf("%d", &n); for (int i = 0; i < n; i++) { int score; scanf("%d", &score); printf("%d pts: %c\n", score, grade(score)); } return 0; }
return leaves the function immediately, so the later checks are not performed even without else. "A" (double quotes) is a string, so it cannot be a char return value. The tests check the boundaries of exactly 90, 80, 70 and 60 points.int is_new_max(int x) that receives an integer x on each call and returns 1 if x is larger than every value passed so far (or if this is the first call), and 0 otherwise. Remember the maximum so far in a static variable inside the function (do not use a global variable). The main function is provided: it passes n integers (1 ≤ n ≤ 50) to the function in order and prints "value: new max" or "value: -" on each line."Run" executes your code with the input above. "Check" tests it against 4 prepared inputs.
static int max; and static int first = 1;, inside the function. A static variable keeps its value after the function returns, and it is initialized only once, when the program starts. If first is 1, treat the call as "the first one".#include <stdio.h> int is_new_max(int x) { static int first = 1; static int max; if (first || x > max) { first = 0; max = x; return 1; } return 0; } int main(void) { int n; scanf("%d", &n); for (int i = 0; i < n; i++) { int x; scanf("%d", &x); if (is_new_max(x)) { printf("%d: new max\n", x); } else { printf("%d: -\n", x); } } return 0; }
balance (initial value 0) for the account balance, write a function void deposit(int amount) that deposits money and a function int withdraw(int amount) that withdraws money. withdraw reduces the balance and returns 1 if the balance is sufficient; otherwise it returns 0 without changing the balance. The main function is provided: it reads the number of operations n (1 ≤ n ≤ 20) and n lines of "d amount" (deposit) or "w amount" (withdrawal) (amounts are 1–100000)."Run" executes your code with the input above. "Check" tests it against 4 prepared inputs.
balance += amount;. In withdraw, handle the failure case first with if (amount > balance) return 0;.#include <stdio.h> int balance = 0; // balance (global variable) void deposit(int amount) { balance += amount; } int withdraw(int amount) { if (amount > balance) { return 0; } balance -= amount; return 1; } int main(void) { int n; scanf("%d", &n); for (int i = 0; i < n; i++) { char cmd; int amount; scanf(" %c %d", &cmd, &amount); if (cmd == 'd') { deposit(amount); printf("Deposit %d: balance %d\n", amount, balance); } else if (withdraw(amount)) { printf("Withdraw %d: balance %d\n", amount, balance); } else { printf("Withdraw %d: insufficient funds (balance %d)\n", amount, balance); } } printf("Final balance: %d\n", balance); return 0; }
int balance = 0; inside a function, a new local variable with the same name is created and the global variable does not change (name shadowing). Withdrawing exactly the balance (the 2nd operation, w 500) must succeed, so the condition is amount > balance. Global variables are convenient because they can be changed from anywhere, but that also makes it hard to track where they changed, so use them sparingly.int days_in_month(int y, int m) that takes a year y and a month m (1–12) and returns the number of days in that month, and a function int day_of_year(int y, int m, int d) that takes a year, month and day and returns which day of the year it is, counting January 1 as day 1. The leap year function is_leap and the main function are provided. The input is always a valid date (1 ≤ y ≤ 9999)."Run" executes your code with the input above. "Check" tests it against 7 prepared inputs.
{31, 28, 31, 30, ...} and return 29 for February in a leap year. day_of_year is "the total days of months 1 through m−1 + d", so you can build it by calling days_in_month repeatedly.#include <stdio.h> int is_leap(int y) { return (y % 4 == 0 && y % 100 != 0) || y % 400 == 0; } int days_in_month(int y, int m) { const int days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; if (m == 2 && is_leap(y)) { return 29; } return days[m - 1]; } int day_of_year(int y, int m, int d) { int total = d; for (int i = 1; i < m; i++) { total += days_in_month(y, i); } return total; } int main(void) { int y, m, d; scanf("%d %d %d", &y, &m, &d); printf("Days in %d/%d: %d\n", y, m, days_in_month(y, m)); printf("%d/%d/%d is day %d\n", y, m, d, day_of_year(y, m, d)); return 0; }
days[m - 1]. If the loop runs to i <= m, it also adds the days of the month itself.void sort_array(int a[], int n) that sorts an int array with n elements in ascending order. Any sorting method is fine (bubble sort, selection sort, etc.). The function print_array that prints the array and the main function are provided: main reads n (1 ≤ n ≤ 100) and n integers, and prints the array before and after sorting."Run" executes your code with the input above. "Check" tests it against 5 prepared inputs.
i from 0 to n−2, run j from 0 to n−2−i and swap neighbours if a[j] > a[j + 1]. Each pass settles the largest remaining value at the right end.#include <stdio.h> void print_array(const char label[], const int a[], int n) { printf("%s:", label); for (int i = 0; i < n; i++) { printf(" %d", a[i]); } printf("\n"); } void sort_array(int a[], int n) { for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - 1 - i; j++) { if (a[j] > a[j + 1]) { int tmp = a[j]; a[j] = a[j + 1]; a[j + 1] = tmp; } } } } int main(void) { int a[100]; int n; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &a[i]); } print_array("Before", a, n); sort_array(a, n); print_array("After", a, n); return 0; }
j < n, a[j + 1] reads outside the array (a[n]), so use j < n - 1 - i.