# מבוא למדעי המחשב תרגיל 5

תרגיל היכרות עם פונקציות ב ++C

פתרון שאלה 1

```/*************************
targil: 5 sheela 1
discription: the program recive: 1) length and width of the rectangle and return the rectangle surface.
**************************/
#include <iostream>
using namespace std;

int area(int orech, int rochav)     // the function recived length and width , and return length*width (surface).
{
int result;
result = orech*rochav;
return result;
}

float area(int radius)                   // the function recived radius , and return circle surface.
{
float const pi = 3.14159;

float setachMaaagal;
return (float)setachMaaagal;
}
int main()
{
cout << "enter length and width of the rectangle:" << endl;

int orech, rochav, malben;

cin >> orech >> rochav;

while (orech <= 0 || rochav <= 0)     // to make shur that the input is correct.
{
cout << "ERROR" << endl;
cin >> orech >> rochav;
}

malben = area(orech, rochav);       // the main function use the area function for the surface.
cout << malben << endl;

float setachMaaagal;
cout << "enter radius of the circle" << endl;

while (radius <= 0)                    // to make shur that the input is correct.
{
cout << "ERROR" << endl;
}
setachMaaagal = area(radius);          // the main function use the second-area function for the circle surface.

cout << setachMaaagal << endl;

//	system ("pause");
return 0;
}
/*********OUTPUT******
enter length and width of the rectangle:
50
60
3000
62
12076.3

*********************/

```

פתרון שאלה 2

```/*************************
targil: 5 sheela 2
discription:  the program can Identify comleated numbers and print comleated numbers Under a given number .
**************************/
#include <iostream>
using namespace std;

bool perfect(int num)                 // this functiom can chack if number is comleated or not

{
int  holdNum, numLoop, modolo, sum = 0;

holdNum = num;                               // holdNum created to keep the num value in case the num change dworing the program
modolo = num - 1;
numLoop = num;

for (int i = 1; i < numLoop; i++)           // this loop take the input "num" and chack all the numbers from this num down to 1, ....
{                                           //..... (for example if the num is: '7', the loop chack 6,5,4,3,2,1) and do "%num", if the result is 0 its mean that the num divisible in this number

holdNum = num;

holdNum = num%modolo;

if (num%modolo == 0)                // its mean: the num (the input) divisible on this number
{
sum += modolo;                   // so in that case the program added the number to the sum
}

modolo--;                       // in the next rotation to do "num%(num -1)" , and in the next . '"num%(num -2)"....

}

if (sum == num)                    // "sum" here, us the sum of the numbers that num can be divisible in, so if sum = num , the number is complete
{
return true;
}
else
{
return false;
}

}

void function2(int n )        // this functiom can get number and can separate is to 1,2,3,4,5,6......... to nember and each number sent to the first function to check if is compleat number
{

int num, chack;

num = n;
n = 1;
for (int i = n; i < num; i++)
{

chack = perfect(n);
if (chack)
{

cout << n << " ";

}

n++;
}

}

int main()
{

int number;

cin >> number;
while (number < 0)                // the while loop here is to make shure that the input is not negative number.
{
cout << "EROOR" << endl;
cin >> number;
}

function2(number);                  // the input number will go to the function2 to separate it to 1,2,3,4,5,6......... to nember and each number will.....
cout << endl;                       //.....    be sent to rhe first function (perfect) to chek if between the numbers there is a compleate number.

function2(500);                      //we wont that the progran print anyway the compleat nubers between 0 to 500. no metter what the input is.

//	system("pause");
return 0;
}

/*********OUTPUT*****8

enter a number:
445
6  28
6 28 496

**********************

enter a number:
3

6 28 496

*********************/

```

פתרון שאלה 3

