Algorithms

Moderators: None (Apply to moderate this forum)
Number of threads: 384
Number of posts: 762

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
HELP WITH THIS PSEUDOCODE PLEASE! Posted by papakait on 14 Apr 2009 at 9:47 AM
Can anyone help me write a pseudocode for this problem please?


Write a program that inputs an even integer from the user (we will refer to this as x in the rest of the handout) and outputs all even numbers between zero and the input number (inclusive).

Hints:

1. Remember to declare needed variable(s). How many variables do you need and of what type?

2. The problem statement is essentially asking you to print the number sequence 0 2 4 6 … x. What is the relationship between each number in the sequence and the next number?

3. You will need a while loop to generate the sequence of number and output them. What should be the loop condition if you want to stop after you have printed x?

Report
Re: HELP WITH THIS PSEUDOCODE PLEASE! Posted by OMichael on 14 Apr 2009 at 11:16 AM
declare x, count as integer

count = 0

do until count >= x

print count & ", "
count = x + 2

loop


Hope this helps
Report
Re: HELP WITH THIS PSEUDOCODE PLEASE! Posted by Actor on 14 Apr 2009 at 1:09 PM
   integer X, Count

   read X
   for Count = 0 to X step 2
      write Count


Or if you must use a while loop..

   integer X, Count

   read X
   Count = 0
   while Count <= X
      write Count
      Count = Count + 2


Report
Re: HELP WITH THIS PSEUDOCODE PLEASE! Posted by papakait on 14 Apr 2009 at 2:50 PM
thanks so much!
Report
Re: more help with pseudcode Posted by papakait on 15 Apr 2009 at 2:24 PM
I tried using Actors but it isn't working. It says the words "write" and/or "read" aren't declared. Im writing this for CH is there any other way I can do it, or other words? (We've been using cout and cin things)?

Now that you have been working on Ch programs for the past few weeks, you should have become more familiar with the process of translating a problem/solution description into code. In this lab, you are given a problem statement that makes use of the programming elements learned over the past few weeks. You are expected to devise an algorithm to solve this problem and translate it into code. You are expected to work on your own. Some hints are given at the end of this handout to help you with this process. Remember that your code will not look like everyone else’s.

Write a program that inputs an even integer from the user (we will refer to this as x in the rest of the handout) and outputs all even numbers between zero and the input number (inclusive).

Hints:

1. Remember to declare needed variable(s). How many variables do you need and of what type?

2. The problem statement is essentially asking you to print the number sequence 0 2 4 6 … x. What is the relationship between each number in the sequence and the next number?

3. You will need a while loop to generate the sequence of number and output them. What should be the loop condition if you want to stop after you have printed x?

Save your program file as lab12.ch in your H drive. Test your program and be sure that it prints the correct sequence of numbers. Transfer a copy of the finalized version of your program to your Submissions folder.

Report
Re: more help with pseudcode Posted by Actor on 15 Apr 2009 at 7:38 PM
: I tried using Actors but it isn't working. It says the words
: "write" and/or "read" aren't declared.

Your request was for an algorithm in pseudo code. By definition pseudo code is not likely to compile. "write" and "read" are generic terms and you need to translate into whatever the equivalent is in the language you are using.

I've no idea what CH is but if you are using cin and cout it seems you are using something similar to C++, which would look something like this.
main()
{
   int x, count ;

   cin >> x ;
   count = 0 ;
   while (count <= x){
      cout << x ;
      count += 2 ;
   }


Report
Re: HELP WITH THIS PSEUDOCODE PLEASE! Posted by Actor on 14 Apr 2009 at 1:12 PM
:
: declare x, count as integer
: 
: count = 0
: 
: do until count >= x  // does not handle "inclusive" case
: 
: print count & ", "
: count = x + 2        // wrong!
: 
: loop
:
:
: Hope this helps
:

Report
Re: HELP WITH THIS PSEUDOCODE PLEASE! Posted by OMichael on 14 Apr 2009 at 6:25 PM

: declare x, count as integer
: 
: count = 0
: 
: do until count >= x + 2 // fixed i think, i have no way to debug :( 
: print count & ", "
: count = count + 2        // fixed :)
: 

Report
Re: HELP WITH THIS PSEUDOCODE PLEASE! Posted by Actor on 15 Apr 2009 at 2:28 AM
:
:
: 
: : declare x, count as integer
: : 
: : count = 0
: : 
: : do until count >= x + 2 // will allow case x+1 if x is odd 
: : print count & ", "
: : count = count + 2        // fixed :)
: : 
:
:




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.