Arrays

Here you can find array function examples that are very useful and are built upon to create 2D and 3D array functions

Array Functions

Sum

#include <stdio.h>

int sumA(int a[], int s) {
 int sum = 0;
 for (int i = 0; i < s; i++)
  sum += a[i];
 return sum;
}

Add

#include <stdio.h>
#include <stdlib.h>

int* addA(int a[], int b[], int s) {
 int *sum = malloc(s * sizeof(int));
 for (int i = 0; i < s; i++)
  sum[i] = a[i] + b[i];
 return sum;
}

Subtract

#include <stdio.h>
#include <stdlib.h>

int* subA(int a[], int b[], int s) {
 int *dif = malloc(s * sizeof(int));
 for (int i = 0; i < s; i++)
  dif[i] = a[i] - b[i];
 return dif;
}

Multiply

#include <stdio.h>
#include <stdlib.h>

int* multA(int a[], int b[], int s) {
 int *prod = malloc(s * sizeof(int));
 for (int i = 0; i < s; i++)
  prod[i] = a[i] * b[i];
 return prod;
}

Divide

#include <stdio.h>
#include <stdlib.h>

float* divA(float a[], float b[], int s) {
 float *quot = malloc(s * sizeof(float));
 for (int i = 0; i < s; i++)
  quot[i] = a[i] / b[i];
 return quot;
}

Min

#include <stdio.h>

int minA(int a[], int s) {
 int min = a[0];
 for (int i = 0; i < s; i++) {
  if (a[i] < min) min = a[i];
 }
 return min;
}

Max

#include <stdio.h>

int maxA(int a[], int s) {
 int max = a[0];
 for (int i = 0; i < s; i++) {
  if (a[i] > max) max = a[i];
 }
 return max;
}

Range

#include <stdio.h>

int rngA(int a[], int s) {
 return maxA(a, s) - minA(a, s);
}

Average

#include <stdio.h>

float avgA(int a[], int s) {
 return sumA(a, s) / (float) s;
}

Sum of Squares

#include <stdio.h>
#include <math.h>

double sos(int a[], int s) {
 float sos = 0;
 for (int i = 0; i < s; i++)
  sos += pow(a[i] - avgA(a, s), 2);
 return sos;
}

Variance

#include <stdio.h>

double var(double sos, int s) {
 return sos / (s - 1);
}

Sample Standard Deviation

#include <stdio.h>
#include <math.h>

double StdDev(int a[], int s){
 return sqrt(var(sos(a, s), s));
}

Count in Range

#include <stdio.h>

int ctRng(int a[], int s, int lb, int ub) {
 int ct = 0;
 for (int i = 0; i < s; i++)
  if (a[i] >= lb && a[i] <= ub)
   ct++;
 return ct;
}

Convert Str Case

#include <stdio.h>
#include <string.h>

void cvtCaseStr(char str[], char op) {
 for (int i = 0; i < strlen(str); i++) {
  if ((op == 'u' && str[i] >= 'a' && str[i] <= 'z')
  || (op == 'l' && str[i] >= 'A' && str[i] <= 'Z'))
   str[i] = cvtCase(str[i]);
 }
}

Invert String

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char* invStr(char str[]) {
 int i = 0, j = strlen(str) - 1;
 char *n = malloc(j * sizeof(char) + sizeof(char));
 while (str[i] != '\0') {
  n[i] = str[j];
  j--; i++;
 }
 n[i] = '\0';
 return n;
}

Sort

#include <stdio.h>

void ias(int a[], int s, char d) {
for (int i = 0; i < s; i++) {
for (int j = i + 1; j < s; j++) {
 if (d == 'a')
  if (a[i] > a[j])
   swp(&a[i], &a[j]);
 if (d == 'd')
  if (a[i] < a[j])
   swp(&a[i], &a[j]);
}
}
}

Shift

#include <stdio.h>

void shftA(int a[], int s, char d) {
 int t;
 if (d == 'l') {
  t = a[0];
  for (int i = 0; i < s - 1; i++)
   a[i] = a[i + 1];
  a[s-1] = t;
 }
 if (d == 'r') {
  t = a[s - 1];
  for (int i = s - 1; i > 0; i--)
   a[i] = a[i - 1];
  a[0] = t;
 }
}

Shift sub Array

#include <stdio.h>

void shftSA(int a[], int s, char d, int beg) {
 if (d == 'l')
  for (int i = beg; i < s; i++)
   a[i] = a[i + 1];
 if (d == 'r')
  for (int i = s - 1; i > beg; i--)
   a[i] = a[i - 1];
}

