SlideShare a Scribd company logo
‫الدولية‬ ‫العربية‬ ‫الجامعة‬
‫المعل‬ ‫الهندسة‬ ‫كلية‬‫و‬‫م‬‫واالتصاالت‬ ‫اتية‬
Dr. konbos
2012-2013
Arab international University
Programming 1
Faculty of Informatics Engineering
1Presentation 1
‫مراجعة‬:
‫تعليمة‬cout<<X++‫قسمين‬ ‫الى‬ ‫مقسومة‬ ‫تعليمة‬ ‫هي‬:
‫ثم‬ ‫ومن‬ ‫العنصر‬ ‫طباعة‬‫اضافة‬"1"‫للعنصر‬
cout<<x;
X=x+1;
‫تعليمة‬ ‫اما‬cout<<++x;‫تعني‬ ‫فهي‬
‫زيادة‬"1"‫العنصر‬ ‫طباعة‬ ‫ثم‬ ‫ومن‬ ‫للعنصر‬
X=x+1;
cout<<x;
‫تعليمة‬cout<<setw(x);‫الكتابة‬ ‫مؤشر‬ ‫الزاحة‬ ‫تعليمة‬ ‫هي‬x‫مرة‬‫وتحتاج‬
‫المكتبة‬ ‫لتضمين‬<iomanip>
‫مالحظة‬:
‫حلقة‬ ‫باستخدام‬ ‫نقوم‬ ‫ال‬ ‫التكرار‬ ‫مرات‬ ‫عدد‬ ‫معرفة‬ ‫عدم‬ ‫حال‬ ‫في‬for‫هذه‬ ‫في‬ ‫اي‬
‫الشرط‬ ‫ينكسر‬ ‫ان‬ ‫الى‬ ‫نكرر‬ ‫الحلقة‬
‫تمرين‬:
‫بادخال‬ ‫المستخدم‬ ‫يقوم‬ ‫مجال‬ ‫ضمن‬ ‫االعداد‬ ‫مجموع‬ ‫يحسب‬ ‫برنامج‬ ‫بكتابة‬ ‫قم‬
‫وهو‬ ‫فيه‬ ‫رقم‬ ‫اصغر‬x‫وهو‬ ‫فيه‬ ‫رقم‬ ‫واكبر‬y:
‫الحل‬ ‫طريقة‬:
1-‫العنصران‬ ‫بادخال‬ ‫المستخدم‬ ‫يقوم‬x & y
2-‫من‬ ً‫ا‬‫بدء‬ ‫عدادها‬ ‫يسير‬ ‫حلقة‬ ‫باستخدام‬ ‫المجموع‬ ‫بحساب‬ ‫نقوم‬x‫عند‬ ‫وينته‬y
‫الحل‬:
2#include<iostream>
using namespace std;
void main()
{
int x,y ,sum=0;
cin>>x>>y;
for(int i=x; i<=y; i++)
sum=sum+I;
cout<<”sum= ”<<sum<<endl;
}
‫لعددين‬ ‫االكبر‬ ‫المشترك‬ ‫القاسم‬ ‫بحساب‬ ‫يقوم‬ ‫برنامج‬ ‫بكتابة‬ ‫قم‬
‫الحل‬ ‫طريقة‬:
1-‫الرقمين‬ ‫بادخال‬ ‫المستخدم‬ ‫يقوم‬
2-‫الرقم‬ ‫بطرح‬ ‫نقوم‬‫الكبير‬ ‫الرقم‬ ‫من‬ ‫الصغير‬‫فيكون‬ ‫الرقمان‬ ‫يتساوى‬ ‫حتى‬
‫للرقمين‬ ‫االكبر‬ ‫المشترك‬ ‫القاسم‬ ‫هو‬ ‫النهائي‬ ‫الناتج‬.
‫الحل‬:
#include<iostream>
using namespace std;
void main()
{
int x,y;
3cout<<”enter the tow variable x,yn”;
cin>>x>>y;
while(x!=y)
{
if(x>y)
x=x-y
else
y=y-x;
}
cout<<”the maximum diverse is ”<<x<<endl;
}
‫مالحظة‬:‫القسمة‬ ‫باق‬ ‫يكون‬ ‫دوما‬‫من‬ ‫المجال‬ ‫من‬ ‫محصور‬0‫العدد‬-1
‫تمرين‬:
‫مجموع‬ ‫بحساب‬ ‫يقوم‬ ‫برنامج‬ ‫بكتابة‬ ‫قم‬‫عدد‬ ‫خانات‬‫بادخاله‬ ‫المستخدم‬ ‫يقوم‬
‫الحل‬ ‫طريقة‬:
‫على‬ ‫القسمة‬ ‫باق‬ ‫طريقة‬ ‫نستخدم‬10‫على‬ ‫العدد‬ ‫نقسم‬ ‫ثم‬ ‫من‬ ‫و‬ ‫الخانات‬ ‫لتجزئة‬10
‫مثال‬:‫العدد‬3987‫بالشكل‬ ‫خانته‬ ‫حساب‬ ‫المطلوب‬7+8+9+3=23
‫الحل‬:
4#include<iostream>
using namespace std;
void main()
{
int x,sum=0;
cin>>x;
while(x!=0)
{
sum=sum+x%10;
x=x/10;
}
cout<<”sum=”<<sum<<endl;
}
End of Presentation 1
5Presentation 2
‫المراجعة‬ ‫تتمة‬:
‫بادخال‬ ‫يقوم‬ ‫برنامج‬ ‫بكتابة‬ ‫قم‬10‫عدد‬ ‫واحسب‬ ‫رقم‬ ‫واصغر‬ ‫اكبر‬ ‫واوجد‬ ‫ارقام‬
‫فيه‬ ‫الفردية‬ ‫و‬ ‫الزوجية‬ ‫االعداد‬:
‫الحل‬ ‫طريقة‬:
‫نقوم‬‫حلقة‬ ‫بانشاء‬for‫االكبر‬ ‫العنصر‬ ‫هو‬ ‫عنصر‬ ‫اول‬ ‫نعتبر‬ ‫ثم‬ ‫العناصر‬ ‫الدخال‬
‫واالصغر‬‫ونق‬‫العناصر‬ ‫بقية‬ ‫به‬ ‫ارن‬.
‫على‬ ‫العدد‬ ‫قسمة‬ ‫باق‬ ‫كان‬ ‫اذا‬2=0‫يزيد‬ ‫الزوجية‬ ‫االعداد‬ ‫عداد‬ ‫نضع‬1
‫الفردية‬ ‫االعداد‬ ‫عداد‬ ‫نزيد‬ ‫ذلك‬ ‫وغير‬1
‫الحل‬:
#include<iostream>
using namespace std;
void main()
{
int x,max,e=0,o=0;
cout<<”enter the first valuen”;
cin>>x;
if(x%2==0)
e++;
6else
o++;
max=x;
for(int i=1; i<=9; i++)
cout<<”enter the either elementn”;
{
cin>>x;
If(x%2==0)
e++;
else
o++;
If(x>max)
max=x;
}
Cout<<”the maximum value is”<<max<<endl<<”the
count of the even number is “<<e<<endl<<”the count of
the odd number is ”<<o<<endl;
}
‫برنامج‬ ‫بكتابه‬ ‫قم‬‫الدخال‬ID‫ورواتب‬10‫واظهار‬ ‫موظفين‬ID‫االعلى‬ ‫الموظف‬
‫راتب‬‫بينهم‬‫رابته‬ ‫يبلغ‬ ‫وكم‬
‫الحل‬
7
#include<iostream>
using namespace std;
void main()
{
const int s=10;
int salary,num,id;
cout<<"enter salary & ID for a first worker"<<endl;
cin>>salury>>id;
int max=salary;
num=id;
for(int i=1;i<s;i++)
{
cout<<"enter salary & ID for worker number
"<<i+1<<endl;
cin>> salary >>id;
if(salary>max)
{
num=id;
8max= salary;
}
}
cout<<"max= "<<max<<endl<<"ID Number=
"<<num<<endl;
}
‫التالي‬ ‫الشكل‬ ‫بطباعة‬ ‫يقوم‬ ‫برنامج‬ ‫بكتابة‬ ‫قم‬:
‫تعليمة‬ ‫نستخدم‬setw‫التمرين‬ ‫هذا‬ ‫في‬
‫الحل‬
#include<iostream>
#include<iomanip>
using namespace std;
void main()
{
int k;
cin>>k;
for(int i=0; i<10; i++)
9{
cout<<setw(k);
for(int j=0; j<=i; j++)
cout<<”* ”;
cout<<endl;
}
k--;
}
‫الدخال‬ ‫تمرين‬ ‫بكتابة‬ ‫قم‬5‫ل‬ ‫عالمات‬4‫المتوسط‬ ‫وعرض‬ ‫بحساب‬ ‫قم‬ ‫ثم‬ ‫طالب‬
‫طالب‬ ‫لكل‬ ‫العالمات‬ ‫هذه‬ ‫لمجموع‬ ‫الحسابي‬
‫الحل‬:
#include<iostream>
using namespace std;
void main()
{
float avg, sum, mark;
for(int i=0; i<4; i++)
{
cout<<”please enter a 5 mark for the studentn”;
sum=0;
for(int j=0; j<5; j++)
10{
cin>>mark;
sum=sum+mark;
}
avg=sum/5;
cout<<”avg for the ”<<i+1<<” student is “<<avg<<endl;
}
}
End of Presentation 2
End of the review
11Presentation 3
‫البعد‬ ‫االحادية‬ ‫المصفوفة‬(one dimensional array: )
1-‫ال‬ ‫المصفوفات‬‫سكانة‬(static array)‫ثابت‬ ‫المصفوفة‬ ‫حجم‬(const)‫نعرفه‬
‫عام‬ ‫متحول‬ ‫شكل‬ ‫على‬(global)‫ال‬ ‫قبل‬ ‫اي‬void main‫نستطيع‬ ‫كي‬
‫اليه‬ ‫الوصل‬
‫البرنامج‬ ‫اجزاء‬ ‫كل‬ ‫من‬
2-‫الديناميكية‬ ‫المصفوفة‬(Dynamic array)‫القادمة‬ ‫بالبحوث‬ ‫معنا‬ ‫ستمر‬
‫مالحظة‬:
‫مصفوفة‬ ‫عناصر‬ ‫لعكس‬‫طريقتان‬ ‫يوجد‬:
‫جديدة‬ ‫مصفوفة‬ ‫بانشاء‬ ‫نقوم‬ ‫انا‬ ‫اما‬Y‫المصوف‬ ‫حجم‬ ‫نفس‬ ‫لها‬‫ة‬X‫يلي‬ ‫كما‬:
#include<iostream>
using namespace std;
const int size=4;
void main()
{
int x[size];
cout<<”enter the value of the matrixn”;
for (int i=0; i<size; i++)
cin>>x[i];
cout<<”the reverse matrix of xn”;
int y[size];
12for(int i=0; i<size; i++)
y[i]=x[size-i-1];
for(int i=0; i<size; i++)
cout<<y[i]<<” “;
cout<<endl;
}
‫تعليمة‬ ‫باستخدام‬ ‫نقوم‬ ‫او‬swap‫ببعضهما‬ ‫عنصرين‬ ‫لتبديل‬ ‫جاهزة‬ ‫تعليمة‬ ‫وهي‬
‫مالحظة‬:‫المصفوفة‬ ‫عناصر‬ ‫نعكس‬ ‫عندما‬ ‫فقط‬‫تعليمة‬ ‫نستخدم‬ ‫عندما‬swap
‫صحيحة‬ ‫نتائج‬ ‫على‬ ‫للحصول‬ ‫المصفوفة‬ ‫حجم‬ ‫نصف‬ ‫الى‬ ‫يسير‬ ‫العداد‬ ‫نجعل‬
#include<iostream>
using namespace std;
const int size=5;
void main()
{
int x[size];
cout<<”enter the value of the matrixn”;
for(int i=0; i<size; i++)
cin>>x[i];
for(int i=0; i<size/2; i++)
swap(x[i],x[size-1-i]);
for(int i=0; i<size; i++)
cout<<x[i]<<” “;
13cout<<endl;
}
ً‫ا‬‫تصاعدي‬ ‫البعد‬ ‫أحادية‬ ‫مصفوفة‬ ‫عناصر‬ ‫لترتيب‬ ‫برنامج‬ ‫بكتابة‬ ‫قم‬
#include<iostream>
using namespace std;
const int size=5;
void main()
{
int ar[size];
cout<<"enter the value of the matrixn”;
for(int i=0; i<size; i++)
cin>>ar[i];
for(int j=0; j<size; j++)
for(int i=0; i<size -1 ; i++)
if(ar[i]>ar[i+1])
swap(ar[i],ar[i+1]);
for(int i=0; i<size; i++)
cout<<ar[i]<<” “;
cout<<endl;
}
‫مالحظة‬:‫التصاعدي‬ ‫الترتيب‬ ‫في‬/‫نضع‬ ‫التنازلي‬‫عناصر‬ ‫جميع‬ ‫على‬ ‫تمر‬ ‫حلقة‬
‫المصفوفة‬ ‫عناصر‬ ‫على‬ ‫تمر‬ ‫اخرى‬ ‫حلقة‬ ‫وبداخلها‬ ‫المصفوفة‬-1.
14‫العنصر‬ ‫و‬ ‫يليه‬ ‫الذي‬ ‫العنصر‬ ‫مع‬ ‫عنصر‬ ‫كل‬ ‫تقارن‬ ‫التي‬ ‫هي‬ ‫الداخلية‬ ‫الحلقة‬ ‫الن‬
‫بعده‬ ‫الذي‬ ‫العنصر‬ ‫مع‬ ‫يقارن‬ ‫ال‬ ‫االخير‬
‫متحول‬ ‫على‬ ‫تحتوي‬ ‫التي‬ ‫المسائل‬bool:
1-‫البحث‬ ‫مسائل‬
2-‫مسائل‬‫االختبار‬(‫كما‬‫البعد‬ ‫الثنائية‬ ‫المصفوفة‬ ‫في‬ ‫سيمر‬)
‫خاطئة‬ ‫دائما‬ ‫البولياني‬ ‫المتحول‬ ‫قيمة‬ ‫نفرض‬ ‫البحث‬ ‫مسائل‬ ‫في‬(false)‫حال‬ ‫وفي‬
‫قيمة‬ ‫للمتحول‬ ‫نسند‬ ‫العنصر‬ ‫وجدنا‬(true)
‫حلقة‬ ‫بانشاء‬ ‫نقوم‬ ‫مصفوفة‬ ‫داخل‬ ‫عنصر‬ ‫عن‬ ‫نبحث‬ ‫ان‬ ‫منا‬ ‫يطلب‬ ‫عندما‬for‫تمر‬
‫المصفوفة‬ ‫عناصر‬ ‫كافة‬ ‫على‬
‫مثال‬:‫بكتابة‬ ‫قم‬‫ر‬ ‫عن‬ ‫بالبحث‬ ‫يقوم‬ ‫برنامج‬‫قم‬‫البعد‬ ‫احادية‬ ‫مصفوفة‬ ‫داخل‬‫و‬
‫الرقم‬ ‫ان‬ ‫تفيد‬ ‫رسالة‬ ‫اظهار‬‫لم‬ ‫ان‬ ‫موجود‬ ‫غير‬ ‫وانه‬ ‫عليه‬ ‫العثور‬ ‫حال‬ ‫في‬ ‫موجود‬
‫عليه‬ ‫يعثر‬.
‫الحل‬:
#include<iostream>
using namespace std;
const int size=5;
void main()
{
int ar[size],k;
bool a=false;
cout<<”enter the element of the matrixn”;
for(int i=0; i<size; i++)
cin>>ar[i];
15cout<<”enter the random numbern”;
cin>>k;
for(int i=0; i<size; i++)
if(ar[i]==k)
a=true;
if(a==true)
cout<<”the number is exist n“;
else
cout<<”the number isn’t existn”;
}
End of presentation 3
16Presentation 4
‫البعد‬ ‫الثنائية‬ ‫المصفوفة‬(tow dimensional array):
‫لالدخال‬ ‫االخرى‬ ‫داخل‬ ‫حلقة‬ ‫تضمين‬ ‫على‬ ‫فيها‬ ‫نعتمد‬,‫للطباعة‬,......‫الخ‬
1-‫البعد‬ ‫ثنائية‬ ‫مصفوفة‬ ‫عناصر‬ ‫ادخال‬ ‫طريقة‬:
#include<iostream>
using namespace std;
const int size=3;
void main()
{
Int ar[size][size];
for(int i=0; i<size; i++)
for(int j=0; j<size; j++)
cin>>ar[i][j];
}
‫استبدال‬ ‫مع‬ ‫الطريقة‬ ‫بنفس‬ ‫تتم‬ ‫والطباعة‬cin>>ِ‫ـ‬‫ب‬cout<<
‫مربعة‬ ‫مصفوفة‬ ‫باختبار‬ ‫فيه‬ ‫تقوم‬ ‫برنامج‬ ‫بكتابة‬ ‫قم‬(4X4)‫متناظرة‬ ‫كانت‬ ‫اذا‬
‫ال‬ ‫ام‬(‫ال‬ ‫ان‬ ‫اي‬‫متساوية‬ ‫الرئيسي‬ ‫القطر‬ ‫طرفي‬ ‫على‬ ‫عناصر‬)
‫الحل‬ ‫طريقة‬:
‫بولياني‬ ‫متحول‬ ‫بتعريف‬ ‫نقوم‬bool‫إبتدائية‬ ‫قيمة‬ ‫يحمل‬true‫ان‬ ‫نفرض‬ ‫الننا‬
‫متناظر‬ ‫غير‬ ‫واحد‬ ‫عنصر‬ ‫وجود‬ ‫حال‬ ‫في‬ ‫اال‬ ‫متناظرة‬ ‫المصفوفة‬
‫الحل‬:
17#include<iostream>
Const int size=4;
using namespace std;
void main()
{
int ar[size][size];
bool a=true;
for(int i=0; i<size; i++)
for(int j=0; j<size; j++)
cin>>ar[i][j];
for(int i=0; i<size; i++)
for(int j=0; j<size; j++)
if(ar[i][j]!=ar[j][i])
a=false;
if(a==true)
cout<<”the given matrix is symmetricn”;
else
cout<<”the given matrix is not symmetricn”;
}
18‫برنامج‬ ‫بكتابة‬ ‫قم‬‫النوع‬ ‫من‬ ‫البعد‬ ‫ثنائية‬ ‫مصفوفة‬ ‫عناصر‬ ‫بادخال‬ ‫يقوم‬float
‫و‬ ‫الثناوي‬ ‫القطر‬ ‫عنصار‬ ‫مجموع‬ ‫بحساب‬ ‫وقم‬‫العلوي‬ ‫المثلث‬ ‫عنصار‬ ‫مجموع‬
‫االول‬ ‫العمود‬ ‫عناصر‬ ‫مجموع‬ ‫و‬.
‫الحل‬ ‫طريقة‬:
‫هو‬ ‫العلوي‬ ‫المثلث‬:i<j
‫هو‬ ‫الثانوي‬ ‫القطر‬:j==size-i-1‫او‬i+j=size-1
‫هو‬ ‫الول‬ ‫العمود‬:j==0
‫الحل‬:
#include<iostream>
using namespace std;
const int size=4;
void main()
{
int ar[size][size],sum=0,sum1=0,sum2=0;
for(int i=0; i<size; i++)
for(int j=0; j<size; j++)
cin>>ar[i][j];
for(int i=0; i<size; i++)
for(int j=0; j<size; j++)
{
if(j==size-1-i)
sum=sum+ar[i][j];
19if(i<j)
sum1=sum1+ar[i][j];
if(j==0)
sum2=sum2+ar[i][j];
}
cout<<”the sum of the second diameter is
”<<sum<<endl<<”the sum of the upper triangle is
”<<sum1<<endl<<”the sum of the first column is
“<<sum2<<endl;
}
‫البعد‬ ‫ثنائية‬ ‫مصفوفة‬ ‫عناصر‬ ‫بادخال‬ ‫يقوم‬ ‫برنامج‬ ‫بكتابة‬ ‫قم‬(4X3)‫بجمع‬ ‫ويقوم‬
‫بعد‬ ‫احادية‬ ‫مصفوفة‬ ‫في‬ ‫النتائج‬ ‫ويضع‬ ‫سطر‬ ‫كل‬ ‫عناصر‬
‫قيمة‬ ‫اكبر‬ ‫اوجد‬ ‫ثم‬ ‫ومن‬
‫الحل‬:
#include<iostream>
const int r=4,c=3;
using namespace std;
void main)(
{
int ar[r][c], x[r]={0};
20for(int i=0; i<r; i++)
for(int j=0; j<c; j++)
cin>>ar[i][j];
int max=ar[0][0];
for(int i=0; i<r; i++)
{
for(int j=0; j<c; j++)
cout<<ar[i][j]<<” “;
cout<<endl;
}
for(int i=0; i<4; i++)
for(int j=0; j<3; j++)
{
x[i]=x[i]+ar[i][j];
if(ar[i][j]>max)
max=ar[i][j];
}
21for(int i=0; i<r; i++)
cout<<x[i]<<” “;
cout<<endl;
cout<<"Max= "<<max<<endl;
}
‫عناصر‬ ‫نفرز‬ ‫ان‬ ‫اردنا‬ ‫اذا‬ ‫مالحظة‬‫ال‬‫سطر‬‫الثاني‬‫ثنائية‬ ‫لمصفوفة‬‫بعد‬‫تصاعديا‬
‫حلقة‬ ‫في‬ ‫نضع‬for‫التالي‬:
If(ar[1][j]>ar[1][j+1])
swap(ar[1][j],ar[1][j+1])
End of presentation 4
22Presentation 5
Function
‫السؤال‬ ‫من‬ ‫معينة‬ ‫لقضية‬ ‫حل‬ ‫عن‬ ‫عبارة‬ ‫هو‬ ‫التابع‬‫او‬ ‫عناصر‬ ‫كادخال‬
‫او‬ ‫جمع‬ ‫او‬ ‫طباعة‬........‫الخ‬‫و‬‫ان‬ ‫يمكن‬‫عدة‬ ‫الحل‬ ‫هذا‬ ‫يستخدم‬‫متكررة‬ ‫مرات‬
‫التوابع‬ ‫من‬ ‫نميز‬:
)bool, int , double, float 1-‫التوابع‬‫قيمة‬ ‫تعيد‬ ‫التي‬(
‫كلمة‬ ‫مع‬ ‫مقترنين‬ ‫ويكونوا‬return;‫الحل‬ ‫نهاية‬ ‫في‬‫بعد‬ ‫شيء‬ ‫اي‬ ‫التابع‬ ‫يقرأ‬ ‫وال‬
‫تعليمة‬return;‫كلمة‬ ‫بمثابة‬ ‫انها‬ ‫اي‬break;
‫وحيدة‬ ‫قيمة‬ ‫سوى‬ ‫ارجاع‬ ‫اليمكن‬ ‫التوابع‬ ‫هذه‬ ‫وفي‬‫وإل‬‫ظهاره‬‫التابع‬ ‫استدعاء‬ ‫عند‬ ‫ا‬
‫ال‬ ‫داخل‬ ‫في‬void main()‫بطباعتها‬ ‫نقوم‬ ‫او‬ ‫لمتحول‬ ‫نسندها‬ ‫ان‬ ‫اما‬
)void ‫التوابع‬‫قيمة‬ ‫تعيد‬ ‫ال‬( -2
‫عند‬ ‫ويوضع‬ ‫محددة‬ ‫بوظيفة‬ ‫ويقوم‬ ‫قيمة‬ ‫اي‬ ‫يعيد‬ ‫ال‬ ‫التابع‬ ‫هذا‬
‫طباعته‬ ‫او‬ ‫لمتحول‬ ‫اسناده‬ ‫دون‬ ‫مباشرة‬ ‫استدعائه‬
‫التابع‬ ‫عن‬ ‫مثال‬(1)
‫الجمع‬ ‫ناتج‬ ‫ويعيد‬ ‫صحيحين‬ ‫عددين‬ ‫بجمع‬ ‫يقوم‬ ‫تابع‬ ‫بكتابه‬ ‫قم‬
#include<iostream>
using namespace std;
int sum(int x,int y)
{
int z=0;
z=y+x;
return z;
23}
void main()
{
int x,y;
cin>>x,y;
cout<<”sum=”<<sum(x,y)<<endl;
}
‫مالحظة‬:‫نهايته‬ ‫عند‬ ‫التابع‬ ‫داخل‬ ‫المنشأة‬ ‫المتحوالت‬ ‫تموت‬‫وضعنا‬ ‫اذا‬ ‫اي‬
‫في‬‫ال‬void main()"cout<<z;"‫خطأ‬ ‫لدينا‬ ‫سيظهر‬
‫التابع‬ ‫عن‬ ‫مثال‬(2)
‫احادية‬ ‫مصفوفة‬ ‫بطباعة‬ ‫يقوم‬ ‫تابع‬ ‫بكتابة‬ ‫قم‬
#include<iostream>
using namespace std;
const int size=4;
viod Print(int ar[])
{
for(int i=0; i<size; i++)
cout<<ar[i]<<” “;
cout<<endl;
}
void main()
{
24int ar[size];
for(int i=0; i<size; i++)
cin>>ar[i];
Print(ar);
}
‫قوة‬ ‫الى‬ ‫العدد‬ ‫برقع‬ ‫يقوم‬ ‫تابع‬ ‫بكتابة‬ ‫قم‬‫معينة‬
‫مثال‬:xy
#include<iostream>
using namespace std;
int pow2(int x, int y)
{
int z=1;
for(int i=y; i>0; i--)
z=z*x;
return z;
}
void main()
{
int x,y;
cout<<”enter x, yn”;
cin>>x,y;
25cout<<”x^y= ”<<pow2(x,y)<<endl;
}
‫مالحظة‬:
‫و‬ ‫البداية‬ ‫في‬ ‫التابع‬ ‫ترويسة‬ ‫نضع‬ ‫ان‬ ‫يكفي‬ ‫البرنامج‬ ‫نهاية‬ ‫في‬ ‫التابع‬ ‫وضعنا‬ ‫اذا‬
‫نكت‬‫ب‬‫ال‬ ‫نهاية‬ ‫بعد‬ ‫اي‬ ‫البرنامج‬ ‫نهاية‬ ‫بعد‬ ‫التابع‬ ‫وظيفة‬void main()
‫مالحظة‬:
‫النوع‬ ‫من‬ ‫التابع‬ ‫يقوم‬ ‫ان‬ ‫يمكن‬int , bool , float, double‫من‬ ‫اكثر‬ ‫بارجاع‬
‫المرجعية‬ ‫المتحوالت‬ ‫استخدام‬ ‫حال‬ ‫في‬ ‫هذا‬ ‫ولكن‬ ‫قيمة‬”)&(by reference “
‫الذاكرة‬ ‫الى‬ ‫مباشر‬ ‫مكان‬ ‫باخذ‬ ‫تقوم‬ ‫المتحوالت‬ ‫وهذه‬‫العادية‬ ‫المتحوالت‬ ‫بعكس‬
‫التابع‬ ‫نهاية‬ ‫مع‬ ‫الصورة‬ ‫وتنتهي‬ ‫المتحول‬ ‫عن‬ ‫صورة‬ ‫تاخذ‬ ‫التي‬
‫مثال‬:‫مستطيل‬ ‫ومحيط‬ ‫مساحة‬ ‫لحساب‬ ‫برنامج‬ ‫بكتابة‬ ‫قم‬.
‫المساحة‬ ‫حسابهما‬ ‫يجب‬ ‫قيمتان‬ ‫لدينا‬ ‫هنا‬,‫ال‬‫محيط‬.
#include<iostream>
using namespace std;
int ffff(int x, int y, int &aa )
{
int cc;
aa=x*y;
cc=(x+y)*2;
return cc;
aa‫خرج‬ ‫متحول‬ ‫هو‬
‫له‬ ‫نضع‬ ‫ال‬ ‫لذلك‬
return‫يقوم‬ ‫النه‬
‫الذاكرة‬ ‫الى‬ ‫بالوصول‬
‫مباشرة‬‫في‬ ‫تغيير‬ ‫واي‬
‫ضمن‬ ‫المتحول‬ ‫هذا‬ ‫قيمة‬
‫فيه‬ ‫يثبت‬ ‫التابع‬
26}
void main()
{
int x, y , aa ;
cout<<”enter the length and the Width n”;
cin>>x>>y;
cout<< ffff(x,y,aa);
cout<<aa;
}
‫خرج‬ ‫متحوالت‬ ‫انهما‬ ‫على‬ ‫المتحولين‬ ‫ناخذ‬ ‫للحل‬ ‫اخرى‬ ‫طريقة‬
#include<iostrem>
using namespace std;
void ac(double a, double c , double & aa, double & cc)
{
aa= a * c ;
cc= (a + c)*2
}
Void main()
{
double x , y , aa , cc ;
cin>>x>>y ;
ac(a , c , aa , cc)
‫قيمة‬ ‫بارجاع‬ ‫التابع‬ ‫يقوم‬ ‫التمرين‬ ‫هذا‬ ‫في‬
‫حساب‬ ‫وهي‬ ‫وحيدة‬‫استخدام‬ ‫تم‬ ‫بينما‬ ‫المحيط‬
‫المساحة‬ ‫قيمة‬ ‫لتخزين‬ ‫خرج‬ ‫متحول‬
27cout<<aa<<endl<<cc<<endl;
}
‫مالحظة‬:‫ع‬ ‫فورا‬ ‫تؤخذ‬ ‫المصفوفة‬‫للتاوابع‬ ‫بالنسبة‬ ‫خرج‬ ‫متحول‬ ‫انها‬ ‫لى‬
‫من‬ ‫التوابع‬ ‫نستعمل‬ ‫قيمة‬ ‫من‬ ‫اكثر‬ ‫بحساب‬ ‫يقوم‬ ‫تابع‬ ‫منا‬ ‫يطلب‬ ‫عندما‬ ‫للتبسيط‬
‫النوع‬void
‫أحادية‬ ‫مصفوفة‬ ‫عناصر‬ ‫مجموع‬ ‫بإعادة‬ ‫يقوم‬ ‫تابع‬ ‫بكتابة‬ ‫قم‬
#include<iostream>
using namespace std;
const int size=4;
int sumd(int ar[])
{
int sum=0;
for(int i=0; i<size; i++)
sum=sum+ar[i];
return sum;
}
void main()
{
int ar[size];
for(int i=0; i<size; i++)
cin>>ar[i];
28cout<<”sum= “<<sumd(ar)<<endl;
}
‫خرج‬ ‫متحوالت‬ ‫باستخدام‬ ‫اخرى‬ ‫طريقة‬‫النوع‬ ‫من‬ ‫تابع‬ ‫نستخدم‬ ‫اي‬(void)
‫الحل‬:
#include<iostream>
using namespace std;
const int size=4;
int sumd(int ar[],int &sum)
{
sum=0;
for(int i=0; i<size; i++)
sum=sum+ar[i];
}
void main()
{
int ar[size],sum;
for(int i=0; i<size; i++)
cin>>ar[i];
sumd(ar,sum);
cout<<”sum= ”<<sum<<endl;
}
29‫هذا‬ ‫ومكان‬ ‫المصفوفة‬ ‫في‬ ‫عنصر‬ ‫أكبر‬ ‫بإيجاد‬ ‫يقوم‬ ‫تابع‬ ‫بكتابة‬ ‫قم‬‫العنصر‬
‫الحل‬:
#include<iostream>
using namespace std;
const int size =5;
void maxA(int ar[], int &max, int & pos)
{
max=ar[0];
for(int i=0; i<size; i++)
if(ar[i]>max)
{
max=ar[i]
pos = i+1;
}
}
void main()
{
int ar[size] ,pos , max;
for(int i=0; i<size; i++)
cin>>ar[i];
maxA(ar,max,pos)
30cout<<”max=”<<max<<endl<<”position at
“<<pos<<endl;
}
‫البعد‬ ‫احادية‬ ‫مصفوفة‬ ‫داخل‬ ‫عنصر‬ ‫عن‬ ‫بالبحث‬ ‫يقوم‬ ‫تابع‬ ‫بكتابة‬ ‫قم‬
‫القيمة‬ ‫يعدي‬true‫القيمة‬ ‫ويعيد‬ ‫موجود‬ ‫العنصر‬ ‫كان‬ ‫اذا‬false‫موجود‬ ‫يكن‬ ‫لم‬ ‫اذا‬
‫الحل‬
#include<iostream>
using namespace std;
const int size=5;
bool search(int ar[],int k)
{
bool a=false;
for(int i=0; i<size; i++)
if(ar[i]==k)
a=true;
if(a==true)
return a;
}
void main()
{
bool a;
31int ar[size],k;
for(int i=0; i<size; i++)
cin>>ar[i];
cout<<”enter the random element n”;
a=search(ar,k);
cout<<a<<endl;
if(a==true)
cout<<”foundn”;
else
cout<<”not foundn”;
}
‫تصاعديا‬ ‫عناصرها‬ ‫لفرز‬ ‫وتابع‬ ‫احادية‬ ‫مصفوفة‬ ‫عناصر‬ ‫بعكس‬ ‫يقوم‬ ‫تابع‬ ‫اكتب‬
‫بفرز‬ ‫ويقوم‬ ‫بعد‬ ‫ثنائية‬ ‫مصفوفة‬ ‫دخل‬ ‫باخذ‬ ‫يقوم‬ ‫تابع‬ ‫اكتب‬ ‫ثم‬ ‫لطابعتها‬ ‫وتابع‬
‫الصف‬ ‫عناصر‬‫المصفوفة‬ ‫هذه‬ ‫لطباعة‬ ‫وتابع‬ ‫تصاعديا‬ ‫الثاني‬
‫احادية‬ ‫مصفوفة‬ ‫عناصر‬ ‫بادخال‬ ‫يقوم‬ ‫برنامج‬ ‫اكتب‬,‫التوابع‬ ‫باستدعاء‬ ‫وقم‬ ‫ثنائية‬
‫والعكس‬ ‫الفرز‬ ‫وبعد‬ ‫قبل‬ ‫بالترتيب‬ ‫السابقة‬
‫الحل‬:
#include<iostream>
using namespace std;
const int size=3;
void rev(int ar[size] )
{
cout<<"the reverc of the matrix is :n";
32for( int i=0; i < size/2 ; i++)
swap(ar[i],ar[size-1-i]);
}
void sort(int ar[size])
{
for(int j=0; j<size; j++)
for(int i=0; i<size - 1; i++)
if(ar[i]>ar[i+1])
swap(ar[i],ar[i+1]);
}
void sort2(int ar[size][size])
{
for(int i=0; i<size; i++)
for(int j=0; j<size; j++)
if(ar[1][j]>ar[1][j+1])
swap(ar[1][j],ar[1][j+1]);
}
void print(int ar[size])
{
for(int i=0; i < size; i++)
33cout<<ar[i]<<" ";
cout<<endl;
}
void print2(int ar[size][size])
{
for(int i=0; i<size; i++)
{
for(int j=0; j<size; j++)
cout<<ar[i][j]<<" ";
cout<<endl;
}
}
void main()
{
int x[size][size],ar[size],j,i;
cout<<"enter the value of the matrix :n";
for(i = 0; i < size; i++)
cin>>ar[i];
rev(ar);
print(ar);
sort(ar);
34cout<<"the sort of the matrix is:n";
print(ar);
cout<<"enter the value of the 3X3 matrix:n";
for(i=0; i<size; i++)
for(j=0; j<size; j++)
cin>>x[i][j];
sort2(x);
cout<<"the matrix after sorted the 2nd row IS :n";
print2(x);
cout<<endl;
cout<<"Decorated by : Mhd Ghayth Alsawafn"<<"Thank
you for use it bye bye ^_^n";
}
35‫تعليمة‬template<class t>‫النوع‬ ‫من‬ ‫المتحوالت‬ ‫كل‬ ‫يقبل‬ ‫قالب‬ ‫هي‬int ,
folatdouble‫حيث‬t‫قيمة‬ ‫ان‬ ‫اي‬ ‫متحول‬t‫في‬ ‫المتحول‬ ‫لوضع‬ ‫تبعا‬ ‫تتبدل‬
‫ال‬void main()‫المتغير‬ ‫مكان‬ ‫المتحول‬ ‫طبيعة‬ ‫تحل‬ ‫حيث‬t.
‫مثال‬
1- Write a template function named "Reverse" that
receives 1D array and reverse its elements.
Sample (1):
If the passed array is:
K C U L D O O G
The returned array will be:
G O O D L U C K
‫الحل‬:
#include<iostream>
using namespace std;
const int size=8;
template<class t>
void rev(t ar[])
{
for(int i=0; i<size/2; i++)
swap(ar[i],ar[size-i-1]);
}
36template<class t>
void print(t ar[])
{
for(int i=0; i<size; i++)
cout<<ar[i]<<" ";
cout<<endl;
}
void main()
{
int i;
char ar[size];
cout<<"enter the value of the matrixn";
for( i=0; i<size; i++)
cin>>ar[i];
rev(ar);
cout<<"the reverse of the matrix :n";
print(ar);
}
37‫اخر‬ ‫مثال‬:
2-Write a template function named "sort" that receives 1D
array and sort its elements ascending.
‫الحل‬:
#include<iostream>
using namespace std;
const int size=4;
template<class t>
void sort(t ar[])
{
for(int j=0; j<size; j++)
for(int i=0; i<size-1; i++)
if(ar[i]>ar[i+1])
swap(ar[i],ar[i+1]);
}
template<class t>
void print(t ar[size])
{
for (int i=0; i<size; i++)
cout<<ar[i]<<" ";
38cout<<endl;
}
void main()
{
int ar[size],i;
cout<<"enter the value of the matrixn";
for(i=0; i<size; i++)
cin>>ar[i];
sort(ar);
print(ar);
}
‫اخير‬ ‫مثال‬:
2- Write a function named "search" that receives 2D float
array and a float "num", the function must search for
the "num" in the array and returns true and the row
index at which the element is found and false and "-1"
if the element is not in the array.
‫الحل‬:‫است‬ ‫فيه‬ ‫يلزمنا‬‫السطر‬ ‫ترتيب‬ ‫فيمه‬ ‫ليحمل‬ ‫خرج‬ ‫متحول‬ ‫خدام‬
‫فيه‬ ‫العنصر‬ ‫وجد‬ ‫الذي‬
39#include<iostream>
using namespace std;
const int size=3;
template<class t>
bool search(t ar[size][size],t k ,int & pos)
{
for(int i=0; i<size; i++)
for(int j=0; j<size; j++)
if(ar[i][j]==k)
{
pos=i+1;
return true;
}
return false;
}
void main()
{
40int pos,i,j;
float ar[size][size],k;
bool f;
cout<<"enter the value of the 3X3 matrixn";
for(i=0; i<size; i++)
for(j=0; j<size; j++)
cin>>ar[i][j];
cout<<"enter the random elemnt: ";
cin>>k;
for(i=0; i<size; i++)
{
for(j=0; j<size; j++)
cout<<ar[i][j]<<" ";
cout<<endl;
}
cout<<"the number is found true/false ?n";
f=search(ar,k,pos);
if(f==true)
cout<<"found at "<<pos<<endl;
else
41cout<<"the number not found n -1";
}
‫العشوائي‬ ‫التابع‬rand():
‫عشوائية‬ ‫ارقام‬ ‫لتوليد‬ ‫التابع‬ ‫هذا‬ ‫ويستخدم‬
‫ال‬ ‫من‬ ‫عشوائية‬ ‫ارقام‬ ‫مجموعة‬ ‫بتوليد‬ ‫قم‬ ‫مثال‬0‫ال‬ ‫وحتى‬6
‫القسمة‬ ‫باقي‬ ‫على‬ ‫االعتماد‬ ‫مع‬ ‫العشوائي‬ ‫التابع‬ ‫باستخدام‬ ‫نقوم‬ ‫الحل‬
cout<<rand()%6+1
‫ضمن‬ ‫عشاوئية‬ ‫بقيمة‬ ‫البعد‬ ‫احادية‬ ‫مصفوفة‬ ‫عناصر‬ ‫بتعبئة‬ ‫يقوم‬ ‫تابع‬ ‫بكتابة‬ ‫قم‬
‫المجال‬[20.50]
‫الحل‬
#include<iostream>
using namespace std;
const int size=6;
void kkk(int ar[])
{
for(int i=0; i<size; i++)
ar[i]=rand()%31+20;
}
void main()
{
int ar[size];
42kkk(ar);
for(int i=0; i<size; i++)
cout<<ar[i]<<” “;
cout<<endl;
}
‫منه‬ ً‫ال‬‫قب‬ ‫نضع‬ ‫العشوائي‬ ‫التابع‬ ‫استخدام‬ ‫عند‬ ‫دوما‬ ‫متغيرة‬ ‫نتائج‬ ‫على‬ ‫للحصول‬
‫التابع‬srand()‫المكتبة‬ ‫تضمين‬ ‫الى‬ ‫يحتاج‬ ‫وهو‬#include <cstdlib>‫ويقبل‬
‫متحوالت‬ ‫فقط‬double
‫الحل‬:
#include<iostream>
#include <cstdlib>
using namespace std;
const int size=6;
void kkk(int ar[])
{
for(int i=0; i<size; i++)
ar[i]=rand()%31+20;
}
void main()
{
double k;
43cin>>k;
int ar[size];
srand(k);
kkk(ar);
for(int i=0; i<size; i++)
cout<<ar[i]<<” “;
cout<<endl;
}
‫قم‬‫التالية‬ ‫البرامج‬ ‫خرج‬ ‫بكتابة‬
#include<iostream>
using namesapce std;
int x=7; // (‫عام‬ ‫متحول‬) global variable
int xxx(int &x) // (‫خرج‬ ‫متحول‬) by reference variable
{
x=x+1;
return x;
}
void yyy() // ‫عام‬ ‫متحول‬ ‫يقبل‬ ‫التابع‬ ‫الن‬ ‫يثبت‬ ‫التغيير‬
{
x=x+1;
cout<<x;
}
44void kkk( int x) // ‫التابع‬ ‫هذا‬ ‫في‬ ‫قيمته‬ ‫على‬ ‫المتحول‬ ‫يحافظ‬
{
x=x+100;
cout<<x;
}
void main()
{
int x=10;
cout<<xxx(x); 11
yyy(); 8
cout<<x; 11
kkk(x); 111
cout<<x; 11
yyy(); 9
}
‫هامة‬ ‫مالحظة‬:‫التغيير‬ ‫يثبت‬ ‫العامة‬ ‫المتحوالت‬‫علي‬‫عليها‬ ‫التغيير‬ ‫عند‬ ‫ها‬
‫الثانية‬ ‫الدرجة‬ ‫من‬ ‫معادلة‬ ‫جذور‬ ‫باعادة‬ ‫يقوم‬ ‫تابع‬ ‫بكتابة‬ ‫قم‬
‫الحل‬:‫هنا‬x1 , x2‫قيمة‬ ‫ياخذو‬ ‫و‬ ‫المعادلة‬ ‫جذور‬ ‫هم‬double
45#include<iostream>
#include<cmath>
using namespace std;
void del(double a, double b, double &x1, double &x2,doubleD)
{
x1=(-b-sqrt(D))/(2*a);
x2=(-b+sqrt(D))/(2*a);
}
void main()
{
double D,x1,x2,a,b,c;
cout<<”enter a,b and cn”;
cin>>a>>b>>c;
D=b*b-4*(a*c);
if(D>=0)
{ cout<<”the solution is n”;
del(a,b,x1,x2,D);
cout<<”X1= “<<x1<<endl;
cout<<”X2= “<<x2<<endl;
}
else
cout<<”errorn”;
}
End of presentation 5
End of function
46
Presentation 6
Pointer
‫هو‬‫عبارة‬‫عن‬‫مؤشر‬‫عنوان‬ ‫على‬ ‫للداللة‬‫الذاكر‬ ‫في‬ ‫المتغير‬‫ة‬.
1-‫نضع‬ ‫عندما‬(&)‫المتغير‬ ‫مكان‬ ‫الى‬ ‫باالشارة‬ ‫يقوم‬ ‫انه‬ ‫يعني‬ ‫فهذا‬ ‫المؤشر‬ ‫قبل‬
‫الذاكرة‬ ‫في‬
‫مثال‬:
int x=4;
int*p;
p=&x;
cout <<p // ‫الذاكرة‬ ‫في‬ ‫العنصر‬ ‫مكان‬
2-‫اشارة‬)*(‫المؤشر‬ ‫لتعريف‬ ‫تستخدم‬
‫كالتالي‬ ‫المؤشر‬ ‫بتعريف‬ ‫نقوم‬:int *p(‫النوع‬ ‫من‬ ‫مؤشر‬ ‫بتعريف‬ ‫قمنا‬int)
‫مثال‬:
int x=4;
int*p;
p=&x;
cout <<*p // x ‫قيمة‬ ‫سيكون‬ ‫الخرج‬
7-‫المؤشر‬ ‫نستخدم‬ ‫ان‬ ‫اما‬ ‫بطريقتين‬ ‫المصفوفة‬ ‫عناصر‬ ‫الى‬ ‫اإلشارة‬ ‫يمكن‬
‫اي‬ ‫كالمصفوفة‬p[i].‫نضع‬ ‫او‬*(p+i)
#include<iostream>
using namespace std;
void main()
{
double y;
double *p //‫المؤشر‬ ‫بتعريف‬ ‫قمنا‬
p=&y; // ‫الذاكر‬ ‫في‬ ‫العنصر‬ ‫مكان‬ ‫على‬ ‫يؤشر‬ ‫المؤشر‬‫ة‬
cin>>*p; // ‫الرقم‬ ‫ادخل‬ ‫المستخدم‬ ‫ان‬ ‫نفرض‬9
47cout<<*p; // 9
cout<<p; // ‫الذاكرة‬ ‫في‬ y ‫عنوان‬ ‫سيكون‬ ‫الخرج‬
}
‫قيم‬ ‫بتبديل‬ ‫يقوم‬ ‫برنامج‬ ‫اكتب‬x , y‫المؤشرات‬ ‫باستخدام‬
#include<iostream>
using namespace std;
void main()
{
int*p, *t;
int x , y ;
cin>>x>>y;
p=&x; t=&y; // ‫الذاكرة‬ ‫في‬ ‫المتغيرين‬ ‫مكان‬ ‫الى‬ ‫اشرنا‬
swap(*p,*t)
cout<<”x= ”<<*p<<endl; // x ‫قيمة‬ ‫سيكون‬ ‫الخرج‬
cout<<”y= “<<*t<<endl; // y ‫قيمة‬ ‫سيكون‬ ‫الخرج‬
cout<<p<<endl<<t<<endl;
(‫عناوين‬ ‫سيكون‬ ‫الخرج‬x, y)
}
‫مالحظة‬:
‫في‬‫الساكنة‬ ‫المصفوفة‬(static array)‫مؤشر‬ ‫المصفوفة‬ ‫اسم‬ ‫يعتبر‬"‫ثابت‬"‫على‬
‫عنصر‬ ‫اول‬
‫مالحظة‬:‫المكان‬ ‫لنفس‬ ‫يؤشران‬ ‫لمؤشر‬ ‫مؤشر‬ ‫اسناد‬
‫مالحظة‬:‫حلقة‬ ‫بإنشاء‬ ‫نقوم‬ ‫المؤشر‬ ‫باستخدام‬ ‫مصفوفة‬ ‫عناصر‬ ‫لطباعة‬for
‫فيها‬ ‫ونضع‬cout<<*(p+i)‫الجمع‬ ‫من‬ ‫اقوى‬ ‫الضرب‬ ‫الن‬ ‫االقواس‬ ‫نضع‬ ‫حيث‬
‫الترتيب‬ ‫ذو‬ ‫العنصر‬ ‫الى‬ ‫المؤشر‬ ‫وينتقل‬(p+i)َ‫فإن‬ ‫وضعهم‬ ‫عدم‬ ‫حال‬ ‫وفي‬
‫المؤشر‬ ‫قيمة‬ ‫سيكون‬ ‫الخرج‬+i
‫مثال‬:
#include<iostream>
using namesapce std;
const int size=5;
void main()
20 30 40 5 2
48{
int x[size];
for(int i=0; i<size; i++)
cin>>x[i];
cout<<*x; 20
int *p;
p=&x[0]; // or we write p=x ;
cout<<*p<<endl; 20
cout<<p<<endl; address of p
p p+1 p+2 p+3 p+4
x x+1 x+2 x+3 x+4
cout<<*++p; 30
cout<<*p++; 30
cout<<*p++; 40
cout<<*++x; error x is constant
cout<<p[0]; 5
cout<<p[1]; 2
cout<<*(p-1); 40
}
‫البعد‬ ‫احادية‬ ‫مصفوفة‬ ‫بتعريف‬ ‫به‬ ‫تقوم‬ ‫برنامج‬ ‫اكتب‬
1-‫يقوم‬ ‫تابع‬ ‫واستدعاء‬‫المؤشرات‬ ‫باستخدام‬ ‫مصفوفة‬ ‫عناصر‬ ‫بادخال‬
2-‫المؤشرات‬ ‫باستخدام‬ ‫المصفوفة‬ ‫هذه‬ ‫عناصر‬ ‫بطباعة‬ ‫يقوم‬ ‫اخر‬ ‫تابع‬ ‫واستدعاء‬
3-‫قم‬‫عنصر‬ ‫اصغر‬ ‫مع‬ ‫المصفوفة‬ ‫في‬ ‫عنصر‬ ‫اكبر‬ ‫باستبدال‬ ‫يقوم‬ ‫تابع‬ ‫باستدعاء‬
‫والعكس‬
4-‫اكبر‬ ‫وارجاع‬ ‫المصفوفة‬ ‫في‬ ‫المنتصف‬ ‫العنصر‬ ‫بارجاع‬ ‫يقوم‬ ‫تابع‬ ‫باستدعاء‬ ‫قم‬
‫عنصر‬
‫الحل‬ ‫طريقة‬
20 30 40 5 2
49‫في‬ ‫يوجد‬ ‫الذي‬ ‫العنصر‬ ‫ناخذ‬ ‫فاننا‬ ‫فردية‬ ‫المصفوفة‬ ‫كانت‬ ‫اذا‬ ‫االخير‬ ‫الطلب‬ ‫في‬
‫كانت‬ ‫اذا‬ ‫اما‬ ‫المصفوفة‬ ‫منتصف‬‫المتوسط‬ ‫ناخذ‬ ‫فاننا‬ ‫زوجيه‬
‫فردية‬ ‫المصفوفة‬ ‫اساس‬ ‫على‬ ‫الحل‬‫هو‬ ‫والعنصر‬ar+2:
#include<iostream>
using namespace std;
const int size=5;
void input(int ar[])
{
int*p;
p=ar;
for(int i=0; i<size; i++)
cin>>p[i];
}
void print(int ar[])
{
int*p;
p=ar;
for(int i=0; i<size; i++)
cout<<ar[i]<<" ";
cout<<endl;
}
void swap(int ar[])
{
int pos=0 , pos2=0;
int max=ar[0];
int min=ar[0];
for(int i=1; i<size; i++)
{
if(ar[i]>max)
{
max=ar[i];
pos=i;
}
if(ar[i]<min)
{
min=ar[i];
pos2=i;
50}
}
swap(ar[pos],ar[pos2]);
}
int n0(int ar[] , int&mont)
{
int max=ar[0];
for(int i=1; i<size; i++)
if(ar[i]>max)
max=ar[i];
mont= *(ar+2);
return max;
}
void main()
{
int ar[size],mont;
input(ar);
print(ar);
swap(ar);
print(ar);
cout<<"max= "<<n0(ar,mont);
cout<<"the middel element is"<<mont;
}
End of presentation
6
51Presentation 7
string & Dynamic array
‫الديناميكية‬ ‫المصفوفة‬(Dynamic array)‫بادخال‬ ‫المستخدم‬ ‫يقوم‬ ‫مصفوفة‬ ‫هي‬
‫كالتالي‬ ‫مؤشر‬ ‫طريق‬ ‫عن‬ ‫حجمها‬:
int*p=new int[size];
‫تعليمة‬ ‫ان‬ ‫حيث‬new‫الديناميكي‬ ‫للحجز‬ ‫الذاكرة‬ ‫تجهز‬
#include<iostream>
using namespace std;
void main()
{
int size;
cin>>size;
int *p =new int[size];
}
‫أرقام‬ ‫بإدخال‬ ‫قم‬ ‫ثم‬ ‫الطالب‬ ‫عدد‬ ‫بادخال‬ ‫المستخدم‬ ‫فيه‬ ‫يقوم‬ ‫برنامج‬ ‫بكتابه‬ ‫قم‬ID
‫ثم‬ ‫األرقام‬ ‫هذه‬ ‫وتخزين‬ ‫للطالب‬‫معطى‬ ‫طالب‬ ‫رقم‬ ‫عن‬ ‫بالبحث‬ ‫يقوم‬ ‫تابع‬ ‫استخدم‬
‫القيمة‬ ‫ويعيد‬ ‫المستخدم‬ ‫قبل‬ ‫من‬true‫موجود‬ ‫الرقم‬ ‫كان‬ ‫اذا‬ ‫الطالب‬ ‫وترتيب‬
‫الحل‬ ‫طريقة‬:
‫المصفوفة‬ ‫حجم‬ ‫هنا‬ ‫يمثل‬ ‫الطالب‬ ‫عدد‬(‫ديناميكيه‬ ‫مصفوفة‬ ‫نستخدم‬)
‫بالمصفوفة‬ ‫يمثلون‬ ‫الطالب‬ ‫ارقام‬
‫الحل‬
52#include<iostream>
using namespace std;
bool sear(int *p , int size , int &pos, int id)
{
for(int i=0; i<size; i++)
if(*(p+i)==id)
{
pos=i+1;
return true;
}
return false;
}
void main()
{
cout<<”enter the size of matrixn”;
int size,id,pos;
cin>>size;
int*ID=new int[size];
cout<<”enter the ID of the studentn”;
for(int i=0; i<size; i++)
cin>>*(ID+i);
cout<<”enter the random IDn”;
53cin>>id;
bool a=sear(ID,size,pos,id);
if(a==true)
cout<<”the number is found in the ”
<<pos<<endl;
else
cout<<”the number is not foundn”;
}
‫مالحظة‬:‫وهو‬ ‫دخل‬ ‫لها‬ ‫يكون‬ ‫الديناميكية‬ ‫المصفوفة‬ ‫على‬ ‫تطبق‬ ‫التي‬ ‫التوابع‬ ‫دوما‬
‫عنه‬ ‫صورة‬ ‫لياخذ‬ ‫المصفوفة‬ ‫حجم‬
String
‫ال‬string‫بالشكل‬ ‫تعريفها‬ ‫ويتم‬ ‫المحارف‬ ‫مصفوفة‬ ‫هي‬string s;
#include<iostream>
using namespace std;
void main()
{
string s;
cin>>s; //”ahmad ali”
}
‫بالشكل‬ ‫مصفوفة‬ ‫تشكل‬ ‫سوف‬ ‫الجملة‬ ‫هذه‬damha
54‫ال‬ ‫ان‬ ‫حيث‬cin‫ال‬ ‫في‬string‫الفراغ‬ ‫اليقرأ‬
‫مكتبه‬ ‫تضمين‬ ‫علينا‬ ‫المشكلة‬ ‫هذه‬ ‫ولتفادي‬<string>‫واستخدام‬
‫التعليمة‬getline(cin,s)‫والفراغ‬ ‫الكلمة‬ ‫تظهر‬ ‫التي‬
‫وضعنا‬ ‫إذا‬string s2=”ziad Hasan”;‫مصفوفة‬ ‫لدينا‬ ‫يتشكل‬ ‫فسوف‬
‫بالشكل‬
nasaHdaiz
‫وضعنا‬ ‫واذا‬string s3(s2,4)‫من‬ ‫االولى‬ ‫االربعة‬ ‫االحرف‬ ‫ناخذ‬ ‫اننا‬ ‫يعني‬ ‫فهذا‬
‫ال‬string s2
‫ال‬ ‫لجمع‬string‫نضع‬string‫كالتالي‬ ‫جديدة‬:
string s4 =s2+”hi”;
‫ال‬ ‫حجم‬ ‫لمعرفة‬string‫التابع‬ ‫نستخدم‬ ‫فاننا‬s2.size()
‫مثال‬
#include<iostream>
#include<string>
using namespace std;
void main()
{
string s1="zaid hassan";
cout<< "The size of s1 is " << s1.size() << " characters.n";
55}
‫ال‬ ‫طباعة‬ ‫يمكن‬string‫بالشكل‬ ‫المصفوفة‬ ‫باستخدام‬
for(int i=0; i<size; i++)
cout<<s1[i]<<” “;
‫تعليمة‬isalpha(s[0])‫حرف‬ ‫المحارف‬ ‫مصفوفة‬ ‫من‬ ‫عنصر‬ ‫اول‬ ‫كان‬ ‫اذا‬ ‫تختبر‬
‫ال‬ ‫ام‬
‫مثال‬
#include<iostream>
#include<string>
using namespace std;
void main()
{
string s1="zaid hassan";
cout<< "The size of s1 is " << s1.size() << " characters.n";
if(isalpha(s1[0]))
cout<<true<<endl;
else
cout<<false<<endl;
}
‫تعليمة‬isdigit(s[0])‫او‬ ‫رقم‬ ‫المحارف‬ ‫مصفوفة‬ ‫من‬ ‫عنصر‬ ‫اول‬ ‫كان‬ ‫اذا‬ ‫تختبر‬
‫ال‬
56#include<iostream>
#include<string>
using namespace std;
void main()
{
string s1="zaid hassan";
cout<< "The size of s1 is " << s1.size() << " characters.n";
if(isdigit(s1[0]))
cout<<true<<endl;
else
cout<<false<<endl;
}
‫تعليمة‬ispunct(s[0])‫المحارف‬ ‫مصفوفة‬ ‫من‬ ‫عنصر‬ ‫اول‬ ‫كان‬ ‫اذا‬ ‫تختبر‬
‫المتبقية‬ ‫المفاتيح‬ ‫لوحة‬ ‫اجزاء‬ ‫من‬ ‫جزء‬
#include<iostream>
#include<string>
using namespace std;
void main()
57{
string s1="zaid hassan";
cout<< "The size of s1 is " << s1.size() << " characters.n";
if(ispunct(s1[0]))
cout<<true<<endl;
else
cout<<false<<endl;
}
‫الحرف‬ ‫الستبدال‬ ‫برنامج‬ ‫بكتابة‬ ‫قم‬‘a’‫بالحرف‬‘k’
#include<iostream>
#include<string>
using namespace std;
void main()
{
string s;
getline(cin,s);
cout<<s;
for(int i=0; i<s.size();i++)
if(s[i]==’a’)
s[i]=’k’;
}
M h d G h a y t h
58‫الحرف‬ ‫باستبدال‬ ‫يقوم‬ ‫تابع‬ ‫بكتابه‬ ‫قم‬‘k’‫بالحرف‬‘a’‫بكتابة‬ ‫وقم‬‫يقوم‬ ‫برنامج‬
‫التابع‬ ‫هذا‬ ‫باستدعاء‬
#include<iostream>
#include<string>
using namespace std;
void replace(string s)
{
for(int i=0; i<size; i++)
if(s[i]==’a’)
s[i]=’k’;
}
void main()
{
string s;
getline(cin ,s);
replace(s);
cout<<s<endl;
}
‫احرف‬ ‫عدد‬ ‫بحساب‬ ‫يقوم‬ ‫تابع‬ ‫بكتابة‬ ‫قم‬string‫ارقام‬ ‫عدد‬ ‫وحساب‬‫ها‬
#include<iostream>
#include<string>
void xx(string s, int&nw , int&nd)
59{
nw=0; nd=0;
for(int i=0; i<s.size(); i++)
{
if(isalpha(s[i]))
nw++;
if(isdigit(s[i]))
nd++;
}
}
void main()
{
string k;
getline(cin,k);
int nw,nd;
xx(k,nw,nd);
cout<<”the number of the word is ”<<nw<<endl
<<”and the number of the digit is “<<nd<<endl;
}
‫مراجعة‬ ‫تمرين‬‫لل‬Dynamic array
‫هؤالء‬ ‫رواتب‬ ‫بادخال‬ ‫وقم‬ ‫العمال‬ ‫عدد‬ ‫بادخال‬ ‫المستخدم‬ ‫فيه‬ ‫يقوم‬ ‫برنامج‬ ‫بكتابة‬ ‫قم‬
‫راتب‬ ‫أعلى‬ ‫بايجاد‬ ‫يقوم‬ ‫تابع‬ ‫باستدعاء‬ ‫وقم‬ ‫العمال‬‫بينهم‬
60#include<iostream>
using namespace std;
double maxA(double*m, int size)
{
double max=m[0];
for(int i=1; i<size; i++)
if(m[i]>max)
max=m[i];
return max;
}
void main()
{
cout<<”enter the number of the worker : ”;
int n;
cin>>n;
double *p = new double[n];
cout<<”enter the value of the matrixn”;
for(int i=0; i<n; i++)
cin>>p[i];
cout<<”max= “<<maxA(p,n)<<endl;
}
End of presentation 7
61Presentation 8
struct
struct:‫المعطيات‬ ‫انماط‬ ‫من‬ ‫جديد‬ ‫نوع‬ ‫لتعريف‬ ‫معطيات‬ ‫بنى‬ ‫عن‬ ‫عبارة‬ ‫هي‬
‫ا‬ ‫قبل‬ ‫وتوضع‬‫ل‬void main()
‫مثال‬:
#include<iostream>
#include<string>
using namespace std;
struct employee
{
int id;
double salary;
string name;
};
void main()
{
employee e;
cout<<”enter the id of the employee : ”;
cin>>e.id;
cout<<”enter the salary of the employee : ”;
cin>>e.salary;
cout<<”the name of the employee : ”;
‫معطيات‬ ‫نمط‬ ‫بتعريف‬ ‫قمنا‬
‫لموظف‬
62cin.ignore ();
getline(cin,e.name);
cout<<e.name<<endl;
}
‫نضع‬Ignore‫تعليمة‬ ‫اي‬ ‫قبل‬getline‫حزان‬ ‫من‬ ‫الزائدة‬ ‫االحرف‬ ‫بمسح‬ ‫لتقوم‬
‫الدخل‬
‫المستخدم‬ ‫فيه‬ ‫يقوم‬ ‫برناج‬ ‫بكتابه‬ ‫قم‬‫موظف‬ ‫لكل‬ ‫ان‬ ‫حيث‬ ‫الموظفين‬ ‫عدد‬ ‫بادخال‬
‫اسم‬,‫رقم‬,‫المعلومات‬ ‫طباعه‬ ‫ثم‬ ‫الموظفين‬ ‫معلومات‬ ‫بادخال‬ ‫قم‬ ‫ثم‬ ‫راتب‬
#include<iostream>
#include<string>
using namespace std;
struct employee
{
int id;
double salary;
string name;
};
void main()
{
cout<<"the number of the workers isn";
int n;
63cin>>n;
employee *p=new employee [n];
for(int i=0; i<n; i++)
{
cout<<"please enter the ID of the worker n";
cin>>p[i].id;
cin.ignore();
cout<<"enter the salary of the workern";
cin>>p[i].salary;
cin.ignore();
cout<<"enter the worker namen";
cin.ignore();getline(cin,p[i].name);
}
for(int i=0; i<n; i++)
{
cout<<"the ID of the "<<i+1<<" worker is "<<p[i].id<<endl;
cout<<"the salary of the "<<i+1<<" worker is
"<<p[i].salary<<endl;
cout<<"the name of the "<<i+1<<" worker is
"<<p[i].name<<endl;
cout<<"nn";
}
64‫بإدخال‬ ‫ثم‬ ‫الموظفين‬ ‫عدد‬ ‫إدخال‬ ‫المستخدم‬ ‫ليستطيع‬ ‫السابق‬ ‫البرنامج‬ ‫بتعديل‬ ‫قم‬
‫لكل‬ ‫رواتب‬ ‫لخمس‬ ‫الحسابي‬ ‫المتوسط‬ ‫مع‬ ‫معلوماتهم‬ ‫وطباعة‬ ‫الموظفين‬ ‫معلومات‬
‫موظف‬
‫الحل‬:
#include<iostream>
#include<string>
using namespace std;
struct employee
{
int id;
double salary;
string name;
};
void main()
{
double sum;
cout<<"the number of the workers isn";
int n;
cin>>n;
employee *p=new employee [n];
for(int=0; i<n; i++)
65{
sum=0;
cout<<"plz enter the ID of the worker n";
cin>>p[i].id;
cin.ignore();
cout<<"enter the salary of the workern";
for(int j=0;j<5;j++)
{
cin>>p[i].salary[j];
cin.ignore();
}
cout<<"enter the worker namen";
cin.ignore();getline(cin,p[i].name);
}
for(int i=0; i<n; i++)
{
sum=0;
cout<<"the ID of the "<<i+1<<" worker is
"<<p[i].id<<endl;
for(int j=0;j<5;j++)
{
sum=sum+p[i].salary[j];
66cout<<"the salary of the "<<i+1<<" worker is
"<<p[i].salary[j]<<endl;
}
cout<<"the name of the "<< i+1<<" worker is
"<<p[i].name<<endl;
cout<<"avg="<<sum/5<<endl;
cout<<"nn";
}
}
‫لل‬ ‫بالنسبة‬ ‫موظف‬ ‫عن‬ ‫بالبحث‬ ‫يقوم‬ ‫تابع‬ ‫بكتابه‬ ‫قم‬id‫التابه‬ ‫هذا‬ ‫ويقوم‬ ‫به‬ ‫الخاص‬
‫وجد‬ ‫ان‬ ‫ارقمه‬ ‫و‬ ‫ودخله‬ ‫الموظف‬ ‫اسم‬ ‫بطباعة‬
‫الحل‬:
#include<iostream>
#include<string>
using namespace std;
struct employee
{
int id;
double salary;
string name;
};
67void main()
{
double sum;
cout<<"the number of the workers isn";
int n;
cin>>n;
employee *p=new employee [n];
for(int=0; i<n; i++)
{
sum=0;
cout<<"plz enter the ID of the worker n";
cin>>p[i].id;
cin.ignore();
cout<<"enter the salary of the workern";
for(int j=0;j<5;j++)
{
cin>>p[i].salary[j];
}
cout<<"enter the worker namen";
cin.ignore();getline(cin,p[i].name);
}
68for(int i=0; i<n; i++)
{
sum=0;
cout<<"the ID of the "<<i+1<<" worker is
"<<p[i].id<<endl;
for(int j=0;j<5;j++)
{
sum=sum+p[i].salary[j];
cout<<"the salary of the "<<i+1<<" worker is
"<<p[i].salary[j]<<endl;
}
cout<<"the name of the "<< i+1<<" worker is
"<<p[i].name<<endl;
cout<<"avg="<<sum/5<<endl;
cout<<"nn";
}
int k,pos;
cout<<"pleas enter the random ID to search if it found /
not : ";
cin>>k;
bool a=sear(p,n,k,pos);
if(a==true)
{
69cout<<"the given ID is foundn "<<"the ID is
"<<p[pos].id<<endl
<<"the name of the worker is "<<p[pos].name<<endl;
}
else
cout<<"not foundn";
}
End of presentation 8
End of programming 1
Good luck ^_^
‫الفهرس‬
Presentation 1 page 1
Presentation 2 page 5
Presentation 3 page 11
Presentation 4 page 16
Presentation 5 page 22
Presentation 6 page 46
Presentation 7 page 51
Presentation 8 page 61
Eng -Mohammad konbos
Mhd Ghayth Alsawaf

More Related Content

DOC
البرمجة+ الستركجر
PDF
Shannon code & shannon fano & huffman method - chapter three
PDF
ٍSource Entropy - binary symmetric channe - chapter one - two
PDF
Repatino code - hamming code (7,4) - chapter four
PDF
رياضيات سادس علمي
PPTX
Chapter 4
PDF
ملزمة رياضيات سادس علمي _ العراق
PDF
الافكار البرمجية
البرمجة+ الستركجر
Shannon code & shannon fano & huffman method - chapter three
ٍSource Entropy - binary symmetric channe - chapter one - two
Repatino code - hamming code (7,4) - chapter four
رياضيات سادس علمي
Chapter 4
ملزمة رياضيات سادس علمي _ العراق
الافكار البرمجية

What's hot (20)

PPTX
13th session python fourth gui
PDF
السلسة27
PDF
مراجعة مركزة -قصي هاشم 2015
PDF
M.f ammar
PPTX
Chapter2
PDF
ملزمة الرياضيات للصف الثالث متوسط
PPTX
4- Arrays
DOCX
مقرر معالجة البيانات
PPTX
15th session python sixth gui
PPTX
14th session python fifth gui
PDF
c# المحاضره 4 @ 5 في
PPTX
3- Functions
PPTX
16th session python seventh gui
PDF
أساسيات الرياضيات بأسلوب بسيط -باسم المياحي
PDF
الرياضيات للصف الثالث متوسط
PPTX
12th session python third gui
PDF
الفصل الاول
PPTX
1- Languages Basics
PPTX
[C++ Tutorial] #6- Pointers
PDF
الفصل الرابع
13th session python fourth gui
السلسة27
مراجعة مركزة -قصي هاشم 2015
M.f ammar
Chapter2
ملزمة الرياضيات للصف الثالث متوسط
4- Arrays
مقرر معالجة البيانات
15th session python sixth gui
14th session python fifth gui
c# المحاضره 4 @ 5 في
3- Functions
16th session python seventh gui
أساسيات الرياضيات بأسلوب بسيط -باسم المياحي
الرياضيات للصف الثالث متوسط
12th session python third gui
الفصل الاول
1- Languages Basics
[C++ Tutorial] #6- Pointers
الفصل الرابع
Ad

Similar to Programming 1 full (20)

PDF
أسئلة وإجابتها علي منهج الصف الثالث الاعدادي فصل دراسي ثاني
PDF
Basic functions in Excel
PDF
1com3mbachir
PPTX
ال_تFrequency tables وزيعات التكرارية.pptx
PDF
Computer school-books-3rd-preparatory-1st-term-khawagah-2019-6
PPTX
المحاضرة 16.pptxvvhvhi6hrMhfMjfzmhfjtditsitsi
PPTX
Chapter 1
PDF
PPTX
Chapter 3
PPT
lec1_visual basic2010_visual basic20.ppt
PDF
ملخص الاحصاء التطبيقي - الوحدة الرابعة
PDF
ملخص الاحصاء التطبيقي - الوحدة الخامسة
PDF
أختبارات الصف السابع
PDF
الفصل الأول البيانات بالكامل الترم الثانى
PDF
2 variables and constants
PDF
شيماء موسى
PDF
نظم-العد.pdf
PDF
Computer 3rd-preparatory-second-term-khawagah-6
أسئلة وإجابتها علي منهج الصف الثالث الاعدادي فصل دراسي ثاني
Basic functions in Excel
1com3mbachir
ال_تFrequency tables وزيعات التكرارية.pptx
Computer school-books-3rd-preparatory-1st-term-khawagah-2019-6
المحاضرة 16.pptxvvhvhi6hrMhfMjfzmhfjtditsitsi
Chapter 1
Chapter 3
lec1_visual basic2010_visual basic20.ppt
ملخص الاحصاء التطبيقي - الوحدة الرابعة
ملخص الاحصاء التطبيقي - الوحدة الخامسة
أختبارات الصف السابع
الفصل الأول البيانات بالكامل الترم الثانى
2 variables and constants
شيماء موسى
نظم-العد.pdf
Computer 3rd-preparatory-second-term-khawagah-6
Ad

Programming 1 full

  • 1. ‫الدولية‬ ‫العربية‬ ‫الجامعة‬ ‫المعل‬ ‫الهندسة‬ ‫كلية‬‫و‬‫م‬‫واالتصاالت‬ ‫اتية‬ Dr. konbos 2012-2013 Arab international University Programming 1 Faculty of Informatics Engineering
  • 2. 1Presentation 1 ‫مراجعة‬: ‫تعليمة‬cout<<X++‫قسمين‬ ‫الى‬ ‫مقسومة‬ ‫تعليمة‬ ‫هي‬: ‫ثم‬ ‫ومن‬ ‫العنصر‬ ‫طباعة‬‫اضافة‬"1"‫للعنصر‬ cout<<x; X=x+1; ‫تعليمة‬ ‫اما‬cout<<++x;‫تعني‬ ‫فهي‬ ‫زيادة‬"1"‫العنصر‬ ‫طباعة‬ ‫ثم‬ ‫ومن‬ ‫للعنصر‬ X=x+1; cout<<x; ‫تعليمة‬cout<<setw(x);‫الكتابة‬ ‫مؤشر‬ ‫الزاحة‬ ‫تعليمة‬ ‫هي‬x‫مرة‬‫وتحتاج‬ ‫المكتبة‬ ‫لتضمين‬<iomanip> ‫مالحظة‬: ‫حلقة‬ ‫باستخدام‬ ‫نقوم‬ ‫ال‬ ‫التكرار‬ ‫مرات‬ ‫عدد‬ ‫معرفة‬ ‫عدم‬ ‫حال‬ ‫في‬for‫هذه‬ ‫في‬ ‫اي‬ ‫الشرط‬ ‫ينكسر‬ ‫ان‬ ‫الى‬ ‫نكرر‬ ‫الحلقة‬ ‫تمرين‬: ‫بادخال‬ ‫المستخدم‬ ‫يقوم‬ ‫مجال‬ ‫ضمن‬ ‫االعداد‬ ‫مجموع‬ ‫يحسب‬ ‫برنامج‬ ‫بكتابة‬ ‫قم‬ ‫وهو‬ ‫فيه‬ ‫رقم‬ ‫اصغر‬x‫وهو‬ ‫فيه‬ ‫رقم‬ ‫واكبر‬y: ‫الحل‬ ‫طريقة‬: 1-‫العنصران‬ ‫بادخال‬ ‫المستخدم‬ ‫يقوم‬x & y 2-‫من‬ ً‫ا‬‫بدء‬ ‫عدادها‬ ‫يسير‬ ‫حلقة‬ ‫باستخدام‬ ‫المجموع‬ ‫بحساب‬ ‫نقوم‬x‫عند‬ ‫وينته‬y ‫الحل‬:
  • 3. 2#include<iostream> using namespace std; void main() { int x,y ,sum=0; cin>>x>>y; for(int i=x; i<=y; i++) sum=sum+I; cout<<”sum= ”<<sum<<endl; } ‫لعددين‬ ‫االكبر‬ ‫المشترك‬ ‫القاسم‬ ‫بحساب‬ ‫يقوم‬ ‫برنامج‬ ‫بكتابة‬ ‫قم‬ ‫الحل‬ ‫طريقة‬: 1-‫الرقمين‬ ‫بادخال‬ ‫المستخدم‬ ‫يقوم‬ 2-‫الرقم‬ ‫بطرح‬ ‫نقوم‬‫الكبير‬ ‫الرقم‬ ‫من‬ ‫الصغير‬‫فيكون‬ ‫الرقمان‬ ‫يتساوى‬ ‫حتى‬ ‫للرقمين‬ ‫االكبر‬ ‫المشترك‬ ‫القاسم‬ ‫هو‬ ‫النهائي‬ ‫الناتج‬. ‫الحل‬: #include<iostream> using namespace std; void main() { int x,y;
  • 4. 3cout<<”enter the tow variable x,yn”; cin>>x>>y; while(x!=y) { if(x>y) x=x-y else y=y-x; } cout<<”the maximum diverse is ”<<x<<endl; } ‫مالحظة‬:‫القسمة‬ ‫باق‬ ‫يكون‬ ‫دوما‬‫من‬ ‫المجال‬ ‫من‬ ‫محصور‬0‫العدد‬-1 ‫تمرين‬: ‫مجموع‬ ‫بحساب‬ ‫يقوم‬ ‫برنامج‬ ‫بكتابة‬ ‫قم‬‫عدد‬ ‫خانات‬‫بادخاله‬ ‫المستخدم‬ ‫يقوم‬ ‫الحل‬ ‫طريقة‬: ‫على‬ ‫القسمة‬ ‫باق‬ ‫طريقة‬ ‫نستخدم‬10‫على‬ ‫العدد‬ ‫نقسم‬ ‫ثم‬ ‫من‬ ‫و‬ ‫الخانات‬ ‫لتجزئة‬10 ‫مثال‬:‫العدد‬3987‫بالشكل‬ ‫خانته‬ ‫حساب‬ ‫المطلوب‬7+8+9+3=23 ‫الحل‬:
  • 5. 4#include<iostream> using namespace std; void main() { int x,sum=0; cin>>x; while(x!=0) { sum=sum+x%10; x=x/10; } cout<<”sum=”<<sum<<endl; } End of Presentation 1
  • 6. 5Presentation 2 ‫المراجعة‬ ‫تتمة‬: ‫بادخال‬ ‫يقوم‬ ‫برنامج‬ ‫بكتابة‬ ‫قم‬10‫عدد‬ ‫واحسب‬ ‫رقم‬ ‫واصغر‬ ‫اكبر‬ ‫واوجد‬ ‫ارقام‬ ‫فيه‬ ‫الفردية‬ ‫و‬ ‫الزوجية‬ ‫االعداد‬: ‫الحل‬ ‫طريقة‬: ‫نقوم‬‫حلقة‬ ‫بانشاء‬for‫االكبر‬ ‫العنصر‬ ‫هو‬ ‫عنصر‬ ‫اول‬ ‫نعتبر‬ ‫ثم‬ ‫العناصر‬ ‫الدخال‬ ‫واالصغر‬‫ونق‬‫العناصر‬ ‫بقية‬ ‫به‬ ‫ارن‬. ‫على‬ ‫العدد‬ ‫قسمة‬ ‫باق‬ ‫كان‬ ‫اذا‬2=0‫يزيد‬ ‫الزوجية‬ ‫االعداد‬ ‫عداد‬ ‫نضع‬1 ‫الفردية‬ ‫االعداد‬ ‫عداد‬ ‫نزيد‬ ‫ذلك‬ ‫وغير‬1 ‫الحل‬: #include<iostream> using namespace std; void main() { int x,max,e=0,o=0; cout<<”enter the first valuen”; cin>>x; if(x%2==0) e++;
  • 7. 6else o++; max=x; for(int i=1; i<=9; i++) cout<<”enter the either elementn”; { cin>>x; If(x%2==0) e++; else o++; If(x>max) max=x; } Cout<<”the maximum value is”<<max<<endl<<”the count of the even number is “<<e<<endl<<”the count of the odd number is ”<<o<<endl; } ‫برنامج‬ ‫بكتابه‬ ‫قم‬‫الدخال‬ID‫ورواتب‬10‫واظهار‬ ‫موظفين‬ID‫االعلى‬ ‫الموظف‬ ‫راتب‬‫بينهم‬‫رابته‬ ‫يبلغ‬ ‫وكم‬ ‫الحل‬
  • 8. 7 #include<iostream> using namespace std; void main() { const int s=10; int salary,num,id; cout<<"enter salary & ID for a first worker"<<endl; cin>>salury>>id; int max=salary; num=id; for(int i=1;i<s;i++) { cout<<"enter salary & ID for worker number "<<i+1<<endl; cin>> salary >>id; if(salary>max) { num=id;
  • 9. 8max= salary; } } cout<<"max= "<<max<<endl<<"ID Number= "<<num<<endl; } ‫التالي‬ ‫الشكل‬ ‫بطباعة‬ ‫يقوم‬ ‫برنامج‬ ‫بكتابة‬ ‫قم‬: ‫تعليمة‬ ‫نستخدم‬setw‫التمرين‬ ‫هذا‬ ‫في‬ ‫الحل‬ #include<iostream> #include<iomanip> using namespace std; void main() { int k; cin>>k; for(int i=0; i<10; i++)
  • 10. 9{ cout<<setw(k); for(int j=0; j<=i; j++) cout<<”* ”; cout<<endl; } k--; } ‫الدخال‬ ‫تمرين‬ ‫بكتابة‬ ‫قم‬5‫ل‬ ‫عالمات‬4‫المتوسط‬ ‫وعرض‬ ‫بحساب‬ ‫قم‬ ‫ثم‬ ‫طالب‬ ‫طالب‬ ‫لكل‬ ‫العالمات‬ ‫هذه‬ ‫لمجموع‬ ‫الحسابي‬ ‫الحل‬: #include<iostream> using namespace std; void main() { float avg, sum, mark; for(int i=0; i<4; i++) { cout<<”please enter a 5 mark for the studentn”; sum=0; for(int j=0; j<5; j++)
  • 11. 10{ cin>>mark; sum=sum+mark; } avg=sum/5; cout<<”avg for the ”<<i+1<<” student is “<<avg<<endl; } } End of Presentation 2 End of the review
  • 12. 11Presentation 3 ‫البعد‬ ‫االحادية‬ ‫المصفوفة‬(one dimensional array: ) 1-‫ال‬ ‫المصفوفات‬‫سكانة‬(static array)‫ثابت‬ ‫المصفوفة‬ ‫حجم‬(const)‫نعرفه‬ ‫عام‬ ‫متحول‬ ‫شكل‬ ‫على‬(global)‫ال‬ ‫قبل‬ ‫اي‬void main‫نستطيع‬ ‫كي‬ ‫اليه‬ ‫الوصل‬ ‫البرنامج‬ ‫اجزاء‬ ‫كل‬ ‫من‬ 2-‫الديناميكية‬ ‫المصفوفة‬(Dynamic array)‫القادمة‬ ‫بالبحوث‬ ‫معنا‬ ‫ستمر‬ ‫مالحظة‬: ‫مصفوفة‬ ‫عناصر‬ ‫لعكس‬‫طريقتان‬ ‫يوجد‬: ‫جديدة‬ ‫مصفوفة‬ ‫بانشاء‬ ‫نقوم‬ ‫انا‬ ‫اما‬Y‫المصوف‬ ‫حجم‬ ‫نفس‬ ‫لها‬‫ة‬X‫يلي‬ ‫كما‬: #include<iostream> using namespace std; const int size=4; void main() { int x[size]; cout<<”enter the value of the matrixn”; for (int i=0; i<size; i++) cin>>x[i]; cout<<”the reverse matrix of xn”; int y[size];
  • 13. 12for(int i=0; i<size; i++) y[i]=x[size-i-1]; for(int i=0; i<size; i++) cout<<y[i]<<” “; cout<<endl; } ‫تعليمة‬ ‫باستخدام‬ ‫نقوم‬ ‫او‬swap‫ببعضهما‬ ‫عنصرين‬ ‫لتبديل‬ ‫جاهزة‬ ‫تعليمة‬ ‫وهي‬ ‫مالحظة‬:‫المصفوفة‬ ‫عناصر‬ ‫نعكس‬ ‫عندما‬ ‫فقط‬‫تعليمة‬ ‫نستخدم‬ ‫عندما‬swap ‫صحيحة‬ ‫نتائج‬ ‫على‬ ‫للحصول‬ ‫المصفوفة‬ ‫حجم‬ ‫نصف‬ ‫الى‬ ‫يسير‬ ‫العداد‬ ‫نجعل‬ #include<iostream> using namespace std; const int size=5; void main() { int x[size]; cout<<”enter the value of the matrixn”; for(int i=0; i<size; i++) cin>>x[i]; for(int i=0; i<size/2; i++) swap(x[i],x[size-1-i]); for(int i=0; i<size; i++) cout<<x[i]<<” “;
  • 14. 13cout<<endl; } ً‫ا‬‫تصاعدي‬ ‫البعد‬ ‫أحادية‬ ‫مصفوفة‬ ‫عناصر‬ ‫لترتيب‬ ‫برنامج‬ ‫بكتابة‬ ‫قم‬ #include<iostream> using namespace std; const int size=5; void main() { int ar[size]; cout<<"enter the value of the matrixn”; for(int i=0; i<size; i++) cin>>ar[i]; for(int j=0; j<size; j++) for(int i=0; i<size -1 ; i++) if(ar[i]>ar[i+1]) swap(ar[i],ar[i+1]); for(int i=0; i<size; i++) cout<<ar[i]<<” “; cout<<endl; } ‫مالحظة‬:‫التصاعدي‬ ‫الترتيب‬ ‫في‬/‫نضع‬ ‫التنازلي‬‫عناصر‬ ‫جميع‬ ‫على‬ ‫تمر‬ ‫حلقة‬ ‫المصفوفة‬ ‫عناصر‬ ‫على‬ ‫تمر‬ ‫اخرى‬ ‫حلقة‬ ‫وبداخلها‬ ‫المصفوفة‬-1.
  • 15. 14‫العنصر‬ ‫و‬ ‫يليه‬ ‫الذي‬ ‫العنصر‬ ‫مع‬ ‫عنصر‬ ‫كل‬ ‫تقارن‬ ‫التي‬ ‫هي‬ ‫الداخلية‬ ‫الحلقة‬ ‫الن‬ ‫بعده‬ ‫الذي‬ ‫العنصر‬ ‫مع‬ ‫يقارن‬ ‫ال‬ ‫االخير‬ ‫متحول‬ ‫على‬ ‫تحتوي‬ ‫التي‬ ‫المسائل‬bool: 1-‫البحث‬ ‫مسائل‬ 2-‫مسائل‬‫االختبار‬(‫كما‬‫البعد‬ ‫الثنائية‬ ‫المصفوفة‬ ‫في‬ ‫سيمر‬) ‫خاطئة‬ ‫دائما‬ ‫البولياني‬ ‫المتحول‬ ‫قيمة‬ ‫نفرض‬ ‫البحث‬ ‫مسائل‬ ‫في‬(false)‫حال‬ ‫وفي‬ ‫قيمة‬ ‫للمتحول‬ ‫نسند‬ ‫العنصر‬ ‫وجدنا‬(true) ‫حلقة‬ ‫بانشاء‬ ‫نقوم‬ ‫مصفوفة‬ ‫داخل‬ ‫عنصر‬ ‫عن‬ ‫نبحث‬ ‫ان‬ ‫منا‬ ‫يطلب‬ ‫عندما‬for‫تمر‬ ‫المصفوفة‬ ‫عناصر‬ ‫كافة‬ ‫على‬ ‫مثال‬:‫بكتابة‬ ‫قم‬‫ر‬ ‫عن‬ ‫بالبحث‬ ‫يقوم‬ ‫برنامج‬‫قم‬‫البعد‬ ‫احادية‬ ‫مصفوفة‬ ‫داخل‬‫و‬ ‫الرقم‬ ‫ان‬ ‫تفيد‬ ‫رسالة‬ ‫اظهار‬‫لم‬ ‫ان‬ ‫موجود‬ ‫غير‬ ‫وانه‬ ‫عليه‬ ‫العثور‬ ‫حال‬ ‫في‬ ‫موجود‬ ‫عليه‬ ‫يعثر‬. ‫الحل‬: #include<iostream> using namespace std; const int size=5; void main() { int ar[size],k; bool a=false; cout<<”enter the element of the matrixn”; for(int i=0; i<size; i++) cin>>ar[i];
  • 16. 15cout<<”enter the random numbern”; cin>>k; for(int i=0; i<size; i++) if(ar[i]==k) a=true; if(a==true) cout<<”the number is exist n“; else cout<<”the number isn’t existn”; } End of presentation 3
  • 17. 16Presentation 4 ‫البعد‬ ‫الثنائية‬ ‫المصفوفة‬(tow dimensional array): ‫لالدخال‬ ‫االخرى‬ ‫داخل‬ ‫حلقة‬ ‫تضمين‬ ‫على‬ ‫فيها‬ ‫نعتمد‬,‫للطباعة‬,......‫الخ‬ 1-‫البعد‬ ‫ثنائية‬ ‫مصفوفة‬ ‫عناصر‬ ‫ادخال‬ ‫طريقة‬: #include<iostream> using namespace std; const int size=3; void main() { Int ar[size][size]; for(int i=0; i<size; i++) for(int j=0; j<size; j++) cin>>ar[i][j]; } ‫استبدال‬ ‫مع‬ ‫الطريقة‬ ‫بنفس‬ ‫تتم‬ ‫والطباعة‬cin>>ِ‫ـ‬‫ب‬cout<< ‫مربعة‬ ‫مصفوفة‬ ‫باختبار‬ ‫فيه‬ ‫تقوم‬ ‫برنامج‬ ‫بكتابة‬ ‫قم‬(4X4)‫متناظرة‬ ‫كانت‬ ‫اذا‬ ‫ال‬ ‫ام‬(‫ال‬ ‫ان‬ ‫اي‬‫متساوية‬ ‫الرئيسي‬ ‫القطر‬ ‫طرفي‬ ‫على‬ ‫عناصر‬) ‫الحل‬ ‫طريقة‬: ‫بولياني‬ ‫متحول‬ ‫بتعريف‬ ‫نقوم‬bool‫إبتدائية‬ ‫قيمة‬ ‫يحمل‬true‫ان‬ ‫نفرض‬ ‫الننا‬ ‫متناظر‬ ‫غير‬ ‫واحد‬ ‫عنصر‬ ‫وجود‬ ‫حال‬ ‫في‬ ‫اال‬ ‫متناظرة‬ ‫المصفوفة‬ ‫الحل‬:
  • 18. 17#include<iostream> Const int size=4; using namespace std; void main() { int ar[size][size]; bool a=true; for(int i=0; i<size; i++) for(int j=0; j<size; j++) cin>>ar[i][j]; for(int i=0; i<size; i++) for(int j=0; j<size; j++) if(ar[i][j]!=ar[j][i]) a=false; if(a==true) cout<<”the given matrix is symmetricn”; else cout<<”the given matrix is not symmetricn”; }
  • 19. 18‫برنامج‬ ‫بكتابة‬ ‫قم‬‫النوع‬ ‫من‬ ‫البعد‬ ‫ثنائية‬ ‫مصفوفة‬ ‫عناصر‬ ‫بادخال‬ ‫يقوم‬float ‫و‬ ‫الثناوي‬ ‫القطر‬ ‫عنصار‬ ‫مجموع‬ ‫بحساب‬ ‫وقم‬‫العلوي‬ ‫المثلث‬ ‫عنصار‬ ‫مجموع‬ ‫االول‬ ‫العمود‬ ‫عناصر‬ ‫مجموع‬ ‫و‬. ‫الحل‬ ‫طريقة‬: ‫هو‬ ‫العلوي‬ ‫المثلث‬:i<j ‫هو‬ ‫الثانوي‬ ‫القطر‬:j==size-i-1‫او‬i+j=size-1 ‫هو‬ ‫الول‬ ‫العمود‬:j==0 ‫الحل‬: #include<iostream> using namespace std; const int size=4; void main() { int ar[size][size],sum=0,sum1=0,sum2=0; for(int i=0; i<size; i++) for(int j=0; j<size; j++) cin>>ar[i][j]; for(int i=0; i<size; i++) for(int j=0; j<size; j++) { if(j==size-1-i) sum=sum+ar[i][j];
  • 20. 19if(i<j) sum1=sum1+ar[i][j]; if(j==0) sum2=sum2+ar[i][j]; } cout<<”the sum of the second diameter is ”<<sum<<endl<<”the sum of the upper triangle is ”<<sum1<<endl<<”the sum of the first column is “<<sum2<<endl; } ‫البعد‬ ‫ثنائية‬ ‫مصفوفة‬ ‫عناصر‬ ‫بادخال‬ ‫يقوم‬ ‫برنامج‬ ‫بكتابة‬ ‫قم‬(4X3)‫بجمع‬ ‫ويقوم‬ ‫بعد‬ ‫احادية‬ ‫مصفوفة‬ ‫في‬ ‫النتائج‬ ‫ويضع‬ ‫سطر‬ ‫كل‬ ‫عناصر‬ ‫قيمة‬ ‫اكبر‬ ‫اوجد‬ ‫ثم‬ ‫ومن‬ ‫الحل‬: #include<iostream> const int r=4,c=3; using namespace std; void main)( { int ar[r][c], x[r]={0};
  • 21. 20for(int i=0; i<r; i++) for(int j=0; j<c; j++) cin>>ar[i][j]; int max=ar[0][0]; for(int i=0; i<r; i++) { for(int j=0; j<c; j++) cout<<ar[i][j]<<” “; cout<<endl; } for(int i=0; i<4; i++) for(int j=0; j<3; j++) { x[i]=x[i]+ar[i][j]; if(ar[i][j]>max) max=ar[i][j]; }
  • 22. 21for(int i=0; i<r; i++) cout<<x[i]<<” “; cout<<endl; cout<<"Max= "<<max<<endl; } ‫عناصر‬ ‫نفرز‬ ‫ان‬ ‫اردنا‬ ‫اذا‬ ‫مالحظة‬‫ال‬‫سطر‬‫الثاني‬‫ثنائية‬ ‫لمصفوفة‬‫بعد‬‫تصاعديا‬ ‫حلقة‬ ‫في‬ ‫نضع‬for‫التالي‬: If(ar[1][j]>ar[1][j+1]) swap(ar[1][j],ar[1][j+1]) End of presentation 4
  • 23. 22Presentation 5 Function ‫السؤال‬ ‫من‬ ‫معينة‬ ‫لقضية‬ ‫حل‬ ‫عن‬ ‫عبارة‬ ‫هو‬ ‫التابع‬‫او‬ ‫عناصر‬ ‫كادخال‬ ‫او‬ ‫جمع‬ ‫او‬ ‫طباعة‬........‫الخ‬‫و‬‫ان‬ ‫يمكن‬‫عدة‬ ‫الحل‬ ‫هذا‬ ‫يستخدم‬‫متكررة‬ ‫مرات‬ ‫التوابع‬ ‫من‬ ‫نميز‬: )bool, int , double, float 1-‫التوابع‬‫قيمة‬ ‫تعيد‬ ‫التي‬( ‫كلمة‬ ‫مع‬ ‫مقترنين‬ ‫ويكونوا‬return;‫الحل‬ ‫نهاية‬ ‫في‬‫بعد‬ ‫شيء‬ ‫اي‬ ‫التابع‬ ‫يقرأ‬ ‫وال‬ ‫تعليمة‬return;‫كلمة‬ ‫بمثابة‬ ‫انها‬ ‫اي‬break; ‫وحيدة‬ ‫قيمة‬ ‫سوى‬ ‫ارجاع‬ ‫اليمكن‬ ‫التوابع‬ ‫هذه‬ ‫وفي‬‫وإل‬‫ظهاره‬‫التابع‬ ‫استدعاء‬ ‫عند‬ ‫ا‬ ‫ال‬ ‫داخل‬ ‫في‬void main()‫بطباعتها‬ ‫نقوم‬ ‫او‬ ‫لمتحول‬ ‫نسندها‬ ‫ان‬ ‫اما‬ )void ‫التوابع‬‫قيمة‬ ‫تعيد‬ ‫ال‬( -2 ‫عند‬ ‫ويوضع‬ ‫محددة‬ ‫بوظيفة‬ ‫ويقوم‬ ‫قيمة‬ ‫اي‬ ‫يعيد‬ ‫ال‬ ‫التابع‬ ‫هذا‬ ‫طباعته‬ ‫او‬ ‫لمتحول‬ ‫اسناده‬ ‫دون‬ ‫مباشرة‬ ‫استدعائه‬ ‫التابع‬ ‫عن‬ ‫مثال‬(1) ‫الجمع‬ ‫ناتج‬ ‫ويعيد‬ ‫صحيحين‬ ‫عددين‬ ‫بجمع‬ ‫يقوم‬ ‫تابع‬ ‫بكتابه‬ ‫قم‬ #include<iostream> using namespace std; int sum(int x,int y) { int z=0; z=y+x; return z;
  • 24. 23} void main() { int x,y; cin>>x,y; cout<<”sum=”<<sum(x,y)<<endl; } ‫مالحظة‬:‫نهايته‬ ‫عند‬ ‫التابع‬ ‫داخل‬ ‫المنشأة‬ ‫المتحوالت‬ ‫تموت‬‫وضعنا‬ ‫اذا‬ ‫اي‬ ‫في‬‫ال‬void main()"cout<<z;"‫خطأ‬ ‫لدينا‬ ‫سيظهر‬ ‫التابع‬ ‫عن‬ ‫مثال‬(2) ‫احادية‬ ‫مصفوفة‬ ‫بطباعة‬ ‫يقوم‬ ‫تابع‬ ‫بكتابة‬ ‫قم‬ #include<iostream> using namespace std; const int size=4; viod Print(int ar[]) { for(int i=0; i<size; i++) cout<<ar[i]<<” “; cout<<endl; } void main() {
  • 25. 24int ar[size]; for(int i=0; i<size; i++) cin>>ar[i]; Print(ar); } ‫قوة‬ ‫الى‬ ‫العدد‬ ‫برقع‬ ‫يقوم‬ ‫تابع‬ ‫بكتابة‬ ‫قم‬‫معينة‬ ‫مثال‬:xy #include<iostream> using namespace std; int pow2(int x, int y) { int z=1; for(int i=y; i>0; i--) z=z*x; return z; } void main() { int x,y; cout<<”enter x, yn”; cin>>x,y;
  • 26. 25cout<<”x^y= ”<<pow2(x,y)<<endl; } ‫مالحظة‬: ‫و‬ ‫البداية‬ ‫في‬ ‫التابع‬ ‫ترويسة‬ ‫نضع‬ ‫ان‬ ‫يكفي‬ ‫البرنامج‬ ‫نهاية‬ ‫في‬ ‫التابع‬ ‫وضعنا‬ ‫اذا‬ ‫نكت‬‫ب‬‫ال‬ ‫نهاية‬ ‫بعد‬ ‫اي‬ ‫البرنامج‬ ‫نهاية‬ ‫بعد‬ ‫التابع‬ ‫وظيفة‬void main() ‫مالحظة‬: ‫النوع‬ ‫من‬ ‫التابع‬ ‫يقوم‬ ‫ان‬ ‫يمكن‬int , bool , float, double‫من‬ ‫اكثر‬ ‫بارجاع‬ ‫المرجعية‬ ‫المتحوالت‬ ‫استخدام‬ ‫حال‬ ‫في‬ ‫هذا‬ ‫ولكن‬ ‫قيمة‬”)&(by reference “ ‫الذاكرة‬ ‫الى‬ ‫مباشر‬ ‫مكان‬ ‫باخذ‬ ‫تقوم‬ ‫المتحوالت‬ ‫وهذه‬‫العادية‬ ‫المتحوالت‬ ‫بعكس‬ ‫التابع‬ ‫نهاية‬ ‫مع‬ ‫الصورة‬ ‫وتنتهي‬ ‫المتحول‬ ‫عن‬ ‫صورة‬ ‫تاخذ‬ ‫التي‬ ‫مثال‬:‫مستطيل‬ ‫ومحيط‬ ‫مساحة‬ ‫لحساب‬ ‫برنامج‬ ‫بكتابة‬ ‫قم‬. ‫المساحة‬ ‫حسابهما‬ ‫يجب‬ ‫قيمتان‬ ‫لدينا‬ ‫هنا‬,‫ال‬‫محيط‬. #include<iostream> using namespace std; int ffff(int x, int y, int &aa ) { int cc; aa=x*y; cc=(x+y)*2; return cc; aa‫خرج‬ ‫متحول‬ ‫هو‬ ‫له‬ ‫نضع‬ ‫ال‬ ‫لذلك‬ return‫يقوم‬ ‫النه‬ ‫الذاكرة‬ ‫الى‬ ‫بالوصول‬ ‫مباشرة‬‫في‬ ‫تغيير‬ ‫واي‬ ‫ضمن‬ ‫المتحول‬ ‫هذا‬ ‫قيمة‬ ‫فيه‬ ‫يثبت‬ ‫التابع‬
  • 27. 26} void main() { int x, y , aa ; cout<<”enter the length and the Width n”; cin>>x>>y; cout<< ffff(x,y,aa); cout<<aa; } ‫خرج‬ ‫متحوالت‬ ‫انهما‬ ‫على‬ ‫المتحولين‬ ‫ناخذ‬ ‫للحل‬ ‫اخرى‬ ‫طريقة‬ #include<iostrem> using namespace std; void ac(double a, double c , double & aa, double & cc) { aa= a * c ; cc= (a + c)*2 } Void main() { double x , y , aa , cc ; cin>>x>>y ; ac(a , c , aa , cc) ‫قيمة‬ ‫بارجاع‬ ‫التابع‬ ‫يقوم‬ ‫التمرين‬ ‫هذا‬ ‫في‬ ‫حساب‬ ‫وهي‬ ‫وحيدة‬‫استخدام‬ ‫تم‬ ‫بينما‬ ‫المحيط‬ ‫المساحة‬ ‫قيمة‬ ‫لتخزين‬ ‫خرج‬ ‫متحول‬
  • 28. 27cout<<aa<<endl<<cc<<endl; } ‫مالحظة‬:‫ع‬ ‫فورا‬ ‫تؤخذ‬ ‫المصفوفة‬‫للتاوابع‬ ‫بالنسبة‬ ‫خرج‬ ‫متحول‬ ‫انها‬ ‫لى‬ ‫من‬ ‫التوابع‬ ‫نستعمل‬ ‫قيمة‬ ‫من‬ ‫اكثر‬ ‫بحساب‬ ‫يقوم‬ ‫تابع‬ ‫منا‬ ‫يطلب‬ ‫عندما‬ ‫للتبسيط‬ ‫النوع‬void ‫أحادية‬ ‫مصفوفة‬ ‫عناصر‬ ‫مجموع‬ ‫بإعادة‬ ‫يقوم‬ ‫تابع‬ ‫بكتابة‬ ‫قم‬ #include<iostream> using namespace std; const int size=4; int sumd(int ar[]) { int sum=0; for(int i=0; i<size; i++) sum=sum+ar[i]; return sum; } void main() { int ar[size]; for(int i=0; i<size; i++) cin>>ar[i];
  • 29. 28cout<<”sum= “<<sumd(ar)<<endl; } ‫خرج‬ ‫متحوالت‬ ‫باستخدام‬ ‫اخرى‬ ‫طريقة‬‫النوع‬ ‫من‬ ‫تابع‬ ‫نستخدم‬ ‫اي‬(void) ‫الحل‬: #include<iostream> using namespace std; const int size=4; int sumd(int ar[],int &sum) { sum=0; for(int i=0; i<size; i++) sum=sum+ar[i]; } void main() { int ar[size],sum; for(int i=0; i<size; i++) cin>>ar[i]; sumd(ar,sum); cout<<”sum= ”<<sum<<endl; }
  • 30. 29‫هذا‬ ‫ومكان‬ ‫المصفوفة‬ ‫في‬ ‫عنصر‬ ‫أكبر‬ ‫بإيجاد‬ ‫يقوم‬ ‫تابع‬ ‫بكتابة‬ ‫قم‬‫العنصر‬ ‫الحل‬: #include<iostream> using namespace std; const int size =5; void maxA(int ar[], int &max, int & pos) { max=ar[0]; for(int i=0; i<size; i++) if(ar[i]>max) { max=ar[i] pos = i+1; } } void main() { int ar[size] ,pos , max; for(int i=0; i<size; i++) cin>>ar[i]; maxA(ar,max,pos)
  • 31. 30cout<<”max=”<<max<<endl<<”position at “<<pos<<endl; } ‫البعد‬ ‫احادية‬ ‫مصفوفة‬ ‫داخل‬ ‫عنصر‬ ‫عن‬ ‫بالبحث‬ ‫يقوم‬ ‫تابع‬ ‫بكتابة‬ ‫قم‬ ‫القيمة‬ ‫يعدي‬true‫القيمة‬ ‫ويعيد‬ ‫موجود‬ ‫العنصر‬ ‫كان‬ ‫اذا‬false‫موجود‬ ‫يكن‬ ‫لم‬ ‫اذا‬ ‫الحل‬ #include<iostream> using namespace std; const int size=5; bool search(int ar[],int k) { bool a=false; for(int i=0; i<size; i++) if(ar[i]==k) a=true; if(a==true) return a; } void main() { bool a;
  • 32. 31int ar[size],k; for(int i=0; i<size; i++) cin>>ar[i]; cout<<”enter the random element n”; a=search(ar,k); cout<<a<<endl; if(a==true) cout<<”foundn”; else cout<<”not foundn”; } ‫تصاعديا‬ ‫عناصرها‬ ‫لفرز‬ ‫وتابع‬ ‫احادية‬ ‫مصفوفة‬ ‫عناصر‬ ‫بعكس‬ ‫يقوم‬ ‫تابع‬ ‫اكتب‬ ‫بفرز‬ ‫ويقوم‬ ‫بعد‬ ‫ثنائية‬ ‫مصفوفة‬ ‫دخل‬ ‫باخذ‬ ‫يقوم‬ ‫تابع‬ ‫اكتب‬ ‫ثم‬ ‫لطابعتها‬ ‫وتابع‬ ‫الصف‬ ‫عناصر‬‫المصفوفة‬ ‫هذه‬ ‫لطباعة‬ ‫وتابع‬ ‫تصاعديا‬ ‫الثاني‬ ‫احادية‬ ‫مصفوفة‬ ‫عناصر‬ ‫بادخال‬ ‫يقوم‬ ‫برنامج‬ ‫اكتب‬,‫التوابع‬ ‫باستدعاء‬ ‫وقم‬ ‫ثنائية‬ ‫والعكس‬ ‫الفرز‬ ‫وبعد‬ ‫قبل‬ ‫بالترتيب‬ ‫السابقة‬ ‫الحل‬: #include<iostream> using namespace std; const int size=3; void rev(int ar[size] ) { cout<<"the reverc of the matrix is :n";
  • 33. 32for( int i=0; i < size/2 ; i++) swap(ar[i],ar[size-1-i]); } void sort(int ar[size]) { for(int j=0; j<size; j++) for(int i=0; i<size - 1; i++) if(ar[i]>ar[i+1]) swap(ar[i],ar[i+1]); } void sort2(int ar[size][size]) { for(int i=0; i<size; i++) for(int j=0; j<size; j++) if(ar[1][j]>ar[1][j+1]) swap(ar[1][j],ar[1][j+1]); } void print(int ar[size]) { for(int i=0; i < size; i++)
  • 34. 33cout<<ar[i]<<" "; cout<<endl; } void print2(int ar[size][size]) { for(int i=0; i<size; i++) { for(int j=0; j<size; j++) cout<<ar[i][j]<<" "; cout<<endl; } } void main() { int x[size][size],ar[size],j,i; cout<<"enter the value of the matrix :n"; for(i = 0; i < size; i++) cin>>ar[i]; rev(ar); print(ar); sort(ar);
  • 35. 34cout<<"the sort of the matrix is:n"; print(ar); cout<<"enter the value of the 3X3 matrix:n"; for(i=0; i<size; i++) for(j=0; j<size; j++) cin>>x[i][j]; sort2(x); cout<<"the matrix after sorted the 2nd row IS :n"; print2(x); cout<<endl; cout<<"Decorated by : Mhd Ghayth Alsawafn"<<"Thank you for use it bye bye ^_^n"; }
  • 36. 35‫تعليمة‬template<class t>‫النوع‬ ‫من‬ ‫المتحوالت‬ ‫كل‬ ‫يقبل‬ ‫قالب‬ ‫هي‬int , folatdouble‫حيث‬t‫قيمة‬ ‫ان‬ ‫اي‬ ‫متحول‬t‫في‬ ‫المتحول‬ ‫لوضع‬ ‫تبعا‬ ‫تتبدل‬ ‫ال‬void main()‫المتغير‬ ‫مكان‬ ‫المتحول‬ ‫طبيعة‬ ‫تحل‬ ‫حيث‬t. ‫مثال‬ 1- Write a template function named "Reverse" that receives 1D array and reverse its elements. Sample (1): If the passed array is: K C U L D O O G The returned array will be: G O O D L U C K ‫الحل‬: #include<iostream> using namespace std; const int size=8; template<class t> void rev(t ar[]) { for(int i=0; i<size/2; i++) swap(ar[i],ar[size-i-1]); }
  • 37. 36template<class t> void print(t ar[]) { for(int i=0; i<size; i++) cout<<ar[i]<<" "; cout<<endl; } void main() { int i; char ar[size]; cout<<"enter the value of the matrixn"; for( i=0; i<size; i++) cin>>ar[i]; rev(ar); cout<<"the reverse of the matrix :n"; print(ar); }
  • 38. 37‫اخر‬ ‫مثال‬: 2-Write a template function named "sort" that receives 1D array and sort its elements ascending. ‫الحل‬: #include<iostream> using namespace std; const int size=4; template<class t> void sort(t ar[]) { for(int j=0; j<size; j++) for(int i=0; i<size-1; i++) if(ar[i]>ar[i+1]) swap(ar[i],ar[i+1]); } template<class t> void print(t ar[size]) { for (int i=0; i<size; i++) cout<<ar[i]<<" ";
  • 39. 38cout<<endl; } void main() { int ar[size],i; cout<<"enter the value of the matrixn"; for(i=0; i<size; i++) cin>>ar[i]; sort(ar); print(ar); } ‫اخير‬ ‫مثال‬: 2- Write a function named "search" that receives 2D float array and a float "num", the function must search for the "num" in the array and returns true and the row index at which the element is found and false and "-1" if the element is not in the array. ‫الحل‬:‫است‬ ‫فيه‬ ‫يلزمنا‬‫السطر‬ ‫ترتيب‬ ‫فيمه‬ ‫ليحمل‬ ‫خرج‬ ‫متحول‬ ‫خدام‬ ‫فيه‬ ‫العنصر‬ ‫وجد‬ ‫الذي‬
  • 40. 39#include<iostream> using namespace std; const int size=3; template<class t> bool search(t ar[size][size],t k ,int & pos) { for(int i=0; i<size; i++) for(int j=0; j<size; j++) if(ar[i][j]==k) { pos=i+1; return true; } return false; } void main() {
  • 41. 40int pos,i,j; float ar[size][size],k; bool f; cout<<"enter the value of the 3X3 matrixn"; for(i=0; i<size; i++) for(j=0; j<size; j++) cin>>ar[i][j]; cout<<"enter the random elemnt: "; cin>>k; for(i=0; i<size; i++) { for(j=0; j<size; j++) cout<<ar[i][j]<<" "; cout<<endl; } cout<<"the number is found true/false ?n"; f=search(ar,k,pos); if(f==true) cout<<"found at "<<pos<<endl; else
  • 42. 41cout<<"the number not found n -1"; } ‫العشوائي‬ ‫التابع‬rand(): ‫عشوائية‬ ‫ارقام‬ ‫لتوليد‬ ‫التابع‬ ‫هذا‬ ‫ويستخدم‬ ‫ال‬ ‫من‬ ‫عشوائية‬ ‫ارقام‬ ‫مجموعة‬ ‫بتوليد‬ ‫قم‬ ‫مثال‬0‫ال‬ ‫وحتى‬6 ‫القسمة‬ ‫باقي‬ ‫على‬ ‫االعتماد‬ ‫مع‬ ‫العشوائي‬ ‫التابع‬ ‫باستخدام‬ ‫نقوم‬ ‫الحل‬ cout<<rand()%6+1 ‫ضمن‬ ‫عشاوئية‬ ‫بقيمة‬ ‫البعد‬ ‫احادية‬ ‫مصفوفة‬ ‫عناصر‬ ‫بتعبئة‬ ‫يقوم‬ ‫تابع‬ ‫بكتابة‬ ‫قم‬ ‫المجال‬[20.50] ‫الحل‬ #include<iostream> using namespace std; const int size=6; void kkk(int ar[]) { for(int i=0; i<size; i++) ar[i]=rand()%31+20; } void main() { int ar[size];
  • 43. 42kkk(ar); for(int i=0; i<size; i++) cout<<ar[i]<<” “; cout<<endl; } ‫منه‬ ً‫ال‬‫قب‬ ‫نضع‬ ‫العشوائي‬ ‫التابع‬ ‫استخدام‬ ‫عند‬ ‫دوما‬ ‫متغيرة‬ ‫نتائج‬ ‫على‬ ‫للحصول‬ ‫التابع‬srand()‫المكتبة‬ ‫تضمين‬ ‫الى‬ ‫يحتاج‬ ‫وهو‬#include <cstdlib>‫ويقبل‬ ‫متحوالت‬ ‫فقط‬double ‫الحل‬: #include<iostream> #include <cstdlib> using namespace std; const int size=6; void kkk(int ar[]) { for(int i=0; i<size; i++) ar[i]=rand()%31+20; } void main() { double k;
  • 44. 43cin>>k; int ar[size]; srand(k); kkk(ar); for(int i=0; i<size; i++) cout<<ar[i]<<” “; cout<<endl; } ‫قم‬‫التالية‬ ‫البرامج‬ ‫خرج‬ ‫بكتابة‬ #include<iostream> using namesapce std; int x=7; // (‫عام‬ ‫متحول‬) global variable int xxx(int &x) // (‫خرج‬ ‫متحول‬) by reference variable { x=x+1; return x; } void yyy() // ‫عام‬ ‫متحول‬ ‫يقبل‬ ‫التابع‬ ‫الن‬ ‫يثبت‬ ‫التغيير‬ { x=x+1; cout<<x; }
  • 45. 44void kkk( int x) // ‫التابع‬ ‫هذا‬ ‫في‬ ‫قيمته‬ ‫على‬ ‫المتحول‬ ‫يحافظ‬ { x=x+100; cout<<x; } void main() { int x=10; cout<<xxx(x); 11 yyy(); 8 cout<<x; 11 kkk(x); 111 cout<<x; 11 yyy(); 9 } ‫هامة‬ ‫مالحظة‬:‫التغيير‬ ‫يثبت‬ ‫العامة‬ ‫المتحوالت‬‫علي‬‫عليها‬ ‫التغيير‬ ‫عند‬ ‫ها‬ ‫الثانية‬ ‫الدرجة‬ ‫من‬ ‫معادلة‬ ‫جذور‬ ‫باعادة‬ ‫يقوم‬ ‫تابع‬ ‫بكتابة‬ ‫قم‬ ‫الحل‬:‫هنا‬x1 , x2‫قيمة‬ ‫ياخذو‬ ‫و‬ ‫المعادلة‬ ‫جذور‬ ‫هم‬double
  • 46. 45#include<iostream> #include<cmath> using namespace std; void del(double a, double b, double &x1, double &x2,doubleD) { x1=(-b-sqrt(D))/(2*a); x2=(-b+sqrt(D))/(2*a); } void main() { double D,x1,x2,a,b,c; cout<<”enter a,b and cn”; cin>>a>>b>>c; D=b*b-4*(a*c); if(D>=0) { cout<<”the solution is n”; del(a,b,x1,x2,D); cout<<”X1= “<<x1<<endl; cout<<”X2= “<<x2<<endl; } else cout<<”errorn”; } End of presentation 5 End of function
  • 47. 46 Presentation 6 Pointer ‫هو‬‫عبارة‬‫عن‬‫مؤشر‬‫عنوان‬ ‫على‬ ‫للداللة‬‫الذاكر‬ ‫في‬ ‫المتغير‬‫ة‬. 1-‫نضع‬ ‫عندما‬(&)‫المتغير‬ ‫مكان‬ ‫الى‬ ‫باالشارة‬ ‫يقوم‬ ‫انه‬ ‫يعني‬ ‫فهذا‬ ‫المؤشر‬ ‫قبل‬ ‫الذاكرة‬ ‫في‬ ‫مثال‬: int x=4; int*p; p=&x; cout <<p // ‫الذاكرة‬ ‫في‬ ‫العنصر‬ ‫مكان‬ 2-‫اشارة‬)*(‫المؤشر‬ ‫لتعريف‬ ‫تستخدم‬ ‫كالتالي‬ ‫المؤشر‬ ‫بتعريف‬ ‫نقوم‬:int *p(‫النوع‬ ‫من‬ ‫مؤشر‬ ‫بتعريف‬ ‫قمنا‬int) ‫مثال‬: int x=4; int*p; p=&x; cout <<*p // x ‫قيمة‬ ‫سيكون‬ ‫الخرج‬ 7-‫المؤشر‬ ‫نستخدم‬ ‫ان‬ ‫اما‬ ‫بطريقتين‬ ‫المصفوفة‬ ‫عناصر‬ ‫الى‬ ‫اإلشارة‬ ‫يمكن‬ ‫اي‬ ‫كالمصفوفة‬p[i].‫نضع‬ ‫او‬*(p+i) #include<iostream> using namespace std; void main() { double y; double *p //‫المؤشر‬ ‫بتعريف‬ ‫قمنا‬ p=&y; // ‫الذاكر‬ ‫في‬ ‫العنصر‬ ‫مكان‬ ‫على‬ ‫يؤشر‬ ‫المؤشر‬‫ة‬ cin>>*p; // ‫الرقم‬ ‫ادخل‬ ‫المستخدم‬ ‫ان‬ ‫نفرض‬9
  • 48. 47cout<<*p; // 9 cout<<p; // ‫الذاكرة‬ ‫في‬ y ‫عنوان‬ ‫سيكون‬ ‫الخرج‬ } ‫قيم‬ ‫بتبديل‬ ‫يقوم‬ ‫برنامج‬ ‫اكتب‬x , y‫المؤشرات‬ ‫باستخدام‬ #include<iostream> using namespace std; void main() { int*p, *t; int x , y ; cin>>x>>y; p=&x; t=&y; // ‫الذاكرة‬ ‫في‬ ‫المتغيرين‬ ‫مكان‬ ‫الى‬ ‫اشرنا‬ swap(*p,*t) cout<<”x= ”<<*p<<endl; // x ‫قيمة‬ ‫سيكون‬ ‫الخرج‬ cout<<”y= “<<*t<<endl; // y ‫قيمة‬ ‫سيكون‬ ‫الخرج‬ cout<<p<<endl<<t<<endl; (‫عناوين‬ ‫سيكون‬ ‫الخرج‬x, y) } ‫مالحظة‬: ‫في‬‫الساكنة‬ ‫المصفوفة‬(static array)‫مؤشر‬ ‫المصفوفة‬ ‫اسم‬ ‫يعتبر‬"‫ثابت‬"‫على‬ ‫عنصر‬ ‫اول‬ ‫مالحظة‬:‫المكان‬ ‫لنفس‬ ‫يؤشران‬ ‫لمؤشر‬ ‫مؤشر‬ ‫اسناد‬ ‫مالحظة‬:‫حلقة‬ ‫بإنشاء‬ ‫نقوم‬ ‫المؤشر‬ ‫باستخدام‬ ‫مصفوفة‬ ‫عناصر‬ ‫لطباعة‬for ‫فيها‬ ‫ونضع‬cout<<*(p+i)‫الجمع‬ ‫من‬ ‫اقوى‬ ‫الضرب‬ ‫الن‬ ‫االقواس‬ ‫نضع‬ ‫حيث‬ ‫الترتيب‬ ‫ذو‬ ‫العنصر‬ ‫الى‬ ‫المؤشر‬ ‫وينتقل‬(p+i)َ‫فإن‬ ‫وضعهم‬ ‫عدم‬ ‫حال‬ ‫وفي‬ ‫المؤشر‬ ‫قيمة‬ ‫سيكون‬ ‫الخرج‬+i ‫مثال‬: #include<iostream> using namesapce std; const int size=5; void main() 20 30 40 5 2
  • 49. 48{ int x[size]; for(int i=0; i<size; i++) cin>>x[i]; cout<<*x; 20 int *p; p=&x[0]; // or we write p=x ; cout<<*p<<endl; 20 cout<<p<<endl; address of p p p+1 p+2 p+3 p+4 x x+1 x+2 x+3 x+4 cout<<*++p; 30 cout<<*p++; 30 cout<<*p++; 40 cout<<*++x; error x is constant cout<<p[0]; 5 cout<<p[1]; 2 cout<<*(p-1); 40 } ‫البعد‬ ‫احادية‬ ‫مصفوفة‬ ‫بتعريف‬ ‫به‬ ‫تقوم‬ ‫برنامج‬ ‫اكتب‬ 1-‫يقوم‬ ‫تابع‬ ‫واستدعاء‬‫المؤشرات‬ ‫باستخدام‬ ‫مصفوفة‬ ‫عناصر‬ ‫بادخال‬ 2-‫المؤشرات‬ ‫باستخدام‬ ‫المصفوفة‬ ‫هذه‬ ‫عناصر‬ ‫بطباعة‬ ‫يقوم‬ ‫اخر‬ ‫تابع‬ ‫واستدعاء‬ 3-‫قم‬‫عنصر‬ ‫اصغر‬ ‫مع‬ ‫المصفوفة‬ ‫في‬ ‫عنصر‬ ‫اكبر‬ ‫باستبدال‬ ‫يقوم‬ ‫تابع‬ ‫باستدعاء‬ ‫والعكس‬ 4-‫اكبر‬ ‫وارجاع‬ ‫المصفوفة‬ ‫في‬ ‫المنتصف‬ ‫العنصر‬ ‫بارجاع‬ ‫يقوم‬ ‫تابع‬ ‫باستدعاء‬ ‫قم‬ ‫عنصر‬ ‫الحل‬ ‫طريقة‬ 20 30 40 5 2
  • 50. 49‫في‬ ‫يوجد‬ ‫الذي‬ ‫العنصر‬ ‫ناخذ‬ ‫فاننا‬ ‫فردية‬ ‫المصفوفة‬ ‫كانت‬ ‫اذا‬ ‫االخير‬ ‫الطلب‬ ‫في‬ ‫كانت‬ ‫اذا‬ ‫اما‬ ‫المصفوفة‬ ‫منتصف‬‫المتوسط‬ ‫ناخذ‬ ‫فاننا‬ ‫زوجيه‬ ‫فردية‬ ‫المصفوفة‬ ‫اساس‬ ‫على‬ ‫الحل‬‫هو‬ ‫والعنصر‬ar+2: #include<iostream> using namespace std; const int size=5; void input(int ar[]) { int*p; p=ar; for(int i=0; i<size; i++) cin>>p[i]; } void print(int ar[]) { int*p; p=ar; for(int i=0; i<size; i++) cout<<ar[i]<<" "; cout<<endl; } void swap(int ar[]) { int pos=0 , pos2=0; int max=ar[0]; int min=ar[0]; for(int i=1; i<size; i++) { if(ar[i]>max) { max=ar[i]; pos=i; } if(ar[i]<min) { min=ar[i]; pos2=i;
  • 51. 50} } swap(ar[pos],ar[pos2]); } int n0(int ar[] , int&mont) { int max=ar[0]; for(int i=1; i<size; i++) if(ar[i]>max) max=ar[i]; mont= *(ar+2); return max; } void main() { int ar[size],mont; input(ar); print(ar); swap(ar); print(ar); cout<<"max= "<<n0(ar,mont); cout<<"the middel element is"<<mont; } End of presentation 6
  • 52. 51Presentation 7 string & Dynamic array ‫الديناميكية‬ ‫المصفوفة‬(Dynamic array)‫بادخال‬ ‫المستخدم‬ ‫يقوم‬ ‫مصفوفة‬ ‫هي‬ ‫كالتالي‬ ‫مؤشر‬ ‫طريق‬ ‫عن‬ ‫حجمها‬: int*p=new int[size]; ‫تعليمة‬ ‫ان‬ ‫حيث‬new‫الديناميكي‬ ‫للحجز‬ ‫الذاكرة‬ ‫تجهز‬ #include<iostream> using namespace std; void main() { int size; cin>>size; int *p =new int[size]; } ‫أرقام‬ ‫بإدخال‬ ‫قم‬ ‫ثم‬ ‫الطالب‬ ‫عدد‬ ‫بادخال‬ ‫المستخدم‬ ‫فيه‬ ‫يقوم‬ ‫برنامج‬ ‫بكتابه‬ ‫قم‬ID ‫ثم‬ ‫األرقام‬ ‫هذه‬ ‫وتخزين‬ ‫للطالب‬‫معطى‬ ‫طالب‬ ‫رقم‬ ‫عن‬ ‫بالبحث‬ ‫يقوم‬ ‫تابع‬ ‫استخدم‬ ‫القيمة‬ ‫ويعيد‬ ‫المستخدم‬ ‫قبل‬ ‫من‬true‫موجود‬ ‫الرقم‬ ‫كان‬ ‫اذا‬ ‫الطالب‬ ‫وترتيب‬ ‫الحل‬ ‫طريقة‬: ‫المصفوفة‬ ‫حجم‬ ‫هنا‬ ‫يمثل‬ ‫الطالب‬ ‫عدد‬(‫ديناميكيه‬ ‫مصفوفة‬ ‫نستخدم‬) ‫بالمصفوفة‬ ‫يمثلون‬ ‫الطالب‬ ‫ارقام‬ ‫الحل‬
  • 53. 52#include<iostream> using namespace std; bool sear(int *p , int size , int &pos, int id) { for(int i=0; i<size; i++) if(*(p+i)==id) { pos=i+1; return true; } return false; } void main() { cout<<”enter the size of matrixn”; int size,id,pos; cin>>size; int*ID=new int[size]; cout<<”enter the ID of the studentn”; for(int i=0; i<size; i++) cin>>*(ID+i); cout<<”enter the random IDn”;
  • 54. 53cin>>id; bool a=sear(ID,size,pos,id); if(a==true) cout<<”the number is found in the ” <<pos<<endl; else cout<<”the number is not foundn”; } ‫مالحظة‬:‫وهو‬ ‫دخل‬ ‫لها‬ ‫يكون‬ ‫الديناميكية‬ ‫المصفوفة‬ ‫على‬ ‫تطبق‬ ‫التي‬ ‫التوابع‬ ‫دوما‬ ‫عنه‬ ‫صورة‬ ‫لياخذ‬ ‫المصفوفة‬ ‫حجم‬ String ‫ال‬string‫بالشكل‬ ‫تعريفها‬ ‫ويتم‬ ‫المحارف‬ ‫مصفوفة‬ ‫هي‬string s; #include<iostream> using namespace std; void main() { string s; cin>>s; //”ahmad ali” } ‫بالشكل‬ ‫مصفوفة‬ ‫تشكل‬ ‫سوف‬ ‫الجملة‬ ‫هذه‬damha
  • 55. 54‫ال‬ ‫ان‬ ‫حيث‬cin‫ال‬ ‫في‬string‫الفراغ‬ ‫اليقرأ‬ ‫مكتبه‬ ‫تضمين‬ ‫علينا‬ ‫المشكلة‬ ‫هذه‬ ‫ولتفادي‬<string>‫واستخدام‬ ‫التعليمة‬getline(cin,s)‫والفراغ‬ ‫الكلمة‬ ‫تظهر‬ ‫التي‬ ‫وضعنا‬ ‫إذا‬string s2=”ziad Hasan”;‫مصفوفة‬ ‫لدينا‬ ‫يتشكل‬ ‫فسوف‬ ‫بالشكل‬ nasaHdaiz ‫وضعنا‬ ‫واذا‬string s3(s2,4)‫من‬ ‫االولى‬ ‫االربعة‬ ‫االحرف‬ ‫ناخذ‬ ‫اننا‬ ‫يعني‬ ‫فهذا‬ ‫ال‬string s2 ‫ال‬ ‫لجمع‬string‫نضع‬string‫كالتالي‬ ‫جديدة‬: string s4 =s2+”hi”; ‫ال‬ ‫حجم‬ ‫لمعرفة‬string‫التابع‬ ‫نستخدم‬ ‫فاننا‬s2.size() ‫مثال‬ #include<iostream> #include<string> using namespace std; void main() { string s1="zaid hassan"; cout<< "The size of s1 is " << s1.size() << " characters.n";
  • 56. 55} ‫ال‬ ‫طباعة‬ ‫يمكن‬string‫بالشكل‬ ‫المصفوفة‬ ‫باستخدام‬ for(int i=0; i<size; i++) cout<<s1[i]<<” “; ‫تعليمة‬isalpha(s[0])‫حرف‬ ‫المحارف‬ ‫مصفوفة‬ ‫من‬ ‫عنصر‬ ‫اول‬ ‫كان‬ ‫اذا‬ ‫تختبر‬ ‫ال‬ ‫ام‬ ‫مثال‬ #include<iostream> #include<string> using namespace std; void main() { string s1="zaid hassan"; cout<< "The size of s1 is " << s1.size() << " characters.n"; if(isalpha(s1[0])) cout<<true<<endl; else cout<<false<<endl; } ‫تعليمة‬isdigit(s[0])‫او‬ ‫رقم‬ ‫المحارف‬ ‫مصفوفة‬ ‫من‬ ‫عنصر‬ ‫اول‬ ‫كان‬ ‫اذا‬ ‫تختبر‬ ‫ال‬
  • 57. 56#include<iostream> #include<string> using namespace std; void main() { string s1="zaid hassan"; cout<< "The size of s1 is " << s1.size() << " characters.n"; if(isdigit(s1[0])) cout<<true<<endl; else cout<<false<<endl; } ‫تعليمة‬ispunct(s[0])‫المحارف‬ ‫مصفوفة‬ ‫من‬ ‫عنصر‬ ‫اول‬ ‫كان‬ ‫اذا‬ ‫تختبر‬ ‫المتبقية‬ ‫المفاتيح‬ ‫لوحة‬ ‫اجزاء‬ ‫من‬ ‫جزء‬ #include<iostream> #include<string> using namespace std; void main()
  • 58. 57{ string s1="zaid hassan"; cout<< "The size of s1 is " << s1.size() << " characters.n"; if(ispunct(s1[0])) cout<<true<<endl; else cout<<false<<endl; } ‫الحرف‬ ‫الستبدال‬ ‫برنامج‬ ‫بكتابة‬ ‫قم‬‘a’‫بالحرف‬‘k’ #include<iostream> #include<string> using namespace std; void main() { string s; getline(cin,s); cout<<s; for(int i=0; i<s.size();i++) if(s[i]==’a’) s[i]=’k’; } M h d G h a y t h
  • 59. 58‫الحرف‬ ‫باستبدال‬ ‫يقوم‬ ‫تابع‬ ‫بكتابه‬ ‫قم‬‘k’‫بالحرف‬‘a’‫بكتابة‬ ‫وقم‬‫يقوم‬ ‫برنامج‬ ‫التابع‬ ‫هذا‬ ‫باستدعاء‬ #include<iostream> #include<string> using namespace std; void replace(string s) { for(int i=0; i<size; i++) if(s[i]==’a’) s[i]=’k’; } void main() { string s; getline(cin ,s); replace(s); cout<<s<endl; } ‫احرف‬ ‫عدد‬ ‫بحساب‬ ‫يقوم‬ ‫تابع‬ ‫بكتابة‬ ‫قم‬string‫ارقام‬ ‫عدد‬ ‫وحساب‬‫ها‬ #include<iostream> #include<string> void xx(string s, int&nw , int&nd)
  • 60. 59{ nw=0; nd=0; for(int i=0; i<s.size(); i++) { if(isalpha(s[i])) nw++; if(isdigit(s[i])) nd++; } } void main() { string k; getline(cin,k); int nw,nd; xx(k,nw,nd); cout<<”the number of the word is ”<<nw<<endl <<”and the number of the digit is “<<nd<<endl; } ‫مراجعة‬ ‫تمرين‬‫لل‬Dynamic array ‫هؤالء‬ ‫رواتب‬ ‫بادخال‬ ‫وقم‬ ‫العمال‬ ‫عدد‬ ‫بادخال‬ ‫المستخدم‬ ‫فيه‬ ‫يقوم‬ ‫برنامج‬ ‫بكتابة‬ ‫قم‬ ‫راتب‬ ‫أعلى‬ ‫بايجاد‬ ‫يقوم‬ ‫تابع‬ ‫باستدعاء‬ ‫وقم‬ ‫العمال‬‫بينهم‬
  • 61. 60#include<iostream> using namespace std; double maxA(double*m, int size) { double max=m[0]; for(int i=1; i<size; i++) if(m[i]>max) max=m[i]; return max; } void main() { cout<<”enter the number of the worker : ”; int n; cin>>n; double *p = new double[n]; cout<<”enter the value of the matrixn”; for(int i=0; i<n; i++) cin>>p[i]; cout<<”max= “<<maxA(p,n)<<endl; } End of presentation 7
  • 62. 61Presentation 8 struct struct:‫المعطيات‬ ‫انماط‬ ‫من‬ ‫جديد‬ ‫نوع‬ ‫لتعريف‬ ‫معطيات‬ ‫بنى‬ ‫عن‬ ‫عبارة‬ ‫هي‬ ‫ا‬ ‫قبل‬ ‫وتوضع‬‫ل‬void main() ‫مثال‬: #include<iostream> #include<string> using namespace std; struct employee { int id; double salary; string name; }; void main() { employee e; cout<<”enter the id of the employee : ”; cin>>e.id; cout<<”enter the salary of the employee : ”; cin>>e.salary; cout<<”the name of the employee : ”; ‫معطيات‬ ‫نمط‬ ‫بتعريف‬ ‫قمنا‬ ‫لموظف‬
  • 63. 62cin.ignore (); getline(cin,e.name); cout<<e.name<<endl; } ‫نضع‬Ignore‫تعليمة‬ ‫اي‬ ‫قبل‬getline‫حزان‬ ‫من‬ ‫الزائدة‬ ‫االحرف‬ ‫بمسح‬ ‫لتقوم‬ ‫الدخل‬ ‫المستخدم‬ ‫فيه‬ ‫يقوم‬ ‫برناج‬ ‫بكتابه‬ ‫قم‬‫موظف‬ ‫لكل‬ ‫ان‬ ‫حيث‬ ‫الموظفين‬ ‫عدد‬ ‫بادخال‬ ‫اسم‬,‫رقم‬,‫المعلومات‬ ‫طباعه‬ ‫ثم‬ ‫الموظفين‬ ‫معلومات‬ ‫بادخال‬ ‫قم‬ ‫ثم‬ ‫راتب‬ #include<iostream> #include<string> using namespace std; struct employee { int id; double salary; string name; }; void main() { cout<<"the number of the workers isn"; int n;
  • 64. 63cin>>n; employee *p=new employee [n]; for(int i=0; i<n; i++) { cout<<"please enter the ID of the worker n"; cin>>p[i].id; cin.ignore(); cout<<"enter the salary of the workern"; cin>>p[i].salary; cin.ignore(); cout<<"enter the worker namen"; cin.ignore();getline(cin,p[i].name); } for(int i=0; i<n; i++) { cout<<"the ID of the "<<i+1<<" worker is "<<p[i].id<<endl; cout<<"the salary of the "<<i+1<<" worker is "<<p[i].salary<<endl; cout<<"the name of the "<<i+1<<" worker is "<<p[i].name<<endl; cout<<"nn"; }
  • 65. 64‫بإدخال‬ ‫ثم‬ ‫الموظفين‬ ‫عدد‬ ‫إدخال‬ ‫المستخدم‬ ‫ليستطيع‬ ‫السابق‬ ‫البرنامج‬ ‫بتعديل‬ ‫قم‬ ‫لكل‬ ‫رواتب‬ ‫لخمس‬ ‫الحسابي‬ ‫المتوسط‬ ‫مع‬ ‫معلوماتهم‬ ‫وطباعة‬ ‫الموظفين‬ ‫معلومات‬ ‫موظف‬ ‫الحل‬: #include<iostream> #include<string> using namespace std; struct employee { int id; double salary; string name; }; void main() { double sum; cout<<"the number of the workers isn"; int n; cin>>n; employee *p=new employee [n]; for(int=0; i<n; i++)
  • 66. 65{ sum=0; cout<<"plz enter the ID of the worker n"; cin>>p[i].id; cin.ignore(); cout<<"enter the salary of the workern"; for(int j=0;j<5;j++) { cin>>p[i].salary[j]; cin.ignore(); } cout<<"enter the worker namen"; cin.ignore();getline(cin,p[i].name); } for(int i=0; i<n; i++) { sum=0; cout<<"the ID of the "<<i+1<<" worker is "<<p[i].id<<endl; for(int j=0;j<5;j++) { sum=sum+p[i].salary[j];
  • 67. 66cout<<"the salary of the "<<i+1<<" worker is "<<p[i].salary[j]<<endl; } cout<<"the name of the "<< i+1<<" worker is "<<p[i].name<<endl; cout<<"avg="<<sum/5<<endl; cout<<"nn"; } } ‫لل‬ ‫بالنسبة‬ ‫موظف‬ ‫عن‬ ‫بالبحث‬ ‫يقوم‬ ‫تابع‬ ‫بكتابه‬ ‫قم‬id‫التابه‬ ‫هذا‬ ‫ويقوم‬ ‫به‬ ‫الخاص‬ ‫وجد‬ ‫ان‬ ‫ارقمه‬ ‫و‬ ‫ودخله‬ ‫الموظف‬ ‫اسم‬ ‫بطباعة‬ ‫الحل‬: #include<iostream> #include<string> using namespace std; struct employee { int id; double salary; string name; };
  • 68. 67void main() { double sum; cout<<"the number of the workers isn"; int n; cin>>n; employee *p=new employee [n]; for(int=0; i<n; i++) { sum=0; cout<<"plz enter the ID of the worker n"; cin>>p[i].id; cin.ignore(); cout<<"enter the salary of the workern"; for(int j=0;j<5;j++) { cin>>p[i].salary[j]; } cout<<"enter the worker namen"; cin.ignore();getline(cin,p[i].name); }
  • 69. 68for(int i=0; i<n; i++) { sum=0; cout<<"the ID of the "<<i+1<<" worker is "<<p[i].id<<endl; for(int j=0;j<5;j++) { sum=sum+p[i].salary[j]; cout<<"the salary of the "<<i+1<<" worker is "<<p[i].salary[j]<<endl; } cout<<"the name of the "<< i+1<<" worker is "<<p[i].name<<endl; cout<<"avg="<<sum/5<<endl; cout<<"nn"; } int k,pos; cout<<"pleas enter the random ID to search if it found / not : "; cin>>k; bool a=sear(p,n,k,pos); if(a==true) {
  • 70. 69cout<<"the given ID is foundn "<<"the ID is "<<p[pos].id<<endl <<"the name of the worker is "<<p[pos].name<<endl; } else cout<<"not foundn"; } End of presentation 8 End of programming 1 Good luck ^_^ ‫الفهرس‬ Presentation 1 page 1 Presentation 2 page 5 Presentation 3 page 11 Presentation 4 page 16 Presentation 5 page 22 Presentation 6 page 46 Presentation 7 page 51 Presentation 8 page 61 Eng -Mohammad konbos Mhd Ghayth Alsawaf