{"id":5360,"date":"2020-09-06T22:20:01","date_gmt":"2020-09-06T20:20:01","guid":{"rendered":"https:\/\/blue.pri.ee\/ttu\/?p=5360"},"modified":"2026-06-15T12:53:31","modified_gmt":"2026-06-15T10:53:31","slug":"3-lab-loops","status":"publish","type":"post","link":"https:\/\/blue.pri.ee\/ttu\/labs\/3-lab-loops\/","title":{"rendered":"PR1EN3: Loops"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/3-lab-loops\/#Lab_materials\" >Lab materials<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/3-lab-loops\/#Tasks\" >Tasks<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/3-lab-loops\/#Task_1_W03-1_Sum_of_5_numbers\" >Task 1 [W03-1]: Sum of 5 numbers<\/a><ul class='ez-toc-list-level-5' ><li class='ez-toc-heading-level-5'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/3-lab-loops\/#Requirements\" >Requirements<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-5'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/3-lab-loops\/#Hints\" >Hints<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-5'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/3-lab-loops\/#Testing\" >Testing<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/3-lab-loops\/#Task_2_W03-2_Cash_register_with_input_validation\" >Task 2 [W03-2]: Cash register with input validation<\/a><ul class='ez-toc-list-level-5' ><li class='ez-toc-heading-level-5'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/3-lab-loops\/#Requirements-2\" >Requirements<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-5'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/3-lab-loops\/#Testing-2\" >Testing<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/3-lab-loops\/#Extra_tasks\" >Extra tasks<\/a><ul class='ez-toc-list-level-5' ><li class='ez-toc-heading-level-5'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/3-lab-loops\/#Extra_task_1_W03-3_Mario_tower\" >Extra task 1 [W03-3]: Mario tower<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-5'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/3-lab-loops\/#Extra_task_2_W03-4_Mirrored_tower_with_specified_height\" >Extra task 2 [W03-4]: Mirrored tower with specified height<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-5'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/3-lab-loops\/#Hints-2\" >Hints<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/3-lab-loops\/#After_the_class_you_should\" >After the class, you should<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/3-lab-loops\/#Additional_content\" >Additional content<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h3><span class=\"ez-toc-section\" id=\"Lab_materials\"><\/span><strong>Lab materials<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<ul>\n<li>Slides: <a href=\"https:\/\/blue.pri.ee\/ttu\/files\/iax0583\/slaidid-en\/03_preprocessor_compilation.pdf\"><strong>Preprocessor and compilation<\/strong><\/a><\/li>\n<li>Slides:\u00a0<a href=\"https:\/\/blue.pri.ee\/ttu\/files\/iax0583\/slaidid-en\/03_Loops.pdf\"><strong>Loops<\/strong><\/a><\/li>\n<li>Coding guide: <strong><a href=\"https:\/\/blue.pri.ee\/ttu\/coding-guides\/loops\/\">loops<\/a><\/strong><\/li>\n<li>Code example: <strong><a href=\"https:\/\/blue.pri.ee\/ttu\/programming-i\/samples\/loops-multiplication-table\/\">multiplication table<\/a><\/strong><\/li>\n<li>Program used for compilation demo: <strong><a href=\"https:\/\/blue.pri.ee\/ttu\/files\/iax0583\/programmikood\/age_check.c\">age check<\/a><\/strong><\/li>\n<\/ul>\n<h3><span class=\"ez-toc-section\" id=\"Tasks\"><\/span>Tasks<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>This lab has two base tasks for which you need to create a total of four separate programs. There are also two extra tasks, separate from the base tasks, that can be written as a single program.<\/p>\n<h4><span class=\"ez-toc-section\" id=\"Task_1_W03-1_Sum_of_5_numbers\"><\/span>Task 1 [W03-1]: Sum of 5 numbers<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>You are tasked with creating a program that will find the sum of five numbers entered by the user. <strong>The program must be created 3 times &#8211; one for each loop type<\/strong>. All <span style=\"color: #ff0000;\"><strong>3 programs<\/strong><\/span> must do the exact same thing.<\/p>\n<h5><span class=\"ez-toc-section\" id=\"Requirements\"><\/span>Requirements<span class=\"ez-toc-section-end\"><\/span><\/h5>\n<ul>\n<li>Program asks the user for 5 integers<\/li>\n<li>After every number, except the last one, the program prints the current subtotal<\/li>\n<li>After the last number is entered, the program prints the final sum instead of the subtotal<\/li>\n<li>Program must give a clear instructions to the user<\/li>\n<li>You cannot have any magical numbers in your code<\/li>\n<li>Design your program in a way that if you would need to do the same task for 10, 20 or even 1000 numbers, <strong>you\u00a0will only need to change one number in your code.\u00a0Test it before submitting &#8211; change that number to be 7 and see if your program still works and the output makes sense!<\/strong><\/li>\n<li>Create the same program using a while, do while and a for loop<\/li>\n<\/ul>\n<h5><span class=\"ez-toc-section\" id=\"Hints\"><\/span>Hints<span class=\"ez-toc-section-end\"><\/span><\/h5>\n<ul>\n<li>Think carefully which variables you will need! Regardless of how many numbers you need to add up, the amount of variables should remain the same<\/li>\n<li>It&#8217;s recommended to start counting loops from 0. This is because most things that are counted in the computer, start from 0. However you should display a number incremented by 1 to the user &#8211; non-developers usually count from 1.\n<ul>\n<li>(i + 1) takes the value if i and uses the incremented value in that place only. The incremented value is not stored and does not affect the program elsewhere.\u00a0 E.g. <span class=\"lang:c decode:true crayon-inline\">printf(&#8220;%d&#8221;, i + 1);<\/span><\/li>\n<li>Operations such as\u00a0 i++, ++i, i += 1 and i = i + 1 store the updated value after incrementing (permanent).<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h5><span class=\"ez-toc-section\" id=\"Testing\"><\/span>Testing<span class=\"ez-toc-section-end\"><\/span><\/h5>\n<p>This is the expected output of the program<\/p>\n<pre class=\"theme:cisco-router toolbar:1 nums:false lang:default highlight:0 decode:true\" title=\"Sample: 5-number sum\">This program takes 5 numbers from the user and adds them up. Subtotal is shown after each addition until the last one.\r\nEnter number 1 \/ 5\r\n9\r\nSubtotal 9\r\nEnter number 2 \/ 5\r\n3\r\nSubtotal 12\r\nEnter number 3 \/ 5\r\n2\r\nSubtotal 14\r\nEnter number 4 \/ 5\r\n1\r\nSubtotal 15\r\nEnter number 5 \/ 5\r\n5\r\nThe final sum is 20<\/pre>\n<h4><span class=\"ez-toc-section\" id=\"Task_2_W03-2_Cash_register_with_input_validation\"><\/span>Task 2 [W03-2]: Cash register with input validation<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>In this task, we will revisit the task from last week and add a few features to it. You can either use the provided starter code or your own solution from last week.<\/p>\n<p>Download the lab task 2 starter code [optional]: <strong><a href=\"https:\/\/blue.pri.ee\/ttu\/files\/iax0583\/aluskoodid\/3_1_cashier_starter_loop.c\">https:\/\/blue.pri.ee\/ttu\/files\/iax0583\/aluskoodid\/3_1_cashier_starter_loop.c<\/a><\/strong><\/p>\n<h5><span class=\"ez-toc-section\" id=\"Requirements-2\"><\/span>Requirements<span class=\"ez-toc-section-end\"><\/span><\/h5>\n<p>You must add the following features to the program<\/p>\n<p>1. Input validation for loyalty card and extra discount<\/p>\n<ul>\n<li>Ask the user for input\u00a0<strong>until<\/strong> either 0 or 1 is entered<\/li>\n<li>For invalid input, print an error message<\/li>\n<li>Recommendation: Use an exit-controlled loop.<\/li>\n<\/ul>\n<p>2. PIN code can be entered up to 3 times<\/p>\n<ul>\n<li>If the PIN code has been entered incorrectly for 3 times, write an error message and close the program.<\/li>\n<li>If a correct PIN is entered within 3 attempts, check the balance and if possible, do the transaction.<\/li>\n<li>Recommendation: Use an entry-controlled counting loop.<\/li>\n<\/ul>\n<h5><span class=\"ez-toc-section\" id=\"Testing-2\"><\/span>Testing<span class=\"ez-toc-section-end\"><\/span><\/h5>\n<pre class=\"theme:cisco-router toolbar:1 nums:false lang:default highlight:0 decode:true\" title=\"Sample: Cash register using loops\">Enter purchase total: 59.99\r\nDid client present loyalty card?\r\n1 - yes\r\n0 - no\r\n3\r\n\r\nError! Enter 1 for yes or 0 for no!\r\n\r\nDid client present loyalty card?\r\n1 - yes\r\n0 - no\r\n1\r\n\r\nApply extra discount?\r\n1 - yes\r\n0 - no\r\n0\r\n\r\nInvoice total: 53.99\r\n\r\nPlease enter card PIN code: 3333\r\nError! Incorrect PIN code!\r\nYou have 2 attempts left!\r\n\r\nPlease enter card PIN code: 1234\r\nPIN code confirmed\r\n\r\nPurchase complete!\r\nNew account balance is 46.01\r\nThank you, come again!<\/pre>\n<h4><span class=\"ez-toc-section\" id=\"Extra_tasks\"><\/span>Extra tasks<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6818\" src=\"https:\/\/blue.pri.ee\/ttu\/wp-content\/uploads\/2020\/09\/mario.png\" sizes=\"auto, (max-width: 708px) 100vw, 708px\" srcset=\"https:\/\/blue.pri.ee\/ttu\/wp-content\/uploads\/2020\/09\/mario.png 708w, https:\/\/blue.pri.ee\/ttu\/wp-content\/uploads\/2020\/09\/mario-300x95.png 300w\" alt=\"\" width=\"708\" height=\"224\" \/><\/p>\n<h5><span class=\"ez-toc-section\" id=\"Extra_task_1_W03-3_Mario_tower\"><\/span>Extra task 1 [W03-3]: Mario tower<span class=\"ez-toc-section-end\"><\/span><\/h5>\n<ul>\n<li>Using nested loops, create a representation of the Mario stairs using the pound symbol. A total of 13 rows of steps.<\/li>\n<li>Place an ASCII stick man on top of the stairs<\/li>\n<li>The width of the top platform must be enough for the stick man to rest his feet comfortably<\/li>\n<li>In the end of each row, show the number of blocks used for that row and the total number of used blocks until that moment<\/li>\n<li>Alignment should be as shown in the example below<\/li>\n<\/ul>\n<pre class=\"theme:cisco-router toolbar:1 nums:false lang:default highlight:0 decode:true\" title=\"Sample: Mario\"> O\r\n-|-\r\n |\r\n\/ \\               row  total\r\n###                3      3\r\n####               4      7\r\n#####              5     12\r\n######             6     18\r\n#######            7     25\r\n########           8     33\r\n#########          9     42\r\n##########        10     52\r\n###########       11     63\r\n############      12     75\r\n#############     13     88\r\n##############    14    102\r\n###############   15    117\r\n<\/pre>\n<h5><span class=\"ez-toc-section\" id=\"Extra_task_2_W03-4_Mirrored_tower_with_specified_height\"><\/span>Extra task 2 [W03-4]: Mirrored tower with specified height<span class=\"ez-toc-section-end\"><\/span><\/h5>\n<ul>\n<li>Ask the user for the width of the platform (range 7 -20 blocks)<\/li>\n<li>The steps must be mirrored this time as is in the Mario game when approaching the castle.<\/li>\n<li>The bottom-most step must start from the left edge of the terminal window, no spaces allowed before it.<\/li>\n<\/ul>\n<pre class=\"theme:cisco-router toolbar:1 nums:false lang:default highlight:0 decode:true\" title=\"Sample: Mario 2\">Enter base width [7 - 20]: 25\r\nError! Allowed base width is 7 - 20! \r\nEnter base width [7 - 20]: 13\r\n           O\r\n          -|-\r\n           |\r\n          \/ \\    row  total\r\n          ###     3      3\r\n         ####     4      7\r\n        #####     5     12\r\n       ######     6     18\r\n      #######     7     25\r\n     ########     8     33\r\n    #########     9     42\r\n   ##########    10     52\r\n  ###########    11     63\r\n ############    12     75\r\n#############    13     88\r\n\r\n<\/pre>\n<h5><span class=\"ez-toc-section\" id=\"Hints-2\"><\/span>Hints<span class=\"ez-toc-section-end\"><\/span><\/h5>\n<ul>\n<li>Task is based on the idea of nesting loops inside of each other<\/li>\n<li>Think of which symbol is invisible<\/li>\n<li>Text and numbers can be padded <strong><a href=\"https:\/\/blue.pri.ee\/ttu\/coding-guides\/some-printf-tips\/\">https:\/\/blue.pri.ee\/ttu\/coding-guides\/some-printf-tips\/<\/a><\/strong><\/li>\n<\/ul>\n<h4><span class=\"ez-toc-section\" id=\"After_the_class_you_should\"><\/span><span id=\"After_the_class,_you_should_be_able_to\" class=\"ez-toc-section\"><\/span><strong>After the class, you should<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h4>\n<ul>\n<li>Know what steps need to be gone through to get from the source code that we write to a executable program<\/li>\n<li>Know what is a #define macro and how it works<\/li>\n<li>Know what is a magic number and how to handle them<\/li>\n<li>Know about problems with non-initialized variables.<\/li>\n<li>Use a switch statement.<\/li>\n<li>Use all 3 types of loops for a specified number of iterations.<\/li>\n<li>Know how to create a loop that runs infinitely<\/li>\n<li>Know the increment\/decrement operators.<\/li>\n<li>Know the difference between loops that check the conditions before or after executing the loop body.<\/li>\n<li>Be able to nest blocks of code (e.g. conditional statement inside of a loop, loop inside of a loop, &#8230;)<\/li>\n<li>Know about two control statements: break and continue.<\/li>\n<li>Know, that we DO NOT use goto statements.<\/li>\n<li>Use expressions and constants as printf arguments.<\/li>\n<li>Be able to model loops in UML<\/li>\n<\/ul>\n<h4><span class=\"ez-toc-section\" id=\"Additional_content\"><\/span><span id=\"Additional_content\" class=\"ez-toc-section\"><\/span><strong>Additional content<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h4>\n<ul>\n<li>Magic number (programming)<br \/>\n<a href=\"https:\/\/en.wikipedia.org\/wiki\/Magic_number_(programming)\">https:\/\/en.wikipedia.org\/wiki\/Magic_number_(programming)<\/a><\/li>\n<li>C preprocessor<br \/>\n<a href=\"https:\/\/en.wikipedia.org\/wiki\/C_preprocessor\">https:\/\/en.wikipedia.org\/wiki\/C_preprocessor<\/a><\/li>\n<li>GOTO, Goto &amp; Goto &#8211; Computerphile<br \/>\n<a href=\"https:\/\/www.youtube.com\/watch?v=FGAWniPGKjc\">https:\/\/www.youtube.com\/watch?v=FGAWniPGKjc<\/a><\/li>\n<li>Bjarne Stroustrup Quotes<br \/>\n<a href=\"https:\/\/www.stroustrup.com\/quotes.html\">https:\/\/www.stroustrup.com\/quotes.html<\/a><\/li>\n<li>Loops in C and C++<br \/>\n<a href=\"https:\/\/www.geeksforgeeks.org\/loops-in-c-and-cpp\/\">https:\/\/www.geeksforgeeks.org\/loops-in-c-and-cpp\/<\/a><\/li>\n<li>C break and continue<br \/>\n<a href=\"https:\/\/www.programiz.com\/c-programming\/c-break-continue-statement\">https:\/\/www.programiz.com\/c-programming\/c-break-continue-statement<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Lab materials Slides: Preprocessor and compilation Slides:\u00a0Loops Coding guide: loops Code example: multiplication table Program used for compilation demo: age check Tasks This lab has two base tasks for which you need to create a total of four separate programs. There are also two extra tasks, separate from the base tasks, that can be written &hellip; <a href=\"https:\/\/blue.pri.ee\/ttu\/labs\/3-lab-loops\/\" class=\"more-link\">Loe edasi <span class=\"screen-reader-text\">PR1EN3: Loops<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[75,77],"tags":[],"class_list":["post-5360","post","type-post","status-publish","format-standard","hentry","category-labs","category-pr1_en"],"_links":{"self":[{"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/posts\/5360","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/comments?post=5360"}],"version-history":[{"count":15,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/posts\/5360\/revisions"}],"predecessor-version":[{"id":11477,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/posts\/5360\/revisions\/11477"}],"wp:attachment":[{"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/media?parent=5360"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/categories?post=5360"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/tags?post=5360"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}