Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
Photo

pseudocode that prints numbers between 100


  • Please log in to reply

#1
DavidNode

DavidNode

    Member

  • Member
  • PipPip
  • 27 posts
Hey guys i am new in C programming. i need to write a pseudo code that prints numbers between 100 in descending order.
That is, from 100 to 0.please tell me if i am correct or not. Here is my pseudo code; and thanks.
 
    BEGIN
    c= 100 
    while (c <= 100)
DO
c= c-1
    END DO
    END
 
 
    
 

  • 0

Advertisements


#2
Spike

Spike

    nOoB

  • Member
  • PipPipPipPip
  • 1,357 posts

 

Hey guys i am new in C programming. i need to write a pseudo code that prints numbers between 100 in descending order.
That is, from 100 to 0.please tell me if i am correct or not. Here is my pseudo code; and thanks.
 
    BEGIN
    c= 100 
    while (c <= 100)
DO
c= c-1
    END DO
    END

Hey David,

 

The method of writing pseudo code, is usually writing informal high level code, which is easily understandable by humans but following structural conventions and logic of an actual programming language. I mention this because I noticed you included aspects such as "END", "BEGIN" and "DO" which isn't necessary because it looks like you're focusing on syntax. When writing pseudo code, you want to concentrate solely on the logic of what you are trying to achieve irrelevant of the language.

 

As for the logic you're kinda almost there but your while condition would be considered incorrect for a descending order number loop.

 

--

int c = 100 // Instantiating in pseudo code is not necessary, although I am instantiating c as an int (So logically I can see that I will be working with a variable named c which is of an integer/number type and not some other data type. I have also assigned the value 100 because I know this variable see will require starting at 100.)

 

while(c >= 0) // You will notice the logical operator is the >= because I want the loop to continue executing for as long as c is greater than 0 because we are subtracting down from 100.

  print c; // Printing variable c to the screen (Notice I don't use a language specific way of printing to screen, all I am concerned with is printing to screen, not with how it is achieved in a specific programming language)

  c = c - 1 // Decrement c by 1

end while

 

print c; // I print C for the last time to display 0, because it would not print the value 0 in the loop. This is just "one" way of doing it, you can go about order of printing to screen in several different way

---

 

That's basically it... Let's look at the logic in your code:

 
BEGIN
  c = 100 // This is good as we can see c needs to start at 100 
WHILE (c <= 100) // You loop will continue to execute for as long as c is less than or equal to 100
DO
  c = c-1 // This good because we need to decrement 100 by 1
END DO
END
 
So here's the problem with this logic; because your loop will execute as long as c is less than or equal to 100, you're loop will never end and will be stuck in an infinite loop. This is because for your loop to break out (Exit) you need c to somehow be greater than 100 at some point within your loop, where if you'll see c being decremented by 1 each time will only result in a number smaller than 100 for as long as the loop runs, there's no way of c ever being greater than 100 at any point in time.
 
Also you mentioned printing the numbers... You're code would literally have to contain some pseudo code that represents that you will be physically printing to the screen.

 

I really hope I have explained the logic in a understandable way... Please let me know if you require any further explanation. I have also included a cleaner version of my pseudo code without comments and explanations.

int c = 100

while (c >= 0)
  print c
  c = c - 1
end while

print c

A side tip on working with loops; the best thing you can do is step through your logic 1 line at a time in the order that your computer would execute the code while keeping in mind the value of all your variables on that line.

 

Peace Out :cool:


  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP