General Functions
Here you can find general function examples that dont use arrays and serve as a stepping stone for moving to more advnced functions
Sum
- #include <stdio.h>
-
- int sum(){
- int tmp, sum = 0;
- scanf("%d", &tmp);
- while (!feof(stdin)) {
- sum += tmp;
- scanf("%d", &tmp);
- }
- return sum;
- }
-
Min
- #include <stdio.h>
-
- int min(){
- int tmp;
- scanf("%d", &tmp);
- int min = tmp;
- while (!feof(stdin)) {
- if (tmp < min) min = tmp;
- scanf("%d", &tmp);
- }
- return min;
- }
-
Max
- #include <stdio.h>
-
- int max(){
- int tmp;
- scanf("%d", &tmp);
- int max = tmp;
- while (!feof(stdin)) {
- if (tmp > max) max = tmp;
- scanf("%d", &tmp);
- }
- return max;
- }
-
Range
- #include <stdio.h>
-
- int rng(){
- int max, min, tmp;
- scanf("%d", &tmp);
- max = min = tmp;
- while (!feof(stdin)) {
- if (tmp < min) min = tmp;
- if (tmp > max) max = tmp;
- scanf("%d", &tmp);
- }
- return max - min;
- }
-
Average
- #include <stdio.h>
-
- float avg(){
- int tmp, sum = 0, cnt = 0;
- scanf("%d", &tmp);
- while (!feof(stdin)) {
- sum += tmp; cnt++;
- scanf("%d", &tmp);
- }
- return (float)sum / cnt;
- }
-
Convert Case
- #include <stdio.h>
-
- char cvtCase(char c) {
- if (c >= 'a' && c <= 'z') c -= 32;
- else if (c >= 'A' && c <= 'Z') c += 32;
- else c = 0;
- return c;
- }
-
Invert Integer
- #include <stdio.h>
-
- int invInt(int x) {
- int r, inv = 0;
- while (x > 0) {
- r = x % 10;
- inv = (inv * 10) + r;
- x = x / 10;
- }
- return inv;
- }
-
Distance Between 2 Points
- #include <stdio.h>
- #include <math.h>
-
- typedef struct Pts {
- int x;
- int y;
- } Pt;
-
- float db2p(Pt a, Pt b) {
- return sqrt(pow((b.x - a.x), 2)
- + pow((b.y - a.y), 2));
- }
-
Vowel Check
- #include <stdio.h>
- #include <ctype.h>
-
- int vCK (char c) {
- if (c == 'a' || c == 'e' || c == 'i'
- || c == 'o' || c == 'u')
- return 1;
- else if (isalpha(c)) return 0;
- else return -1;
- }
-
Positive Check
- #include <stdio.h>
-
- int pCK (int x) {
- if (x > 0) return 1;
- else if (x < 0) return -1;
- else return 0;
- }
-
Swap 2 integers
- #include <stdio.h>
-
- void swp(int *a, int *b) {
- int t = *a;
- *a = *b;
- *b = t;
- }
-
Fahrenheit to Celsius
- #include <stdio.h>
-
- float f2c(float f) {
- return (f - 32) / 1.8;
- }
-
Celsius to Fahrenheit
- #include <stdio.h>
-
- float c2f(float c) {
- return c * 1.8 + 32;
- }
-