<repo_name>cstamper6/cToolbox<file_sep>/src/ordNumLister.c
/* C program to list ordinal numbers of given prompt */

#include <stdio.h>

void main() {

	int x;
	char * ord[20] = {"1st", "2nd", "3rd", "4th", "5th", "6th",
	"7th", "8th", "9th", "10th", "11th", "12th", "13th", "14th",
	"15th", "16th", "17th", "18th", "19th", "20th"};

	printf("Enter a number from 1 to 20:\n");
	scanf("%i", &x);

	if (x < 1 || x > 20) {
		printf("Number is not in the range from 1 to 20\n");
		return;
	} else {
		printf("Here are the first %i ordinal numbers:\n", x);
		for (int i = 0; i < x; i++) {
			printf("%s\n", ord[i]);
		}
	}
}
<file_sep>/src/matrixMult.c
/* C program to multiply two MxM matrices */

#include <stdio.h>

int A[10][10];
int B[10][10];
int C[10][10];

int main() {

  // scan in size of matrices

  int input;
  scanf("%i", &input);

  // store matrix values

  for (int i = 0; i < input; i++) {
    for (int j = 0; j < input; j++) {
      scanf("%i", &A[i][j]);
    }
  }

  for (int i = 0; i < input; i++) {
    for (int j = 0; j < input; j++) {
      scanf("%i", &B[i][j]);
    }
  }

  // Multiply matrix A with matrix B and store in C

  for (int i = 0; i < input; i++) {
    for (int j = 0; j < input; j++) {
      for (int k = 0; k < input; k++) {
      C[i][j] += A[i][k] * B[k][j];
      }
    }
  }

// print matrix C

  for (int i = 0; i < input; i++) {
    for (int j = 0; j < input; j++) {
      printf("%6d", C[i][j]);
    }
    printf("\n");
  }

}
<file_sep>/src/rhymeSequencer.c
/* C program to sequence rhymes into a story */

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

char animals[20][15];
char lyrics[20][60];
int  number;

void nurseryrhyme(int current) {

  printf("%*s", current, "");                   // print "current" number of spaces

  if (current == 0) {
    printf("There was an old lady who swallowed a %s;\n", animals[0]);
  } else {
    printf("She swallowed the %s to catch the %s;\n", animals[current-1], animals[current]);
  }

  if(current < number-1) {                      
    nurseryrhyme(current+1);
  } else {
    current++;
  }

  if (strcmp(animals[current], "END\n") == 0) {
    for (int i = number - 1; i >= 0; i--) {
      printf("%*sI don't know why she swallowed a %s - %s", i, "", animals[i], lyrics[i]);
    }
  }
}


int main() {
  int i=0;

  while (1) {
    fgets(animals[i], 15, stdin);               // reads the next animal name
    if (strcmp(animals[i], "END\n") == 0) {     // if it is "END\n" - done reading
      break;
    }

    int length = strlen(animals[i]);
    animals[i][length - 1] = '\0';              // strips the newline at the end
    fgets(lyrics[i], 60, stdin);                // reads the lyric corresponding to the animal

    i++;
  }

  number = i;

  nurseryrhyme(0);
}
<file_sep>/src/numFormatter.c
/* 
* C program to format given input by value 
* Values may be decimal, hex, or octal
*/

#include <stdio.h>

void main() {

	int input;
	int ints[6];

	printf("Enter six integers:\n");

	for (int i = 0; i < 6; i++) {
		scanf("%i", &input);
		ints[i] = input;
	}

	// prints header and first line of ints
	printf("1234567890bb1234567890\n%*i%*i\n", 10, ints[0], 12, ints[1]);
	// prints next two lines of ints
	printf("%*i%*i\n%*i%*i\n", 10, ints[2], 12, ints[3], 10, ints[4], 12, ints[5]);

}
<file_sep>/src/moddedText.c
/* C program to mod the text of the prompted input */

#include <stdio.h>
#include <string.h>
#define MAX 1024

int main () {

	char buf[MAX], c;
  	int len, length, i;


	do {
        	// read a line
		fgets(buf, MAX, stdin);
        	// calculate its length
		len = strlen(buf) -1;  // -1 for \n
        	// modify the line by switching characters
		if (len == 0) {  //exit upon  empty line
			break;
		}

		for (i=0; i<len; i++) {  //iterates and checks each letter then prints
			c = buf[i];
    			if (c == 'e'|| c == 'E') {
				c = '3';
			} if (c == 'i' || c == 'I') {
				c = '1';
			} if (c == 'o' || c == 'O') {
				c = '0';
			} if (c == 's' ||c == 'S') {
				c = '5';
			}
			printf("%c", c);
	        } 
		printf("\n");
  	} while (len > 1);
}
<file_sep>/src/binPatternGen.c
/* C program to generate all binary patterns of length of given input */

#include <stdio.h>

