You are here:

C/Arrarys

Advertisement


Question
QUESTION: I have coded a program in the C language that checks for repeated numbers. However, I need to write it in pseudo code (natural language) so the code can be broken down. Here is what I have:

  1.
     int array[BIG_SIZE];
  2.
     int n = 0;
  3.
      

  4.
      
  5.
     int r = rand();
  6.
     int i;
  7.
      
  8.
     for ( i = 0; i < n; i++ ) {
  9.
     if ( r == array[i] )
 10.
     break;
 11.
     }
 12.
      
 13.
     if ( i == n )
 14.
     array[n++] = r;

I know it's quite the reverse to go from the programming language then to a natural language. I've been C programming for awhile so it's a bit odd now for me to write in just simple, basic words. Can you please help?

ANSWER: Hello Crysta.
Your program doesn't really check for repeated numbers. Maybe it could be used to generate an array of unique random numbers, but it needs an outer loop to do that.

An outer loop would run while n < BIG_SIZE
In the outer loop, generate a number
In the inner loop, go through existing numbers and see if any of them are equal to the generated number.
If the inner loop does not find the generated number in the array then place it into the array.

The algorithm is something like this.

n = 0
while n < BIG_SIZE
 r = random()

 found = false
 for i = 0 to n-1
     if array[i] == r then
        found = true
        break
     end if
 next i
 if not found then array[n++] = r
end while


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

QUESTION: So, checking for repeated numbers in pseudo code for random numbers would be:
 
For index= 0 to 4

     If gameNumbers[size]== random then

         found=true

         break

     End if

   next gameNumbers

   if not found then gameNumbers[n++]=random

Answer
Hello Crysta

It is not clear to me what you are trying to do because your pseudo code and your problem description don't match.

Your problem statement says that you want to find repeated numbers in an array, but your pseudo code is getting numbers and putting them into an array.

If you want to check an array for duplicate numbers, and the array already has numbers in it, and you don't want to sort the array, then here is how to do it.

For each element (e) in the array, go through the all the other elements in the array and see if any of them match e.

Here is the pseudo code.

for i = 0 to BIG_SIZE
  e = array[i];
  found = false
  for j = 0 to BIG_SIZE
     if (e = array[j] and i != j) then
        found = true
        quit the j loop
     end if
  next j
  if found == true then
     print number e has duplicates
  end if
next i

If on the other hand, you want to generate an array of unique random numbers, then the algorithm in my last answer is the one to use.

The pseudo code you sent me in the follow up is hard for me to understand because I'm not sure about the meaning of size, and n, and how they relate to index.

If you have any question about the how either of the algorithms work, feel free to ask. I hope I've helped you a little, but if not, feel free to get another opinion.

Best regards
Zlatko

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.