Skip to content

Commit

Permalink
Added files
Browse files Browse the repository at this point in the history
  • Loading branch information
erikasan authored Aug 30, 2019
1 parent 05757dc commit 83ce256
Show file tree
Hide file tree
Showing 14 changed files with 207 additions and 0 deletions.
13 changes: 13 additions & 0 deletions array.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <iostream>


using namespace std;

int main(){
int a[] = {1, 2, 3, 4, 5};
int b[5];
for (auto c : b){
cout << c << endl;
}
return 0;
}
11 changes: 11 additions & 0 deletions excercise.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <iostream>

int main(){
int values = 0, sum = 0;
std::cin >> values;
while (std::cin){
sum += values;
}
std::cout << sum << std::endl;
return 0;
}
10 changes: 10 additions & 0 deletions for.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <iostream>

int main(){
int sum = 0;
for (int val = 1; val <= 10; ++val){
sum += val;
}
std::cout << sum << std::endl;
return 0;
}
28 changes: 28 additions & 0 deletions function.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <iostream>

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;
}
5 changes: 5 additions & 0 deletions hello_world.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include <iostream>
int main()
{
std::cout << "Hello, world!" << std::endl;
}
22 changes: 22 additions & 0 deletions if.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <iostream>

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;
}
18 changes: 18 additions & 0 deletions iterator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <iostream>
#include <vector>

using namespace std;

int main(){
vector<int> 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;
}
24 changes: 24 additions & 0 deletions pointer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <iostream>

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;
}
12 changes: 12 additions & 0 deletions range_for.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <iostream>
#include <string>

using namespace std;

int main(){
string str = "some string";
for (auto c : str){
cout << c << endl;
}
return 0;
}
9 changes: 9 additions & 0 deletions simple.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

#include <iostream>
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;
}
12 changes: 12 additions & 0 deletions string.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <iostream>
#include <string>

using namespace std;

int main(){
string inputString, concatString;
while (getline(cin, inputString)){
concatString += (inputString + ' ');
}
cout << concatString << endl;
}
19 changes: 19 additions & 0 deletions test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <cctype>
#include <array>
#include <cstdlib>
#include <cstdio>
using namespace std;


int main()
{
const int n = 5;
double b[n] = {};

n = 2;
}
}
13 changes: 13 additions & 0 deletions vector.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <iostream>
#include <vector>
#include <string>
using namespace std;

int main(){
vector<int> v1, v2;
v1 = {1, 2, 3};
v2 = {1, 2, 3};
bool trueOrFalse = v1 == v2;
cout << trueOrFalse << endl;
return 0;
}
11 changes: 11 additions & 0 deletions while.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <iostream>

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;
}

0 comments on commit 83ce256

Please sign in to comment.