CME111 Programming Languages I Week 5 Examples and Analysis of Algorithms Assist. Prof. Dr. Caner ÖZCAN
BONUS HOMEWORK For the following questions (which solved in lab. practice), draw flow diagrams by using the tools such as "Microsoft Visio", "Clickcharts Diagram Flowchart 1.24", and who prepare these algorithms on the computer, will be given bonus points for a visa for the first 5 students. The project will be delivered face to face (do not send via mail!!) and final delivery October 31, Monday at 13: 00. Made solutions must be delivered in the drawing file (not visual output!!). (Note: English of questions have been announced in the course.) 1. İki sayının toplamını bulan algoritma. 2. Doğum tarihi verilen kişinin yaşını bulan algoritma. 3. Verilen yılın artık yıl olup olmadığını bulan algoritma. 4. Verilen bir S sayısının, T tabanına göre logaritmasını bulan algoritma. 5. Kenarları A, B, C, D olarak verilen bir dörtgenin kare olup olmadığını bulan algoritma. 6. A ve B sayıları 50 den büyük ise C=A+B işlemini yapan algoritma. 7. Bir uçak 15 dakika boyunca düzgün hızlanarak hızı 480 km/dk oluyor. Sonra 20 dk. sabit hızla gidiyor ve 15 dk. boyunca düzgün yavaşlayarak hızı sıfır oluyor. Herhangi bir t anında hızı veren algoritma. 8. 1 den 100 e kadar olan rakamları ekrana yazan algoritma. 9. 500 den 100 e kadar olan sayıları ekrana yazan algoritma. 10. 100 den 200 e kadar olan çift sayıları ekrana yazan algoritma. 11. Girilen 10 adet sayı içerisinden; pozitiflerin ve negatiflerin sayısını ayrı ayrı bulan algoritma. 12. Girilecek 10 adet sayıdan en büyük ve en küçük olanını bulan algoritma. 13. Bir işletmede çalışan elemanlara normal ücretlerine ek olarak ürettikleri parça sayısına göre ek ücret verilmektedir. Parça başına ücret aşağıdaki şartlara göre verilmektedir: a) Parça sayısı (0,10) arası için parça başına 5000, b) Parça sayısı [10,30) arası için parça başına 8000, c) Parça sayısı 30 ve yukarısı için parça başına 12000 Buna göre normal maaşı ve ürettiği parça sayısı bilgilerini alarak elamanın alacağı toplam maaşı hesaplayan algoritma. 14. Verilen iki sayıyı bölme operatörü kullanmadan birbirine bölerek, bölüm ve kalan değerlerini hesaplayan algoritma (/ ve % operatörleri kullanılmayacak).
Example: Algorithm of Brewing Tea Start Add water to the teapot Put teapot to the cooker and fire it Does the water boil? Yes No Wait Brew tea and add water to the teapot Put teapot to the cooker Does tea brew and water boil? Yes No Wait Service End
Example: Passing Across Algorithm Start Look at the left side of the road Is car coming? Yes Wait No Walk to refuge Look at the right side of the road Is car coming? Yes Wait No Walk across End
Example: Traffic Algorithm Start Look at the traffic lights and read lights Is it the green light? Yes Slow No Is it the yellow light? Yes No Stop Look at the traffic lights and read lights Is it the green light? Go on Yes No Wait End
Example: Multiplication of Two Number Algorithm Flow Chart Step 1-Start Step 2-Read A Step 3-Read B Step 4-Calculate C=A*B Step 5-Write C Step 6-Stop
Example: Square and Cube of Sum of Two Number Algorithm Flow Chart Step 1-Start Step 2-Read BIRSA and IKISA Step 3-TO=BIRSA+IKISA TOKA=TO^2 TO3=TO^3 Step 4-Write TOKA, TO3 TOKA, TO3 Step 5-Stop
Example: Determine given number is odd or even Algorithm Flow Chart Step 1: Start Step 2: Enter one number(x) Step 3: If x mod 2 = 0 then write even else write odd. Step 4: End
Example: Print out greater number which are entered. Algorithm Flow Chart Step 1: Start Step 2: Enter first number.(x) Step 3: Enter second number.(y) Step 4: If x>y then write 1. number is greater. Step 5: If y>x then write 2. number is greater.. Step 6: Else write numbers are equal. Step 7: End
Example: Find and print the average grades of 3 students they received an exam Algorithm Step 1- Start Step 2- INOT=0 Step 3- ISAYI=0 Step 4- Read ONOT Step 5- INOT=INOT+ONOT Step 6- ISAYI=ISAIY+1 Step 7- If ISAYI<3 then go Step 4 Step 8- NORT=INOT/3 Step 9- Write NORT Step 10- Stop Flow Chart
Example: According to entered midterm and final grade, finding that students passed through the course. Algorithm 1. Start 2. Write ( Enter visa grade ) 3. Read vize 4. Write ( Enter final grade ) 5. Read final 6. ortalama=vize * 0.40 + final * 0.60 7. IF ortalama >= 60 THEN 8. Write Student Passed 9. ELSE 10. Write Student Failed 11. End Flow Chart
Example: Print only odd numbers from 1 to 25 Algorithm Flow Chart 1. Start 2. Sayac=1 3. Toplam=0 4. IF Sayac Mod2!= 0 THEN 5. WRITE Sayac 6. Sayac=Sayac+1 7. IF Sayac<26 THEN GO Step 4 8. End
Example: Find the average of n numbers entered from the keyboard. Algorithm 1. Start 2. T = 0 3. Read N 4. I = 1 5. If I <= N Then Go Step 6 Else Go Step 10 6. Read Sayi 7. T = T + Sayi 8. I = I + 1 9. Go Step 5 10. Ort = T / N 11. Write Ort 12. Stop Flow Chart
Analysis Example
Analysis Example
Analysis Example
Analysis Example
References Doç. Dr. Fahri Vatansever, Algoritma Geliştirme ve Programlamaya Giriş, Seçkin Yayıncılık, 12. Baskı, 2015. J. G. Brookshear, Computer Science: An Overview 10th Ed., Addison Wisley, 2009. Kaan Aslan, A dan Z ye C Klavuzu 8. Basım, Pusula Yayıncılık, 2002. Paul J. Deitel, C How to Program, Harvey Deitel. Bayram AKGÜL, C Programlama Ders notları 18