This commit is contained in:
Leon Wilzer 2022-11-06 20:06:28 +01:00
commit b1e513b102
5 changed files with 243 additions and 0 deletions

2
.gitignore vendored
View File

@ -32,3 +32,5 @@
*.out
*.app
# VSC
.vscode

View File

@ -1,5 +1,6 @@
setup:
mkdir build
<<<<<<< HEAD
sheet0:
g++ src/sheet0.cpp -o ./build/sheet0
chmod +x build/sheet0
@ -9,3 +10,13 @@ pizza:
g++ src/pizza.cpp -o ./build/pizza
chmod +x build/pizza
build/pizza
=======
sheet00:
g++ src/sheet00.cpp -o ./build/sheet00
chmod +x build/sheet00
build/sheet00
sheet01:
g++ src/sheet01.cpp -o ./build/sheet01
chmod +x build/sheet01
build/sheet01
>>>>>>> 909c958f6aa3055932860f292e9dcf553a1965af

BIN
build/sheet01 Executable file

Binary file not shown.

230
src/sheet01.cpp Normal file
View File

@ -0,0 +1,230 @@
#include <iostream>
#include <string>
#include <cmath>
void a1()
{
int n;
std::cout << "Enter an integer: ";
std::cin >> n;
std::cout << (n >= 0 && n <= 100) << '\n';
}
void b1()
{
int n;
std::cout << "Enter an integer: ";
std::cin >> n;
if(n>0)
{
std::cout << n << " is greater than 0.\n";
if(n%4==0)
{
std::cout << n << " is also divisible by 4.\n";
if(n%3==0)
{
std::cout << n << " is also divisible by 3.\n";
}
else
{
std::cout<< n << " is not divisible by 3.\n";
}
}
else
{
std::cout << n << " is not divisble by 4.\n";
}
}
else
{
std::cout << n << " is not greater than 0.\n";
}
}
void abcd2()
{
std::string dna;
std::cout << "Enter a String: ";
std::cin >> dna;
int g = 0;
int a = 0;
int c = 0;
int t = 0;
int G = 0;
int A = 0;
int C = 0;
int T = 0;
for(char base : dna)
{
switch(base)
{
case 'G':
G++;
break;
case 'g':
g++;
break;
case 'A':
A++;
break;
case 'a':
a++;
break;
case 'C':
C++;
break;
case 'c':
c++;
break;
case 'T':
T++;
break;
case 't':
t++;
break;
default:
std::cout << "Error; Invalid Base: " << base << "\n";
}
}
std::cout << "g: " << g << " a: " << a << " c: " << c << " t: " << t << "\n";
std::cout << "G: " << G << " A: " << A << " C: " << C << " T: " << T << "\n";
/*
d)
A switch should be used, when checking if a variable has one of many possible values.
*/
}
void ab3()
{
const long double from = 0.0;
const long double to = 1.0;
long double integral_val = 0.0;
long double x = from;
const size_t N = 1000000;
const long double step_width = std::abs(from - to) / static_cast<long double>(N);
for (size_t n = 0; n < N; ++n) {
//integral_val += 4 / (1 + x * x);
//a: integral_val += 3*x*x;
//b: integral_val += 2* std::sqrt(x);
x += step_width;
}
integral_val /= N;
std::cout << integral_val << '\n';
}
void c3()
{
int sum = 0;
for(int k=1; k<=100; k++)
{
sum += k;
}
std::cout << sum << '\n';
}
void d3()
{
int sum = 0;
for(int i=1; i<=10; i++)
{
for(int j=1; j<=10; j++)
{
sum += i;
}
}
std::cout << sum << '\n';
}
void abc4()
{
for(int x=0; x<10; x++)
{
for(int y=0; y<10; y++)
{
std::cout << '#';
}
std::cout << '\n';
}
std::cout << '\n';
for(int x=0; x<10; x++)
{
for(int y=0; y<10; y++)
{
if(y==0 || y==9 || x==0 || x==9)
{
std::cout << '#';
}
else
{
std::cout << ' ';
}
}
std::cout << '\n';
}
std::cout << '\n';
for(int x=0; x<10; x++)
{
for(int y=0; y<10; y++)
{
if(x==y || y==0 || y==9 || x==0 || x==9)
{
std::cout << '#';
}
else
{
std::cout << ' ';
}
}
std::cout << '\n';
}
std::cout << '\n';
}
int main()
{
int selection;
std::cout << "1: 1a\n2: 1b\n3: 2abcd\n4: 3ab\n5: 3c\n6: 3d\n7: 4abc\n";
std::cout << "Please select an Exercise: ";
std::cin >> selection;
switch(selection)
{
case 1:
a1();
break;
case 2:
b1();
break;
case 3:
abcd2();
break;
case 4:
ab3();
break;
case 5:
c3();
break;
case 6:
d3();
break;
case 7:
abc4();
break;
default:
a1();
b1();
abcd2();
ab3();
c3();
d3();
}
return 0;
}