```/*************************
targil: 5 sheela 3
discription: the program recive id without Criticism num, and return the full ID num.
**************************/

#include <iostream>
using namespace std;

int sum(int num)            // this function cem recive number (12345678) and separate it to The parts (1,2,3,4,5,6,7,8)...
{                               //.. and return the part sum (1+2+3+4+5..).
int hold_Num, sum = 0;

hold_Num = num;

for (int i = 0; i < num; i++)
{
num %= 10;
sum += num;
num = hold_Num / 10;
hold_Num = num;
if (num == 1)
{
sum += num;
}
}

return sum;
}

int id(int num)               //  this function cem calculate Criticism ID number acording to ID number
{

int hold_num,  schumsifra, schum = 0;
int rotation = 0;     //  in rotations 0,2,4,6, the program need to duble the num, so the variable rotation is the index (the first rotation here is 0)
int caful_2;

hold_num = num;

for (int i = -8; i < num; i++)
{

num = num % 10;

if (rotation % 2 == 0)    // if the  rotations is  0,2,4,6,  duble the num
{
caful_2 = num * 2;
}
else                      // if the  rotations is  1,3,5,7 , dont duble the num
{
caful_2 = num;
}

schumsifra = sum(caful_2);  // this function use the sum function for the sum.
num = hold_num / 10;
hold_num = num;

schum += schumsifra;

rotation++;

}

int bikoret;
bikoret = 10 - (schum % 10);           // the A mathematical formula for Criticism ID number
return bikoret;

}

int main()
{
int idNum, bikoret;
cout << "enter your ID:" << endl;
cin >> idNum;
bikoret = id(idNum);

cout << "your full ID is:" << endl;
cout << idNum << bikoret << endl;
//	 system("pause");
return 0;
}
/* exsamle: idNume=12345678, the function send the number to id_function, the id_function recive 12345678 and
and separate it to The parts 1,2,3,4,5,6,7,8,... (   [nut sum function !!]  the id_function allso can do that ).
in rotation = 0, the nuber is 8, the id_function dubble it and now is 16, the id_function send the 16 nember to
the sum_function, the sum_function recive 16 and return 7  (the sum of 1+6) and the result (7) added to variable "schum"
in rotation = 1 the nuber is 7 the id_function NOT dubble it , and the result (7) added to variable "schum" (7+7 = 14)
in the last rotation the variable "schum" is 38 (7+7+3+5+8+3+4+1)  and Criticism ID number is 10-(38%10).

```

פתרון שאלה 4

```/*************************
targil: 5 sheela 4
discription:  calculation circle Surface.
**************************/
#include <iostream>
#include <ctime>          // for random numbers
using namespace std;

float randomNum(int r)                 // randomNum function Producer Number between r to -r ('r' is the radius).
{
int  random1, random2, random3,tempResult;
float result;

random1 = rand() % (r + 1);       // number betwwen 0 to r
random2 = rand() % (r + 1);        // number betwwen 0 to r
random3 = rand() % (r + 1);         // number betwwen 0 to r

tempResult = random1 - random2;    // to get allso a negative random numbers

if (random3!=0)                   // te prevent a math error if Dividing by zero.
{
result = (float)tempResult / random3; // to get allso a  random numbers between 0 to 1.

}
else
{
result = random1;

}

return result;

}

bool points(int radius)          // this function can give a random X & Y Coordinates and chack if the point is in the circle surface
{

float x, y;

if (x*x+ y*y <= radius*radius)    //if the point is in the circle surface
{
return true;
}
else
{
return false;
}

}

float radiusAndPoints(int r, int n=1000)   // this function recive  radius and n (n= point number) and calculate the circle Surface (approximately).

{

int point,  yes=0,no=0 ;
float circleSurface;

for (int i = 0; i < n; i++)
{
point = points(r);
if (point)
{
yes += 1;       // yes, the point is in the circle Surface
}
else
{
no += 1;      // no, the point is not in the the circle Surface
}

}

int SquareSurface, r2;
r2 = 2 * r;
SquareSurface = r2*r2;

circleSurface = ((float)yes / n) *SquareSurface;

return circleSurface;
}

int main()

{
srand(time(NULL));  // for random numbers
int r, n;
float result;
cout << "enter the circle radius:" << endl;
cin >> r  ;
cout << "enter the points number:" << endl;
cin >> n;

cout << "the circle Surface is: "<< "["<<result <<"]"<< " (approximately)"<< endl;
system("pause");
return 0;

}
/***********OUTPUT**************
targil: 5 sheela 4
student:benyamin royttenberg
ID:300678802
discription:  calculation circle Surface.
**************************/

```