C/C++
* Algoritm
- Algoritmul reprezinta rezolvarea etapizata, in pasi mici, elementari, a unei probleme.
Scopul folosirii algoritmului este aplicarea lui la o serie pe probleme care au aceeasi metoda de rezolvare; adica, pentru orice dateale problemei, algortimul trebuie sa se incheie cu un raspuns.
Un exemplu in acest sens este acuatia de gradul I. Exista o infinitate de ecuatii de gradul I dar toate se subscriu aceleiasi ecuatii generice A*X+B=0. Algoritmul trebuie sa rezolve aceasta ecuatie generica pentru a obtine rezultate corecte pentru intreaga clasa se ecuatii de gradul I sau sa anunte eroarea in cazul in care simbolul A ar avea valoarea 0.
- Caracteristici
Deducem de aici o serie de proprietati pe care un algoritm corect trebuie sa le indeplineasca:
- sa aiba caracter de generalitate: sa rezolve o intreaga clasa de probleme de acelasi gen (pentru orice date de intrare) .
- sa aiba finitudine: sa ofere un raspuns la problema si sa se incheie in timp util.
- sa fie clar: calculul/etapele sa fie descriese intr-o maniera fara dubii.
DATE DE INTRARE -> ALGORITM -> DATE DE IESIRE
- Reprezentarea algoritmilor
- secventa : pasii sa se execute unul dupa altul.
De asemeni, pentru exprimarea algoritmilor s-au incercat metode care sa poata fi intelese de toti. Iata cateve dintre ele:
- testul : in cazul in care rezolvarea trebuie sa raspunda unei intrebari sa putem aplege traseul logic ce trebuie urmat
- repetitia : sa putem repeta o anumita secventa daca algoritmul o cere
- scheme logice: metoda grafica care specifica traseul ce trebuie urmat
- pseudocod: un set de reguli de scriere , in principiu in limba engleza; pt elevi s-a simplificat, folosindu-se aceleasi notatii si limba romana
- limbaj de programare: mult mai strict ca exprimare dar folosind aceleasi concepte.
** C++
- C++ (pronunțat „C plus plus„) este un limbaj de programare general, compilat. Este un limbaj multi-paradigmă, cu verificarea statică a tipului variabilelor ce suportă programare procedurală, abstractizare a datelor, programare orientată pe obiecte. În anii 1990, C++ a devenit unul din cele mai populare limbaje de programare comerciale, rămânând astfel până azi. Succesul extraordinar pe care il are limbajul C++ a fost asigurat de faptul ca a extins cel mai popular limbaj al momentului, C. Programele scrise in C functioneaza si in C++, si ele pot fi transformate in C++ cu eforturi minime.
- INSTRUCTIUNI
- Instructiunea compusa
-Unde instr 1, instr 2, ..., instr n pot fi oricare dintre instructiunile limbajului de programareCode:You have to register to be able to see this link. Register HERE! If you are already a member please log in! If you still you are not able to see the link you need to activate your account or an administrator need to activate your account!- Instructiunea de atribuire
-Unde "=" se numeste operator de atribuire.Code:You have to register to be able to see this link. Register HERE! If you are already a member please log in! If you still you are not able to see the link you need to activate your account or an administrator need to activate your account!
Modul de executie:
-> se evalueaza expresia aflata la dreapta semnului de atribuire si rezultatul obtinut este memorat in variabila aflata la stanga semnului de atribuire.
OBS!
Variabila si expresia utilizata in cadrul unei instructiuni de atribuire trebuie sa aibe acelasi tip de date sau sa fie de tipuri compatibile de date.
+ => adunare.Code:You have to register to be able to see this link. Register HERE! If you are already a member please log in! If you still you are not able to see the link you need to activate your account or an administrator need to activate your account!
- => scadere.
/ => DIV. (partea intreaga a unui numar impartit la alt numar. Ex : 1234 / 10 = 123 [1234 impartit la 10 ne da 123,4 , deci ne va da 123!])
% => MOD. (partea zecimala a unui numar impartit la alt numar. Ex : 1234 % 10 = 4 [1234 impartit la 10 ne da 123,4 , deci ne va da 4!])
== => Verifica daca doua variabile sunt egale.- Instructiunea de citire
- OBS!
Functiile pentru citirea si afisare datelor se afla in biblioteca iostream (#include <iostream>)
Atunci cand un program utilizeaza cel putin o instructiune de citire sau afisare programul trebuie sa contina in mod obligatoriu in sectiunea de declarare biblioteci linia: #include <iostream>
Forma generala a instructiunii de citire este urmatoarea:
Instructiunea de citire se efectueaza astfel:Code:You have to register to be able to see this link. Register HERE! If you are already a member please log in! If you still you are not able to see the link you need to activate your account or an administrator need to activate your account!
- se intrerupe executia programului si se asteapta introducerea unui nr de valori "=" cu numarul variabilelor aflate in cadrul instructiunii cin.
- valorile introduse sunt memorate pe rand in variabila din linia de instr.
OBS!
Valorile introduse trebuie sa fie de acelasi tip de date ca si tipurile de date aflate in cadrul instructiunii de citire.
Code:You have to register to be able to see this link. Register HERE! If you are already a member please log in! If you still you are not able to see the link you need to activate your account or an administrator need to activate your account!- Instructiunea de scriere
-Modul de executie:Code:You have to register to be able to see this link. Register HERE! If you are already a member please log in! If you still you are not able to see the link you need to activate your account or an administrator need to activate your account!
- se evalueaza expresiile care apar in cadrul liniei de instr. si valorile obtinute se afiseaza pe ecran.
In cadrul unei instrucituni cout se pot afisa si caractere sau siruri de caractere. In limbajul C++ se reprezinta incadrate intre apostrofuri, iar sirurile de caractere se reprezinta intre ghilimele.
Ex: Se citesc de la tastatura 2 nr intregi a si b. Scrieti un program care afiseaza suma si produsul celor 2 nr.
In cadrul instructiunii cout pot sa apara endl care realizeaza saltul la linie noua a cursorului.Code:You have to register to be able to see this link. Register HERE! If you are already a member please log in! If you still you are not able to see the link you need to activate your account or an administrator need to activate your account!
Code:You have to register to be able to see this link. Register HERE! If you are already a member please log in! If you still you are not able to see the link you need to activate your account or an administrator need to activate your account!- Instructiunea de decizie
-Modul de executie:Code:You have to register to be able to see this link. Register HERE! If you are already a member please log in! If you still you are not able to see the link you need to activate your account or an administrator need to activate your account!
- se evalueaza expresia si daca aceasta este diferita de 0 se efectueaza instructiunea 1.
- in cazul in care valoarea expresiei este egala cu 0 atunci:
a) daca instructiunea if este in forma a) se efectueaza instructiunea 2.
b) daca instructiunea if este in forma b) atunci se executa instructiunea care urmeaza in cadrul algoritmului dupa instructiunea de decizie.
Ex: Se citesc 2 nr intrgi a si b. Scriei un program C++ care afiseaza valoarea maxima dintre cele 2 nr citite.
Code:You have to register to be able to see this link. Register HERE! If you are already a member please log in! If you still you are not able to see the link you need to activate your account or an administrator need to activate your account!- Instructiunea repetitiva cu nr cunoscut de pasi(for)
-Modul de executie:Code:You have to register to be able to see this link. Register HERE! If you are already a member please log in! If you still you are not able to see the link you need to activate your account or an administrator need to activate your account!
- se efectueaza expresia de initializare si se valueaza expresia de test. Dca rezultatul expresiei de test este diferit de 0(adica este adevarata) se efectueaza instructiunea subordonata instructiunii for.
- dupa efectuarea instructiunii subordonate se efectueaza expresia de incrementare sau de decrementare, si se evalueaza din nou expresia de test. Daca valoarea acesteia este diferita de 0 se efectueaza din nou instructiunea subordonata secventii for si se efectueaza expresia de incrementare sau de decrementare.
- algoritmul se continua pana cand valoarea expresiei de test este egala cu 0(este falsa).
OBS!
Instructiunea subordonata secventei for poate fi instructiune simpla sau compusa.
Ex: Se citeste n nr nat. Scrieti un prgram care calculeaza suma primelor n nr naturale.
S=1+2+3+...+n, n∈N
Code:You have to register to be able to see this link. Register HERE! If you are already a member please log in! If you still you are not able to see the link you need to activate your account or an administrator need to activate your account!- Instructiunea repetitiva cu test initial(while)
-Modul de executie:Code:You have to register to be able to see this link. Register HERE! If you are already a member please log in! If you still you are not able to see the link you need to activate your account or an administrator need to activate your account!
- se evalueaza expresia si daca aceasta este diferita de 0 se efectueaza instructiunea subordonata. Dupa efectuarea instructiunii se evalueaza din nou expresia si daca aceasta este diferita de 0 se efectueaza din nou instructiunea subordonata. Algoritmul se reia cat timp valoarea expresiei este diferita de 0.
- in cazul in care valoarea expresiei este egala cu 0, se termina de efectuat instructiunea while si se efectueaza instructiunea care urmeaza in cadrul programului dupa instructiunea while.
Ex:
n∈N
S=1+2+...+n.
Code:You have to register to be able to see this link. Register HERE! If you are already a member please log in! If you still you are not able to see the link you need to activate your account or an administrator need to activate your account!- Instructiunea repetitiva cu test final(do-while)
-;Code:You have to register to be able to see this link. Register HERE! If you are already a member please log in! If you still you are not able to see the link you need to activate your account or an administrator need to activate your account!
Modul de executie:
- se efectueaza instructiunea subordonata si apoi se evalueaza expresia, daca expresia este diferita de 0 atunci se efectueaza din nou instructiunea subordonata.
- dupa efectuarea instructiunii se evalueaza din nou expresia, algoritmul reluandu-se cat timp expresia este diferita de 0.
- in cazul in care valoarea expresiei este egala cu 0 se termina de efectuat instructiunea do-while si se executa instructiunea care urmeaza in cadrul programului dupa aceasta.
Ex:
n∈N
S=1+2+...+n;
Code:You have to register to be able to see this link. Register HERE! If you are already a member please log in! If you still you are not able to see the link you need to activate your account or an administrator need to activate your account!
Puteti lucra in CodeBlocks. Se gaseste foarte usor pe internet.
Link Download CodeBlocks : You have to register to be able to see this link. Register HERE! If you are already a member please log in! If you still you are not able to see the link you need to activate your account or an administrator need to activate your account!
Puteti incerca prima porblema mai usoara :
Se da un numar n natural. Sa se calculeze si sa se afiseze pe ecra suna cifrelor numarului n.
}Code:You have to register to be able to see this link. Register HERE! If you are already a member please log in! If you still you are not able to see the link you need to activate your account or an administrator need to activate your account!
Exemplu :
n = 1234
s = 0
Verifica daca n este mai mare ca 0 (este) , s devine 0 + 4 . n devine 123 . Verifica daca n este mai mare ca 0 (este), s devine 4 + 3 . n devine 12 . Verifica daca n este mai mare ca 0 (este) , s devine 7 + 2. n devine 1 . Verifica daca n este mai mare ca 0 (este) , s devine 9 + 1. n devine 0 . Verifica daca n este mai mare ca 0 (nu este) . Ciclul s-a oprit. Se afiseaza pe ecran s=10
Sper ca va este de folos . Accept orice fel de critica .