Results 1 to 16 of 16
Thread: Help me re-write pseudo code?
- 04-21-2011, 04:37 AM #1
Member
- Join Date
- Apr 2011
- Posts
- 11
- Rep Power
- 0
Help me re-write pseudo code?
Here it is:
public static StringList power (String s){
StringList answer = new StringList();
// Base case
if (s.length() == 0){
prepend("", answer);
}else{
StringList partial = power(butFirst(s));
String firstChar = first(s);
//pseudocode
StringList prependedP = new StringList();
For each String x in P
insert firstChar + x into prependedP
//end of pseudocode
answer = p + prependedP;
}
return answer;
}Last edited by csisdifficult; 04-22-2011 at 02:49 PM.
- 04-21-2011, 04:46 AM #2
OK
You write the first letter. Then I will write the second letter. Then we take it in turns.
- 04-21-2011, 05:01 AM #3
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
h
damn limits, am i doing it right? :)
@ op: on this forum you will be doing most of the work, we will simply be helping you out.
-
Original poster, please make a meaningful contribution to this thread before I have to lock it has a homework dump.
- 04-21-2011, 05:13 AM #5
Member
- Join Date
- Apr 2011
- Posts
- 11
- Rep Power
- 0
I apologize, I thought that was what I was implying. I'm not sure how to go about re-writing the pseudo code, so what steps should I take to begin re-writing it?
- 04-21-2011, 05:21 AM #6
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Is the original post pseudo code? It looks closer to real code than pseudo code. Also, please wrap pseudo code, and regular code in code tags.
[code ] //delete space
YOUR CODE HERE
[/code]
Also, ask specific questions, and say what exactly is giving you trouble.
- 04-21-2011, 05:24 AM #7
Sunde if you look close the for each loop near the bottom is the only pseudocode.
- 04-21-2011, 05:25 AM #8
Member
- Join Date
- Apr 2011
- Posts
- 11
- Rep Power
- 0
Okay, here's the code with code tags:
Java Code:public static StringList power (String s){ StringList answer = new StringList(); // Base case if (s.length() == 0){ prepend("", answer); }else{ StringList partial = power(butFirst(s)); String firstChar = first(s); //pseudocode StringList prependedP = new StringList(); For each String x in P insert firstChar + x into prependedP //end of pseudocode answer = p + prependedP; } return answer; }Last edited by csisdifficult; 04-22-2011 at 02:50 PM.
- 04-21-2011, 05:45 AM #9
No you use a for each loop. If you haven't heard of them do some research.
- 04-21-2011, 05:45 AM #10
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Ah, thanks Junky, I'm lazy lol, I only briefly scanned his code.
@op: First you need to understand what a for each loop is. It's basically, for each item x in list of item x.
So I assume you know that arrays must be declared with some type(same with any other collection.) Say you have an array of strings, you can iterate through the loop with a for each loop. I understand a regular for loop does the same thing, but at times a for each loop is nice.
In an array of integers you would do something like this
this loop does something for each item in the loop. Each pass through the loop the current item being worked on can be referenced by x. As a more concreter example, lets multiply each value in the array by 5.Java Code:int[] ints = {1, 2, 3, 4,5, 6, 7, 8}; for(Integer x : ints){ //do something }
Java Code:public class By5{ public static void main(String[] args){ int[] nums = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; for(Integer x : nums){ x *= 5; } for(Integer x : nums){ System.out.println(x); } } }
- 04-21-2011, 05:48 AM #11
You're lazy? You supplied a for each example whereas I told them to look it up themselves.
- 04-21-2011, 05:50 AM #12
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
lol, Im lazy when it comes to reading unformatted code.
- 04-21-2011, 05:50 AM #13
Member
- Join Date
- Apr 2011
- Posts
- 11
- Rep Power
- 0
"For each" loop? I wrote that code with the intention of using just a "while" loop. I've only been taught "while" loops.
Last edited by csisdifficult; 04-22-2011 at 02:50 PM.
- 04-21-2011, 06:07 AM #14
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Do you understand how to use a regular for loop?
- 04-21-2011, 06:11 AM #15
Member
- Join Date
- Apr 2011
- Posts
- 11
- Rep Power
- 0
Yes, but my pseudocode is making it more difficult for me to understand what to write...
What am I initializing, testing, and updating?
So confused...
- 04-21-2011, 06:30 AM #16
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Similar Threads
-
What are you using to write your code?
By CaptainMorgan in forum New To JavaReplies: 947Last Post: 05-14-2013, 05:58 PM -
Starting Pseudo-Code For My Asignment?
By EpyonCustom in forum New To JavaReplies: 26Last Post: 04-07-2011, 11:07 AM -
pseudo code
By jamiem in forum New To JavaReplies: 4Last Post: 12-20-2010, 05:25 PM -
New to java and does not know how to write the code ...
By poisson in forum New To JavaReplies: 57Last Post: 07-15-2010, 04:10 AM -
how do i write this code?
By Libertyman in forum New To JavaReplies: 23Last Post: 06-22-2010, 12:43 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks