Thursday, January 21, 2016

C/C++ language. A funny challenge: sorting numbers without using arrays

From programmr.com: 

"Write a program which accepts 3 integers and sort them in ascending order."

Of course, we can use the ternary operator or some if/else statements in order to find the largest and smallest of the three numbers, respectively.

Once done that, we need to find the intermediate value using if/else statements.

The code and the print screen:

Monday, January 18, 2016

Limbajul PHP. Mostenirea de clase. O problema de sinteza

Enuntul urmator sintetizeaza aspectele esentiale ale mecanismului si sintaxei mostenirii de clase in PHP, intre care accesul din clasa derivata la membrii privati ai clasei parinte prin intermediul metodelor mostenite, apelarea din clasa derivata a constructorului din clasa parinte, apelul membrilor constanti, suprascrierea metodelor.

"Develop a hierarchic structure of classes:Circle and Cylinder 
The base class is Circle and the derived class is Cylinder. It inherits the method Display() from base class Circle. 
You have to define the new necessary fields and methods and override the method Display() to print the total surface area and volume of the cylinder. "


Daca baza cilindrului cu inaltimea H este cercul de raza R cu suprafata S = PI*R*R, atunci suprafata totala a cilindrului este: 2*PI*R*H + 2*S iar volumul este: S*H. 
Codul si printscreenul:

Wednesday, January 13, 2016

Overriding in C++ si PHP

"Implement 2 classes Square and Rectangle that inherit the Shape class and override the function Area."

In acest enunt luat de pe programmr.com cerinta nu este, in mod explicit, de a folosi o clasa de baza PHP abstracta (se putea adauga un corp vid al functiei Area(), {}, in clasa Shape, renuntand la cuvantul-cheie "abstract" ) dar am optat pentru aceasta din motive de "rodaj" al sintaxei. 

De asemenea, inaintea de a prezenta rezolvarea, am considerat util un reminder al sintaxei de derivare si al mecanismului de overriding in PHP si in C++ in doua exemple simple:

Limbajul C/C++. Operatorul ternar versus if/else

Sigur ca pare doar un mic amuzament sa rezolvi o problema de decizie cu operatorul ternar, singurul din tot limbajul care functioneaza cu trei operanzi. Uneori insa, in virtutea conciziei, o astfel de optiune este justificata, dupa cum este justificat, ba chiar preferabil, din considerente de eficienta, sa faci atribuirea:

int x = 0;
while(x <= 10) {x += 1;} in loc de "x = x + 1;"

intrucat in al doilea caz x este evaluat de doua ori.

Enuntul de pe programmr.com:

"Take input of experience and age of a person.
If the person is experienced and his age is equal to or more than 35 the slary of the person is 6000. Otherwise, if the person is experienced and his age is equal to or more than 28 but less than 35, then the salary should be 4800.For experienced person below 28 yr of age the salary should be 3000 and for inexperienced person the salary should be 2000."

Tuesday, January 12, 2016

Limbajul C++. Mostenirea de clase si o problema un pic stranie

Mostenirea implica regulile cunoscute din orice carte care trateaza limbajul C++:

- in cazul mostenirii publice, membrii publici si protected mosteniti in clasa derivata isi pastreaza nivelul de acces iar membrii privati sunt inaccesibili altfel decat prin intermediul eventualelor metode publice din clasa de baza;

- in cazul mostenirii protected, membrii publici si protected ai clasei de baza devin protected in clasa derivata, cei privati fiind inaccesibili cu observatia anterioara;

- in cazul mostenirii private, membrii publici si protected devin privati.

Daca avem urmatoarea situatie de mostenire multipla,

class X
{ public:  char i;};

class Y
{ public: float  i;};

class Z : public X, public Y
{
public:
long i;
};

ce se va initializa la succesiunea
….
z zed; 
zed.i = 500;
….
?

Sau, in alte cuvinte, compilatorul va furniza warning la atribuirea zed.i = 18.21?

Limbajul PHP. Guessing game. Tablouri multidimensionale

Urmatorul enunt de pe www.programmr.com este o problema simpla cu sase scenarii posibile, raspunsurile de selectie fiind introduse de la tastatura.

Se cere sa se identifice prin doua interogari un obiect intr-o matrice 2 x 3 (screen-ul urmator).

Din pacate, softul de pe programmr.com valideaza si topica si compozitia stringului de iesire, dupa urmatorul pattern.

1) "alive" & "outside" sau "alive" & "inside":  Obviously the living thing on your mind is a ...

2) "not alive" & "outside" sau "not alive" & "inside" : Obviously the nonliving thing on your mind is a ...
3) ("alive" sau "not alive") && "both":   Obviously the nonliving thing inside/outside on your mind is a ...

Altminteri, problema e triviala iar varianta comoda este stocarea entitatilor living/not living etc intr-un tablou bidimensional cu cheile indicate mai jos.


Friday, January 8, 2016

Limbajul PHP. The proper way to adress someone ... in English :-)


Conform multiplelor surse, e o mica bataie de cap sa te adresezi "cu eticheta" unei persoane in limba engleza, presupunand ca nu-i cunosti dinainte sexul, varsta si starea civila.

Teoria spune ca daca e vorba de femei majore, nemaritate, folosesti formula ambigua Ms. in fata numelul de familie (last name) sau chiar in fata numelui intreg. Daca femeia-i maritata, folosesti fara teama Mrs. in fata numelui (intreg sau de familie). Ambele formule isi au originea in "Mistress".

Enuntul urmator de pe programmr.com introduce in plus urmatoarea regula fara discriminare de sex: daca varsta este sub cea a majoratului, persoana este chemata pe numele complet. Daca e vorba de un barbat major, se foloseste Mr. inaintea numelui de familie.

"Write a program which displays an appropriate name for a person, using a combination of nested if's and compound conditions. Ask the user for a gender, first name, last name and age.
If the person is female and 20 or over, ask if she is married. If so, display "Mrs." in front of her name. If not, display "Ms." in front of her name. If the female is under 20, display her first and last name.
If the person is male and 20 or over, display "Mr." in front of his name. Otherwise, display his first and last name.
Note that asking a person if they are married should only be done if they are female and 20 or older."