int array[10];
int size;


  void binaryPattern(int array[], int n) {

    if (n > 0) {
      array[size - n] = 0;
      binaryPattern(array, n - 1);
      array[size - n] = 1;
      binaryPattern(array, n - 1);
    } else {
      for (int i = 0; i < size; i++) {
        printf("%u", array[i]);
      }
      printf("\n");
    }
  }


void main() {

  int n;
  scanf("%i", &n);
  size = n;

  binaryPattern(array, n);
}
<file_sep>/README.md
# cToolbox
A variety of programs written in C that accomplish specific mathematical and analytical tasks.

COMP 411 - Computer Organization - Spring 2019

# What I Learned

* Transferable knowledge of a mid-level, structured programming language
* C's extendability and efficiency
* Ability of datatypes and powerful operators
<file_sep>/src/mazeSolver.c
/* C program to solve a maze */

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

#define true 1
#define false 0

#define MAX 100

int maze[100][100];             // 100x100 is the maximum size needed
int wasHere[100][100];
int correctPath[100][100];
int width, height;
int startX, startY, endX, endY;

int recursiveSolve(int x, int y);

int main() {

  int x, y;
  scanf("%d%d", &width, &height);
  scanf("\n");    // This is needed to "eat" the newline after height,
                  // before the actual maze entries begin on the next line

  char tempchar;

  for(y=0; y < height; y++) {
    for(x=0; x < width; x++) {
      scanf("%c", &tempchar);
      maze[y][x]=tempchar;

      if (tempchar == 'S') {
        startX = x;
        startY = y;
      } else if (tempchar == 'F') {
        endX = x;
        endY = y;
      }

      wasHere[y][x] = 0;
      correctPath[y][x] = 0;

    }
    scanf("\n");
  }

  recursiveSolve(startX, startY);

  // Code to print the output maze
  for (int i = 0; i < height; i++) {
    for (int j = 0; j < width;  j++) {
      if (correctPath[i][j] == 1 && maze[i][j] != 'S') {
        maze[i][j] = '.';
      }
      printf("%c", maze[i][j]);
    }
    printf("\n");
  }

}


int recursiveSolve(int x, int y) {

  if (x == endX && y == endY) return 1; // If you reached the end
  if (maze[y][x] == '*' || wasHere[y][x]) return 0;
  // If you are on a wall or already were here
  wasHere[y][x] = 1;
  if (x != 0) // Checks if not on left edge
      if (recursiveSolve(x-1, y)) { // Calls method one to the left
          correctPath[y][x] = 1; // Sets that path value to true;
          return 1;
        }
  if (x != width - 1) // Checks if not on right edge
      if (recursiveSolve(x+1, y)) { // Calls method one to the right
          correctPath[y][x] = 1;
          return 1;
      }
  if (y != 0)  // Checks if not on top edge
      if (recursiveSolve(x, y-1)) { // Calls method one up
          correctPath[y][x] = 1;
          return 1;
      }
  if (y != height - 1) // Checks if not on bottom edge
      if (recursiveSolve(x, y+1)) { // Calls method one down
          correctPath[y][x] = 1;
          return 1;
      }
  return 0;

}
<file_sep>/src/stringBubSort.c
/* C program to bubble sort strings in array WITHOUT use of C string library functions */

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

#define NUM 25   /* number of strings */
#define LEN 1000  /* max length of each string */


int my_compare_strings(char string1[], char string2[]) {

  /* 
  * -1 if string1 comes before string2 in alphabetical order
  * 0 if string1 is the same as string2
  * +1 if string1 comes after string2 in alphabetical order
  */

  int length;
  if (strlen(string1) > strlen(string2)) {
    length = strlen(string1);
  } else if (strlen(string2) > strlen(string1)) {
    length = strlen(string2);
  } else {
    length = strlen(string1);
  }

  for (int i = 0; i < length + 1; i++) {
    if (string1[i] == '\0' && string2[i] != '\0') {
      return -1;
    } else if (string2[i] == '\0' && string1[i] != '\0') {
      return 1;
    } else if (string1[i] < string2[i])  {
      return -1;
    } else if (string1[i] > string2[i]) {
      return 1;
    }
  }

  // if strings are the same
  return 0;
}

void my_swap_strings(char string1[], char string2[]) {
  char temp;    // char variable used in swapping one character at a time

  int length;
  if (strlen(string1) > strlen(string2)) {
    length = strlen(string1);
  } else if (strlen(string2) > strlen(string1)) {
    length = strlen(string2);
  } else {
    length = strlen(string1);
  }

  for (int i = 0; i < length + 1; i++) {
    temp = string1[i];
    string1[i] = string2[i];
    string2[i] = temp;
  }

}


