0% found this document useful (0 votes)
13 views7 pages

Comp. Prog. Lec. 2,3

Uploaded by

tohawk707
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views7 pages

Comp. Prog. Lec. 2,3

Uploaded by

tohawk707
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Computer Programming Lecture 2,3

Recursion
A property of functions that allows calling the function from within the
same function.

n! = n*(n-1) *(n-2) *(n-3) *………...*3*2*1


= n* (n-1)!
= n*(n-1) *(n-2)!
Use recursion to find factorial of n
Int Fact (int n)
{
If (n==1)
return 1;
else
return n*Fact (n-1); }
=======================

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

Datatype Array name [ size] ;


Simple Datatype
Note: every place in the array takes 4 bytes.
Num. of bytes that array reserved = size *4

2. Access Array Elements

I/P, O/P, Modify

Access One Element: Access All Elements:


cin>> X [ z]; For (i=0; i<=4; i++)
cout<< X [z]; cin>> X [ z];
X [z] = X [z] *20; X [z] = X [z] *20;
cout<< X [z];

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;

2. Operation with Pointer

& Adress of
* Content of

Int x;
Int *ptr; ➔ ‫تم تجهيز المؤشر‬
Ptr= &x; ➔ ‫تم التوجيه‬
cin>>*ptr; ➔ ptr‫خزن القيمة المدخلة في محتوى المكان المشار إليه بـ‬
cout>>*ptr; ➔ ptr‫اطبع محتوى المكان المشار إليه بـ‬

int c=10, d=20;


int*ptr a, *ptrb;
ptr a=&c;
ptr b= ptr a;
cout << *ptr a<< “ “<<*ptr b; ➔ 10 10
ptr b=&d;
cout << *ptr a<< “ “<<*ptr b; ➔ 10 20
*ptr a= *ptr b;
cout << *ptr a<< “ “<<*ptr b; ➔ 20 20
3. Pointer to Array

float A[5];
float *ptr;

One Element:
ptr= &A[2];
cin >>*ptr;
cout <<*ptr;

All Elements(by 3 ways):


First Way ‫أسوء طريقة‬
for (int i=0; i<5; i++)
ptr=&A[i];
cin >>*ptr;
cout <<*ptr;
================
Second Way ‫أفضل طريقة‬
ptr=&A[0];
for (int i=0; i<5; i++)
cin >>*(ptr+i);
================
Third Way
ptr=&A[0];
for (int i=0; i<5; i++)
{
cout <<*ptr;
ptr++;
}
By using pointer, write a program to first input 30 int values into array. Then, find
minimum value. Finally, print contents of array and minimum value.

#include <iostream>
using namespace std;

int main()
{
int A[30],Min;
int*ptr;
ptr=&A[0];

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


{cin>>*(ptr+i);
Min =*ptr;
if (*(ptr+i)<Min)
Min= *(ptr+i);
cout << *(ptr+i)<<" ";

}
cout <<endl<<Min;
return 0;
}5
=========================
"‫علّ ْمتَنَاَإِنّكَََأ َ ْنتَََا ْلعَ ِلي َُمَا ْل َح ِكي َُم‬
َ َ‫س ْب َحانَكَََالَ ِع ْل ََمَلَنَاَإِ َّالَ َما‬
ُ "
‫َوزدناَعلما‬،َ ‫َوانفعناَبماَعلمتنا‬،َ ‫اللهمَعلمناَماَينفعنا‬
by Kareem Elhafi
LinkedIn

You might also like