ScoreHero
Home | Forum | Wiki
Inbox [ Login ]Inbox [ Login ]
SearchSearch MemberlistMemberlist
ProfileProfile Log inLog in
The Programming Thread / Project Euler Thread
Goto page Previous  1, 2, 3 ... 5, 6, 7 ... 28, 29, 30  Next
 
Post new topic   Reply to topic    ScoreHero Forum Index -> General Chat
View previous topic :: View next topic  
Author Message
FBMrider86  





Joined: 23 Jan 2007
Posts: 1679
Location: Lawrenceburg, KY <- And I ain't havin' no fun

PostPosted: Mon Sep 13, 2010 9:25 pm    Post subject: Reply with quote

Sarg338 wrote:
Ok, new problem, new post.

I got the program to read from the file correctly. Now, when it gets the number, it converts it into binary... just backwards. Lets take the number 12 for example.

12 in binary is 1100.

The program outputs it as 0011. I can't ask my professor today since he has no office hours to visit. Here's the code.

Code keeps appearing weird in the code tags. Will edit later

Sorry for it looking messy and with no comments. If needed, I'll edit this and include them. I usually try to finish the program and then go back and add comments.

Anyway, anyone see what I'm doing wrong?

Can we see your code? It should be an extremely easy fix.
_________________
Quote:
I think my neighbors have figured out my address. Should I move or kill them?

Custom Guitar Paint Jobs (Submit Yours!)
Proud supporter of EWiggen's Grilled Cheese and GH Combo
socrstopr wrote:
This thread is very disturburbing. Also terrible.
Back to top
View user's profile Send private message MSN Messenger XBL Gamertag: Head0nFire86
Sarg338  





Joined: 07 Feb 2008
Posts: 5143

PostPosted: Mon Sep 13, 2010 9:28 pm    Post subject: Reply with quote

FBMrider86 wrote:
Sarg338 wrote:
Ok, new problem, new post.

I got the program to read from the file correctly. Now, when it gets the number, it converts it into binary... just backwards. Lets take the number 12 for example.

12 in binary is 1100.

The program outputs it as 0011. I can't ask my professor today since he has no office hours to visit. Here's the code.

Code keeps appearing weird in the code tags. Will edit later

Sorry for it looking messy and with no comments. If needed, I'll edit this and include them. I usually try to finish the program and then go back and add comments.

Anyway, anyone see what I'm doing wrong?

Can we see your code? It should be an extremely easy fix.


Posted the pic in previous post. I would think it would be an easy fix, but I can't think of anything I would need to do differently.

I think my professor said something about that since I'm doing it with this algorithm, I need to compute the answer to n%2, store it until n reaches the base case, and then output it starting from the bottom. MY current code is outputting the answer as soon as it is computed.
Back to top
View user's profile Send private message XBL Gamertag: PRS Sarg
Urisma  





Joined: 04 Aug 2007
Posts: 220
Location: The Woodlands, Texas

PostPosted: Mon Sep 13, 2010 9:36 pm    Post subject: Reply with quote

Sarg338 wrote:
Urisma wrote:
0011 is correct. x86 processors are little endian, causing 0x87654321 to be stored as
Code:
0xfff0: 21
0xfff1: 43
0xfff2: 65
0xfff3: 87


its the same for 12, itll be stored 00, then 11. You're probably outputting via memory location one byte or char at a time, which would cause "0011" to be output.


But 12 in binary is 1100, not 0011. It's outputting it backwards to what I need it to. Heres the code I have.

http://i54.tinypic.com/dlq5h1.jpg

For some reason, my code keeps getting cut off when I put it in the code tags. THe only thing missing in the pic are just 2 }s closing out the code.


well, consider what the code is doing on a bit level. 12 % 2 == 0, so it outputs 0. 6 % 2 == 0, so it outputs 0 again. 3 % 2 == 1, so on and so forth. Your code is doing exactly what's written. Using modular math will output it in reverse, since you're outputting the smallest bit on the left side, making the output wrong. I think you should try a while loop with bit fiddling. It'd be much easier. And would work.

Also, why would your professor want you to use n % 2? That sets you up for all sorts of problems (i.e: backwards output)
Back to top
View user's profile Send private message XBL Gamertag: TuftedToaster
Sarg338  





Joined: 07 Feb 2008
Posts: 5143

PostPosted: Mon Sep 13, 2010 9:43 pm    Post subject: Reply with quote

Well, I can't do a while loop, since he wants us to do it recursively. Not sure why he wants us to do it with that algorithm, that's just how he wants us to do it. He gave us a warning about outputting backwards, but I didn't take notes for this program, so I'll probably shoot him an email about it later if no one can help here.
Back to top
View user's profile Send private message XBL Gamertag: PRS Sarg
FBMrider86  





Joined: 23 Jan 2007
Posts: 1679
Location: Lawrenceburg, KY <- And I ain't havin' no fun

PostPosted: Mon Sep 13, 2010 10:05 pm    Post subject: Reply with quote

The easiest thing that comes to mind (for this application anyway) would just be to store the binary digits in a String and display that once the recursion is finished.

