programming 1 full

70
معة العربية الدوليةلجا امعلية الهندسة ال كل و متتصاتية وا اDr. konbos 2012-2013 Arab international University Programming 1 Faculty of Informatics Engineering

Upload: mhd-ghayth-alsawaf

Post on 14-Apr-2017

134 views

Category:

Documents


2 download

TRANSCRIPT

الجامعة العربية الدولية

اتية واالتصاالتموكلية الهندسة المعل

Dr. konbos

2012-2013

Arab international University

Programming 1 Faculty of Informatics Engineering

1 Presentation 1

:مراجعة

:هي تعليمة مقسومة الى قسمين ++cout<<Xتعليمة

للعنصر " 1"اضافة طباعة العنصر ومن ثم

cout<<x;

X=x+1;

فهي تعني ;cout<<++xاما تعليمة

للعنصر ومن ثم طباعة العنصر" 1"زيادة

X=x+1;

cout<<x;

وتحتاج مرة xهي تعليمة الزاحة مؤشر الكتابة ;cout<<setw(x)تعليمة

<iomanip>لتضمين المكتبة

:مالحظة

اي في هذه for في حال عدم معرفة عدد مرات التكرار ال نقوم باستخدام حلقة

الحلقة نكرر الى ان ينكسر الشرط

:تمرين

قم بكتابة برنامج يحسب مجموع االعداد ضمن مجال يقوم المستخدم بادخال

: yواكبر رقم فيه وهو xاصغر رقم فيه وهو

:طريقة الحل

x & yيقوم المستخدم بادخال العنصران -1

yوينته عند xنقوم بحساب المجموع باستخدام حلقة يسير عدادها بدءاً من -2

:الحل

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;

3 cout<<”enter the tow variable x,y\n”;

cin>>x>>y;

while(x!=y)

{

if(x>y)

x=x-y

else

y=y-x;

}

cout<<”the maximum diverse is ”<<x<<endl;

}

1-العدد 0محصور من المجال من دوما يكون باق القسمة: مالحظة

:تمرين

يقوم المستخدم بادخاله خانات عددقم بكتابة برنامج يقوم بحساب مجموع

:طريقة الحل

10لتجزئة الخانات و من ثم نقسم العدد على 10نستخدم طريقة باق القسمة على

23=3+9+8+7المطلوب حساب خانته بالشكل 3987العدد : مثال

:الحل

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

5

Presentation 2

:تتمة المراجعة

ارقام واوجد اكبر واصغر رقم واحسب عدد 10قم بكتابة برنامج يقوم بادخال

:االعداد الزوجية و الفردية فيه

:طريقة الحل

الدخال العناصر ثم نعتبر اول عنصر هو العنصر االكبر forبانشاء حلقة نقوم

.ارن به بقية العناصر ونق واالصغر

1نضع عداد االعداد الزوجية يزيد 0= 2اذا كان باق قسمة العدد على

1وغير ذلك نزيد عداد االعداد الفردية

:الحل

#include<iostream>

using namespace std;

void main()

{

int x,max,e=0,o=0;

cout<<”enter the first value\n”;

cin>>x;

if(x%2==0)

e++;

6 else

o++;

max=x;

for(int i=1; i<=9; i++)

cout<<”enter the either element\n”;

{

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;

8 max= 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--;

}

طالب ثم قم بحساب وعرض المتوسط 4عالمات ل 5قم بكتابة تمرين الدخال

الحسابي لمجموع هذه العالمات لكل طالب

:الحل

#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 student\n”;

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

11 Presentation 3

( :one dimensional array)المصفوفة االحادية البعد

نعرفه (const)حجم المصفوفة ثابت (static array)سكانة المصفوفات ال -1

كي نستطيع void mainاي قبل ال (global)على شكل متحول عام

الوصل اليه من كل اجزاء البرنامج

ستمر معنا بالبحوث القادمة (Dynamic array)المصفوفة الديناميكية -2

:مالحظة

:يوجد طريقتان لعكس عناصر مصفوفة

:كما يلي Xة لها نفس حجم المصوف Yاما انا نقوم بانشاء مصفوفة جديدة

#include<iostream>

using namespace std;

const int size=4;

void main()

{

int x[size];

cout<<”enter the value of the matrix\n”;

for (int i=0; i<size; i++)

cin>>x[i];

cout<<”the reverse matrix of x\n”;

int y[size];

12 for(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 matrix\n”;

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]<<” “;

13 cout<<endl;

}

قم بكتابة برنامج لترتيب عناصر مصفوفة أحادية البعد تصاعدياً

#include<iostream>

using namespace std;

const int size=5;

void main()

{

int ar[size];

cout<<"enter the value of the matrix\n”;

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 matrix\n”;

for(int i=0; i<size; i++)

cin>>ar[i];

15 cout<<”enter the random number\n”;

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 exist\n”;

}

End of presentation 3

16 Presentation 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];

}

>>coutبـِ <<cinوالطباعة تتم بنفس الطريقة مع استبدال

اذا كانت متناظرة (4X4)قم بكتابة برنامج تقوم فيه باختبار مصفوفة مربعة

(عناصر على طرفي القطر الرئيسي متساويةاي ان ال)ام ال

:طريقة الحل

الننا نفرض ان trueيحمل قيمة إبتدائية boolنقوم بتعريف متحول بولياني

المصفوفة متناظرة اال في حال وجود عنصر واحد غير متناظر

:الحل

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 symmetric\n”;

else

cout<<”the given matrix is not symmetric\n”;

}

18 floatيقوم بادخال عناصر مصفوفة ثنائية البعد من النوع قم بكتابة برنامج

مجموع عنصار المثلث العلوي وقم بحساب مجموع عنصار القطر الثناوي و

.و مجموع عناصر العمود االول

:طريقة الحل

i<j :المثلث العلوي هو

i+j=size-1 او j==size-i-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];

19 if(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};

20 for(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];

}

21 for(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

22 Presentation 5

Function

كادخال عناصر او التابع هو عبارة عن حل لقضية معينة من السؤال

مرات متكررةيستخدم هذا الحل عدة يمكن ان و الخ........طباعة او جمع او

:نميز من التوابع

)bool, int , double, float ) التي تعيد قيمةالتوابع -1

وال يقرأ التابع اي شيء بعد في نهاية الحل;return ويكونوا مقترنين مع كلمة

;breakاي انها بمثابة كلمة ;return تعليمة

ا عند استدعاء التابع ظهارهوإلوفي هذه التوابع اليمكن ارجاع سوى قيمة وحيدة

اما ان نسندها لمتحول او نقوم بطباعتها ()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()

{

24 int 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, y\n”;

cin>>x,y;

25 cout<<”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)

في هذا التمرين يقوم التابع بارجاع قيمة

المحيط بينما تم استخدام وحيدة وهي حساب

متحول خرج لتخزين قيمة المساحة

27 cout<<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];

28 cout<<”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)

30 cout<<”max=”<<max<<endl<<”position at

“<<pos<<endl;

}

قم بكتابة تابع يقوم بالبحث عن عنصر داخل مصفوفة احادية البعد

اذا لم يكن موجود falseاذا كان العنصر موجود ويعيد القيمة trueيعدي القيمة

الحل

#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;

31 int 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<<”found\n”;

else

cout<<”not found\n”;

}

اكتب تابع يقوم بعكس عناصر مصفوفة احادية وتابع لفرز عناصرها تصاعديا

وتابع لطابعتها ثم اكتب تابع يقوم باخذ دخل مصفوفة ثنائية بعد ويقوم بفرز

الثاني تصاعديا وتابع لطباعة هذه المصفوفة عناصر الصف

ثنائية وقم باستدعاء التوابع , اكتب برنامج يقوم بادخال عناصر مصفوفة احادية

السابقة بالترتيب قبل وبعد الفرز والعكس

:الحل

#include<iostream>

using namespace std;

const int size=3;

void rev(int ar[size] )

{

cout<<"the reverc of the matrix is :\n";

32 for( 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++)

33 cout<<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);

34 cout<<"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 Alsawaf\n"<<"Thank

you for use it bye bye ^_^\n";

}

35 , intهي قالب يقبل كل المتحوالت من النوع <template<class tتعليمة

folat double حيثt متحول اي ان قيمةt تتبدل تبعا لوضع المتحول في

. tحيث تحل طبيعة المتحول مكان المتغير ()void mainال

مثال

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]);

}

36 template<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 matrix\n";

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]<<" ";

38 cout<<endl;

}

void main()

{

int ar[size],i;

cout<<"enter the value of the matrix\n";

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()

{

40 int pos,i,j;

float ar[size][size],k;

bool f;

cout<<"enter the value of the 3X3 matrix\n";

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

41 cout<<"the number not found \n -1";

}

: ()randالتابع العشوائي

ويستخدم هذا التابع لتوليد ارقام عشوائية

6وحتى ال 0مثال قم بتوليد مجموعة ارقام عشوائية من ال

الحل نقوم باستخدام التابع العشوائي مع االعتماد على باقي القسمة

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];

42 kkk(ar);

for(int i=0; i<size; i++)

cout<<ar[i]<<” “;

cout<<endl;

}

للحصول على نتائج متغيرة دوما عند استخدام التابع العشوائي نضع قبالً منه ويقبل <include <cstdlib#وهو يحتاج الى تضمين المكتبة ()srandالتابع

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;

43 cin>>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;

}

44 void 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

}

