5. Unazat.

Slides:



Advertisements
Similar presentations
第三次小考. #include using namespace std; int aaa(int *ib,int a1,int a2) { int u,v; int m=(a1+a2)/2; if(a1==a2)return ib[a1]; u=aaa(ib,a1,m); cout
Advertisements

While Loops Programming. COMP102 Prog Fundamentals I: while Loops/Slide 2 Shortcut Assignments l C++ has a set of shortcut operators for applying an operation.
Programming Switch command. COMP102 Prog. Fundamentals: Switch command / Slide 2 Multiple Selection: The switch Statement value1 action 1 value2 action.
Computer Science 1620 Accumulators. Recall the solution to our financial program: #include using namespace std; int main() { double balance = ;
1 9/25/06CS150 Introduction to Computer Science 1 Nested Ifs, Logical Operators, exit() Page 194.
Introduction to Programming (in C++) Data and statements Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
 For Loops › for (variable set; condition; incremental or decrement){ // loop beginning › } // loop end  While loops › while (condition) { // beginning.
Current Assignments Homework 3 is due tonight. Iteration and basic functions. Exam 1 on Monday.
Lecture 7: Making Decisions Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
Chapter 05 (Part II) Control Statements: Part II.
CSE202: Lecture 13The Ohio State University1 Function Scope.
Intro. to Computer Programming Eng. Nehal A. Mohamed Spring Semester-2016.
1 C++ Classes and Data Structures Course link…..
T/F  The following code will compile without error. int x = 3, y = 4, z = 5; double k = 3.4; cout
Chapter 13 Introduction to C++ Language
Chapter 3 Selection Statements
Introduction to C++ (Extensions to C)
LESSON 2 Basic of C++.
Section 3 Review Mr. Crone.
Introduction to C++ Programming Language
Intro to Programming Week # 6 Repetition Structure Lecture # 10
Chapter 3 L7.
Programming Fundamentals
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Math Library and IO formatting
לולאות קרן כליף.
بسم الله الرحمن الرحيم.
Reserved Words.
Dynamic Memory Allocation Reference Variables
Function Basics.
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Programming -2 برمجة -2 المحاضرة-5 Lecture-5.
Chapter 2 Elementary Programming
Random Number Generation
Elementet e gjuhës C++.
Të llogaritet shuma e elementeve të vektorit.
Elementet e gjuhës C++.
Andy Wang Object Oriented Programming in C++ COP 3330
אבני היסוד של תוכנית ב- C++
אבני היסוד של תוכנית ב- C++
Screen output // Definition and use of variables
הרצאה 03 אבני היסוד של תוכנית ב- C
6. Unaza While dhe Do While
F U N K S I O N E T.
Counting Loops.
Starting Out with C++: From Control Structures through Objects
Pointers & Functions.
اصول کامپیوتر ۱ مبانی کامپیوتر و برنامه‌سازی
Code::Block vs Visual C++
PROGRAMIM I UNIVERSITETI I TETOVËS.
Pass by Reference.
CSC 270 – Survey of Programming Languages
Summary Two basic concepts: variables and assignments Basic types:
Formatting the Output The C++ standard library supplies many manipulators: endl, setw, fixed, showpoint, setprecesion. If we want to use endl, fixed, or.
Degëzimet.
Formatted Input, Output & File Input, Output
Programim I Degëzimet Gazmend Xhaferi.
Variablat dhe konstantet
Degëzimet.
Let’s all Repeat Together
Output Formatting Bina Ramamurthy 4/16/2019 BR.
Pointers & Functions.
(Dreaded) Quiz 2 Next Monday.
Unazat FOR.
Unazat while.
Programming Strings.
Programming Fundamental
Operatorët.
Output Formatting Bina Ramamurthy 9/25/2019 BR.
Presentation transcript:

5. Unazat

Unaza FOR  Numri i ekzekutimeve të ciklit është i njohur . for(inicializim; kushti; azhurnim) komanda; for(inicializim; kushti; azhurnim) komanda; for(inicialzimim; kushti; azhurnim){ komanda1; komanda2; … komandaN; } Komanda e zakonshme. Komanda for e përbërë. Komanda for e ndërthurur.

for(i=fillim; i<=fund; i=i+hapi){ blloku; } PARAMETRA TË UNAZËS: i – variabla e unazës. fillim – vlera fillestare variablës i. fund – vlera përfundimtare e variablës i. hapi – hapi me të cilin ndryshohet variabla i. blloku – komandat të përfshira brenda unazës. blloku i<=fund Po Jo i=i+hapi; i=fillim;

Komanda FOR e zakonshme Shembull 1. Të llogaritet shuma e numrave natyrorë mes 1 dhe n (n<100). Zgjidhje 1. #include <iostream> using namespace std; int main() { int n,i,Shuma=0; cout << "n: "; cin >> n; if(n>=1 && n<=99) { for(i=1; i<=n; i++) Shuma=Shuma+i; cout << "Shuma=" << Shuma << endl; } else cout << "Gabim! n:[1,99]"; return 0; Shembuj daljesh Dalje 1 n: 5 Shuma=15 Dalje 2 n: 50 Shuma=1275 Dalje 3 n: 125 Gabim! n:[1,100] Zgjidhje 2. … for(int i=1; i<=n; Shuma+=i, i++);

Shembull 2. Shuma e katrorëve të numrave natyrorë çift mes 2 dhe n. #include <iostream> using namespace std; int main() { int n,Shuma=0; cout << "n: "; cin >> n; for(int i=2; i<=n; i=i+2) Shuma=Shuma+(i*i); cout << "Shuma=" << Shuma << endl; return 0; } Shembuj daljesh Dalje 1 n: 6 Shuma=56 Dalje 2 n: 20 Shuma=1540

Shembull 3. Të llogaritet faktorieli i numrit n. n! = 1*2*3* … *n #include <iostream> using namespace std; int main() { int n,F=1; cout << "n: "; cin >> n; for(int i=1; i<=n; i++) F=F*i; cout << n << "! = " << F << endl; return 0; } Shembuj daljesh Dalje 1 n: 3 3! = 6 Dalje 2 n: 4 4! = 24 Dalje 3 n: 5 5! = 120 3! = 1*2*3=6 4! = 1*2*3*4=24 5! = 1*2*3*4*5=120

Shembull 4. Të llogaritet vlera e funksionit z=(m+1)! – 2x + 1 #include <iostream> using namespace std; int main() { int x,m,z,F=1; cout << "x m: "; cin >> x >> m; for(int i=2; i<=m+1; i++) F=F*i; z=F-2*x+1; cout << "z=" << z << endl; return 0; } Shembuj daljesh Dalje 1 x m: 5 4 z=111 Dalje 2 x m: 3 7 z=40315

Shembull 5. Të shtypet tabela e sipërfaqeve dhe e perimetrave të rrethit me reze r, e cila ndryshon mes vlerës 1 dhe 5 me hap 0.5 #include <iostream> #include <iomanip> using namespace std; int main() { const int a=1, b=5; const double pi=3.14159; double r; cout << " r S P \n"; cout << fixed << setprecision(2); for(r=a; r<=b; r=r+0.5) cout << setw(6) << r << setw(8) << pi*r*r << setw(8) << 2*pi*r << endl; return 0; }

Komanda FOR e përbërë Shembull 1. Të shtypet tabela e faktorielëve të numrave natyror mes 1 dhe n. #include <iostream> #include <iomanip> using namespace std; int main() { int n,F=1; cout << "n: "; cin >> n; cout << " i F \n"; for(int i=2; i<=n; i++) { F*=i; cout << setw(5) << i << setw(10) << F << endl; } return 0;

Shembull 2. Të llogaritet për numra natyrorë x dhe n. #include <iostream> #include <iomanip> using namespace std; int main() { int x,n; cout << "x n: "; cin >> x >> n; cout << " n y \n"; for(int i=1, y=1; i<=n; i++) { y=y*x; cout << setw(5) << i << setw(10) << y << endl; } return 0;

Shembull 2. Shuma e katrorëve të numrave natyrorë mes a dhe b, duke mos i përfshirë katrorët e numrave 5,6,9. (fq.177) #include <iostream> #include <iomanip> using namespace std; int main() { int a,b,Shuma=0; bool z; cout << "a b: "; cin >> a >> b; cout << " n Katrori \n"; for(int i=a; i<=b; i++) { z = (i==5) ||(i==6) || (i==9); if(z==false) { Shuma+=i*i; cout << setw(5) << i << setw(12) << i*i << endl; } cout << "Shuma=" << Shuma << endl; return 0; Pa variablën logjike z: if(i!=5 && i!=6) && i!=9)…

Sa është shuma për a=9 dhe b=3?

Shembull 3. Të gjinden numrat natyrorë mes m dhe n që janë të plotpjestueshëm me numrin r. #include <iostream> #include <iomanip> using namespace std; int main() { int m,n,r,x,y; cout << "m n r: "; cin >> m >> n >> r; cout << "Te plotpjestueshem me " << r << endl << " Numri Numri/" << r << endl; for(int i=m; i<=n; i++) { x=i/r; y=x*r; if(y==i) cout << setw(5) << i << setw(10) << x << endl; } return 0;

Komanda FOR të ndërthurura Shembull 1. Të shtypen numrat: #include <iostream> #include <iomanip> using namespace std; int main() { for(int i=0; i<=9; i++) for(int j=0; j<=i; j++) cout << setw(2) << j; cout << endl; } return 0;

Shembull 2. Të shtypet tabela e vlerave të funksionit: për vlera të variablës k mes 0 dhe n. x,m,n – vlera hyrëse #include <iostream> #include <iomanip> using namespace std; int main() { int k,m,n; double x,y,Shuma; cout << "x m n: "; cin >> x >> m >> n; cout << " k y \n"; for(k=0; k<=n; k++) { Shuma=0; for(int i=2; i<=m+1; i++) Shuma+=(2*k+i-1); y=x/3-4*Shuma; cout << setw(5) << k << setprecision(4) << fixed << setw(15) << y << endl; } return 0;

Shembull 3. Të shtypen numrat e Piagorës x,y dhe z për të cilët vlen barazimi: #include <iostream> #include <iomanip> using namespace std; int main() { int x,y,z,n; cout << "n: "; cin >> n; cout << " x y z \n"; for(x=1; x<=n-2; x++) for(y=x+1; y<=n-1; y++) for(z=y+1; z<=n; z++) if(x*x+y*y==z*z) cout << setw(5) << x << setw(5) << y << setw(5) << z << endl; return 0; }

Komanda FOR me variabël numruese të tipit Char Shembull 1. Të shtypen shkronjat e mëdhe dhe kodet e tyre: #include <iostream> using namespace std; int main() { char k; int x,n=0; for(k='A'; k<='Z'; k++) { x=k; cout << k << " " << x << " "; n++; if(n%8==0) cout << endl; } cout << endl; return 0;

Komanda FOR me variabël numruese të tipit Char Shembull 1. Të shtypen shkronjat: #include <iostream> #include <iomanip> using namespace std; int main() { char x,y; for(x='Z'; x>='A'; x--) { for(y='A'; y<=x; y++) cout << setw(2) << y; cout << endl; } return 0;