Skip to content
This repository has been archived by the owner on Dec 31, 2024. It is now read-only.

Commit

Permalink
Added docs code
Browse files Browse the repository at this point in the history
  • Loading branch information
Sprinter05 committed Mar 7, 2024
1 parent b014427 commit a7c73e1
Show file tree
Hide file tree
Showing 7 changed files with 443 additions and 0 deletions.
40 changes: 40 additions & 0 deletions docs/code/Boletin_4/Ejercicio_08.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// SPDX-FileCopyrightText: 2024 Sprinter05
//
// SPDX-License-Identifier: GPL-3.0-only
#include <stdio.h>
#define N 10

/*Design and implement a program in C language, by sequential search allow
finding the lowest and the highest index were a given X can be found into an
array of integers. The program should allow the user to properly enter the array
of integers and ask for the value you want to find.*/

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<N; i++){
if(nums[i] == guess){
lGuess = i;
break;
}
}
// Upper bound
for(int i=N; 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");
}
32 changes: 32 additions & 0 deletions docs/code/Boletin_4/Ejercicio_11.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// SPDX-FileCopyrightText: 2024 Sprinter05
//
// SPDX-License-Identifier: GPL-3.0-only
#include <stdio.h>

/*Design and implement a recursive function in C language that returns
the sum of the elements of an array of integers.*/

// 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;
}
50 changes: 50 additions & 0 deletions docs/code/Boletin_4/Ejercicio_27.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// SPDX-FileCopyrightText: 2024 Sprinter05
//
// SPDX-License-Identifier: GPL-3.0-only
#include <stdio.h>
#define N 100

/*Design and implement a program in C language to read a sentence
(maximum 100 characters) and then showing every word of it followed
by the number of letters that compose it. For simplicity, assume that
the words can be separated only by spaces, commas or periods.*/

// 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;
}
33 changes: 33 additions & 0 deletions docs/code/Boletin_4/Ejercicio_28.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// SPDX-FileCopyrightText: 2024 Sprinter05
//
// SPDX-License-Identifier: GPL-3.0-only
#include <stdio.h>
#define N 256

/*Design and implement a program in C language to read a phrase (sequence of
characters to end of line), replace all sequences of two or more spaces with
a single space and the show the modified phrase.*/

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;
}
50 changes: 50 additions & 0 deletions docs/code/Boletin_4/Ejercicio_36.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// SPDX-FileCopyrightText: 2024 Sprinter05
//
// SPDX-License-Identifier: GPL-3.0-only
#include <stdio.h>
#include <stdbool.h>
#define N 100

/*Implement a program that ask for five integers (ni > 0) and display each number and
factorial decomposition. Use a suitable data structure to store both the numbers
and decompositions.*/

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<N;i++){factors[i]=0;}
while(toDecomp != 1){
if(toDecomp%primes == 0){
toDecomp/=primes;
factors[loops]=primes;
loops++;
primes=2;
} else {primes++;}
}
// Print the result
printf("\nNumber %d factorial decomposition is: ", og);
for(int i=0;factors[i]!=0;i++){
if(factors[i+1]==0){
printf("%d", factors[i]);
} else {printf("%d*", factors[i]);}
}
}

int main(){
// Define variables and ask for parameters
int decompose[5];
printf("Type 5 integers separated by spaces:\n");
// Its just 5 parameters no for loop needed here
scanf("%d %d %d %d %d", &decompose[0], &decompose[1], &decompose[2], &decompose[3], &decompose[4]);
// Repeat the decomp for all numbers
for(int i=0;i<5;i++){
factorialDecomp(decompose[i]);
}
printf("\n");
return 0;
}
51 changes: 51 additions & 0 deletions docs/code/Boletin_4/Ejercicio_37.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// SPDX-FileCopyrightText: 2024 Sprinter05
//
// SPDX-License-Identifier: GPL-3.0-only
#include <stdio.h>
#define N 11
#define M 100

/*Implement a program that reads a date and store it in a string that
fit your 10 characters (dd/mm/yyyy) adding zeros to the left of the day,
month and year if needed, and calculate and store in memory the next 100 leap years.
The program should give the user the option to change the date entered and
see on the screen the n leap years from the given date.*/

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<M; i++){
printf("%d ", leaps[i]);
}
printf("\n\n");
}

int main(){
// Define variables
char date[N];
int day, month, year;
// Endless loop so the user can change parameters (1==1)
while(1){
// Ask for parameters
printf("Input a date in the dd/mm/yyyy format: ");
scanf("%d/%d/%d", &day, &month, &year);
// Fix the string so it can be passed to the function more easily
snprintf(date, N, "%02d/%02d/%04d", day, month, year);
checkLeaps(100, year);
}
return 0;
}
Loading

0 comments on commit a7c73e1

Please sign in to comment.