You are here:

C/Pseudo Code (using arrays)

Advertisement


Question
QUESTION: Greetings Zlatko, I appreciate that you are taking time out to look over my question. So, thank you.

I need to write a program, in Pseudo Code, that will generate a lottery number that has 7 digits. the program has to have an INTEGER array that has 7 elements. A loop needs to be written in which steps through the array, randomly generating a number in the range of 0 through 9 for each element. I also have to use the RANDOM function with this. I then have to writer another loop that displays the array contents.

I looked it over in the programming book that explains arrays but I am still not quite understanding what to do or where to start. I wanted to see if another way of explaining it and showing me how to program this would help. Thank you so much once again. I look forward to your expertise.

ANSWER: Hello Sasha

Pseudo code is high level instruction written in an natural language, like English. Pseudo code should be written in enough detail so that it can be easily translated into a real programming language without too much effort. Usually 1 line of pseudo code translates into 1 or more statements of a programming language. There is no official pseudo code language, so everyone makes it up to suit their particular purpose.

If you understand the idea of a loop, and an array, you should have no problem.

An array is a list of elements in memory that you access with an array name, and an index. Usually the index is in square or round brackets. I'll use square brackets to simulate C. For example, if you have an array "A" of integers, then the first item in the array is A[0]. In C, arrays start at index 0. In other languages it is different. In pseudo code, you can make up the rules, but if you want to translate to C, then I suggest you use 0 as the staring point. Assigning to an array element is done like this example: A[0] = 123 and you can write your pseudo code like that.

A loop is something that is done repeatedly for a number of times. Usually, you use a counter to count how many times you want a loop to repeat.

A psuedo code loop might look like this:
For counter is 0 through 6 do
end for

Putting these ideas together, you get pseudo code like this
For counter is 0 through 6 do
  A[counter] = RANDOM
end for
For counter is 0 through 6 do
  print A[counter]
end for

Depending on what RANDOM is defined to do, you may need to do more processing. If RANDOM returns a random integer which might be greater than 10, then you would want to use RANDOM modulo 10. In C, the modulo operator is %.

I hope that helps you to understand the idea of pseudo code. Again, remember that pseudo code is not a standard language, so what I write will be different from what your instructor writes.

Best regards
Zlatko

---------- FOLLOW-UP ----------

QUESTION: Hello Zlatko and thank you so much for the thorough response. I believe I have it but I may be overloading my array (put 10 elements into a 7 element array) and I forgot to check for repeating lottery numbers. How should I do that? This is what I have:

1        // Constant array size.

2        Constant Integer SIZE= 7

3        

4        // Declare array to hold lottery numbers .

5        Declare Integer lotteryNumbers[SIZE]

6        

7        //Declare variable for a loop counter.

8        Declare Integer index

9         

10    // Step through array randomly generating numbers in range

11    // of 0 through 9 and set each array element to random.

12    For index= 0 to 9

13          Set lotteryNumbers[index]= RANDOM (0, 9)

14    End For

15

16 // Print the contents of the array.

17  For index 0 to 9

18        print lotteryNumbers[index]

19  End For

ANSWER: Hello Sasha

Yes, you are overloading your array. The array is supposed to hold 7 elements so the index goes from 0 to 6, meaning that the body of the loop is executed even when index is 6. I like to say "0 through 6" to emphasize that the body is executed even when the index is 6, but all that matters is that you and your instructor are clear on what is happening. That accounts for 7 elements. The array elements are between 0 and 9, so that may be confusing you. The array elements and the size of the array are two different ideas.

I hope that helps.

Best regards
Zlatko

---------- FOLLOW-UP ----------

QUESTION: Ok, so I can change leave this as is: lotteryNumbers[index]= RANDOM (0, 9) and just change For index=0 through 9? Now, how would I check for repeating numbers? Should I find a way to implement 'count' into the code?

Answer
Sasha, the changes you are proposing are correct. Just change
For index=0 through 9
to
For index=0 through 6

and leave
lotteryNumbers[index]= RANDOM (0, 9)
as is.

To check for repeating numbers, I can think of two options.

Option 1
Every time you generate a number, go through all the other generated numbers to see if the new number has been used.

Option 2
Create another array that is 10 elements long to keep track of used numbers. Lets call the array Used. If Used[x] is 1, it means that x has already been generated by RANDOM. If Used[x] is 0, it means that x has not been generated. Start by setting all of Used to 0, and every time a number is generated, and accepted, set Used[number] = 1

Try writing up the pseudo code for one of those options.

There is another way to approach this problem, and that is to create an array of 10 elements containing the numbers 0 through 9, and then shuffle the array, and print out the first 7 elements.  That approach is a little different from what you've started with, so you might want to stay with option 1 or 2 above.

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Zlatko

Expertise

No longer taking questions.

Experience

No longer taking questions.

Education/Credentials
No longer taking questions.

©2012 About.com, a part of The New York Times Company. All rights reserved.