Code:
private static String withRecur(int n, String bin)
    {
       
        if(n <= 1)
        {
            bin = Integer.toString(n) + bin;
            return bin; //return the entire String of binary digits
        }
        else
        {
            bin = Integer.toString(n%2) + bin; //tacks the new digit to the String
            return withRecur((n/2), bin);
        }

    }


You just change the function to be a String instead of an int. I wrote this in Java but you shouldn't have any trouble doing that in C++. The only issue is if he doesn't want you to use Strings or Integer to String conversion.
_________________
Quote:
I think my neighbors have figured out my address. Should I move or kill them?

Custom Guitar Paint Jobs (Submit Yours!)
Proud supporter of EWiggen's Grilled Cheese and GH Combo
socrstopr wrote:
This thread is very disturburbing. Also terrible.
Back to top
View user's profile Send private message MSN Messenger XBL Gamertag: Head0nFire86
Sarg338  





Joined: 07 Feb 2008
Posts: 5143

PostPosted: Mon Sep 13, 2010 10:15 pm    Post subject: Reply with quote

FBMrider86 wrote:
The only issue is if he doesn't want you to use Strings or Integer to String conversion.


I don't even know how to implement strings since I wasn't in any beginning C++ classes all last year (and he knows it, since I was in his classes) so I'm pretty sure he would notice something.

Oh well. I have an appointment with him tomorrow anyway, so I'll just ask him about it then. Thanks for the help guys.
Back to top
View user's profile Send private message XBL Gamertag: PRS Sarg
Bj0rn  





Joined: 31 Aug 2007
Posts: 522
Location: Stockholm, Sweden

PostPosted: Mon Sep 13, 2010 10:41 pm    Post subject: Reply with quote

Sarg338 wrote:
Well, I can't do a while loop, since he wants us to do it recursively. Not sure why he wants us to do it with that algorithm, that's just how he wants us to do it. He gave us a warning about outputting backwards, but I didn't take notes for this program, so I'll probably shoot him an email about it later if no one can help here.

Hey, the recursion itself can fix the backwards problem. Look at you code. "BinaryOutput" doesn't need to return a value, does it? (Btw, does your program even compile? Not all control paths have a return value). Since all it does is print stuff, it should be changed to void. If you then remove the "return" before "BinaryOutput(n/2);", and run the program, you will get the same output. But swap the lines "cout << n%2;" and "BinaryOutput(n/2);" and you might be pleasantly surprised by the result. I'll let you figure out why this works yourself.

EDIT: I realized that you will get line breaks in the wrong spots. Fix this by removing every "endl" in the function, and instead add a "cout << endl;" in the main function loop right after calling your function.
_________________
My accomplishment thread
Champion of 6 ScoreHero leagues:
S7: GH3X AAA-2
S10: GH3X AAA-1, GHAX
S11: GH3X AAA, GH5X AAA
S16: GH3X
My youtube channel
Back to top
View user's profile Send private message XBL Gamertag: BurningThunder
FBMrider86  





Joined: 23 Jan 2007
Posts: 1679
Location: Lawrenceburg, KY <- And I ain't havin' no fun

PostPosted: Mon Sep 13, 2010 11:11 pm    Post subject: Reply with quote

Bj0rn wrote:
Sarg338 wrote:
Well, I can't do a while loop, since he wants us to do it recursively. Not sure why he wants us to do it with that algorithm, that's just how he wants us to do it. He gave us a warning about outputting backwards, but I didn't take notes for this program, so I'll probably shoot him an email about it later if no one can help here.

Hey, the recursion itself can fix the backwards problem. Look at you code. "BinaryOutput" doesn't need to return a value, does it? (Btw, does your program even compile? Not all control paths have a return value). Since all it does is print stuff, it should be changed to void. If you then remove the "return" before "BinaryOutput(n/2);", and run the program, you will get the same output. But swap the lines "cout << n%2;" and "BinaryOutput(n/2);" and you might be pleasantly surprised by the result. I'll let you figure out why this works yourself.

EDIT: I realized that you will get line breaks in the wrong spots. Fix this by removing every "endl" in the function, and instead add a "cout << endl;" in the main function loop right after calling your function.


Good call, such a simple fix indeed.
_________________
Quote:
I think my neighbors have figured out my address. Should I move or kill them?

Custom Guitar Paint Jobs (Submit Yours!)
Proud supporter of EWiggen's Grilled Cheese and GH Combo
socrstopr wrote:
This thread is very disturburbing. Also terrible.
Back to top
View user's profile Send private message MSN Messenger XBL Gamertag: Head0nFire86
Sarg338  





Joined: 07 Feb 2008
Posts: 5143

PostPosted: Mon Sep 13, 2010 11:16 pm    Post subject: Reply with quote

Bj0rn wrote:
Sarg338 wrote:
Well, I can't do a while loop, since he wants us to do it recursively. Not sure why he wants us to do it with that algorithm, that's just how he wants us to do it. He gave us a warning about outputting backwards, but I didn't take notes for this program, so I'll probably shoot him an email about it later if no one can help here.

