1. الدولية العربية الجامعة
المعل الهندسة كليةومواالتصاالت اتية
Dr. konbos
2012-2013
Arab international University
Programming 1
Faculty of Informatics Engineering
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;
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
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};
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.
الحل:است فيه يلزمناالسطر ترتيب فيمه ليحمل خرج متحول خدام
فيه العنصر وجد الذي
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
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
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
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 : ”;
معطيات نمط بتعريف قمنا
لموظف
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