17 Commits

Author SHA1 Message Date
SinusFox 0bf25a3967 Merge pull request #8 from SinusFox/task-8
Task 8
2023-06-06 01:30:27 -07:00
SinusFox eb1bade54a Lottery: 6 of 49 2023-06-06 10:29:16 +02:00
SinusFox 662edb7b2e task 9 2023-06-06 08:25:30 +02:00
SinusFox 5c0b4baf2b removed pointer 2023-05-31 09:29:44 +02:00
SinusFox e1417143f2 coding task 8 2023-05-31 09:02:17 +02:00
SinusFox 0b44293624 Merge pull request #7 from SinusFox/task7-header-file
added header file
2023-05-30 01:31:07 -07:00
SinusFox 088c70fef0 added header file 2023-05-30 10:16:44 +02:00
SinusFox 5779ad76ee Merge pull request #6 from SinusFox/tasks-6-and-7
Tasks 6 and 7
2023-05-30 00:38:40 -07:00
SinusFox 8c5a52eedd task 7 2023-05-30 09:37:47 +02:00
SinusFox 0ceddeb021 task 6 2023-05-30 09:37:38 +02:00
SinusFox a02beb7847 added rounding 2023-05-11 16:35:10 +02:00
SinusFox dacd6e6901 Merge pull request #5 from SinusFox/exercise-4
Exercise 4 and 5
2023-05-10 11:54:37 +02:00
SinusFox c06531f315 exercise 5 2023-05-10 11:26:57 +02:00
SinusFox 9744b306ce exercise 4 2023-05-10 10:28:11 +02:00
SinusFox 0a6014c1da Merge pull request #4 from SinusFox/exercise-3
Exercise 3
2023-05-10 10:17:10 +02:00
SinusFox ea2aef7ed1 fix: modulo by zero 2023-05-10 09:42:58 +02:00
SinusFox 3409df4e15 added exercise 3 2023-05-10 09:35:11 +02:00
13 changed files with 382 additions and 10 deletions
+100
View File
@@ -0,0 +1,100 @@
#include "10_Lotto.h"
int main() {
// vars
int usr_input[ARRLENGTH] = {0}, ran_nums[ARRLENGTH] = {0}, amnt_correct;
// input
for (int i = 0; i < ARRLENGTH; i++) {
usr_input[i] = Input(usr_input);
}
// initializing number generator
Initialize();
// fill lottery array
RanArray_Fill(ran_nums);
// check for correct guesses and writes the num of correct guesses in amnt_correct
amnt_correct = CheckGuess(usr_input, ran_nums);
// output
Output(amnt_correct);
// debugging
for (int i = 0; i < ARRLENGTH; i++) // output user input
{
printf("\nusr_in %i: %i", i, usr_input[i]);
}
for (int i = 0; i < ARRLENGTH; i++) // output random numbers
{
printf("\nran_num %i: %i", i, ran_nums[i]);
}
return 0;
}
void Initialize() {
time_t t;
srand(time(&t));
}
int Input(int usr_input[]) {
int input;
while (1) {
printf("\nPlease type in a number: ");
scanf("%i", &input); // user input number
if (CheckNotInNums(usr_input, input) && ValidNum(input)) {
break; // returns num if accectable
}
printf("\nNumber is not allowed."); // error: invalid number
}
return input;
}
void Output(int amnt_correct) {
printf("\nYou got %i numbers right!", amnt_correct);
}
int CheckGuess(int usr_in[], int ran_nums[]) {
int count = 0;
for (int i = 0; i < ARRLENGTH; i++) // iterating through usr_in
{
if (CheckNotInNums(ran_nums, usr_in[i]) == 0) { // checks and counts how often each num of usr_in is in ran_nums
count++;
continue;
}
}
return count; // returns amount of right guesses
}
void RanArray_Fill(int arr[]) {
int random_number;
for (int i = 0; i < ARRLENGTH; i++) { // fill array with random values from 1 to 49
while (1) {
random_number = RanNum();
if (CheckNotInNums(arr, random_number)) { // check if random number doesn't exist yet
arr[i] = random_number; // if check successfull: writes random number in array
break;
}
}
}
}
int RanNum() {
return (rand() % 49) + 1; // returns random values from 1 to 49
}
int CheckNotInNums(int arr[], int num) {
for (int i = 0; i < ARRLENGTH; i++) {
if (arr[i] == num) return 0; // returns 0 if num exists already
}
return 1; // returns 1 if num doesn't exist yet
}
int ValidNum(int input) {
if (input > 0 && input < 50) return 1; // returns 1 if input is between 0 and 50, thus valid
return 0; // returns 0 if input is NOT between 0 and 50, thus invalid
}
+16
View File
@@ -0,0 +1,16 @@
// includes
#include <stdio.h>
#include <time.h>
// defines
#define ARRLENGTH 6
// function declarations
void Initialize();
int Input(int[]);
void Output(int);
int CheckGuess(int[], int[]);
void RanArray_Fill(int[]);
int RanNum();
int CheckNotInNums(int[], int);
int ValidNum(int);
+2 -9
View File
@@ -1,14 +1,7 @@
#include <stdio.h> #include <stdio.h>
int main() { int main() {
int i = 70; int j = 1;
printf("Printing %i,\nalthough printing foxes is superior.", j);
printf("Printing ");
for (unsigned int j = i; j < 89; j += 9) {
printf("%c", j);
}
printf(", Since they are better :P\n");
return 0; return 0;
} }
+32
View File
@@ -0,0 +1,32 @@
#include <stdio.h>
int main() {
// creating variables
int first = 0, second = 0, addition = 0, subtraction = 0, multiplication = 0, modulo = 0;
double division = 0;
// receiving numbers from user
printf("Please type in the first integer: ");
scanf("%i", &first);
printf("Please type in the second integer: ");
scanf("%i", &second);
// checking numbers
printf("Your input: %i and %i\nCalculating...\n\n", first, second);
// calculating
addition = first + second;
subtraction = first - second;
multiplication = first * second;
if (second != 0) {
division = first / second;
modulo = first % second;
} else {
printf("\nError: Dividing by 0 is not allowed. Output will be 0 for devision and modulo.\n\n");
}
// output of calculation
printf("The results are:\n+ = %i\n- = %i\n* = %i\n/ = %f\nmod = %i", addition, subtraction, multiplication, division, modulo);
return 0;
}
@@ -0,0 +1,33 @@
/* Extended means using doubles in this case, so floating point numbers can be used. Thus,
the modulo operation is not usable anymore and has been removed. */
#include <stdio.h>
int main() {
// creating variables
double first = 0, second = 0, addition = 0, subtraction = 0, multiplication = 0, division = 0;
// receiving numbers from user
printf("Please type in the first integer: ");
scanf("%lf", &first);
printf("Please type in the second integer: ");
scanf("%lf", &second);
// checking numbers
printf("Your input: %lf and %lf\nCalculating...\n\n", first, second);
// calculating
addition = first + second;
subtraction = first - second;
multiplication = first * second;
if (second != 0) {
division = first / second;
} else {
printf("\nError: Dividing by 0 is not allowed. Output will be 0 for devision and modulo.\n\n");
}
// output of calculation
printf("The results are:\n+ = %lf\n- = %lf\n* = %lf\n/ = %f\n", addition, subtraction, multiplication, division);
return 0;
}
@@ -0,0 +1,56 @@
#include <stdio.h>
#include <math.h>
int main() {
// variables
double amount = 0, usd = 1.09643, jpy = 148.33512; // eur = 1 as base currency
char currentCurrency = 0, targetCurrency;
// input
printf("1 EUR\n2 USD\n3 JPY\n\nPlease type in the current currency: ");
scanf("%i", &currentCurrency);
printf("Please type in the amount: ");
scanf("%lf", &amount);
printf("\n1 EUR\n2 USD\n3 JPY\n\nPlease type in the target currency: ");
scanf("%i", &targetCurrency);
// check for valid input
if (currentCurrency < 1 || currentCurrency > 4 || targetCurrency < 1 || targetCurrency > 4) {
printf("Invalid currencies.");
return 1;
}
// calculating
if (currentCurrency != targetCurrency) {
// converting to base currency
switch (currentCurrency) {
case 2:
amount /= usd;
break;
case 3:
amount /= jpy;
break;
default:
break;
}
// converting to target currency
switch (targetCurrency)
{
case 2:
amount *= usd;
break;
case 3:
amount *= jpy;
default:
break;
}
}
// rounding fun
amount = round((amount*100))/100;
// output
printf("Amount in target currency is: %lf", amount);
return 0;
}
@@ -0,0 +1,58 @@
#include <stdio.h>
void out(double* number) {
printf("The result is %lf.", *number);
}
int main() {
// vars
double numOne = 0, numTwo = 0, numOut = 0;
char cancel, operation;
// main loop
do {
// input for calculation
printf("Please type in the first number: ");
scanf("%lf", &numOne);
fflush(stdin);
printf("\nPlease type in the operation (+/-/*/:): ");
scanf("%c", &operation);
fflush(stdin);
printf("Please type in the second number: ");
scanf("%lf", &numTwo);
fflush(stdin);
// calculations
switch (operation) {
case '+': // add
numOut = numOne + numTwo;
out(&numOut);
break;
case '-': // subtract
numOut = numOne - numTwo;
out(&numOut);
break;
case '*': // multiply
numOut = numOne * numTwo;
out(&numOut);
break;
case ':': // divide
if (numTwo != 0) {
numOut = numOne / numTwo;
out(&numOut);
} else {
printf("\nSecond number can't be 0.");
}
break;
default: // other -> invalid operation
printf("\nInvalid operation.");
}
// input 'q' if programm should be exited
printf("\nWould you like to enter another calculation? (any other key = continue, q = quit): ");
scanf("%c", &cancel);
fflush(stdin);
} while (cancel != 'q'); // end of do-while-loop
return 0;
}
+13
View File
@@ -0,0 +1,13 @@
#include "7_Matrix_1x1.h"
int main() {
// printing the values as matrix
for (int i = 1; i <= UPTO; i++) { // y axis
for (int j = 1; j <= UPTO; j++) { // x axis
printf("%5i", (i*j)); // printing each value, i*j = the value
}
printf("\n"); // end of line on x axis
}
return 0;
}
+3
View File
@@ -0,0 +1,3 @@
#include <stdio.h>
#define UPTO 20 // maximum value
+30
View File
@@ -0,0 +1,30 @@
#include "8_Power_Calculator.h"
void main() {
unsigned long base, power;
// input
printf("Please type in the base: ");
scanf("%i", &base);
printf("\nPlease type in the power: ");
scanf("%i", &power);
// calculate
base = Calculate(base, power);
// output
printf("\nThe number is: %i.", base);
return 0;
}
unsigned long Calculate(unsigned long base, unsigned long power) {
unsigned long baseOriginal = base;
if (power == 0) return 1; // power zero
if (power > 0) { // power positive
for (unsigned int i = 1; i < power; i++) {
base *= baseOriginal;
}
}
return base;
}
+5
View File
@@ -0,0 +1,5 @@
// libraries
#include <stdio.h>
//functions
unsigned long Calculate(unsigned long, unsigned long);
+30
View File
@@ -0,0 +1,30 @@
// Bubblesort
#include "9_Num_Sort.h"
int main() {
// vars
int nums[MAXARR] = {12,15,17,6,2,4,7,6,3,10};
// sort
for (int max = sizeof(nums)/4 - 1; max >= 0; max--) {
//vars
int temp = 0;
// main loop
for (int i = 0; i < sizeof(nums)/4 - 1; i++) { // sizeof -> bytes, so divided by type size; -1 to avoid overflow
if (nums[i] > nums[i + 1]) {
temp = nums[i]; // switching the numbers
nums[i] = nums[i + 1];
nums[i + 1] = temp;
}
}
}
// output
for (int i = 0; i < sizeof(nums)/4; i++) { // sizeof -> bytes, so divided by type size; -1 to avoid overflow
printf("%i\n", nums[i]);
}
return 0;
}
+3
View File
@@ -0,0 +1,3 @@
#include <stdio.h>
#define MAXARR 10