{"id":974,"date":"2010-05-11T19:58:41","date_gmt":"2010-05-11T19:58:41","guid":{"rendered":"http:\/\/www.codingfriends.com\/?p=974"},"modified":"2011-01-14T13:57:38","modified_gmt":"2011-01-14T13:57:38","slug":"problem-5","status":"publish","type":"post","link":"https:\/\/www.codingfriends.com\/index.php\/2010\/05\/11\/problem-5\/","title":{"rendered":"Problem 5"},"content":{"rendered":"<p><span id=\"zipfile\"><a href='http:\/\/www.codingfriends.com\/wp-content\/uploads\/2010\/05\/Assignment2_Code.zip'><\/a><\/span>The problem is &#8220;Determining the Range, Write a ConsoleProgram that reads in a list of integers, one per line, until a sentinel value of 0 (which you should be able to change easily to some other value). When the sentinel is read, your program should display the smallest and largest values in the list.<\/p>\n<p>Your program should handle the following special cases:<\/p>\n<ul>\n<li>If the user enters only one value before the sentinel, the program should report that value as both the largest and smallest.<\/li>\n<li>If the user enters the sentinel on the very first input line, then no values have been entered, and your program should display a message to that effect.<\/li>\n<\/ul>\n<p>&#8221;<\/p>\n<p>So once again broken down the problem into parts, first part need to read in the values from the keyboard and the next part is to set the lowest and the highest values (whilst making sure that no value takes the sentinel value).<\/p>\n<p>Here is a example of the output from the running program<\/p>\n<pre lang=\"bash\">\r\nThis program finds the smallest and largest integers in a list.  Enter values, one per line, using a 0 to signal the end of the list\r\n?32\r\n?2\r\n?4\r\n?5\r\n?45\r\n?1\r\n?0\r\nSmallest value was 1\r\nLargest value was 45\r\n<\/pre>\n<p>source code in full, I have included within zip file and also the PDF file of the full assignment problems.<\/p>\n<pre lang=\"java\">\r\n\/*\r\n * File: FindRange.java\r\n * --------------------\r\n * This program is a stub for the FindRange problem, which finds the\r\n * smallest and largest values in a list of integers.\r\n *\/\r\n\r\nimport acm.program.*;\r\nimport java.io.Console;\r\n\r\npublic class FindRange extends ConsoleProgram {\r\n\r\n\tpublic void run() {\r\n\t\tint readInValue=sential;\r\n\t\tprintln(\"This program finds the smallest and largest integers in a list.  Enter values, one per line, using a \"+sential+\" to signal the end of the list\");\r\n\t\tdo \r\n\t\t{\r\n\t\t\treadInValue = readInt(\"?\");\r\n\t\t\tif (readInValue != sential)\r\n\t\t\t{\r\n\t\t\t\tif (readInValue > largestValue)\r\n\t\t\t\t\tlargestValue = readInValue;\r\n\t\t\t\tif (readInValue < smallestValue || smallestValue == sential)\r\n\t\t\t\t\tsmallestValue = readInValue;\r\n\t\t\t}\r\n\t\t} while (readInValue != sential);\r\n\t\tif (largestValue == smallestValue &#038;&#038; largestValue == sential)\r\n\t\t{\r\n\t\t\tprintln(\"No values inputted\");\r\n\t\t}\r\n\t\telse\r\n\t\t{\r\n\t\t\tprintln(\"Smallest value was \"+ smallestValue);\r\n\t\t\tprintln(\"Largest value was \" + largestValue);\r\n\t\t}\r\n\t}\r\n\r\n\tprivate int largestValue = 0, smallestValue = 0;\r\n\tprivate static final int sential = 0;\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The problem is &#8220;Determining the Range, Write a ConsoleProgram that reads in a list of integers, one per line, until a sentinel value of 0 (which you should be able to change easily to some other value). When the sentinel is read, your program should display the smallest and largest values in the list. Your &hellip; <a href=\"https:\/\/www.codingfriends.com\/index.php\/2010\/05\/11\/problem-5\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Problem 5<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[],"class_list":["post-974","post","type-post","status-publish","format-standard","hentry","category-java"],"_links":{"self":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/974","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/comments?post=974"}],"version-history":[{"count":5,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/974\/revisions"}],"predecessor-version":[{"id":1334,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/974\/revisions\/1334"}],"wp:attachment":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/media?parent=974"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/categories?post=974"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/tags?post=974"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}