Remove Element

#include <stdio.h>
#include <stdlib.h>

void rmElm(int a[], int *s, int l) {
 shftSA(a, *s, 'l', l);
 *s -= 1;
 realloc(a, *s * sizeof(int));
}

Add Element

#include <stdio.h>
#include <stdlib.h>

void addElm(int a[], int *s, int l, int v) {
 *s += 1;
 realloc(a, *s * sizeof(int));
 shftSA(a, *s, 'r', l);
 a[l] = v;
}

Int Search

#include <stdio.h>
#include <stdlib.h>

int* srchElm(int a[], int s, int v) {
 int* l = malloc(sizeof(int));
 l[0] = 0;
 for (int i = 0; i < s; i++)
  if (a[i] == v) {
   l[0]++;
   realloc(l, (l[0] + 1) * sizeof(int));
   l[l[0]] = i;
  }
 return l;
}

Array Generation

Word

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

char* genWord(int len, char op) {
 time_t t;
 srand((unsigned) time(&t));
 char *str = malloc(len * sizeof(char) + sizeof(char));
 for (int i = 0; i < len; i++) {
  if (op == 'u')
   str[i] = rand() % 26 + 65;
  if (op == 'l')
   str[i] = rand() % 26 + 97;
  if (op == 'm') {
   if (rand() % 2)
    str[i] = rand() % 26 + 97;
   else
    str[i] = rand() % 26 + 65;
  }
 }
 str[len] = '\0';
 return str;
}

Sentence

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

char* genSen(int len, char op) {
 time_t t;
 srand((unsigned) time(&t));
 char *str = malloc(len * sizeof(char) + sizeof(char));
 for (int i = 0; i < len; i++) {
  if (!(rand() % 6)) str[i] = ' ';
  else {
   if (op == 'u')
    str[i] = rand() % 26 + 65;
   if (op == 'l')
    str[i] = rand() % 26 + 97;
   if (op == 'm') {
    if (rand() % 2)
     str[i] = rand() % 26 + 97;
    else
     str[i] = rand() % 26 + 65;
   }
  }
 }
 str[len] = '\0';
 return str;
}

Str

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

char* genStr(int len) {
 time_t t;
 srand((unsigned) time(&t));
 char *str = malloc(len * sizeof(char) + sizeof(char));
 for (int i = 0; i < len; i++)
  str[i] = rand() % 95 + 32;
 str[len] = '\0';
 return str;
}

Int

#include <stdio.h>
#include <time.h>
#include <math.h>
#include <stdlib.h>

int* genDa(int len) {
 time_t t;
 srand((unsigned) time(&t));
 int *a = malloc(len * sizeof(int));
 for (int i = 0; i < len; i++) {
  a[i] = 0;
  switch (rand() % 4 + 1) {
   case 4:
    a[i] += rand() % 10;
    a[i] *= 10;
   case 3:
    a[i] += rand() % 10;
    a[i] *= 10;
   case 2:
    a[i] += rand() % 10;
    a[i] *= 10;
   case 1:
    a[i] += rand() % 10;
    break;
  }
  if (rand() % 2) a[i] *= -1;
 }
 return a;
}

Float

#include <stdio.h>
#include <time.h>
#include <math.h>
#include <stdlib.h>

float* genFa(int len) {
 time_t t;
 srand((unsigned) time(&t));
 float *a = malloc(len * sizeof(float));
 for (int i = 0; i < len; i++) {
  a[i] = 0;
  for (int j = 0; j < 6; j++) {
   a[i] += rand() % 10;
   a[i] *= 10;
  }
  a[i] += rand() % 10;
  a[i] /= pow(10, rand() % 7 + 1);
  if (rand() % 2) a[i] *= -1;
 }
 return a;
}

Double

#include <stdio.h>
#include <time.h>
#include <math.h>
#include <stdlib.h>

double* genLFa(int len) {
 time_t t;
 srand((unsigned) time(&t));
 double *a = malloc(len * sizeof(double));
 for (int i = 0; i < len; i++) {
  a[i] = 0;
  for (int j = 0; j < 14; j++) {
   a[i] += rand() % 10;
   a[i] *= 10;
  }
  a[i] += rand() % 10;
  a[i] /= pow(10, rand() % 15 + 1);
  if (rand() % 2) a[i] *= -1;
 }
 return a;
}