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
47 changes: 20 additions & 27 deletions Lesson-1/README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# C++ Lesson 1
# C Lesson 1
Date: 7/11/13
Duration: 1.5 Hours

## Goals/Objectives:
1. Download Tool
2. Get familiar with C++
2. Get familiar with C
3. Learn commenting, variables, and commands.

## Ice Breakers + Introduction
Expand All @@ -17,7 +17,7 @@ A. 0, that’s a hardware problem.
## Terminology / Computers:
- A program in simplest terms is a set of instructions
- The instructions you write in a program is called source code
- Source code is written in a programming language, such as C++, Java, or Python
- Source code is written in a programming language, such as C, Java, or Python
- There is no best language; each has its own benefits
- To create a program, you can use any text editor, such as TextEdit or Notepad++
- However, you probably want to use an Integrated Development Environment, IDE
Expand All @@ -27,15 +27,12 @@ A. 0, that’s a hardware problem.
- A compiler takes the source code you’ve written, and converts it into a language that the computer can understand. This language is known as binary, machine code, executable, or compiled code.


Hello World: [helloworld.cpp](examples/helloworld.cpp)
Hello World: [helloworld.c](examples/helloworld.c)


- `#include <iostream>`
- Include the `iostream` library. A library is a file that someone else has written which includes many functions that we can use. The `iostream` library includes the C++ basic standard input-output declarations.
- `#include <stdio.h>`
- Include the `stdio` library. A library is a file that someone else has written which includes many functions that we can use. The `stdio` library includes the C basic standard input-output declarations.
- The `#` symbol is a directive for the preprocessor
- `using namespace std;`
- Include the standard namespace. It tells the compiler where to look in the library.
- This is a statement, so we require a semicolon at the end.
- `int main()`
- You don’t need to understand what the `int` means in this context for this lesson
- Know that `()` signifies that `main` is a function. It also allows you to put parameters into it, but we are not doing so in this program.
Expand All @@ -45,22 +42,18 @@ Hello World: [helloworld.cpp](examples/helloworld.cpp)
- `//Hello World Demo`
- `//` means that the line is a comment line. A comment does not have any effect on the program. Anything you write after the double slash will be ignored.
- Commenting allows the programmer to include short explanations or comments about a specific line or block of code.
- `cout << "Hello World";`
- `cout` is a function declared in the `iostream` library. The `<<` means to output what is after it to a stream, which is usually your screen. It essentially prints what is after the `<<` to the screen.
- `"Hello World"` is what we display to the screen. To print out simple text, we surround it in quotation marks.
- Since `cout` is a statement, we put a semicolon after it.
- `printf("Hello World");`
- `printf` is a function declared in the `stdio` library. The `("Hello World")` means to print the text `Hello World` on the screen;
- Since `printf` is a statement, we put a semicolon after it.
- `}`
- `}` is the end of the function. The program quits when it reaches the end curly brace.

- using namespace `std` is frowned upon due to naming conflicts.
- If using namespace `std` is not used, then you must type `std::` before any function you use from the `std` namespace.
- In our program, we would write `std::cout`.
- This program can be written in one line.
- `int main(){ std::cout << "Hello World!" };`
- `int main(){ printf("Hello World!") };`

- The block comment, `/* */`, allows commenting on multiple lines. An example is shown below.

```c++
```c
/* We can comment on
multiple lines */
```
Expand Down Expand Up @@ -95,7 +88,7 @@ multiple lines */
- `x = 5; //x is equal to 5.`
- `5 = x; //5 is equal to x. What? How can you change a number?`

```c++
```c
int x = 5;
x = 10;
```
Expand All @@ -115,18 +108,18 @@ x = 10;
- Division: `/`
- Say we want to add `10` to a number, this is how we would do it. Let’s print out the result.

```c++
```c
int num = 10;
num = num + 10;
cout << num;
printf("%i",num);
```

- What does it print out?
- Let’s do more operations; continue from the previous lines of code.

```c++
```c
num = num*5 + 6 / 7;
cout << num;
printf("%i",num);
```

- Try to do the math, and what do you get?
Expand All @@ -143,20 +136,20 @@ cout << num;

- Let’s try add two variables together

```c++
```c
int num1 = 4;
int num2 = 8;
int result = num1 + num2;
cout << result;
printf("%i",result);
```

