Skip to content

Latest commit

 

History

History
321 lines (277 loc) · 18.9 KB

Lab #1.md

File metadata and controls

321 lines (277 loc) · 18.9 KB

Programming 2 - Lab #1

By TA Ahmed Arafat (Ahmed Mohamed Yousry)

Topics To Be Discussed:

1. Some Important Points About Our Lab

2. Most Important Courses In The College VIP

3. What is The Programming ?

4. Why Computer (Programming) exists in this life ?

5. History Of The Computer Briefly

6. Computer Main Components

7. What Actually Happens When A Program Is Run ?

8. Logical Structure of the RAM

9. C++ (Syntax Vs Principles)

10. Why we prefer using java in Programming 2 ?

11. Structure of C++ Program

12. Variables in C++

13. Compile Time Vs Run Time

14. What happens in the main memory when we run a C++ program

1. Some Important Points About Our Lab

  • The Attendance is quite important for me, because ut shows how you are committed and discipline towards the course & of course because are labs depends on each other which means that being an absent in one lab will definitely affect the understanding of the other one. Also, it will hugely affect your grades in the final project/practical, for example if you attended most of our labs, but you got 12/15 in the final project, I will raise you to 15.
    I think know you realized how important it is LOL.
    VIP Note: attending less than 50% of the labs will mean that you will not have a final project, Be Caruful

  • I want from you all to Create accounts on:

  1. GitHub Dont forget to follow me :)
  2. Codeforces
  3. HackerRank We will solve some problem on them
  • For each lab you will have 2 things, the lab recorded on YouTube & a documentation on GitHub (Just like what you are reading)

  • Programming is like going to the gym, imagine you have a championship about bodybuilding, and you have months to practice, it doesn't make any sense to what all 89 days and then in the day before the championship you decided to go to the gym and workout for 6 continues hours, this is not gonna work AT ALL, the same is applied to the programming you have to practice at least 2 hours each week + attending the lecture & the lab each week, so that your brain become familiar with the syntax and reading/writing code & most important to learn how to think and how to convert your ideas into working code

  • Programming is 99% logic, 1% memorizing the syntax. We are not going to memorize a dozen of chapters or pages that you have to memorize in order to solve the final exam, this is not the programming at all, it all depends on logic & PRACTICING

  • Your life in the tech field depends on your programming level, this is a fact that you gonna realize later

  • For each lab, there will be an assignment to practice ^-^

2. Most Important Courses In The College VIP

  • From my point of view (which depends on the market & the field), the most important courses you will take in the college are (ranked from the most to the least):
  1. Programming X (X means Programming 1/2/3)
  2. Database X (X means 1/2)
  3. Operating System X (X Means 1/2)
  4. Network X (X means Network 1/2)
  5. Data Security (System Security)
  6. System Analysis
  • These courses are the fundamental of information technology & software engineering fields, so that you have to study them well, not just to get an A+ in the course, but to finish the course with maximum benefits from them

3. What is The Programming ?

  • If I told you to define the Computer (programming) to a child, that will you say??
  • Simply, programming is typing some words (statements) that are understandable by the machine (computer) to perform some task (solve a problem) instead of me (as a human being)
  • This is the easiest & most forward definition of programming

4. Why Computer (Programming) exists in this life ?

  • Or we can say in other word, what is the properties or the advantage of the programming (computer) compared to us as humans
  • There are 4 main advantage of the computer:
  1. Speed: if I told you to play with me a gain, if I told you to calculate in your mind the summation of numbers from 1 to 5 then you will think for a while then you wil answer with 15, but if i told you to get the summation this time from 1 to 1,000,000, then you will be shocked. When you learn how to code we will write this simple program to calculate the summation from 1 to N, the computer you will see will calculate it in a blank, but you have to note that the computer is not smart like us, it just executes the commands we till him, not more not less, if your commands are logically right then the output will be right otherwise don't expect a desired output
  2. Error Ratio: We as humans may be exhausted, tired, not in a good mood, having problems in our live, not sleeping well, all the previous examples can cause the human to make error(s) or mistake(s) while performing a task, unlike humans the computer does not cause eny errors, to be more specific in the previous example (calculate numbers from 1 to N) you have the same statements (code) and you entered number 5, the output each time will be 15, it cannot be 16 in the tenth attempt. Imagine you are trying to log in your facebook account, can you login if you entered a password wrong, can the server makes a mistake and pass you, this is a fatal problem, so it cannot be happened
  3. Availability: If I told you to solve mathematical problems for 1 hour directly, you will not have any problem, but if I told you to solve problems for 12 hours, you will call me a mad person, that's because we humans cannot work continuously for many hours directly. However, The server (huge computer) can work 24 hours a day, 7 days a week. Briefly Server-Client means that for example whenever you want to view for example your profile on facebook, you send a request to the facebook server telling him you want to view (access) this page, the facebook check that you have the authority view this resource, and if so it will perform an SQL query to get your data something like this : SELECT * FROM users WHERE ID = YourFaceBookID, then the result query will be sent to you back as a response. This means that it can serve anyone at anytime
  4. Concurrency: We as humans can perform one task at a single time,it is impossible to read a book while cooking or study will watch a movie, out brain is designed to concentrate on only on thing , this is for humans while machines (computer) can perform many tasks (commands) at the same time, this concept is called concurrency or multithreading. Regard the above example, the facebook server serve billions of users or as we said above (requests) each second.

