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

  1. #include <stdio.h>
  2.  
  3. int sum(){
  4.  int tmp, sum = 0;
  5.  scanf("%d", &tmp);
  6.  while (!feof(stdin)) {
  7.   sum += tmp;
  8.   scanf("%d", &tmp);
  9.  }
  10.  return sum;
  11. }

Min

  1. #include <stdio.h>
  2.  
  3. int min(){
  4.  int tmp;
  5.  scanf("%d", &tmp);
  6.  int min = tmp;
  7.  while (!feof(stdin)) {
  8.   if (tmp < min) min = tmp;
  9.   scanf("%d", &tmp);
  10.  }
  11.  return min;
  12. }

Max

  1. #include <stdio.h>
  2.  
  3. int max(){
  4.  int tmp;
  5.  scanf("%d", &tmp);
  6.  int max = tmp;
  7.  while (!feof(stdin)) {
  8.   if (tmp > max) max = tmp;
  9.   scanf("%d", &tmp);
  10.  }
  11.  return max;
  12. }

Range

  1. #include <stdio.h>
  2.  
  3. int rng(){
  4.  int max, min, tmp;
  5.  scanf("%d", &tmp);
  6.  max = min = tmp;
  7.  while (!feof(stdin)) {
  8.   if (tmp < min) min = tmp;
  9.   if (tmp > max) max = tmp;
  10.   scanf("%d", &tmp);
  11.  }
  12.  return max - min;
  13. }

Average

  1. #include <stdio.h>
  2.  
  3. float avg(){
  4.  int tmp, sum = 0, cnt = 0;
  5.  scanf("%d", &tmp);
  6.  while (!feof(stdin)) {
  7.   sum += tmp; cnt++;
  8.   scanf("%d", &tmp);
  9.  }
  10.  return (float)sum / cnt;
  11. }

Convert Case

  1. #include <stdio.h>
  2.  
  3. char cvtCase(char c) {
  4.  if (c >= 'a' && c <= 'z') c -= 32;
  5.  else if (c >= 'A' && c <= 'Z') c += 32;
  6.  else c = 0;
  7.  return c;
  8. }

Invert Integer

  1. #include <stdio.h>
  2.  
  3. int invInt(int x) {
  4.  int r, inv = 0;
  5.  while (x > 0) {
  6.   r = x % 10;
  7.   inv = (inv * 10) + r;
  8.   x = x / 10;
  9.  }
  10.  return inv;
  11. }

Distance Between 2 Points

  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. typedef struct Pts {
  5.  int x;
  6.  int y;
  7. } Pt;
  8.  
  9. float db2p(Pt a, Pt b) {
  10.  return sqrt(pow((b.x - a.x), 2)
  11.  + pow((b.y - a.y), 2));
  12. }

Vowel Check

  1. #include <stdio.h>
  2. #include <ctype.h>
  3.  
  4. int vCK (char c) {
  5.  if (c == 'a' || c == 'e' || c == 'i'
  6.  || c == 'o' || c == 'u')
  7.   return 1;
  8.  else if (isalpha(c)) return 0;
  9.  else return -1;
  10. }

Positive Check

  1. #include <stdio.h>
  2.  
  3. int pCK (int x) {
  4.  if (x > 0) return 1;
  5.  else if (x < 0) return -1;
  6.  else return 0;
  7. }

Swap 2 integers

  1. #include <stdio.h>
  2.  
  3. void swp(int *a, int *b) {
  4.  int t = *a;
  5.  *a = *b;
  6.  *b = t;
  7. }

Fahrenheit to Celsius

  1. #include <stdio.h>
  2.  
  3. float f2c(float f) {
  4.  return (f - 32) / 1.8;
  5. }

Celsius to Fahrenheit

  1. #include <stdio.h>
  2.  
  3. float c2f(float c) {
  4.  return c * 1.8 + 32;
  5. }