Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .ccls
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
clang
%c -std=c17
%cpp -std=c++2a
%h %hpp --include=Global.h
-Iinc
-DMACRO

9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,12 @@ build
.pylintrc
.ipynb_checkpoints
a.out
ams562_desktop.py

*.out
lecture_code/*/.ccls-cache/*
lecture_code/*/*/.ccls-cache/*
lecture_code/lecture0/.ccls-cache/@@home@agh@projects@ams562-work@ams562-notes@lecture_code@lecture0/@home@agh@software@clang+llvm-10.0.0@lib@clang@10.0.0@include@__stddef_max_align_t.h
.ccls-cache/*

*/.ccls-cache/*
4 changes: 3 additions & 1 deletion intro/lec0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ do not make your academic litter brothers/sisters hate you.
"I'm so glad that I commented!"
+++++++++++++++++++++++++++++++

Actually, you are the one who benefit the most from making comments. While
You are the one who benefits the most from making comments. While
doing research, an important thing that everybody needs to keep in mind is to
make the work reproducible. One needs to make sure that his/her results can be
reproduced in the future. Therefore, make comments for yourselves in the
Expand All @@ -81,6 +81,8 @@ Of course, there are better tools, e.g. `Git`_ see :ref:`here <soft_req_git>`,
to help you manage your work. But making comments are the most fundamental
requirement that.

Good versus Bad Comments
+++++++++++++++++++++++++++++++
Let's take a look at the following examples:

.. code-block:: cpp
Expand Down
86 changes: 67 additions & 19 deletions intro/simple_cpp.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,57 @@
.. _intro_simple_cpp:

Simple `C++`_
=============
=======================

Here, we will present the most basic C++ program that does something.
The purpose of this section is to

* Give you a chance to use the programming environment
* Give a first feel of how you control the computer using C++.

.. contents:: Table of Contents
:local:
:backlinks: top

.. _intro_simple_cpp_main:

The ``main`` functions
----------------------
The simplest `C++`_ program
-------------------------------

Here is the simplest `C++`_ program you can write.

.. code-block:: cpp

int main() {return 0;}

Every `C++`_ program must have a function ``main``.
A function is a named sequence of instructions that the computer executes.
Each function has four parts:

1. A *return* type `int`, specificies the kind of result
2. A *name*, ``main``
3. A *parameter list* ``()``
4. A *function body* enclosed in the set of curly braces ``{}``, lists the sequence of statements

The ``main`` function must return an integer to indicate your system the exit
status of the program.

Each `C++`_ program is written in a ``main`` function in `C++`_, where a
``main`` function must return an integer to indicate your system the exit
status of the program.

.. note:: 0 is used for indicating exiting successfully.

Here is the simplest `C++`_ program, :code:`int main() { return 0; }`,
which does nothing but just returns ``EXIT_SUCCESS`` to your system.

.. note:: 0 is used for indicating exiting successfully.

.. _intro_simple_cpp_compile:

Compile the one-line program
----------------------------
Compile and link the one-line program
---------------------------------------------

Unlike `Python`_ and `MATLAB`_, where you can directly invoke the scripts, all
`C++`_ programs must be first **compiled** into executable binaries.
By compiling a program, we mean translating the human-readable text instructions
into something that the computer can execute.
In this class, we use the g++ compiler to
translate our source code into executable file.

.. note::

Expand Down Expand Up @@ -71,7 +95,7 @@ You should see ``0`` on the screen.

"Hello World!"
--------------

Here we will introduce the classic "Hello World!" program written in C++.
Unfortunately, you cannot write one line code for "Hello World!" in `C++`_. In
`Python`_, you can write a ``hello_world.py`` script with:

Expand All @@ -96,23 +120,47 @@ You should see "Hello World!" on your screen. Or even something like:
However, there is no built-in ``print`` method in `C++`_, we have to include
the standard input and output library, i.e. ``iostream``.

Here is the classic "Hello World!" program.

.. code-block:: cpp
:linenos:

#include <iostream>
// This program outpouts the message "Hello, World!" to the monitor
#include <iostream> // #include directive

int main() {
std::cout << "Hello World!" << std::endl;
int main() {// C++ programs start by executing the main function
std::cout << "Hello World!\n";// output "Hello, World!"
return 0;
// notice that semicolons terminate statements.
}
// quotes delimit a string literal
// \n is notation for new line

Let's parse some of this program

* ``#include <iostream>`` is an ``#include`` directive. This is where we included the definition of ``std::cout``
* ``std::cout`` character output stream
* ``<<`` is the output operator
* ``"Hello World!\n`` String literal that is displayed on screen
* ``// This is a comment``

Once we include the IO library (line 1), we can use the standard output
streamer, i.e. ``std::out``, to write out messages (line 4).

Now, copy the program into ``hello_world.cpp``, and compile and run it.

.. note::

This section is basically to demonstrate some simple codes. There will be
a specific section talking about IO.
It's important you mind the details when writing programs in CPP.
Try making a mistake and see how the compliler responds

* Forget the header
* Forget to terminate the string
* Misspell returned
* Forget a semicolon
* Forget a {

The hello world program is crucial because it represents the most basic program that you can write.
Its purpose is to get you familiar with writing programs in C++.
The rest of your programs should include the same basic structure.
In future lectures we will go over more information about I/O in C++


3 changes: 3 additions & 0 deletions lecture_code/lecture0/ex1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@


int main() { return 0; } // minimal C++ program
14 changes: 14 additions & 0 deletions lecture_code/lecture0/hello_world.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// This is the famous hello world program
//#include <iostream>

int main() {
// we print hello world using the
// cout (character output stream)
// defined in iostream
// note "Hello World!\n" is a string literal
// \n is a newline character
// \ is used to specify special characters
std::cout << "Hello World!\n";
// what happens when we forget to return 0?
return 0;
}
12 changes: 12 additions & 0 deletions lecture_code/lecture1/cin_demo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// This is the famous hello world program
#include <iostream>

int main() {
// best practice, always indicate the user what to enter
std::cout << "Please enter your first and last names: ";
std::string fname, lname;
// the program will hang here until receives the user input
std::cin >> fname >> lname;
std::cout << "Hello! " << fname << ' ' << lname << std::endl;
return 0;
}
9 changes: 9 additions & 0 deletions lecture_code/lecture1/declare_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

#include <iostream>

int main(void) {
int temp;
std::cout << temp << std::endl; // undefined behaviour
temp = 5;
std::cout << temp << std::endl;
}
26 changes: 26 additions & 0 deletions lecture_code/lecture1/fundamental_types.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

#include <iostream>

int main(void) {
bool b; // Boolean, true/false
char c; // character 'a','z','9'
int i; // integer, -265, 32, 29
double d; // double-precision floating point number
long l; // double-precision floating point number
double *ptr; // pointer to double (address to variable)
unsigned ui; // non-negative integer, for example 0,1,999

unsigned short fu = 0b1111111111111111;
short fi = 0b0111111111111111;

std::cout << "size of bool = " << sizeof(b) << " byte" << std::endl;
std::cout << "size of char = " << sizeof(c) << " byte" << std::endl;
std::cout << "size of double = " << sizeof(d) << " byte" << std::endl;
std::cout << "size of int = " << sizeof(i) << " byte" << std::endl;
std::cout << "size of long = " << sizeof(l) << " byte" << std::endl;
std::cout << "size of pointer = " << sizeof(ptr) << " byte" << std::endl;
std::cout << "size of ui = " << sizeof(ui) << " byte" << std::endl;

std::cout << "max int = " << fi << std::endl;
std::cout << "max unsigned = " << fu << std::endl;
}
7 changes: 7 additions & 0 deletions lecture_code/lecture1/hello_world.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include <iostream>

int main() {
std::cout << "Hello AMS 562\n";

return 0;
}
54 changes: 54 additions & 0 deletions lecture_code/lecture1/initialize.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include <complex>
#include <iostream>
#include <string>
#include <vector>

void print_var(int a) { std::cout << "int " << a << ::std::endl; }
void print_var(long int a) { std::cout << "long int " << a << ::std::endl; }
void print_var(unsigned long int a) {
std::cout << "unsigned long int " << a << ::std::endl;
}
void print_var(unsigned int a) {
std::cout << "unsigned int " << a << ::std::endl;
}
void print_var(float a) { std::cout << "float " << a << ::std::endl; }
void print_var(double a) { std::cout << "double " << a << ::std::endl; }
void print_var(std::string a) { std::cout << "string " << a << ::std::endl; }
void print_var(char a) { std::cout << "char " << a << std::endl; }
void print_var(bool a) { std::cout << "bool " << a << std::endl; }
void print_var(std::complex<double> a) {
std::cout << "complex " << a.real() << " " << a.imag() << std::endl;
}

int main() {
double d1 = 2.3; // initialize d1 to 2.3
double d2{2.3}; // initialize d2 to 2.3
double d3 = {2.3}; // is optional with {}

std::complex<double> z = 1; // a complex number with double-precision
std::complex<double> z2{d1, d2};
std::complex<double> z3 = {d1, d2};
print_var(z);
print_var(z2);
print_var(z3);

std::vector<int> v{1, 2, 3, 4, 5, 6}; // vector of ints

int i1 = 7.8; // i1 becomes 7
// int i2{7.8}; // error: narrowing conversion of double to int
// = allows narrowing conversion
// {} does not
unsigned int wha = -1; // What is the value?
int whaaaa{wha};
print_var(wha);
print_var(whaaaa);

auto b = true; // a bool
auto ch = 'x'; // a char
auto i = 124; // a int
auto d = 1.2; // a double
auto z = sqrt(y); // the type retured
auto bb{true}; // a bool

return 0;
}
11 changes: 11 additions & 0 deletions lecture_code/lecture1/iostreams.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// This is the famous hello world program
#include <iostream>

int main() {
std::cout << "Hello World!\n";
std::cout << "Hello World!" << std::endl;
std::cout << "1 +1 =" << (1 + 1) << std::end;
std::cout << "Size of double is: " << sizeof(double) << std::endl;

return 0;
}
52 changes: 52 additions & 0 deletions lecture_code/lecture1/literal.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include <iostream>
#include <string>
// This code depends on function overloading
// Function overloading is defining multiple functions with the same name
// Each function has the same name but differ in their input parameters
// You

void print_var(int a) { std::cout << "int " << a << ::std::endl; }
void print_var(long int a) { std::cout << "long int " << a << ::std::endl; }
void print_var(unsigned long int a) {
std::cout << "unsigned long int " << a << ::std::endl;
}
void print_var(unsigned int a) {
std::cout << "unsigned int " << a << ::std::endl;
}
void print_var(float a) { std::cout << "float " << a << ::std::endl; }
void print_var(double a) { std::cout << "double " << a << ::std::endl; }
void print_var(std::string a) { std::cout << "string " << a << ::std::endl; }
void print_var(char a) { std::cout << "char " << a << ::std::endl; }

int main() {
// integer literals
print_var(0b00001); // binary integer literal
print_var(0b01011);
print_var(0xFFFFFu);
print_var(0xFFFF);
print_var(0b00001); // binary
print_var(0x0BF0); // hexadecimal
print_var(-32);
print_var(-32u);
print_var(32ul);
print_var(32l);
print_var(32u);
// floating point literals
print_var(1.0f);
print_var(3.2e-1f);
// doubles
print_var(3.2e2);
print_var(-2.0);
print_var(-5.21);
// String Literlas
print_var("\"A\"");
print_var("\'");
print_var("\tHello World!\n");
print_var("\Hello\v World!\n");
// Char literals
print_var('a');
// literals
// 32, 1, -2, -100, 30001,...
// 32l // long literal
return 0;
}
17 changes: 17 additions & 0 deletions lecture_code/lecture1/precision.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <iostream>
// floats have 7 digits of precision
// doubles have 15

// Question is 1.1 = 1.10000004

int main() {
float single_lhs = 1.1;
float single_rhs = 1.10000004; // 10 sig
// == operator tests equality
std::cout << (single_lhs == single_rhs) << std::endl;
// try the same with double
double double_lhs = 1.1;
double double_rhs = 1.10000004;
std::cout << (double_lhs == double_rhs) << std::endl;
return 0;
}
12 changes: 12 additions & 0 deletions lecture_code/lecture1/printmul2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <iostream>

double mul2(double x) { return x * 2; }
void printxmul2(double x) {
std::cout << "2 * " << x << " is " << mul2(x) << "\n";
}

int main() {
printxmul2(2.234);

return 0;
}
Loading