How to write a program that displays the smallest of five input?
Im helping my daughter with her school homework can anybody help me on How to write a program that displays the smallest of five input values that may include duplicate values (e.g., 6, 4, 8, 6, 7). Does anybody know how to get the input, processing and output?
Public Comments
- It depends what programming language you want to use to write the program in - but as a very rough guide, here is the program in "pseudocode" 1. Enter the values into an array - [6, 4, 8, 6, 7] 2. Allocate a temporary variable, we'll call it $temp, and set it to a value larger than any in the array, say 10 3. Step through the values in the array, one at a time 4. For each value, if it is less than the value in $temp, set $temp to equal that value 5. At the end of the loop, $temp will contain the lowest value Here's the code in PHP (it's similar to C, and structurally similar to a lot of other languages) $values = array(6,4,8,6,7); $temp = 10; foreach($values as $v){ //foreach is a PHP function which steps through all the values in an array if($v < $temp) $temp = $v; } echo $temp; //this will display the lowest number A note: This may not be the fastest way of doing it, just the simplest. Faster methods would involve sorting the array by order first, and then the smallest value would be the first one (hence you wouldn't need to check through every value (n) in the array) Robin http://www.solarisedesign.co.uk
- I know how to do that on Javascript and Visual Basic Which one you prefer? For example, this is an html page with Javascript that does what you asked. Simply copy and paste this all into notepad and save as .html page then open in your browser. Here's the script: <html> <head> <script type="text/javascript"> function disp_prompt() { var number1=prompt("Please enter a decimal number1","0"); if (number1!=null && number1!="" && number1/number1!=1) { alert("please enter a number!"); } var number2=prompt("Please enter a decimal number2","0"); if (number2!=null && number2!="" && number2/number2!=1) { alert("please enter a number!"); } var number3=prompt("Please enter a decimal number3","0"); if (number3!=null && number3!="" && number3/number3!=1) { alert("please enter a number!"); } var number4=prompt("Please enter a decimal number4","0"); if (number4!=null && number4!="" && number4/number4!=1) { alert("please enter a number!"); } var number5=prompt("Please enter a decimal number5","0"); if (number5!=null && number5!="" && number5/number5!=1) { alert("please enter a number!"); } document.write(number1+", "+number2+", "+number3+", "+number4+", "+number5+".<br/>"); a=(Math.min(number1,number2)); b=(Math.min(a,number3)); c=Math.min(b,number4); d=Math.min(c,number5); document.write(d); } </script> </head> <body> <input type="button" onclick="disp_prompt()" value="Start entering numbers" /> </body> </html> I know it's not perfect.. but it works.
- Here is the complete code in C programming language (with process, input, and output): #include <stdio.h> int main(void) { int values[5] = { 6, 4, 8, 6, 7 }; int smallest; int i; /* input your values */ for (i = 0; i < 5; i++) { printf("Enter an integer value: "); scanf("%d", &value[i]); } /* now, find the smallest of all values */ smallest = values[0]; for (i = 1; i < 5; i++) /* note: we start from value[1] */ { if (values[i] < smallest) { smallest = values[i]; } } /* output */ printf("\n\nThe smallest value is %d\n", smallest); return 0; }
Powered by Yahoo! Answers