Programming Concepts With C++1
Programming Concepts With C++1
• Step 3: Maintenance
2
The Problem Analysis–Coding–Execution Cycle
• Understand the Overall problem
• Understand problem requirements
– Does program require user interaction?
– Does program manipulate data?
– What is the output? افهم المشكله بشكل عام
• If the problem is complex, divide it into subproblems واعرف هل مطلوب تفاعل مع اليوزر
ما هي القوانين اللي هستخدمها
– Analyze each subproblem as above ماهي المدخلت والمخرجات؟
• Once compiled and linked, loader can place program into main memory for execution
• The final step is to execute the program
• Compiler guarantees that the program follows the rules of the language
– Does not guarantee that the program will run correctly
The Language of a Computer
• Machine language: language of a computer .اللغة اللي بيفهمها الكومبيوتر هي الصفار والوحايد فقط
• Binary digit (bit): The digit 0 or 1 لما يكون عندي سيكونس من الصفار والوحايد يسمى الباينري كود
• Binary code: A sequence of 0s and 1s .وهذه هي اللغه الوحيده التي يفهمها الكومبيوتر
• Byte: A sequence of eight bits
100100 010001 //Load كان الول لزم تكتب الكواد بالشكل دا كدا
!! ودا صعب جدا،اصفار ووحايد
100110 010010 //Multiply ..فعملو مايسمى بلغة السيمبلي وفيها يتم استخدام رموز
LOAD rate
MULT hour
STOR wage
• High-level languages include Basic, FORTRAN, COBOL, Pascal,C, C++, C#, and Java
• Compiler: translates a program written in a high-level language machine language
• ;The equation wages = rate • hours can be written in C++ as: wages = rate * hours
بعد السمبلي ،الموضوع اتطور وظهر ما يسمى High-level languagesوهي بيتم فيها كتابة مفهومه باللغه النجليزية ،لكن بردو
الكومبيوتر مش هيفهمها ،ومحتاج مترجم يترجمها الى الصفار والوايد ،والمترجم دا يسمى Compiler:
The Problem Analysis–Coding–Execution Cycle
Problem Analysis
هنا ملخص كل اللي فات:
أول حاجه بحلل المشكله وادرسها
واعرف ال inputوال output
Using Any Algorithm Design والقوانين اللي هستخدمها.
Programming ام هضع الحل في صورة
Language
خطوات (.)Algorithm
ام تأتي خطوة الكود ودا باستخدام
اي لغة برمجه.
Coding محتاجين compilerيحول لغة
البرمجه من الـ high level
languageالى الـ low level
language
compiler وايضا من وظائف الـ
errors Compilerانه يكتشف الخطاء
Translate code from
high-level language
في الكود واانت تصححها .
to low-level language بعدها يتم مرحلة التنفيذ .
ام النتيجة.
Execution
errors
وممكن ااناء التنفيذ يحصل
ايرورفبنرجع تاني نعيد تحليل
المشكله من البداية
Get results
10
يبقى ملخص كل اللي فات:
اني افكر في المشكله ام أكتب الكود.
الن هنتعلم ازاي هنكتب الكود !!
وهنتعرف الول على بعض المفاهيم..
Then
Think Write Code
11
Basic Definitions
• Programming language: a set of rules, symbols, and special words used to write computer programs.
• Computer program Sequence of statements whose objective is to accomplish a task.
• Syntax: rules that specify which statements (instructions) are legal
12
Computer System
Oper
2
Oper
Input 2
Operation
Output
1
Processing
14
Example 2
14
Your First C++ Program
#include <iostream>
usingnamespace std; C++ هنكتب أول برنامج لينا بلغة
int main() DevC++ اسمهplatform احنا هنستخدم
{ File=>New=>New هفتحه وافتح
cout << "My first C++ program." << endl; project=>Console Application
return0;
} هو المسؤول عن تحويل الكود الليcompilerالـ
Sample Run: هكتبه من لغة الهاي ليفل الى لغة الصفار والوحايد
وايضا هو اللي هيعرفني الخطاء اللي ممكن،
My first C++ program.
.اعملها في الكود
Processing a C++ Program
15
Processing a C++ Program (cont'd.)
16
Interacting With User: Displaying Messages on Screen
• In C++ , we use Cout << “ Text To be Displayed on The screen “ ;
• To use Cout << , we must use أول حاجه هنتعلمها جملة الطباعه على الشاشه جملة
cout الـ
– #include <iostream> ; لزم ييجي بعدها علمتين اقل من <<واسمهم
– using namespace std; ; ولزم انهي الجملة بـinsertion operator الـ
• #include <iostream> notifies the preprocessor to include the contents of the input/output stream header file
<iostream> in the program
لزم استدعي المكتبةcoutوعشان استخدم امر ال
• We can use the Escape Sequence to format the Displayed Text.
. التي تحتوي على تعريف المر دا
اول سطرين دول هما اللي بتستدعي فيهم المكتبة
cout الخاصة بأمر الـ
comment
• We can put commenton our programs using
• // to comment a single line
// this progrm is written by Fatima Alabbasy
• /*
Mulitple Lines كومنت يعني كلم،عشان أعمل كومنت في الكود
• */ to commentmultiple lines اكتبه لنفسي او لحد هياخد مني الكود يكمل عليه
/* This program is written by Fatima Alabbasy .والكلم دا الكومبايلر مش هيشوفه ول هيقرأه
قبل السطر// :في طريقتين
On Monday 11/11/2024
أو لو الكومنت طويل شوية أكتر من سطر في
*/ /* * وفي نهايته اكتب/ بدايت الكومنت اكتبـ
Example 2
• In C++ , we use Cin >> Variable; To accept an input from the user.
cin المر الااني للتفاعل مع اليوزر هو امر
• To use Cin >>, we must use #include <iostream> ;
• #include<iostream> notifies the preprocessor to include in the program the contents of the input/output stream header
file <iostream>.
• A variable is a location in the computer’s memory where a value can be stored for
use by a program.
• All variables must be declared with a name and a data type before they can be used in a program.
• Declaration
DataType Identifier;
Int width ;
24
C++ Data Types أنواع المتغيرات
Character or small signed: -128 to 127
char 1byte
integer. unsigned: 0 to 255
signed: -2147483648 to 2147483647
int Integer. 4bytes
unsigned: 0 to 4294967295
signed: -32768 to 32767
short int (short) Short Integer. 2bytes
unsigned: 0 to 65535
signed: -2147483648 to 2147483647
long int (long) Long integer. 4bytes
unsigned: 0 to 4294967295
Boolean value. It can
bool take one of two values: true or 1byte true or false
false.
float Floating point number. 4bytes +/- 3.4e +/- 38 (~7 digits)
Double precision floating
double 8bytes +/- 1.7e +/- 308 (~15 digits)
point number.
Long double precision
long double 8bytes +/- 1.7e +/- 308 (~15 digits)
floating point number.
25
Working With Variable
int length ;
Length 5
int width;
int area; Width 10
cin>>Length; Cin>>width;
Area = Length * width ;
Area 50
:ملحظة مهمه
lenght او مالx واسمه مالint لما اجي اعرف متغير نوعه رقم صحيح. وماينفعش ول حرف كابتلsmall اوامر الكود كلها لزم تكون حروف
. الكومبايلر بيعطي أمر للميموري تحجز مكان للمتغير باسمه:ايه اللي بيحصل
26
Rules on Variable Names
• DO NOT use reservedwords as variable names (e.g. if, else, int, float, case, for, …).
• The first character has to be a letter or underscore. It can not be a numeric digit.
• The secondand the other characters of the name canbe any letter, any number, or an underscore “_”.
Examples
Some valid names:
my_name, m113_1, salary, bluemoon , _at
Some invalid names:
my name, my-name , 1stmonth , salary! , guns&roses ,
:قواعد تسمية المتغيرات
.مالcout بمعنى ماينفعش اسميه باسم:ممنوع استخدم الكلمات المحجوزة في اللغه وانا بسمي المتغير بتاعي
_ باقي الحروف عادي ممكن يجي فيها ارقام عادي او.اول حرف في اسم المتغير لزم يكون حرف وليس رقم
وايضا ممنوع في اسم المتغير يكون في. كل العلمات دي ممنوعه$ , = أو @ أ, أ% باقي العلمات ممنوع استخامها في تسمية المتغير مال * او
موجود أمالة على السماء الصحيحه واسماء المتغيرات الخاطئة.مسافات
وهكذاlength لو هيحسي طول سميهarea يعني لو المتغير هيكون فيه المساحه سميه..نصيحة وانت بتسمي المتغير سمية اسم معبر
Frequently used data types .دي ملحظات مهمة لزم نعرفها عن انواع الداتا
والفروق بينهم
The data type unsigned is used to represent positive integers.
float and double data types for storing realnumbers.
The float data type has a precisionof seven digits-This means after the decimal point you can have seven
digits
-Example: 3.14159 534.322344 0.1234567
-The double data type has a precision of fifteen digits
Example : -3738.7878787878 .141592653589790
30
Frequently used data types
• We can use Scientific Notation to represent real numbersthat are very large or very small in value.
• The letters e or E is used to represent times 10 to the power.
Example:
• 1.23 x 10 5 is represented as 1.23e5 or 1.23e+5 or 1.23E5
• 1 x 10 -9 is represented as 1e-9
.البروجرامر الشاطر بيعرف يستخدم النوع الصحيح لستغلل المساحة في الميموري افضل استخدام
1.23E5 أو1.23e+5 أو1.23e5 هيc++ بلغة ال1.23 x 10 5 طريقة كتابة الرقم دا
Arithmetic Operations
Parentheses are used in C++ expressions in the same manner as in algebraic expressions.
For example, to multiply a times the quantityb+c we write: a * ( b + c ).
There is no arithmetic operator for exponentiation in C++, So x2 is
represented as x * x.
31
Precedence of arithmetic operations
Example :
• ? = 5 * 2 % ( 7 – 4)
Example
بمعنى،الجزء دا مهم جدا عشان بيكون سبب ان نتيجة العمليات الحسابية يكون خطأ
:مال
16.666 المفروض الجابة6 لما اقسمها على100الـ
الول، لكن انا هنا في الكود عرفت ثلث متغيرات
تمام؟cnt و:sum والااني والاالث ارقام غير صحيحة،نوعه رقك صحيحint avg
لكن انا معرفهfloat ، المفروض الجابة تطلعfloat علىfloat طيب لو قسمت
!! 16.6666 فقط وليس16 على انه رقم صحيح يبقى الجابه هتطلعavg ال
38
Data Type of an Arithmetic Expression
• Only the integer part of the result will be considered if two operands are integer
Even when the target variable is float
float على انه رقم كسرavg طيب هنا انا عرفت الـ
ايه اللي هيحصل؟int والمتغيرات التانيه اعرفها على انها ارقام صحيحة
16.666 المفروض الجابة6 لما اقسمها على100المفروض الـ
int يبقى الجابه هتطلع ليint علىint لكن انا هنا انا قسمت رقم
!! 16.6666 فقط وليس16 هتطلع
.float في المعادله نوعهoperends عشان احل المشكله هنا اخلى احد ال
39
Type Casting
{
;int i=5, j=3
;float div
;div=(float) i/j
;cout<< div
}
After type casting , The div will be 1.66667
40
Increment and Decrement Operators
– Post-increment: variable++
• Decrement operator: decrement variable by 1 :الجزء دا مهم جدا جدا
. تستخدم لزيادة قيمة المتغير بواحد++ الـ
. تستخدم لنقصان قيمة المتغير بواحد-- الـ
– Pre-decrement: --variable بواحد فهتكون قيمتهI هتزود الـI++ اذنI=5 مال لو
وهكذا6 :الجديده
– Post-decrement: variable -- لها شكلينIncrement and Decrement عمليات الـ
:اما تكون قبل اسم المتغير او بعده كما هو موضح
• Examples :
41
Increment and Decrement Operators
• If the value produced by ++ or – – is not used in an expression, it does not matter whether it is a
pre or a post increment (or decrement).
• W hen ++ (or – –) is used before the variable name, the computer first increments (or decrements)
the value of the variable and then uses its new value to evaluate the expression.
• When ++ (or – –) is used after the variable name, the computer uses the current value of the variable
to evaluate the expression, and then it increments (or decrements) the value of the variable.
• There is a difference between the following
x = 5;
Cout << ++x;