int main()
{
  char Strings[NUM][LEN];

  printf("Please enter %d strings, one per line:\n", NUM);
  
  for (int i = 0; i < NUM; i++) {
    fgets(Strings[i], LEN, stdin);
  }

  puts("\nHere are the strings in the order you entered:");

  for (int i = 0; i < NUM; i++) {
    printf("%s", Strings[i]);
  }

  /* Bubble sort */
  /*
  * my_compare_strings() to compare two strings
  * If they are out of order, my_swap_strings() to swap their contents
  */

  for (int i = 0; i < NUM - 1; i++) {
    for (int j = 0; j < NUM - i - 1; j++) {
      if (my_compare_strings(Strings[j], Strings[j+1]) > 0) {
        my_swap_strings(Strings[j], Strings[j+1]);
      }
    }
  }

  /* Output sorted list */

  puts("In alphabetical order, the strings are:");

  for (int i = 0; i < NUM; i++) {
    printf("%s", Strings[i]);
  }

}
<file_sep>/src/triangularNum.c
/* C program to determine if the input is a triangular number */

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

void main() {

	int input;
	int tRoot;
	int tRootInt;
	int tNumber;

	printf("Number ?\n");
	scanf("%d", &input);

	tRoot = (sqrt(8*(input)+1)-1)/2;
        tRootInt = (int) tRoot;
        tNumber = (tRootInt*(tRootInt+1))/2;

	if (input == 0) {
		printf("Done\n");
		return;
	} else if (input == tNumber) {
		printf("%d is a triangular number\n", input);
		main();
	} else {
		printf("%d is not triangular, nearest triangular number below %d is %d\n", input, input, tNumber);
		main();
	}
}
<file_sep>/src/fpNumbers.c
/* C program to calculate various values of given input */

#include <stdio.h>

void main() {

	double input;
	double sum = 0;
	double min = TMP_MAX;
	double max = -TMP_MAX;
	double product = 1;

	printf("Enter 10 floating-point numbers:\n");

	for (int i = 0; i < 10; i++) {

		scanf("%lf", &input);
		sum += input;
		if (min > input) { min = input; }
		if (max < input) { max = input; }
		product *= input;
	}

	printf("Sum is %.5lf\nMin is %.5lf\nMax is %.5lf\nProduct is %.5lf\n", sum, min, max, product);
}
<file_sep>/src/matrixAdd.c
/* C program to find the sum of two matrices represented by 2-D arrays */

#include <stdio.h>

int main() {
  	int A[3][3];    // matrix A
  	int B[3][3];    // matrix B
  	int C[3][3];    // matrix to store their sum
	int i,j;

	puts("Please enter 9 values for matrix A:");
  	for (i=0;i<3;i++) {	//3 values for each row
		for (j=0;j<3;j++) {
			scanf("%d",&A[i][j]);  //gets values for matrix a
		}
	}
	puts("Please enter 9 values for matrix B:");
        for (i=0;i<3;i++) {     //3 values for each row
                for (j=0;j<3;j++) {
                        scanf("%d",&B[i][j]);  //gets values for matrix b
                }
        }

	for (i=0;i<3;i++) {
                for (j=0;j<3;j++) {
			C[i][j]=A[i][j]+B[i][j];  //sums for matrix c
		}
	}

	puts("C = B + A =");
	for (i=0;i<3;i++) {
		for (j=0;j<3;j++) {
			printf("%10d", C[i][j]);  //prints 3 per line
		} printf("\n");
	}

}
<file_sep>/src/collatzSequence.c
/* C program to compute the Collatz sequence of given input */

#include <stdio.h>

int main() {
	int n;

	puts("Please enter the starting number of the Collatz sequence:");
	scanf("%d", &n);

	printf("%d", n);
	do {
		if (n == 1) {
			printf("1");
			break;
		} else if (n%2 == 0) {
			n = n/2;
		} else {
			n = 3*n+1;
		}
		printf(", %d", n);
	} while (n > 1);
	printf("\n");
}
<file_sep>/src/relaxedPalindrome.c
/* 
* C program to determine if the prompted input is a palindrome
* Disregards capitals, spaces, and punctuation
*/

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define MAX 1000

int main()
{
  char text[MAX], c;
  int i;  //
  int ind, isPal;  //isPal=0=false; isPal=1=true

  puts("Type some text (then ENTER):");
  fgets(text, MAX, stdin);
  ind = strlen(text) - 2; // -1 for \n; -1 for 0 based index

  puts("Your input in reverse is:");
  for (i=ind; i>=0; i--) {  //index is 0 based
    printf("%c",text[i]);
    text[i] = tolower(text[i]);  //changes chars to lowercase after print
  }
  printf("\n");

  //  for (i=len-1;i>=

  i = 0;
  while (ind >= 0) {  //iterates backwards last char to first
    if (!isalpha(text[i])) { //skips char if not alpha; checks with the same j
      i++;
    } else if (!isalpha(text[ind])) {  //skips char; checks with same i
      ind--;
    } else if (text[i] == text[ind]) {
      i++;  //continues inwards to next char if match
      ind--;
      isPal = 1;  //bool for print
    } else {
      isPal = 0;
      break;
    }
  }
  if (isPal == 1) {
    printf("Found a palindrome!\n");
  }
}
