‘C’ PROGRAMMING - Tips N TRIKS

Thursday, 13 February 2014

‘C’ PROGRAMMING

‘C’ PROGRAMMING: PSEUDOCODES

Image
Hi friends! Am back with my next article following from where I left it the other day.
As we discussed, systematic approach to any task gets it simple to complete. I would again suggest you to note down every step as you start your programming in ‘C’ and further you can leave it as per your personal requirements.


I had asked you to try to solve the problem without any third variable. Here comes the solution:
Say x=5 & y=9, now if we add x and y such that:
x=x+y, i.e. now x is 14
y=x-y, the value of y is now 14-9=5. Again,
x=x-y so the value of x will be now 14-5=9, and that’s our answer.
Hope you could write the same very steps in form of pseudo code(algorithm) now.
Let’s take another example because examples are better to understand and get the concept.
Solving a quadratic equation, we deal with the coefficients of x2, x and the constant value. In ‘C’ we cant give the quadratic equation directly in its original form say, ax2+bx+c=0, either we could give the input in form of its coefficients i.e. a, b & c.
As we know that in a quadratic equation the solution depends on the value of d(discriminant).
So, let’s start our pseudo coding:
1. START
2. Read a, b, c (we don’t use and or ‘&’, ‘&’ has its own meaning in ‘C’)
3. d=b2-4ac (we will write it as d=b*b-4*a*c for easy implementation in ‘C’)
4. If d<0, print “The given equation has imaginary roots”. GOTO 7
5. If d=0, print “Equal roots with x=-b/2c” (written as b/(2*c)), GOTO 7
6. If d>0, print “Real and unequal roots as x=(-b+sqrt(d))/(2*a) and x=(-b-sqrt(d))/(2*a)”
7. STOP
Here you might be thinking about the use of ‘GOTO’, let me make it clear for you all in solving a quadratic equation the answer comes depending on the value of ‘d’. When d<0 the roots are imaginary and our solution ends there so we need to write ‘STOP’ right after it. Its same when d>0 or d=0, so by not writing stop after every interval we just wrote it once and diverted the program statement to it.
On this point I would like to share my personal experience in class. when the teacher asked us to write the algorithm to solve a given quadratic equation, I gave the input as whole equation in its original form and was confused that why are we writing a, b and c only. Similarly I also missed the ‘GOTO’ statements which means all the three statement lines will be checked before proceeding.
Let’s take another simple but interesting example. How would you print the largest number among three given numbers?
Say a=13, b=7, c=21. Clearly by looking we can see that c is the greatest amongst all with a to follow and then c.
But have you thought how the machine will proceed to it? In what sequence will it solve the problem?
Let’s go on for the algorithm for now you’all must be able to understand it.
1. START
2. Read x, y, z
3. If a>b
{
If a>c print “a is the greatest”
else print “c is the greatest”
` }
else
{
If b>c print “b is the greatest”
else print “c is the greatest”
` }
4. STOP
Here I used another style for writing it using conditional(If) branching. Hope you’ll get to it till next time. You are warmly welcome to comment with your doubts, comments and suggestions. And for you to do, Try writing an algorithm to print the largest number without using branching and also when you don’t know the number of variables. You could also try to write it in both the ways which I showed today. Give it a try because you can’t succeed until you try. All the very best, believe me its very simple to program when you know the basics, you can’t do anything with the rules.
For any previous knowledge refer my last post.

No comments:

Post a Comment