Hey, the recursion itself can fix the backwards problem. Look at you code. "BinaryOutput" doesn't need to return a value, does it? (Btw, does your program even compile? Not all control paths have a return value). Since all it does is print stuff, it should be changed to void. If you then remove the "return" before "BinaryOutput(n/2);", and run the program, you will get the same output. But swap the lines "cout << n%2;" and "BinaryOutput(n/2);" and you might be pleasantly surprised by the result. I'll let you figure out why this works yourself.

EDIT: I realized that you will get line breaks in the wrong spots. Fix this by removing every "endl" in the function, and instead add a "cout << endl;" in the main function loop right after calling your function.


I thought it had something to do with switching those 2 around, but since I did that before and it didn't work, I kind of dismissed it. Thanks for the help though.

Also, I had it returning a value just because every other example he has given has had it returning a value, so I thought it must return a value to the function so it knows what n is so it can %2 it.

Thanks for help man, appreciate it.
Back to top
View user's profile Send private message XBL Gamertag: PRS Sarg
Sarg338  





Joined: 07 Feb 2008
Posts: 5143

PostPosted: Wed Sep 15, 2010 11:16 pm    Post subject: Reply with quote

Woohoo, finished the other program (output the approx. Square root of a number to 6 decimal places.). I had to look up how to output it to certain decimal places, but after I got that and played around with the algorithm until it spit out what it should have. It was surprisingly easier than the Binary one (or I didn't repeat the same mistakes as I did).

Thanks for the help on the programs. Now we start to learn about Classes and ADTs in class.
Back to top
View user's profile Send private message XBL Gamertag: PRS Sarg
Southparkhero  





Joined: 23 Aug 2008
Posts: 3251
Location: Some place in NJ.

PostPosted: Thu Sep 16, 2010 12:40 am    Post subject: Reply with quote

This may be a very stupid question, but what programming languages are compatible with Windows 7 64 bit?

By programming languages, I mean the actual program you use to program in.
_________________
sdfadf
Back to top
View user's profile Wiki User Page Send private message
Sarg338  





Joined: 07 Feb 2008
Posts: 5143

PostPosted: Thu Sep 16, 2010 12:45 am    Post subject: Reply with quote

Southparkhero wrote:
This may be a very stupid question, but what programming languages are compatible with Windows 7 64 bit?

By programming languages, I mean the actual program you use to program in.


I use Microsoft Visual Studio for my C++ Assignments. Although I have 32-bit, I'm sure you could check out the site and see if it's compatible.
Back to top
View user's profile Send private message XBL Gamertag: PRS Sarg
Urisma  





Joined: 04 Aug 2007
Posts: 220
Location: The Woodlands, Texas

PostPosted: Thu Sep 16, 2010 2:49 am    Post subject: Reply with quote

Southparkhero wrote:
This may be a very stupid question, but what programming languages are compatible with Windows 7 64 bit?

By programming languages, I mean the actual program you use to program in.


notepad works on x64.
Back to top
View user's profile Send private message XBL Gamertag: TuftedToaster
thefunneverends  





Joined: 28 Jul 2007
Posts: 65
Location: Ohio

PostPosted: Thu Sep 16, 2010 4:10 am    Post subject: Reply with quote

Southparkhero wrote:
This may be a very stupid question, but what programming languages are compatible with Windows 7 64 bit?

By programming languages, I mean the actual program you use to program in.


Eclipse. Great for Java and with the right plug-ins, other languages work too (though I've never tried).

http://www.eclipse.org/
Back to top
View user's profile Send private message
FBMrider86  





Joined: 23 Jan 2007
Posts: 1679
Location: Lawrenceburg, KY <- And I ain't havin' no fun

PostPosted: Thu Sep 16, 2010 4:21 am    Post subject: Reply with quote

If you have Windows 7 64-bit (as I do) you should have no problems finding an IDE that will run, regardless of whether or not it's 32 bit or 64 bit. If you're a student and your school is part of the MSDNAA you can get a lot of software for free. Both my copy of Windows 7 and Visual Studio Pro 2008 are because of that. Finally a freebie that's useful from college. Otherwise Microsoft lets you download certain IDEs (C++, C#, VB, etc.) for free, they just aren't bundled together.

thefunneverends wrote:
Eclipse. Great for Java and with the right plug-ins, other languages work too (though I've never tried).

http://www.eclipse.org/

Also this. Eclipse and NetBeans are both fantastic.
_________________
Quote:
I think my neighbors have figured out my address. Should I move or kill them?

Custom Guitar Paint Jobs (Submit Yours!)
Proud supporter of EWiggen's Grilled Cheese and GH Combo
socrstopr wrote:
This thread is very disturburbing. Also terrible.
Back to top
View user's profile Send private message MSN Messenger XBL Gamertag: Head0nFire86
Display posts from previous:   
Post new topic   Reply to topic    ScoreHero Forum Index -> General Chat All times are GMT
Goto page Previous  1, 2, 3 ... 5, 6, 7 ... 28, 29, 30  Next
Page 6 of 30

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum




Copyright © 2006-2024 ScoreHero, LLC
Terms of Use | Privacy Policy


Powered by phpBB