This repository has been archived by the owner on Dec 12, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #101 from Sprinter05/main
Mas ejercicios para el Boletín 4
- Loading branch information
Showing
15 changed files
with
863 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// SPDX-FileCopyrightText: 2024 Sprinter05 | ||
// | ||
// SPDX-License-Identifier: GPL-3.0-only | ||
|
||
// Libraries | ||
#include <stdio.h> | ||
#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<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"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// SPDX-FileCopyrightText: 2024 Sprinter05 | ||
// | ||
// SPDX-License-Identifier: GPL-3.0-only | ||
|
||
// Libraries | ||
#include <stdio.h> | ||
|
||
// 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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// SPDX-FileCopyrightText: 2024 Sprinter05 | ||
// | ||
// SPDX-License-Identifier: GPL-3.0-only | ||
|
||
// Libraries | ||
#include <stdio.h> | ||
#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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// SPDX-FileCopyrightText: 2024 Sprinter05 | ||
// | ||
// SPDX-License-Identifier: GPL-3.0-only | ||
|
||
// Libraries | ||
#include <stdio.h> | ||
#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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// SPDX-FileCopyrightText: 2024 Sprinter05 | ||
// | ||
// SPDX-License-Identifier: GPL-3.0-only | ||
|
||
// Libraries | ||
#include <stdio.h> | ||
#include <stdbool.h> | ||
#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<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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// SPDX-FileCopyrightText: 2024 Sprinter05 | ||
// | ||
// SPDX-License-Identifier: GPL-3.0-only | ||
|
||
// Libraries | ||
#include <stdio.h> | ||
#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<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; | ||
} |
Oops, something went wrong.