- What do we get?
- We can modify this program:

```c++
```c
int num1 = 4;
int num2 = 8;
cout << num1 + num2;
printf("%i",num1 + num2);
```

- As you can see, we have printed out the first number plus the second number without needing another variable to hold the result. This poses advantages and disadvantages depending on the situation.
6 changes: 6 additions & 0 deletions Lesson-1/examples/helloworld.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include <stdio.h>
int main()
{
//Hello World Demo
printf("Hello, World!");
}
7 changes: 0 additions & 7 deletions Lesson-1/examples/helloworld.cpp

This file was deleted.

10 changes: 5 additions & 5 deletions Lesson-2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ Duration: 1.5 Hours
## Demos (30 minutes):

### I/O
- `cout`, `cin`
- Display text, get input, and use it in a sentence: [sentence.cpp](examples/sentence.cpp)
- Addition calculator of 3 numbers: [calculator.cpp](examples/calculator.cpp)
- `printf`, `scanf`, `fgets`
- Display text, get input, and use it in a sentence: [sentence.c](examples/sentence.c)
- Addition calculator of 3 numbers: [calculator.c](examples/calculator.c)
- after you go over this program with the class, modify it
- `int num1, num2, num3, result;`
- `std::cout << num1 + num2 + num3;`
- `printf("%i",num1 + num2 + num3);`
- `if` statement
- `if` / `else`
- Grades: [grades.cpp](examples/grades.cpp)
- Grades: [grades.c](examples/grades.c)
- explain why this program is not good
- introduce concept of else if
- modify Grades program by replacing ifs with else ifs
Expand Down
16 changes: 16 additions & 0 deletions Lesson-2/examples/calculator.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <stdio.h>
int main()
{
int num1;
int num2;
int num3;
int result;
printf("Enter any 3 numbers\r\nNumber 1: ");
scanf("%i", &num1);
printf("Number 2: ");
scanf("%i", &num2);
printf("Number 3: ");
scanf("%i", &num3);
result = num1 + num2 + num3;
printf("%i",result);
}
16 changes: 0 additions & 16 deletions Lesson-2/examples/calculator.cpp

This file was deleted.

27 changes: 27 additions & 0 deletions Lesson-2/examples/grades.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <stdio.h>
int main()
{
float grade;
printf("What is your grade percentage?");
scanf("%f",&grade);
if(grade >= 90)
{
printf("You got an A");
}
if(grade >= 80)
{
printf("You got a B");
}
if(grade >= 70)
{
printf("You got a C");
}
if(grade >= 60)
{
printf("You got a D");
}
if(grade < 60)
{
printf("You got an F");
}
}
27 changes: 0 additions & 27 deletions Lesson-2/examples/grades.cpp

This file was deleted.

8 changes: 8 additions & 0 deletions Lesson-2/examples/sentence.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <stdio.h>
int main()
{
char input[20];
printf("What is your name?");
fgets(input,20,stdin);
printf("Hello, %s", input);
}
8 changes: 0 additions & 8 deletions Lesson-2/examples/sentence.cpp

This file was deleted.

12 changes: 12 additions & 0 deletions Lesson-2/solutions/calculator.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <stdio.h>
int main()
{
int num1, num2, num3;
printf("Enter any 3 numbers\r\nNumber 1: ");
scanf("%i", &num1);
printf("Number 2: ");
scanf("%i", &num2);
printf("Number 3: ");
scanf("%i", &num3);
printf("%i",num1 + num2 + num3);
}
12 changes: 0 additions & 12 deletions Lesson-2/solutions/calculator.cpp

This file was deleted.

27 changes: 27 additions & 0 deletions Lesson-2/solutions/grades.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <stdio.h>
int main()
{
float grade;
printf("What is your grade percentage?");
scanf("%f",&grade);
if(grade >= 90)
{
printf("You got an A");
}
else if(grade >= 80)
{
printf("You got a B");
}
else if(grade >= 70)
{
printf("You got a C");
}
else if(grade >= 60)
{
printf("You got a D");
}
else
{
printf("You got an F");
}
}
27 changes: 0 additions & 27 deletions Lesson-2/solutions/grades.cpp

This file was deleted.