Results 1 to 4 of 4

Math Help - Loop Statements

  1. #1
    Newbie
    Joined
    Apr 2009
    Posts
    2

    Red face Loop Statements

    hello..i've been searching for some websites that could help me in this thing. i am very new to things like pseudocode, although it's been like 4 months already..i couldnt understand a single thing. i hope someone could help me. Especially to those who are computer genius. Please..could someone help me.. i am okay when the teacher explains it. but when it comes to excercise..i am totally blank. so here's the question:

    Construct a flowchart or pseudocode that reads an integer x and a number y. THe integer x should be between 1 to 10 and the numbe y should have x digits. The number y is displayed in two parts diagonally beginning with the leftmost unit digit. The process continues untill the rightmost digit is reached.

    The example of the user interface screen for x=4 is as shown below:
    Please jey in an integer x (1-10) : 4
    Please key in any 4-digit number y: 1956
    1 956
    19 56
    195 6


    I really hope there's someone could tutor me..
    Last edited by mr fantastic; April 9th 2009 at 03:54 AM. Reason: Deleted excess smilies
    Follow Math Help Forum on Facebook and Google+

  2. #2
    MHF Contributor
    Grandad's Avatar
    Joined
    Dec 2008
    From
    South Coast of England
    Posts
    2,570
    Thanks
    1

    Pseudocode

    Hello blurryqueen
    Quote Originally Posted by blurryqueen View Post
    hello..i've been searching for some websites that could help me in this thing. i am very new to things like pseudocode, although it's been like 4 months already..i couldnt understand a single thing. i hope someone could help me. Especially to those who are computer genius. Please..could someone help me.. i am okay when the teacher explains it. but when it comes to excercise..i am totally blank. so here's the question:

    Construct a flowchart or pseudocode that reads an integer x and a number y. THe integer x should be between 1 to 10 and the numbe y should have x digits. The number y is displayed in two parts diagonally beginning with the leftmost unit digit. The process continues untill the rightmost digit is reached.

    The example of the user interface screen for x=4 is as shown below:
    Please jey in an integer x (1-10) : 4
    Please key in any 4-digit number y: 1956
    1 956
    19 56
    195 6

    I really hope there's someone could tutor me..
    Welcome to Math Help Forum!

    I don't know if you are expected to validate the data input (in other words to check that x lies between 1 and 10, and that y has x digits), but the pseudocode I've written here assumes that you are. If you haven't covered this sort of data validation, then you can simplify the program by omitting the loops after the two input statements.

    Code:
    begin
      input x
      loop until (x > 1) and (x < 10)
        input x
      end loop
      input y
      loop until y has x characters
        input y
      end loop
      i = 1
      loop until i = x
        output leftmost i characters of y
        output " "
        output rightmost (x - i) characters of y
        output newline character
        i = i + 1
      end loop
    end

    You'll see that the first input statement comes before the validation loop that checks that x is in the right range (and I'm assuming that x lies strictly between 1 and 10; x = 1 would not produce any output). If the first input value of x is OK, then the loop won't be executed at all. But if x is not valid, then you'll stay in that loop until it is.

    The second validation loop, following the input y statement, works in the same way.

    i is a counter for the main loop. It is set equal to 1 before the loop starts; the loop then outputs the required string of characters, and a newline, before incrementing the value of i. Exit from the loop will occur when i = x. (In the example you gave, outputs occur for i = 1, 2 and 3, but not for i = 4.)

    I haven't included detailed instructions about how the program would validate the length of y (checking that it has x characters), or how it would output the leftmost or rightmost n characters. Nearly all programming languages will have functions that will allow you to do this in a single line of code.

    I hope that you understand how this works. Let us know if you need any further explanation.

    Grandad
    Last edited by mr fantastic; April 9th 2009 at 03:54 AM.
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Newbie
    Joined
    Apr 2009
    Posts
    2
    OMG..@_@ although it doesnt make any sense to me, thanx.lols. caould u make a c program for it? i need to try it out in my turbo c..
    Last edited by mr fantastic; April 9th 2009 at 03:55 AM. Reason: Deleted excess smilies
    Follow Math Help Forum on Facebook and Google+

  4. #4
    MHF Contributor
    Grandad's Avatar
    Joined
    Dec 2008
    From
    South Coast of England
    Posts
    2,570
    Thanks
    1
    Hello blurryqueent
    Quote Originally Posted by blurryqueen View Post
    OMG..@_@ although it doesnt make any sense to me, thanx.lols. caould u make a c program for it? i need to try it out in my turbo c..
    Sorry - it's too long since I wrote anything in C, but I've attached an Excel spreadsheet with some VB code that works, complete with error traps and validation.

    Grandad
    Attached Files Attached Files
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. For Loop for an m-by-1 matrix
    Posted in the Math Software Forum
    Replies: 20
    Last Post: July 21st 2010, 04:59 PM
  2. Using a for loop and while loop.
    Posted in the Math Software Forum
    Replies: 1
    Last Post: April 19th 2010, 01:18 PM
  3. Matlab Split vectors,if statements, and "for loop"
    Posted in the Math Software Forum
    Replies: 5
    Last Post: February 27th 2010, 02:30 AM
  4. Loop invariants
    Posted in the Discrete Math Forum
    Replies: 1
    Last Post: July 13th 2009, 04:58 PM
  5. Matlab loop help
    Posted in the Math Software Forum
    Replies: 2
    Last Post: July 20th 2008, 10:47 AM

Search Tags


/mathhelpforum @mathhelpforum