5. History Of The Computer Briefly

  • During the World War II(1939-1945), the nazis was having the advantage & the upper hand for many reasons, but one of the most important & critical one is that all of their radio messages that are sent to the soldiers in the battlefield was Encrypted, this means that if the message (Plaintext is the readable text by human) is hi the message sent in the radio will be for example zl (this is called the Ciphertext which is the plaintext after encryption), so even if the allies can interpret radio waves (messages), they cannot understand anything. In this time a Mathematician called Alan Turing with his team was trying to invent a machine that can break the cryptography of the enigma machine (machine used by nazis to encrypt text), they succeeded to build a machine called Bombe (refers to a device that British cryptologists used to decipher encrypted German military communications during World War II). This machine was able to use logic to decipher the encrypted messages produced by the Enigma. experts assume that is invention has shortened length of the ware for at least 5 years, Alan Turing was the godfather of computers nowadays
  • then after a device called vacuum tube was the fundamental unit of first generation of the computer (when the computer size was enormous that occupies a whole room) , it acts as a switch which means that it can turn on/off current (1/0 statues), and using logic gates it was used to process simple instructions, but the problem with it is that it was huge, easily heated, damaged easily & of course slow


First Generation Of Computers


Vacuum Tube

  • In the current era, the fundamental unit of the computer's processor is Transistor which is a semiconductor device for amplifying, controlling, and generating electrical signals
  • A single processor (CPU) contains billions of transistors that process & executes instructions


Transistor


Modern CPUs


Billions Of Transistors Inside A CPU

6. Computer Main Components

  • The Main 3 Hardware components are CPU,RAM & Hard Disk,they are connected to each other by the help of Motherboard


Computer Main Components

7. What Actually Happens When A Program Is Run ?

  • When you as a user click on Word program, word program data is stored permanently on Hard Disk (assume word is 10GB storage in HDD), permanently means that is the current is off from your computer, your data will still be stored (Also Can be called Nonvolatile). When you click on the program, main statements that must be executed in order for the program to run will be copied from Hard Disk to RAM, The RAM is a temporally (volatile) memory which means that when you turned of the PC of the current is off, all the data inside it will be vanished. let's assume that from 10GB of word only 2GB will be copied to the RAM, now the RAM will send these instructions to the CPU which will process the instruction that will be sent again to the RAM to be temporarily stored, if a data inside the RAM should be stored permanently then it will be copied from RAM to Hard Disk, and when toy close the program the data of that program will be removed from the RAM


Running Word Program

  • The logical question here is why do RAM exists, why CPU not interacting directly with HDD ?!
  • The problem here is that the CPU is so fast while the HDD is so slow. When a fast hardware component interacts with other slow components, the fast component will decrease its speed to be compatible with the slower component, this phenomenon is called bottleneck. This means no matter the speed of the CPU it will be, we won't be able to utilize this speed no matter what, so that the RAM was invented as it is way faster than the HDD & at the same type has an enough space (4-32GB) not like CPU's cache which might be only 8MB

8. Logical Structure of the RAM

  • The most important part for programmers among the above three parts, can you guess ?!
  • The RAM is the most important part for the programmers, there is an advanced branch in computer science called Data Structure
  • Data Structure: is a branch in CS that is concerned with storing & organizing the data in the RAM, it is considered to be one of the main topics the professional programmer must know
  • When a program is slow/crashes/consume a lot of memory, one of the most important reason for all of the above problem is that the programmer who wrote the code has used the wrong Data Structure (or not using it at all)
  • Let's observe the Logical Structure of the RAM from the following Diagram:


Logical Structure of the RAM

  • As we can see above there is 4 main sections in the RAM:
  1. Code Section: In this section, source code of your file (*.cpp/*.java/*.php) exists + machine code (binary code which is understandable by the CPU *)
  2. Static/Global Variables: We will understand the meaning of the word Static in the future in OOP, while the word Global will understand in this Lab
  3. Stack: The part that stores the executed Main() Function or any other Function(s), will discuss it later
  4. Heap: We can access this section indirectly using Pointers,we will discuss about them later

* The compiler is the software that is written by the creator of a Programming Language, it's job is to convert your code (that is written in English) to Binary (1/0) to be executed by the processor

9. C++ (Syntax Vs Principles)

  • You have to know the following critical point, if you have learned a programming language deeply, deeply means you have practice with it or you have solved problems using it or even worked on projects. Then I want to congratulate you because you have learned almost all other language
  • This is because all programming languages have the same Concepts, or it can be called Principles
  • All Programming Languages contains Topics like:
    1. Variables
    2. Data Types
    3. Arithmetic Operators
    4. Assignment Operators
    5. Comparison Operators
    6. Logical Operators
    7. Conditions
    8. Loops
    9. Functions
    10. Arrays
    11. Input/Output
    12. Object Oriented Programming (OOP) (Programming 2 Main Topic)
  • They only differ in the Syntax, Syntax means the way of writing the code so that the compiler can understand what you are typing to be able to convert it into binary
  • So the bottom line, all programming languages have the same Principles but differ Syntax

11. Structure of C++ Program

  • Now Let's type the normal structure of a C++ program to print a message in the terminal (black screen where we run our program in)
#include <iostream>
using namespace std;

int main()
{
    cout<<"Hello World";
}
  • iostream: A library used to be able to use Input/Output in C++ (cin & cout)
  • using namespace std; : used to specify our scope is standard namespace in c++, you can run your program without this line, but you have to write std:: after cout/cin/string/endl and so on, something like this std::cout<<"Hello World";
  • int main(): it is the entry point when you run a program (the part which will be executed by the controller during run time)

12. Variables in C++

  • Variables : They are block or chamber reserved by the compiler in the main memory (RAM), used to store a value that will be used & retrieved during the executing of the program.
  • For example if you are executing a program that calculate the age of a user in days, when the user enter a value (his age in years), you may want to perform any calculations with this value later, so that you store it in a variable
  • Each variable must have 2 things, a Data Type & a Name

12.1 Data Types

  • Data types specify the value that will be stored in the variable

  • They include:

    1. int : Store whole numbers like 1,2,3, range up to 10^9, allocate only 4 Bytes
    2. short : Like int but allocate only 2 Bytes in the memory, range up to 10^4
    3. long : Like int but allocate only 8 Bytes in the memory, range up to 10^18
    4. float : Store decimal numbers like 3.14, range up to 10^9, allocate only 4 Bytes
    5. double : Like float but allocate only 8 Bytes in the memory, range up to 10^18
    6. char : Store single character in single quotes like 'm'/'f', allocates 1 Bytes
    7. string : Store Array of characters like "Ahmed Arafat", allocates 32 Bytes
    8. bool : store only True 1 or False 0, allocates 1 Byte
  • Let's see this sample of code:

#include <bits/stdc++.h>
using namespace std;

int main()
{
    short num1 = 10;
    int num2 = 1000000;
    long num3 = 10000000000;
    float num4 = 3.14;
    double num5 = 3.14;
    char c = 'm';
    string str = "Ahmed Arafat";
    bool is_male = 1;
}

12.2 Variable Name

  • There are some rules that must be followed while naming a variable
    1. Use one of the following characters:
      • a to z
      • A to Z
      • 0 to 9 (numbers)
      • underscore (_ used instead of space)
    2. Don't start the name of the variable with a number, int 1x; is wrong while int x1; is correct
      This is because the Physical Structure of The RAM is that it consists of many blocks each block is 1 Btyes & the most important part is that this block is Addressable (Has a physical address), this address is in hexadecimal number (from 0 to 9 & from A to F).
      When you create a variable like this int x;, the compiler will then store this name in a table in which refers to an address (something like 0F200), so then x -> 0F200. When you type cout<<x;, the compiler will go to the table and see which address this variable name refers to, and then it will print the value stored in it
      The problem here is that when you have a variable with name 1x, then in that table 1x -> 0F200. When you type cout<<1x;, the compiler will not understand whether 1x is the variable name or the address, so to prevent this ambiguity we cannot start a variable name with a number
    3. Don't name a variable with Keywords *: like for/while/int/using/namesapce/ do/if/else/break/continue/switch/class and so on

*Keywords: is a predefined or reserved word in C++ library with a fixed meaning and used to perform an internal operation. C++ Language supports more than 64 keywords.


Keywords In C++

  • Don't Forget The Following :
#include <bits/stdc++.h>
using namespace std;

int main()
{
    int x;// Declaration
    int y = 10;// Initialization
}

13. Compile Time Vs Run Time

  • In Codeblocks, when I press f9 button, the compiler first compile the file, this means that it will check each line to make sure that it can understand what you have typed , in other word it checks the Syntax, if there is anything wrong in it will then till you the line that contains the problem

  • This phase or time is called Compile Time or Compilation Time

  • After checking of the syntax by the compiler, the program will be now executed and then will be transformed from compile time to run time (executing)

  • Here are some examples of syntax error (compilation error):

include <bits/stdc++.h>
using namespace std;

int main()
{
    int x;// Declaration
    int y = 10;// Initialization
}

O/P : Error at line #1, expected # sign

  • Another example:
#include <bits/stdc++.h>
using namespace std;

int main()
{
    int x // Declaration
    int y = 10;// Initialization
}

O/P : Error at line #6, expect ; at end of the line

14. What happens in the main memory when we run a C++ program

  • I prefer to watch the Lab rather than typing this part because it needs a whiteboard to explain it accurately, so please check the last part of Lab #1 on YouTube.