Comp. Prog. Lec. 2,3
Comp. Prog. Lec. 2,3
Recursion
A property of functions that allows calling the function from within the
same function.
Arrays
================
Array: a group or collection of similar kinds of data
items that are stored in contiguous memory spaces. Index 0
Array size: number of array parameters. Index 1 Size
Index 2
Index: num. indicates the position of Index 3
parameter within the array (starting from 0) Index 4
.
.
.
Index n
1. Array Declaration
3. Array Initialization
int A [5] = {5,10,15,20,25};
عند وضع قيمة ابتدائية للمصفوفة، Sizeالحالة الوحيدة اللي ممكن مكتبش فيه ال
4. Array of characters
char name [10];
cin>> name;
cout<< name;
loop بدون استخدامaccess elementsدي المصفوفة الوحيدة اللي بتتعامل باسمها مباشرة في
null char (\0) بيتم تمييز اخر حرف بـSizeلو كتبت عدد حروف اقل من ال
Write a program to first input 50 floats numbers into array A.
Then, print on screen the sum and avg. of the values in the array.
#include <iostream>
using namespace std;
int main()
{ float s, Avg;
float A [5];
for (int i=0; i<5; i++)
{cin>> A[i];}
s=0;
for (int i=0; i<5; i++)
{s+=A[i];}
Avg= s/5;
cout<<s<<endl<<Avg;
return 0;}
===========
5. Copy Array
Int x [ ]= {5,0,15,20,25};
Int y [5];
for (int i=0; i<5; i++)
y [i] = x[i];
6. Comparing Arrays
Int flag =1;
for (int i=0; i<5; i++)
if (x [i] != y [i])
{flag=0;
Break;}
7. Passing Array to function
Array Name, Size علشان امرر قيم المصفوفة لدالة محتاج الـ
Int x [ ]={10,20,30,40,50}
Void show data (int xx [ ] ,int size)
{ for (int i=0; i< size; i++)
cout<< xx[i]; }
show data (x,5);
==================
Write a program to first input 30 floats numbers into array A.
Then, call a function called Sum () to find and return sum of values at even index in
the array. Finally, print contents of the array, the sum and avg.
#include <iostream>
using namespace std;
float Sum (float[], int);
int main()
{ float A [30],R;
for (int i=0; i<30; i++)
{cin>>A[i];}
R= Sum(A,30);
for (int i=0; i<30; i++)
{cout<<A[i]<<" ";}
cout<<endl<<R<<endl;
cout<<R/15;
return 0;}
float Sum (float x [],int size)
{ float s=0;
for (int i=0; i<size; i+=2)
s=s+x[i];
return s;}
Pointers
================
1. Pointer Declaration
Datatype * ptr-name;
Int * ptr;
& Adress of
* Content of
Int x;
Int *ptr; ➔ تم تجهيز المؤشر
Ptr= &x; ➔ تم التوجيه
cin>>*ptr; ➔ ptrخزن القيمة المدخلة في محتوى المكان المشار إليه بـ
cout>>*ptr; ➔ ptrاطبع محتوى المكان المشار إليه بـ
float A[5];
float *ptr;
One Element:
ptr= &A[2];
cin >>*ptr;
cout <<*ptr;
#include <iostream>
using namespace std;
int main()
{
int A[30],Min;
int*ptr;
ptr=&A[0];
}
cout <<endl<<Min;
return 0;
}5
=========================
"علّ ْمتَنَاَإِنّكَََأ َ ْنتَََا ْلعَ ِلي َُمَا ْل َح ِكي َُم
َ َس ْب َحانَكَََالَ ِع ْل ََمَلَنَاَإِ َّالَ َما
ُ "
َوزدناَعلما،َ َوانفعناَبماَعلمتنا،َ اللهمَعلمناَماَينفعنا
by Kareem Elhafi
LinkedIn