开发者

Random Characters in pseudocode

开发者 https://www.devze.com 2022-12-13 21:21 出处:网络
My assignment is to create in pseudocode a module that accepts a 200 element array of alpha characters.How do I write the code to choose the 200 characters at random and put them in an array?

My assignment is to create in pseudocode a module that accepts a 200 element array of alpha characters. How do I write the code to choose the 200 characters at random and put them in an array?

My full assignment is:

Create a module in pseudocode which accepts a 200 element array of characters, determines the number of occurrences of each of the five vowels in the array (a, e, i, o, u), and prints the number开发者_Go百科 of occurrences of each vowel to the screen. [25 pts]

I am sure there is an easier way of putting this but this is what I figured out:

Module vowels(characterArray)
    Declare Boolean found
    Declare Integer Index
    Declare Integer vowelA
    Declare Integer vowelE
    Declare Integer vowelI
    Declare Integer vowelO
    Declare Integer vowelU
    Set found = false
    Set index = 0
    Set vowelA = 0
    Set vowelE = 0
    Set vowelI = 0
    Set vowelO = 0
    Set vowelU = 0
    While found == false AND index <= size – 1
        If characterArray[index] == ucase$(“a”)
            Set vowelA = vowelA + 1
            If characterArray[index] == ucase$( “e”)
                Set vowelE = vowelE + 1
                If characterArray[index] == ucase$( “i”)
                    Set vowelI = vowelI + 1
                    If characterArray[index] == ucase$( “o”)
                        Set vowelO = vowelO + 1
                        If characterArray[index] == ucase$( “u”)
                            Set vowelU = vowelU + 1
                        Else
                            Set found = true
                        Endif
                    Endif
                Endif
            Endif
        Endif
    Endwhile
        Display “Number of A’s: “ ,vowelA
        Display “Number of E’s: “ ,vowelE
        Display “Number of I’s: “ ,vowelI
        Display “Number of O’s: “ ,vowelO
        Display “Number of U’s: “ ,vowelU
End Module


You're not too far off. As you have it coded right now, though, you'll only look for an 'e' if you've already found that the character is an 'a'. Ditto for 'i', 'o', and 'u'. Think about it step by step and you'll get it.


The easiest way is to realize that there are 26 alphanumeric characters. So, generate a number between 1 and 26 (or 0 and 25), and convert that number to a letter between A and Z. Repeat 200 times to get a string.


Try to think about how you would write this with functions. Also, you're not really using the found variable, assuming the variable size is equal to the size of the array. In most languages, it's conventional to have the loop end condition be, not index <= size – 1, but rather index < size.


Here are some more hints:

  1. You need to iterate over the entire array (no need to break early)
  2. Use a switch statement to simplify the if-else (incorrect) logic you have.
  3. Handle lower and upper case vowels ('a' and 'A' have different values)
0

精彩评论

暂无评论...
验证码 换一张
取 消