Exercises of introduction to programming

In this post, I propose two exercises of introduction to programming, the first topic of my Java notes. It is about thinking, proposing a solution to the proposed problem, and then translating it into a flow diagram and pseudocode.

There are several possible solutions to solve the same problem, some more efficient than others. But of course, before we start programming, we have to propose the solution to the problem that we want to solve by programming.

Here you can see a possible solution for each proposed exercise, which of course is not the only one possible and which is probably not the most efficient.

Exercise 1:

Draw the flow diagram and write pseudocode for an algorithm that calculates the factorial of a number n.

The flow chart of a possible solution would be:

Flowchart-factorial

And the associated pseudocode:

Start
   We start the result with a value equal to 1.
   We start counter with a value equal to 1.
   We check if the counter is less than or equal to the number n of which we want to calculate the factorial
      If true:
         We make the result equal to its current value multiplied by the counter.
         And we increase the value of the counter by 1.
      If false:
      We finish the program. As result, we will have the value of the factorial of the number n.
End

Exercise 2

Draw the flowchart and write the pseudocode of an algorithm that orders a set of n numbers from highest to lowest.

The flow chart of a possible solution would be:

Flowchart_order

And the associated pseudocode:

Start
   We place ourselves in the first position on the list
   If the following value is greater than the current one:	
      We change the position values and start over from the beginning of the list.
   If the following value is not greater than the current value:
      We move one position forward.
      If this position is the last on the list, we are done and we have the list ordered.
      If this position is not the last on the list, we return to the point of the first check.
End

NOTA:

Este post es parte de la colección “Java” que reproduce los apuntes de la clase que imparto sobre el tema en ESIC. Puedes ver el índice de esta colección aquí.

Leave a Reply

Your email address will not be published. Required fields are marked *