ها عند التغيير عليهاعلي المتحوالت العامة يثبت التغيير:مالحظة هامة

قم بكتابة تابع يقوم باعادة جذور معادلة من الدرجة الثانية

doubleهم جذور المعادلة و ياخذو قيمة x1 , x2هنا :الحل

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 c\n”;

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<<”error\n”;

}

End of presentation 5

End of function

46

Presentation 6

Pointer

.ةالمتغير في الذاكرللداللة على عنوان مؤشر عن عبارة هوقبل المؤشر فهذا يعني انه يقوم باالشارة الى مكان المتغير ( &)عندما نضع -1

في الذاكرة :مثال

int x=4; int*p; p=&x; cout <<p // مكان العنصر في الذاكرة

تستخدم لتعريف المؤشر )*(اشارة -2

(intقمنا بتعريف مؤشر من النوع ) int *p: نقوم بتعريف المؤشر كالتالي

: مثال

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نفرض ان المستخدم ادخل الرقم

47 cout<<*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

51 Presentation 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 matrix\n”;

int size,id,pos;

cin>>size;

int*ID=new int[size];

cout<<”enter the ID of the student\n”;

for(int i=0; i<size; i++)

cin>>*(ID+i);

cout<<”enter the random ID\n”;

53 cin>>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 found\n”;

}

دوما التوابع التي تطبق على المصفوفة الديناميكية يكون لها دخل وهو : مالحظة

حجم المصفوفة لياخذ صورة عنه

String

;string sهي مصفوفة المحارف ويتم تعريفها بالشكل stringال

#include<iostream>

using namespace std;

void main()

{

string s;

cin>>s; //”ahmad ali”

}

d a m h a هذه الجملة سوف تشكل مصفوفة بالشكل

54 اليقرأ الفراغ stringفي ال cinحيث ان ال

واستخدام <string>ولتفادي هذه المشكلة علينا تضمين مكتبه

التي تظهر الكلمة والفراغ getline(cin,s)التعليمة

فسوف يتشكل لدينا مصفوفة ;”string s2=”ziad Hasanإذا وضعنا

بالشكل

n a s a H d a i z

فهذا يعني اننا ناخذ االحرف االربعة االولى من string s3(s2,4)واذا وضعنا

string s2ال

:جديدة كالتالي stringنضع string لجمع ال

string s4 =s2+”hi”;

()s2.sizeفاننا نستخدم التابع stringلمعرفة حجم ال

مثال

#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;

}

’k‘بالحرف ’a‘قم بكتابة برنامج الستبدال الحرف

#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 برنامج يقوم وقم بكتابة ’a‘بالحرف ’k‘قم بكتابه تابع يقوم باستبدال الحرف

باستدعاء هذا التابع

#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 matrix\n”;

for(int i=0; i<n; i++)

cin>>p[i];

cout<<”max= “<<maxA(p,n)<<endl;

}

End of presentation 7

61 Presentation 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 : ”;

قمنا بتعريف نمط معطيات

لموظف

62 cin.ignore ();

getline(cin,e.name);

cout<<e.name<<endl;

}

لتقوم بمسح االحرف الزائدة من حزان getlineقبل اي تعليمة Ignoreنضع

الدخل

بادخال عدد الموظفين حيث ان لكل موظف قم بكتابه برناج يقوم فيه المستخدم

راتب ثم قم بادخال معلومات الموظفين ثم طباعه المعلومات, رقم , اسم

#include<iostream>

#include<string>

using namespace std;

struct employee

{

int id;

double salary;

string name;

};

void main()

{

cout<<"the number of the workers is\n";

int n;

63 cin>>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 worker\n";

cin>>p[i].salary;

cin.ignore();

cout<<"enter the worker name\n";

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<<"\n\n";

}

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 is\n";

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 worker\n";

for(int j=0;j<5;j++)

{

cin>>p[i].salary[j];

cin.ignore();

}

cout<<"enter the worker name\n";

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];

66 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<<"\n\n";

}

}

الخاص به ويقوم هذا التابه idقم بكتابه تابع يقوم بالبحث عن موظف بالنسبة لل

بطباعة اسم الموظف ودخله و ارقمه ان وجد

:الحل

#include<iostream>

#include<string>

using namespace std;

struct employee

{

int id;

double salary;

string name;

};

67 void main()

{

double sum;

cout<<"the number of the workers is\n";

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 worker\n";

for(int j=0;j<5;j++)

{

cin>>p[i].salary[j];

}

cout<<"enter the worker name\n";

cin.ignore();getline(cin,p[i].name);

}

68 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];

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<<"\n\n";

}

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)

{

69 cout<<"the given ID is found\n "<<"the ID is

"<<p[pos].id<<endl

<<"the name of the worker is "<<p[pos].name<<endl;

}

else

cout<<"not found\n";

}

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