Commit 877c2748 authored by Roger Kaeppeli's avatar Roger Kaeppeli
Browse files

Add week01

parent 5460c1fb
cmake_minimum_required(VERSION 2.8)
project(PT1_week01)
# compile the programs from source
add_executable(loop.exe loop.cpp)
add_executable(swap.exe swapfixed.cpp)
# add_executable(swap0.exe swap.cpp) # won t work because of swap3 ;-)
add_executable(prepost.exe prepost.cpp)
add_executable(hello.exe hello.cpp)
#include <iostream>
int main() {
std::cout << sizeof("Hello") << std::endl;
return 0;
}
#include <iostream>
int main() {
std::cout << "Enter a number: ";
unsigned int n;
std::cin >> n;
std::cout << "for loop\n";
for (int i=1;i<=n;++i)
std::cout << i << "\n";
std::cout << "while loop\n";
int i=0;
while (i<n)
std::cout << ++i << "\n";
std::cout << "do loop\n";
i=1;
do
std::cout << i++ << "\n";
while (i<=n);
std::cout << "endless loop\n";
i=1;
while (true) {
if(i>n) break;
std::cout << i++ << "\n";
}
return 0;
}
#include <iostream>
int main() {
int a = 0;
std::cout << a++;
std::cout << ++a;
std::cout << a;
std::cout << "\n";
return 0;
}
#include <iostream>
void swap1 (int a, int b) { int t=a; a=b; b=t; }
void swap2 (int& a, int& b) { int t=a; a=b; b=t; }
void swap3 (const int & a, const int& b) { int t=a; a=b; b=t; }
void swap4 (int *a, int *b) { int *t=a; a=b; b=t; }
void swap5 (int* a, int* b) { int t=*a; *a=*b; *b=t; }
int main() {
// Which will compile? What is the effect of:
{ int a=1; int b=2; swap1(a,b); std::cout << a << " " << b << "\n"; }
{ int a=1; int b=2; swap2(a,b); std::cout << a << " " << b << "\n"; }
{ int a=1; int b=2; swap3(a,b); std::cout << a << " " << b << "\n"; }
{ int a=1; int b=2; swap4(&a,&b); std::cout << a << " " << b << "\n"; }
{ int a=1; int b=2; swap5(&a,&b); std::cout << a << " " << b << "\n"; }
return 0;
}
#include <iostream>
void swap1 (int a, int b) { int t=a; a=b; b=t; }
void swap2 (int& a, int& b) { int t=a; a=b; b=t; }
void swap4 (int *a, int *b) { int *t=a; a=b; b=t; }
void swap5 (int* a, int* b) { int t=*a; *a=*b; *b=t; }
int main() {
// Which will compile? What is the effect of:
{ int a=1; int b=2; swap1(a,b); std::cout << a << " " << b << "\n"; }
{ int a=1; int b=2; swap2(a,b); std::cout << a << " " << b << "\n"; }
{ int a=1; int b=2; swap4(&a,&b); std::cout << a << " " << b << "\n"; }
{ int a=1; int b=2; swap5(&a,&b); std::cout << a << " " << b << "\n"; }
return 0;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment