diff --git a/docs/code/Boletin_4/Ejercicio_08.c b/docs/code/Boletin_4/Ejercicio_08.c new file mode 100644 index 0000000..56d7631 --- /dev/null +++ b/docs/code/Boletin_4/Ejercicio_08.c @@ -0,0 +1,37 @@ +// SPDX-FileCopyrightText: 2024 Sprinter05 +// +// SPDX-License-Identifier: GPL-3.0-only + +// Libraries +#include +#define N 10 + +int main(){ + // Define variables and ask for parameters + int nums[N], guess, lGuess, uGuess; + printf("\nInput an array of 10 elements by pressing Enter after each element: "); + for(int i=0; i<10; i++){ + scanf("%d", &nums[i]); + } + // Input guess + printf("\nWhat number do you want to find?: "); + scanf("%d", &guess); + // Lower bound + for(int i=0; i=0; i--){ + if(nums[i] == guess){ + uGuess = i; + break; + } + } + // Print solutions + printf("\nLower bound solution: %d", lGuess); + printf("\nUpper bound solution: %d", uGuess); + printf("\n"); +} \ No newline at end of file diff --git a/docs/code/Boletin_4/Ejercicio_11.c b/docs/code/Boletin_4/Ejercicio_11.c new file mode 100644 index 0000000..b1c4770 --- /dev/null +++ b/docs/code/Boletin_4/Ejercicio_11.c @@ -0,0 +1,31 @@ +// SPDX-FileCopyrightText: 2024 Sprinter05 +// +// SPDX-License-Identifier: GPL-3.0-only + +// Libraries +#include + +// Sum all the elements of the array using recursion +int arraySum(int n, int sumNums[n], int loops, int accum){ + if (loops == 0){return accum;} else{ + accum+=sumNums[n-loops]; + loops--; + arraySum(n, sumNums, loops, accum); + } +} + +int main(){ + // Define variables and ask for parameters + int n = 10; + int toSum[n], accum = 0, loops = n; + int result; + printf("Input the 10 numbers for the array separated by Enter:\n"); + for(int i=0; i<10; i++){ + scanf("%d", &toSum[i]); + } + // Recursion fun starts here + result = arraySum(n, toSum, loops, accum); + // Print result + printf("\nThe result of the sum is %d\n", result); + return 0; +} \ No newline at end of file diff --git a/docs/code/Boletin_4/Ejercicio_27.c b/docs/code/Boletin_4/Ejercicio_27.c new file mode 100644 index 0000000..8b53f23 --- /dev/null +++ b/docs/code/Boletin_4/Ejercicio_27.c @@ -0,0 +1,47 @@ +// SPDX-FileCopyrightText: 2024 Sprinter05 +// +// SPDX-License-Identifier: GPL-3.0-only + +// Libraries +#include +#define N 100 + +// Count the amount of characters in the string +void countString(char toCount[N]){ + int count = 0; + for(int i=0; toCount[i]!='\0'; i++){ + count++; + } + printf("Word %s has %d characters\n", toCount, count); +} + +// Separate the string and fix multiple separators in the string by ignoring them +void separateString(char toSplit[N]){ + char splitted[N][N]; + int count=0, row=0, column=0; + while(toSplit[count] != '\n'){ + if(toSplit[count] == ' ' || toSplit[count] == ',' || toSplit[count] == '.'){ + splitted[row][column] = '\0'; + column = 0; + if (toSplit[count-1] != ' ' && toSplit[count-1] != ',' && toSplit[count-1] != '.'){row++;} + } else { + splitted[row][column] = toSplit[count]; + column++; + } + count++; + } + for(int i=0; splitted[i][0] != '\0'; i++){ + countString(splitted[i]); + } +} + +int main(){ + // Define variables and ask for parameters + char sentence[N]; + printf("Type a sentence of max 100 characters:\n"); + // fgets instead of gets for buffer overflow protection + fgets(sentence, N, stdin); + // Start the fun + separateString(sentence); + return 0; +} \ No newline at end of file diff --git a/docs/code/Boletin_4/Ejercicio_28.c b/docs/code/Boletin_4/Ejercicio_28.c new file mode 100644 index 0000000..32c3299 --- /dev/null +++ b/docs/code/Boletin_4/Ejercicio_28.c @@ -0,0 +1,31 @@ +// SPDX-FileCopyrightText: 2024 Sprinter05 +// +// SPDX-License-Identifier: GPL-3.0-only + +// Libraries +#include +#define N 256 + +int main(){ + // Define variables and ask for parameters + int i=0, j=0; + char sentence[N], fixedSentence[N]; + printf("Type a sentence\n"); + // fgets instead of gets for buffer protection + fgets(sentence, N, stdin); + // Fix the sentence's multiple spaces + while(sentence[i]!='\0'){ + if(sentence[i] == ' ' && sentence[i+1] == ' '){ + i++; + continue; + } else { + fixedSentence[j]=sentence[i]; + i++; + j++; + } + } + // Print the result after apprending the end of string to the fixed variable + fixedSentence[j+1] = '\0'; + printf("\nThe sentence with fixed spaces is:\n%s", fixedSentence); + return 0; +} \ No newline at end of file diff --git a/docs/code/Boletin_4/Ejercicio_36.c b/docs/code/Boletin_4/Ejercicio_36.c new file mode 100644 index 0000000..74c3b32 --- /dev/null +++ b/docs/code/Boletin_4/Ejercicio_36.c @@ -0,0 +1,48 @@ +// SPDX-FileCopyrightText: 2024 Sprinter05 +// +// SPDX-License-Identifier: GPL-3.0-only + +// Libraries +#include +#include +#define N 100 + +void factorialDecomp(int toDecomp){ + int factors[N], primes=2, loops=0, og=toDecomp; + // Check if its 1 or 0 and just print out that the decomp is 1 + if(toDecomp == 0 | toDecomp == 1){ + printf("\nNumber %d factorial decomposition is: 1", og); + return; + } + // Repeat the factorization with each reduced number until its 0 + for(int i=0;i +#define N 11 +#define M 100 + +void checkLeaps(int n, int year){ + int closeLeap, leaps[M], counter=1; + //Check closest + for(int i=0;i<4;i++){ + if((year+i)%4 == 0){ + closeLeap = year+i; + break; + } + } + // Get all the following leap years until the max amount is satisfied + leaps[0]=closeLeap; + while(counter != 100){ + leaps[counter]=closeLeap+(4*counter); + counter++; + } + // Print results + printf("\nFollowing 100 leap years:\n"); + for(int i=0; i // Main library +#include // For managing the program +#include // Bool function +#include // Use UNIX commands +#include // For random numbers +#define N 16 // Change this number to change the matrix size +#define SPEED 2 // Higher means faster: MUST BE GREATER OR EQUAL THAN 1 +#define clear() printf("\033[H\033[J") // Clean screen + +// NOTE: RUN FROM VSCODE TERMINAL OR THE MODERN WINDOWS TERMINAL APP NOT CMD OTHERWISE UTF-8 EMOJIS WILL NOT DISPLAY +// ALSO MAKE SURE UTF-8 SUPPORT IS ENABLED IN YOUR TERMINAL OF CHOICE, APPLIES TO ALL OPERATING SYSTEMS + +int gameOfLife[N][N]; +int updatedGame[N][N]; +int ticks=0; // Generations + +// THIS IS THE MATRIX THAT WILL RUN IF THE OPTION c (choose) IS CHOSEN AT RUNTIME +// THIS EXAMPLE MATRIX IS OF SIZE 16X16 SO IF YOU CHANGE N MODIFY THE SIZE HERE TOO +// 0 = DISABLED, 1 = ENABLED +int chooseGameOfLife[N][N] = { + {1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, + {1,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0}, + {0,0,1,1,0,0,0,1,0,0,1,0,0,0,0,0}, + {0,0,1,1,0,0,0,1,0,0,1,0,0,0,0,0}, + {0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0}, + {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, + {0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0}, + {0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0}, + {0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0}, + {0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0}, + {0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0}, + {0,0,0,0,0,0,1,0,0,0,1,1,0,0,0,0}, + {0,0,1,0,0,1,0,0,0,0,1,0,1,0,0,0}, + {0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0}, + {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, + {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} +}; + +// Make sure the cell is not outsided of the matrix +bool outOfBound(int row, int column){ + bool oob; + if(row >= N || row < 0 || column >= N || column < 0){oob = true;} + else {oob = false;} + return oob; +} + +// Check rules 1, 2 and 3 for death status +void checkDeath(int row, int column){ + int liveNeighbours=0; + // Check neighbours + if(outOfBound(row+1,column) == false){if(gameOfLife[row+1][column] == 1){liveNeighbours++;}} + if(outOfBound(row+1,column+1) == false){if(gameOfLife[row+1][column+1] == 1){liveNeighbours++;}} + if(outOfBound(row+1,column-1) == false){if(gameOfLife[row+1][column-1] == 1){liveNeighbours++;}} + if(outOfBound(row-1,column) == false){if(gameOfLife[row-1][column] == 1){liveNeighbours++;}} + if(outOfBound(row-1,column+1) == false){if(gameOfLife[row-1][column+1] == 1){liveNeighbours++;}} + if(outOfBound(row-1,column-1) == false){if(gameOfLife[row-1][column-1] == 1){liveNeighbours++;}} + if(outOfBound(row,column+1) == false){if(gameOfLife[row][column+1] == 1){liveNeighbours++;}} + if(outOfBound(row,column-1) == false){if(gameOfLife[row][column-1] == 1){liveNeighbours++;}} + // Any live cell with fewer than two live neighbours dies, as if by underpopulation. + if(liveNeighbours<2){updatedGame[row][column]=0;} + // Any live cell with two or three live neighbours lives on to the next generation. + else if(liveNeighbours>=2 && liveNeighbours<=3){updatedGame[row][column]=1;} + // Any live cell with more than three live neighbours dies, as if by overpopulation. + else if(liveNeighbours>3){updatedGame[row][column]=0;} +} + +// Check rule 4 for revive status +void checkRevive(int row, int column){ + int liveNeighbours=0; + // Check neighbours + if(outOfBound(row+1,column) == false){if(gameOfLife[row+1][column] == 1){liveNeighbours++;}} + if(outOfBound(row+1,column+1) == false){if(gameOfLife[row+1][column+1] == 1){liveNeighbours++;}} + if(outOfBound(row+1,column-1) == false){if(gameOfLife[row+1][column-1] == 1){liveNeighbours++;}} + if(outOfBound(row-1,column) == false){if(gameOfLife[row-1][column] == 1){liveNeighbours++;}} + if(outOfBound(row-1,column+1) == false){if(gameOfLife[row-1][column+1] == 1){liveNeighbours++;}} + if(outOfBound(row-1,column-1) == false){if(gameOfLife[row-1][column-1] == 1){liveNeighbours++;}} + if(outOfBound(row,column+1) == false){if(gameOfLife[row][column+1] == 1){liveNeighbours++;}} + if(outOfBound(row,column-1) == false){if(gameOfLife[row][column-1] == 1){liveNeighbours++;}} + // Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction. + if(liveNeighbours==3){updatedGame[row][column]=1;} + // Remain dead + else {updatedGame[row][column]=0;} +} + +// Run a generation +void tick(){ + // Run generation + for(int i=0;i +#define N 10 + +int main(){ + // Define variables and ask for parameters + int nums[N], guess, lGuess, uGuess; + printf("\nInput an array of 10 elements by pressing Enter after each element: "); + for(int i=0; i<10; i++){ + scanf("%d", &nums[i]); + } + // Input guess + printf("\nWhat number do you want to find?: "); + scanf("%d", &guess); + // Lower bound + for(int i=0; i=0; i--){ + if(nums[i] == guess){ + uGuess = i; + break; + } + } + // Print solutions + printf("\nLower bound solution: %d", lGuess); + printf("\nUpper bound solution: %d", uGuess); + printf("\n"); +} \ No newline at end of file diff --git a/src/Boletin_4/Ejercicio_11.c b/src/Boletin_4/Ejercicio_11.c new file mode 100644 index 0000000..b1c4770 --- /dev/null +++ b/src/Boletin_4/Ejercicio_11.c @@ -0,0 +1,31 @@ +// SPDX-FileCopyrightText: 2024 Sprinter05 +// +// SPDX-License-Identifier: GPL-3.0-only + +// Libraries +#include + +// Sum all the elements of the array using recursion +int arraySum(int n, int sumNums[n], int loops, int accum){ + if (loops == 0){return accum;} else{ + accum+=sumNums[n-loops]; + loops--; + arraySum(n, sumNums, loops, accum); + } +} + +int main(){ + // Define variables and ask for parameters + int n = 10; + int toSum[n], accum = 0, loops = n; + int result; + printf("Input the 10 numbers for the array separated by Enter:\n"); + for(int i=0; i<10; i++){ + scanf("%d", &toSum[i]); + } + // Recursion fun starts here + result = arraySum(n, toSum, loops, accum); + // Print result + printf("\nThe result of the sum is %d\n", result); + return 0; +} \ No newline at end of file diff --git a/src/Boletin_4/Ejercicio_27.c b/src/Boletin_4/Ejercicio_27.c new file mode 100644 index 0000000..8b53f23 --- /dev/null +++ b/src/Boletin_4/Ejercicio_27.c @@ -0,0 +1,47 @@ +// SPDX-FileCopyrightText: 2024 Sprinter05 +// +// SPDX-License-Identifier: GPL-3.0-only + +// Libraries +#include +#define N 100 + +// Count the amount of characters in the string +void countString(char toCount[N]){ + int count = 0; + for(int i=0; toCount[i]!='\0'; i++){ + count++; + } + printf("Word %s has %d characters\n", toCount, count); +} + +// Separate the string and fix multiple separators in the string by ignoring them +void separateString(char toSplit[N]){ + char splitted[N][N]; + int count=0, row=0, column=0; + while(toSplit[count] != '\n'){ + if(toSplit[count] == ' ' || toSplit[count] == ',' || toSplit[count] == '.'){ + splitted[row][column] = '\0'; + column = 0; + if (toSplit[count-1] != ' ' && toSplit[count-1] != ',' && toSplit[count-1] != '.'){row++;} + } else { + splitted[row][column] = toSplit[count]; + column++; + } + count++; + } + for(int i=0; splitted[i][0] != '\0'; i++){ + countString(splitted[i]); + } +} + +int main(){ + // Define variables and ask for parameters + char sentence[N]; + printf("Type a sentence of max 100 characters:\n"); + // fgets instead of gets for buffer overflow protection + fgets(sentence, N, stdin); + // Start the fun + separateString(sentence); + return 0; +} \ No newline at end of file diff --git a/src/Boletin_4/Ejercicio_28.c b/src/Boletin_4/Ejercicio_28.c new file mode 100644 index 0000000..32c3299 --- /dev/null +++ b/src/Boletin_4/Ejercicio_28.c @@ -0,0 +1,31 @@ +// SPDX-FileCopyrightText: 2024 Sprinter05 +// +// SPDX-License-Identifier: GPL-3.0-only + +// Libraries +#include +#define N 256 + +int main(){ + // Define variables and ask for parameters + int i=0, j=0; + char sentence[N], fixedSentence[N]; + printf("Type a sentence\n"); + // fgets instead of gets for buffer protection + fgets(sentence, N, stdin); + // Fix the sentence's multiple spaces + while(sentence[i]!='\0'){ + if(sentence[i] == ' ' && sentence[i+1] == ' '){ + i++; + continue; + } else { + fixedSentence[j]=sentence[i]; + i++; + j++; + } + } + // Print the result after apprending the end of string to the fixed variable + fixedSentence[j+1] = '\0'; + printf("\nThe sentence with fixed spaces is:\n%s", fixedSentence); + return 0; +} \ No newline at end of file diff --git a/src/Boletin_4/Ejercicio_36.c b/src/Boletin_4/Ejercicio_36.c new file mode 100644 index 0000000..74c3b32 --- /dev/null +++ b/src/Boletin_4/Ejercicio_36.c @@ -0,0 +1,48 @@ +// SPDX-FileCopyrightText: 2024 Sprinter05 +// +// SPDX-License-Identifier: GPL-3.0-only + +// Libraries +#include +#include +#define N 100 + +void factorialDecomp(int toDecomp){ + int factors[N], primes=2, loops=0, og=toDecomp; + // Check if its 1 or 0 and just print out that the decomp is 1 + if(toDecomp == 0 | toDecomp == 1){ + printf("\nNumber %d factorial decomposition is: 1", og); + return; + } + // Repeat the factorization with each reduced number until its 0 + for(int i=0;i +#define N 11 +#define M 100 + +void checkLeaps(int n, int year){ + int closeLeap, leaps[M], counter=1; + //Check closest + for(int i=0;i<4;i++){ + if((year+i)%4 == 0){ + closeLeap = year+i; + break; + } + } + // Get all the following leap years until the max amount is satisfied + leaps[0]=closeLeap; + while(counter != 100){ + leaps[counter]=closeLeap+(4*counter); + counter++; + } + // Print results + printf("\nFollowing 100 leap years:\n"); + for(int i=0; i // Main library +#include // For managing the program +#include // Bool function +#include // Use UNIX commands +#include // For random numbers +#define N 16 // Change this number to change the matrix size +#define SPEED 2 // Higher means faster: MUST BE GREATER OR EQUAL THAN 1 +#define clear() printf("\033[H\033[J") // Clean screen + +// NOTE: RUN FROM VSCODE TERMINAL OR THE MODERN WINDOWS TERMINAL APP NOT CMD OTHERWISE UTF-8 EMOJIS WILL NOT DISPLAY +// ALSO MAKE SURE UTF-8 SUPPORT IS ENABLED IN YOUR TERMINAL OF CHOICE, APPLIES TO ALL OPERATING SYSTEMS + +int gameOfLife[N][N]; +int updatedGame[N][N]; +int ticks=0; // Generations + +// THIS IS THE MATRIX THAT WILL RUN IF THE OPTION c (choose) IS CHOSEN AT RUNTIME +// THIS EXAMPLE MATRIX IS OF SIZE 16X16 SO IF YOU CHANGE N MODIFY THE SIZE HERE TOO +// 0 = DISABLED, 1 = ENABLED +int chooseGameOfLife[N][N] = { + {1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, + {1,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0}, + {0,0,1,1,0,0,0,1,0,0,1,0,0,0,0,0}, + {0,0,1,1,0,0,0,1,0,0,1,0,0,0,0,0}, + {0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0}, + {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, + {0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0}, + {0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0}, + {0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0}, + {0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0}, + {0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0}, + {0,0,0,0,0,0,1,0,0,0,1,1,0,0,0,0}, + {0,0,1,0,0,1,0,0,0,0,1,0,1,0,0,0}, + {0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0}, + {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, + {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} +}; + +// Make sure the cell is not outsided of the matrix +bool outOfBound(int row, int column){ + bool oob; + if(row >= N || row < 0 || column >= N || column < 0){oob = true;} + else {oob = false;} + return oob; +} + +// Check rules 1, 2 and 3 for death status +void checkDeath(int row, int column){ + int liveNeighbours=0; + // Check neighbours + if(outOfBound(row+1,column) == false){if(gameOfLife[row+1][column] == 1){liveNeighbours++;}} + if(outOfBound(row+1,column+1) == false){if(gameOfLife[row+1][column+1] == 1){liveNeighbours++;}} + if(outOfBound(row+1,column-1) == false){if(gameOfLife[row+1][column-1] == 1){liveNeighbours++;}} + if(outOfBound(row-1,column) == false){if(gameOfLife[row-1][column] == 1){liveNeighbours++;}} + if(outOfBound(row-1,column+1) == false){if(gameOfLife[row-1][column+1] == 1){liveNeighbours++;}} + if(outOfBound(row-1,column-1) == false){if(gameOfLife[row-1][column-1] == 1){liveNeighbours++;}} + if(outOfBound(row,column+1) == false){if(gameOfLife[row][column+1] == 1){liveNeighbours++;}} + if(outOfBound(row,column-1) == false){if(gameOfLife[row][column-1] == 1){liveNeighbours++;}} + // Any live cell with fewer than two live neighbours dies, as if by underpopulation. + if(liveNeighbours<2){updatedGame[row][column]=0;} + // Any live cell with two or three live neighbours lives on to the next generation. + else if(liveNeighbours>=2 && liveNeighbours<=3){updatedGame[row][column]=1;} + // Any live cell with more than three live neighbours dies, as if by overpopulation. + else if(liveNeighbours>3){updatedGame[row][column]=0;} +} + +// Check rule 4 for revive status +void checkRevive(int row, int column){ + int liveNeighbours=0; + // Check neighbours + if(outOfBound(row+1,column) == false){if(gameOfLife[row+1][column] == 1){liveNeighbours++;}} + if(outOfBound(row+1,column+1) == false){if(gameOfLife[row+1][column+1] == 1){liveNeighbours++;}} + if(outOfBound(row+1,column-1) == false){if(gameOfLife[row+1][column-1] == 1){liveNeighbours++;}} + if(outOfBound(row-1,column) == false){if(gameOfLife[row-1][column] == 1){liveNeighbours++;}} + if(outOfBound(row-1,column+1) == false){if(gameOfLife[row-1][column+1] == 1){liveNeighbours++;}} + if(outOfBound(row-1,column-1) == false){if(gameOfLife[row-1][column-1] == 1){liveNeighbours++;}} + if(outOfBound(row,column+1) == false){if(gameOfLife[row][column+1] == 1){liveNeighbours++;}} + if(outOfBound(row,column-1) == false){if(gameOfLife[row][column-1] == 1){liveNeighbours++;}} + // Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction. + if(liveNeighbours==3){updatedGame[row][column]=1;} + // Remain dead + else {updatedGame[row][column]=0;} +} + +// Run a generation +void tick(){ + // Run generation + for(int i=0;i