diff --git a/array.cpp b/array.cpp new file mode 100644 index 0000000..8fbc757 --- /dev/null +++ b/array.cpp @@ -0,0 +1,13 @@ +#include + + +using namespace std; + +int main(){ + int a[] = {1, 2, 3, 4, 5}; + int b[5]; + for (auto c : b){ + cout << c << endl; + } + return 0; +} diff --git a/excercise.cpp b/excercise.cpp new file mode 100644 index 0000000..cf8c8c6 --- /dev/null +++ b/excercise.cpp @@ -0,0 +1,11 @@ +#include + +int main(){ + int values = 0, sum = 0; + std::cin >> values; + while (std::cin){ + sum += values; + } + std::cout << sum << std::endl; + return 0; +} diff --git a/for.cpp b/for.cpp new file mode 100644 index 0000000..3258098 --- /dev/null +++ b/for.cpp @@ -0,0 +1,10 @@ +#include + +int main(){ + int sum = 0; + for (int val = 1; val <= 10; ++val){ + sum += val; + } + std::cout << sum << std::endl; + return 0; +} diff --git a/function.cpp b/function.cpp new file mode 100644 index 0000000..9805ef3 --- /dev/null +++ b/function.cpp @@ -0,0 +1,28 @@ +#include + +using namespace std; + +int factorial(int n){ + if (n == 0){ + return 1; + } + + else{ + int product = 1; + for (int i = n; i != 0; --i){ + product *= i; + } + return product; + } +} + + +int main(){ + int n; + while (cin >> n){ + auto nFactorial = factorial(n); + cout << nFactorial << ' '; + } + cout << endl; + return 0; +} diff --git a/hello_world.cpp b/hello_world.cpp new file mode 100644 index 0000000..bb54da9 --- /dev/null +++ b/hello_world.cpp @@ -0,0 +1,5 @@ +#include +int main() +{ + std::cout << "Hello, world!" << std::endl; +} diff --git a/if.cpp b/if.cpp new file mode 100644 index 0000000..f20c9c2 --- /dev/null +++ b/if.cpp @@ -0,0 +1,22 @@ +#include + +int main(){ + int currVal = 0, val = 0; + if (std::cin >> currVal) { + int cnt = 1; + while (std::cin >> val) { + if (val == currVal) { + ++cnt; + } + else { + std::cout << currVal << " occurs " << cnt + << " times" << std::endl; + currVal = val; + cnt = 1; + } + } + std::cout << currVal << " occurs " << cnt + << " times" << std::endl; + } + return 0; +} diff --git a/iterator.cpp b/iterator.cpp new file mode 100644 index 0000000..4ddb493 --- /dev/null +++ b/iterator.cpp @@ -0,0 +1,18 @@ +#include +#include + +using namespace std; + +int main(){ + vector ivec{1, 2, 3, 4, 5}; + for (auto b = ivec.begin(); b != ivec.end(); ++b){ + cout << *b << endl; // Bruk *-operatoren for å hente ut elementer + } + for (auto b = ivec.begin(); b != ivec.end(); ++b){ + *b = 0; + } + for (auto b = ivec.begin(); b != ivec.end(); ++b){ + cout << *b << endl; + } + return 0; +} diff --git a/pointer.cpp b/pointer.cpp new file mode 100644 index 0000000..0595cea --- /dev/null +++ b/pointer.cpp @@ -0,0 +1,24 @@ +#include + +using namespace std; + +int main(){ + int ival = 42; + int *p = &ival; // p is a pointer and holds the address of ival by using the + // &-operator + + cout << *p << endl; // Yields the object which p points to by using the + // *-operator (prints 42) + + cout << p << endl; // Yields the address of ival + + *p = 0; // We can assign a new value to ival by using *p + + cout << ival << endl; // prints 0 + + int jval = 69; + + p = &jval; // p is now a pointer to jval + + return 0; +} diff --git a/range_for.cpp b/range_for.cpp new file mode 100644 index 0000000..162abdf --- /dev/null +++ b/range_for.cpp @@ -0,0 +1,12 @@ +#include +#include + +using namespace std; + +int main(){ + string str = "some string"; + for (auto c : str){ + cout << c << endl; + } + return 0; +} diff --git a/simple.cpp b/simple.cpp new file mode 100644 index 0000000..50c1542 --- /dev/null +++ b/simple.cpp @@ -0,0 +1,9 @@ + +#include +int main() +{ + std::cout << "Enter two numbers:" << std::endl; + int v1 = 0, v2 = 0; + std::cin >> v1 >> v2; + std::cout << "The sum of " << v1 << " and " << v2 << " is " << v1 + v2 << std::endl; +} diff --git a/string.cpp b/string.cpp new file mode 100644 index 0000000..3264b48 --- /dev/null +++ b/string.cpp @@ -0,0 +1,12 @@ +#include +#include + +using namespace std; + +int main(){ + string inputString, concatString; + while (getline(cin, inputString)){ + concatString += (inputString + ' '); + } + cout << concatString << endl; +} diff --git a/test.cpp b/test.cpp new file mode 100644 index 0000000..0c053b8 --- /dev/null +++ b/test.cpp @@ -0,0 +1,19 @@ +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + +int main() +{ + const int n = 5; + double b[n] = {}; + + n = 2; + } +} diff --git a/vector.cpp b/vector.cpp new file mode 100644 index 0000000..044534d --- /dev/null +++ b/vector.cpp @@ -0,0 +1,13 @@ +#include +#include +#include +using namespace std; + +int main(){ + vector v1, v2; + v1 = {1, 2, 3}; + v2 = {1, 2, 3}; + bool trueOrFalse = v1 == v2; + cout << trueOrFalse << endl; + return 0; +} diff --git a/while.cpp b/while.cpp new file mode 100644 index 0000000..f297136 --- /dev/null +++ b/while.cpp @@ -0,0 +1,11 @@ +#include + +int main(){ + int sum = 0, val = 1; + while (val <= 10) { + sum += val; + ++val; + } + std::cout << "Sum of 1 to 10 inclusive is " << sum << std::endl; + return 0; +}