Exercise 5 draft
Added Exercise 5 but incomplete
This commit is contained in:
38
Informatik_I/Exercise_5/Task_2/functions.cpp
Normal file
38
Informatik_I/Exercise_5/Task_2/functions.cpp
Normal file
@@ -0,0 +1,38 @@
|
||||
// PRE: x > 0
|
||||
// POST: returns n^x
|
||||
int f1(int n, int x) {
|
||||
int res = 1;
|
||||
for (; x > 0; x--) {
|
||||
res *= n;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
// PRE: n > 0
|
||||
// POST: returns number of devisions by 10 with n
|
||||
int f2(int n) {
|
||||
int i = 0;
|
||||
while (n > 0) {
|
||||
n = n / 10;
|
||||
++i;
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
||||
// PRE: n > 1
|
||||
// POST: returns a bool to determine if n is prime
|
||||
bool f3(int n) {
|
||||
int i = 2;
|
||||
for (; n % i != 0; ++i);
|
||||
return n == i;
|
||||
}
|
||||
|
||||
// PRE: n is square number
|
||||
// POST: returns root of n
|
||||
int f4(int n) {
|
||||
int i = 0;
|
||||
while (i * i != n) {
|
||||
++i;
|
||||
}
|
||||
return i;
|
||||
}
|
Reference in New Issue
Block a user