Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think it is important to create a pseudo code or algorithm of the problem <strong>first</strong> and then if it works deal with the programming <strong>later</strong>. Otherwise you'll be solving two things at the same time 1. Problem logic and 2. Implementation details.</p> <p>This is how I would do it.</p> <pre><code>//The three numbers should be entered by a user in the main method. MAIN PROGRAM starts declare a , b , c as numbers //The numbers should be positive and less than 200. // see validNumCheck below. //part 1.If not, the program asks the user to renter the number. //part 2.The user will have three chances to enter a valid number for each number. //part 3. If the number is still invalid after the three trials, the program displays an error message to the user and ends. // ok then read a number and validate it. attempts = 0; maxAttempts = 3; //part 2. three chances... . loop_while ( attemtps &lt; maxAttempts ) do // or 3 directly. number = readUserInput(); // part 1. reenter the number... if( numcheck( number ) == false ) then attempts = attempts + 1; // one failure.. try again. else break the loop. end end // part 3:. out of the loop. // either because the attempts where exhausted // or because the user input was correct. if( attempts == maxAttemtps ) then displayError("The input is invalid due to ... ") die(); else a = number end // Now I have to repeat this for the other two numbers, b and c. // see the notes below... MAIN PROGRAM ENDS </code></pre> <p>And this would be the function to "validNumCheck" </p> <pre><code> // You are encouraged to write a separate method for this part of program – for example: validNumCheck bool validNumCheck( num ) begin if( num &lt; 0 and num &gt; 200 ) then // invalid number return false; else return true; end end </code></pre> <p>So, we have got to a point where a number "a" could be validated but we need to do the same for "b" and "c"</p> <p>Instead of "copy/paste" your code, and complicate your life trying to tweak the code to fit the needs you can create a function and delegate that work to the new function. </p> <p>So the new pseudo code will be like this:</p> <pre><code>MAIN PROGRAM STARTS declare a , b , c as numbers a = giveMeValidUserInput(); b = giveMeValidUserInput(); c = giveMeValidUserInput(); print( a, b , c ) MAIN PROGRAM ENDS </code></pre> <p>And move the logic to the new function ( or method ) </p> <p>The function giveMeValidUserInput would be like this ( notice it is almost identical to the first pseudo code ) </p> <pre><code>function giveMeValidUserInput() starts maxAttempts = 3; attempts = 0; loop_while ( attemtps &lt; maxAttempts ) do // or 3 directly. number = readUserInput(); if( numcheck( number ) == false ) then attempts = attempts + 1; // one failure.. try again. else return number end end // out of the loop. // if we reach this line is because the attempts were exhausted. displayError("The input is invalid due to ... ") function ends </code></pre> <p>The validNumCheck doesn't change.</p> <p>Passing from that do code will be somehow straightforward. Because you have already understand what you want to do ( analysis ) , and how you want to do it ( design ).</p> <p>Of course, this would be easier with experience. </p> <p>Summary </p> <p>The <a href="https://stackoverflow.com/questions/137375/process-to-pass-from-problem-to-code-how-did-you-learn">steps to pass from problem to code are</a>:</p> <ol> <li><p>Read the problem and understand it ( of course ) .</p></li> <li><p>Identify possible "functions" and variables.</p></li> <li><p>Write how would I do it step by step ( algorithm )</p></li> <li><p>Translate it into code, if there is something you cannot do, create a function that does it for you and keep moving.</p></li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload