calgorithmpseudocode

Translating C programming into pseudocode


I'm writing a pseudo-code for an algorithm. The result looks very much like C language because it's the language I'm most familiar with.

The code is as follows:

START
Declare int m, int n
Input value of m and n
Declare int array source[n], target[m][n]

For(int i = 0; i < n; i++)
    Input value of source[i]

For(int i = 0; i < m; i++)
    For(int j =  0; j < n; j++)
        Input value of target[i][j]

Declare int maxsource, minsource, maxtarget, mintarget
For(int i = 0; i < n; i++)
    If i == 0 
        Minsource = source[i]
        Maxsource = source[i]
    Else
        If minsource > source[i]
            Minsource = source[i]
        Else if maxsource < source[i]
            Maxsource = source[i]
        Endif
    Endif

For(int i = 0; i < m; i++)
    For(int j = 0; j < n; j++)
        If j == 0
            Mintarget = target[i][j]
            Maxtarget = target[i][j]
        Else
            If mintarget > target[i][j]
                Mintarget = target[i][j]
            Else if maxtarget < target[i][j]
                Maxtarget = target[i][j]
            Endif
        Endif
    If minsource == mintarget && maxsource == maxtarget
        Print true
        STOP
    Else if maxtarget > maxsource
        Print false
        STOP
    Endif

Print false
STOP

How do I turn this into more pseudo-code-like? Any tips?


Solution

  • If I understand you well, you do not want the pseudo-code to look much like a program code so that someone using different progarmming language can use it and solve the same task. I have some tips that could help you achieve that. The main idea is that you should avoid using syntactic elements of the programming language. Think in terms of the problem beign solved, not the programming language. Write your pseudo-code in simple language that any one can read and understand. It should be simple that someone with out coding skills can follow it and solve thesame task. Here is an example of simple pseudo-code of a function that finds the second maximum element in an array.

    The function takes two inputs:
      arr - array of integers
      length - lenght of the array
    It returns:
      secMax - Second maximum element
    

    Then the steps (should be in a simple language)

    Define and set the maximum and second maximum as the minimum possible value
    
    Repeat for each element of the array
    {
      If the current element is greater than the current maximu
      {
        then make current maximum as second maximum
        Make the maximum as current array element
        (Now you have new maximum and second maximum)
      }
      else if the current array element is less than maximum but is greater than second 
      maximum
      { 
      then make it second maximum
      }
     
    }
    return second maximum
    

    As you can see I just explained the steps without using syntactic elements of any programming language. So now anyone can implement my steps using his/her comfortable language to solve the same task.

    Let's say I want to implement this using C, my code will be something like this:

    //Defining the function according to the information above
    //It takes to inputs and returns an integer
    
    int findSecondMaximum(int arr[], int length)
    {
      //Define and Set the maximum and second maximum as the minimum possible value
        int max, secondMax;
        max = secondMax = INT_MIN;
    
      for (int i = 0; i < length; i++) //Repeat for each element of the array
      {
         //If the current element is greater than the current maximum
         if (vect[i] > max)
         {
           //then make the current maximum as second maximum
            secondMax = max;
               
          //Make the maximum as the current array element
              max = vect[i];
         }
    
         /*else if the current array element is less than maximum but is greater than 
         second maximum*/
         else if (vect[i] > secondMax && vect[i] < max)
         { 
           //make it second maximum
            secondMax = vect[i];
         }
     
      }
      //return second maximum
       return secondMax;
      
    }
    
    

    I hope this would help.