• Hey, guest user. Hope you're enjoying NeoGAF! Have you considered registering for an account? Come join us and add your take to the daily discourse.

Programming |OT| C is better than C++! No, C++ is better than C

Yeah, its usually good practice to put script at the bottom of a page. Also declare a type so <script type="text/javascript" blah blah </script>

EDIT: And another thing I noticed is that your second javascript source says ...jquery.ui.min.us when it should probably be ...jquery.ui.min.js
 

Chris R

Member
1) fix the src for jQuery UI :)
2) $(document).ready(function() {}); waits until the DOM finishes loading, meaning you can put your javascript in the documents header if you wish (really no wrong way to do it, almost a personal/shop preference).

As for why your jQuery stuff isn't working, if correctly sourcing the jQuery UI js doesn't fix your issue, try switching your .Click() call to use .on ( http://jsfiddle.net/S5gkE/1/ )
 

Chris R

Member
thanks everyone, it was the .us instead of the .js. I blame min.us and me being an idiot lol

Glad to hear it is working now. By the way, what browser are you using to test in? If you opened the debugger in your browser of choice you would have seen an error pointing out that the source you were trying to load doesn't actually exist.
 

doodle

Member
Gaf, I have an upcoming assignment due in my algorithms class that I just can't seem to wrap my head around. The assignment has to do with Huffman coding or the order-preserving type. Huffman coding is easy enough and in past semesters this professor did not have a time restriction on the program, but this semester it is required to run in n(log(n)) time or else we will be penalized. Everything that I've tried has been well over n(log(n)). I'm not looking for answers, just a nudge in the right direction.
 

usea

Member
Gaf, I have an upcoming assignment due in my algorithms class that I just can't seem to wrap my head around. The assignment has to do with Huffman coding or the order-preserving type. Huffman coding is easy enough and in past semesters this professor did not have a time restriction on the program, but this semester it is required to run in n(log(n)) time or else we will be penalized. Everything that I've tried has been well over n(log(n)). I'm not looking for answers, just a nudge in the right direction.
Need some more information. What exactly are you trying to do? Generating the codes or reading them? Give some details.

If you look at the wikipedia article, it specifically mentions an nlogn algorithm under Compression http://en.wikipedia.org/wiki/Huffman_encoding#Compression
 

doodle

Member
We're generating the codes and printing them out after the program is ran. I'm sorry, I should have been more clear! It's pretty simple if we were doing it the regular way in which the order of the symbols is not preserved. What we are doing is reading a list of probabilities in from a file that will correspond to a counter i (to replace their actual character). From there, we have to create a Huffman tree, but the order that they were read in can not be changed in order to do so. My idea was to scan the array of probabilities that was read in and find the two that are right next to each other that add up to have the smallest probability. After that, it would be a matter of comparing the new node that was created from the previous two nodes to the nodes to the left and right of the two nodes that were used to create it. After finding which one is smallest, you'd create a new node connecting those two. I'm sorry for the horrible explanation. It works on paper, but I feel like I'm doing something wrong.
 

Kirby102

Member
I was wondering if you guys can help me resolve this issue, it's weird how this error has popped out of nowhere, I've never seen it before.

I'm using C# on Visual Studio 2010 and I'm working on a Forms application, but when I open the project solution file, this error pops up on my forms (design) class:

Y6tPV.png


I have installed the required .NET framework, yet it's still appearing. Can anyone help me with this? I haven't tried it on Windows 7, because I have installed Windows 8 as of late.
 

injurai

Banned
I see a lot of unanswered questions in here, and I would just like to put in a plug for Stackoverflow.com

It's insane the amount of knowledgeable people that just answer questions on that site. Recently I was having some problems with getting my .dll files for MinGW to path correctly and it prevent my code from being portable. There was no documentation on the issue, but the help I got was immense.

All I know is that compilers are janky as fuck. Virtual machines are the way of the future.
 
Can someone help with me the syntax that would be needed to count the number of asterisks in a program and then print how many there are in total?

I've created a program that asks the user to enter the height and width of a rectangle, for which then prints this in a rectangle of asterisks.

Next I'm trying to come up with a way of totalling the asterisks and printing below. Can anyone help me with this perhaps?

My current code can be seen below:

Code:
#include<iostream>
using namespace std;

int main()
{
	int height;
	int width;

	//Parameters for rectangle.

	cout <<"Enter the height of the rectangle: ";
	cin >> height;
	cout <<"Enter the width of the rectangle: ";
	cin >> width;

	//Drawing the rectangle.

        for(int i=0; i<height; i++)
        {
            for(int j=0; j<width; j++)
                cout << "*";
            cout << endl;
		}

	cin.ignore(2);
	return 0;
}

This is using C++ by the way. Much appreciated for any help.
 

injurai

Banned
Can someone help with me the syntax that would be needed to count the number of asterisks in a program and then print how many there are in total?

I've created a program that asks the user to enter the height and width of a rectangle, for which then prints this in a rectangle of asterisks.

Next I'm trying to come up with a way of totalling the asterisks and printing below. Can anyone help me with this perhaps?

My current code can be seen below:

Code:
#include<iostream>
using namespace std;

int main()
{
	int height;
	int width;

	//Parameters for rectangle.

	cout <<"Enter the height of the rectangle: ";
	cin >> height;
	cout <<"Enter the width of the rectangle: ";
	cin >> width;

	//Drawing the rectangle.

        for(int i=0; i<height; i++)
        {
            for(int j=0; j<width; j++)
                cout << "*";
            cout << endl;
		}

	cin.ignore(2);
	return 0;
}

This is using C++ by the way. Much appreciated for any help.

It looks like the fact that you omitted the second for loop's brackets (although that should be legal) it isn't pairing the closing brace for the first for loop.

I'm a little confused on what your problem is. Is there a specific error its throwing?
 

usea

Member
I was wondering if you guys can help me resolve this issue, it's weird how this error has popped out of nowhere, I've never seen it before.

I'm using C# on Visual Studio 2010 and I'm working on a Forms application, but when I open the project solution file, this error pops up on my forms (design) class:

Y6tPV.png


I have installed the required .NET framework, yet it's still appearing. Can anyone help me with this? I haven't tried it on Windows 7, because I have installed Windows 8 as of late.
Your target profile is set to the client profile, which is not installed or something. You almost certainly want to target the plain .NET Framework 4 instead of the client profile. Go to your project properties (Project -> Properties) and in the first tab (Application) there should be a dropdown labeled Target framework.
 

usea

Member
We're generating the codes and printing them out after the program is ran. I'm sorry, I should have been more clear! It's pretty simple if we were doing it the regular way in which the order of the symbols is not preserved. What we are doing is reading a list of probabilities in from a file that will correspond to a counter i (to replace their actual character). From there, we have to create a Huffman tree, but the order that they were read in can not be changed in order to do so. My idea was to scan the array of probabilities that was read in and find the two that are right next to each other that add up to have the smallest probability. After that, it would be a matter of comparing the new node that was created from the previous two nodes to the nodes to the left and right of the two nodes that were used to create it. After finding which one is smallest, you'd create a new node connecting those two. I'm sorry for the horrible explanation. It works on paper, but I feel like I'm doing something wrong.
Are you certain your algorithm is not nlogn? From what you described (granted, I'm not 100% clear on it) it sounds like it would be. You're doing a log n operation (tree insertion) and you're doing it n times. The array traversal sounds like it's 2n, but that is irrelevant.

Why do the nodes with the lowest probability have to be next to each other? Just wondering.

edit: sorry for double posting again.
 
Can someone help with me the syntax that would be needed to count the number of asterisks in a program and then print how many there are in total?

I've created a program that asks the user to enter the height and width of a rectangle, for which then prints this in a rectangle of asterisks.

Next I'm trying to come up with a way of totalling the asterisks and printing below. Can anyone help me with this perhaps?

My current code can be seen below:

This is using C++ by the way. Much appreciated for any help.
Wouldn't it just be the entered height times the width? Just print that. Or put a count in you for loops.
 

doodle

Member
Are you certain your algorithm is not nlogn? From what you described (granted, I'm not 100% clear on it) it sounds like it would be. You're doing a log n operation (tree insertion) and you're doing it n times. The array traversal sounds like it's 2n, but that is irrelevant.

Why do the nodes with the lowest probability have to be next to each other? Just wondering.

edit: sorry for double posting again.

I assume that you know for Huffman coding
that you pair the two with the smallest probabilities. In our project, the ordering can not be changed so you can only add nodes that are beside each other.
 
Managed to get the rectangle to count the total asterisks displayed.
Had to add an 'int count' into the code and then insert the follow-up below the cout for the rectangle.

Thanks for the help.
 

Anustart

Member
Question for c# and xna. Anyone know how I can take a font and just plain string data and create the effect from Star Wars intros? With the text kinda scrolling into the background at an angle?

I'm only versed in 2d, does this require some 3d witchcraft? Need to figure out how to get that text angled backward like that and scroll back and eventully vanish.
 

Tantalus

Neo Member
Question for c# and xna. Anyone know how I can take a font and just plain string data and create the effect from Star Wars intros? With the text kinda scrolling into the background at an angle?

I'm only versed in 2d, does this require some 3d witchcraft? Need to figure out how to get that text angled backward like that and scroll back and eventully vanish.

You could probably achieve the same effect in 2D by having each line of text move upwards while decreasing in size and gradually becoming more transparent when it reaches a certain point. Combine that with the slanted text effect (not sure if this is possible in XNA or if you'd have to use a custom font that already has the "slanted" look to it), and it would probably be possible to recreate the scrolling effect pretty well.
 

Anustart

Member
You could probably achieve the same effect in 2D by having each line of text move upwards while decreasing in size and gradually becoming more transparent when it reaches a certain point. Combine that with the slanted text effect (not sure if this is possible in XNA or if you'd have to use a custom font that already has the "slanted" look to it), and it would probably be possible to recreate the scrolling effect pretty well.

Thanks for this. Think I'm just going to get my hands dirty and try to render the text as a texture onto a plane then scroll that backwards and see what happen.

Just don't see another way :( Heck it just took me a good 30 minutes to figure out how to get a custom font that isn't installed on a machine to display. I'm so slow :/
 

usea

Member
Thanks for this. Think I'm just going to get my hands dirty and try to render the text as a texture onto a plane then scroll that backwards and see what happen.

Just don't see another way :( Heck it just took me a good 30 minutes to figure out how to get a custom font that isn't installed on a machine to display. I'm so slow :/
That is the best way. An alternative would be to render the text onto a bitmap and then transform the bitmap. Basically same deal. AFAIK you can't directly skew text when drawing to give it that slanted look, you have to skew the image you drew it on.
 

Anustart

Member
That is the best way. An alternative would be to render the text onto a bitmap and then transform the bitmap. Basically same deal. AFAIK you can't directly skew text when drawing to give it that slanted look, you have to skew the image you drew it on.

Another quick question, I need all this text centered, and currently I'm doing this via GraphicsDevice.Viewport.Width / 2, GraphicsDevice.Viewport.Height / 2

Problem is this doesn't take into account the scale of my text. How Can I keep it centered while scaling?


Edit: Nevermind. Quickly realized I should try spritefont.MeasureString(string) * scale, and indeed it works.
 

usea

Member
Another quick question, I need all this text centered, and currently I'm doing this via GraphicsDevice.Viewport.Width / 2, GraphicsDevice.Viewport.Height / 2

Problem is this doesn't take into account the scale of my text. How Can I keep it centered while scaling?


Edit: Nevermind. Quickly realized I should try spritefont.MeasureString(string) * scale, and indeed it works.
http://gamedevsoc.eusa.ed.ac.uk/2010/06/horizontally-center-aligned-text-in-xna/
edit: nevermind, you got it.
 

Anustart

Member
Bah, sorry for all the questions tonight. So I'm trying to move this primitive i've covered with my text texture. I thought I could accomplish this by simply changing the y-coordinate in the Vector3. I add .01 to the float per frame, which in my mind would mean the object goes up! But nope, this simply begins to stretch my texture both up and down :/

Driving me nuts.

Code:
yPosition += .01f;

            verts[0].Position = new Vector3(-xPosition, yPosition, 0);
            verts[1].Position = new Vector3(xPosition, yPosition, 0);
            verts[2].Position = new Vector3(-xPosition, -yPosition, 0);
            verts[3].Position = new Vector3(xPosition, -yPosition, 0);

verts is my VertexPositionTexture array. Any clues?
 

Haly

One day I realized that sadness is just another word for not enough coffee.
vert[0] and vert[1] are moving up the y-axis while vert[2] and vert[3] are moving down on the y-axis so, yes, it's stretching.

Example:
If yPosition = 5 and xPosition = 2, this what you're doing to your primitive:
Code:
[0][ ]|[ ][1]
[ ][ ]|[ ][ ]
[ ][ ]|[ ][ ]
[ ][ ]|[ ][ ]
[ ][ ]|[ ][ ]
-----0,0------
[ ][ ]|[ ][ ]
[ ][ ]|[ ][ ]
[ ][ ]|[ ][ ]
[ ][ ]|[ ][ ]
[3][ ]|[ ][2]

What you need is to be able to calculate its half height and half width so you can write:

Code:
verts[0].Position = new Vector3(xPosition - halfWidth, yPosition + halfHeight, 0);
verts[1].Position = new Vector3(xPosition + halfwidth, yPosition + halfHeight, 0);
verts[2].Position = new Vector3(xPosition - halfWidth, yPosition - halfHeight, 0);
verts[3].Position = new Vector3(xPosition + halfwidth, yPosition - halfHeight, 0);
 

Anustart

Member
vert[0] and vert[1] are moving up the y-axis while vert[2] and vert[3] are moving down on the y-axis so, yes, it's stretching.

What you need is to be able to calculate its half height and half width so you can write:

Code:
verts[0].Position = new Vector3(xPosition - halfWidth, yPosition + halfHeight, 0);
verts[1].Position = new Vector3(xPosition + halfwidth, yPosition + halfHeight, 0);
verts[2].Position = new Vector3(xPosition - halfWidth, yPosition - halfHeight, 0);
verts[3].Position = new Vector3(xPosition + halfwidth, yPosition - halfHeight, 0);

Oh my, simple math fail on my part! Should have realized -yPosition was going to cause some problems! Thank you very much.

Edit : Is there a reason simply adding another Float to represent the lower Y coordinates makes the texture just not show up at all if I increment it's value?

Example, I have:

yPosition += .01f;
negY += .01f;

verts[0].Position.Y = yPosition;
verts[3].Position.Y = negY;


If I comment out negY += .01f, the texture loads, if uncommented, it won't show at all :/
 

Anustart

Member
Sorry for the bump, but I need to figure this out tonight :/

I asked on stackoverflow also, but no answer in 2 hours over there :( Any more help?
 
Hey! Just recently started programming in C after being a mostly Java and occasional C# and Python person for about 3 years. I have to use sockets for an assignment and I have some difficulties with the bind() call. (we are required to program on Linux). Making the socket and setting a socket option works, but when I want to bind it to localhost (INADDR_LOOPBACK), I get the error "Cannot assign requested address". I dug a little deeper and errno was set to EADDRNOTAVAIL (according to the manpage for bind "A nonexistent interface was requested or the requested address was not local."). This seems paradoxical because I use localhost as an address. I have no idea what's going on.

The relevant code:
Code:
struct sockaddr_in serv_addr;
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_LOOPBACK;
serv_addr.sin_port = htons(options.portno);
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
{
    bail_out(EXIT_FAILURE, "bind");
}
 

Beowulf28

Member
So I have this C# problem for class that is driving me nuts.

Who's the smartest?

We'd like to write a list of people, ordered so that no one appears in the list before anyone he or she is less smart than. The input will be a list of pairs of names, one pair per line, where the first element in a pair names a person smarter than the person named by the second element of the pair. That is, each input line looks like:
<smarter-person> <less-smart-person>

For example:
Einstein Feynmann
Feynmann Gell-Mann
Gell-Mann Thorne
Einstein Lorentz
Lorentz Planck
Hilbert Noether
Poincare Noether

(We don't mention computer scientists for obvious reasons.) There is no limit to the number of lines of input. Your output should be a list of all the distinct input names, without duplicates, one per line, ordered as described above. For example, given the input shown above, one valid output would be:
Einstein
Feynmann
Gell-Mann
Thorne
Lorentz
Planck
Hilbert
Poincare
Noether

Now I know that I should put these into a list but that's really all I got right now. I'm not sure how I can order them.
 
So I have this C# problem for class that is driving me nuts.



Now I know that I should put these into a list but that's really all I got right now. I'm not sure how I can order them.

The way to approach this one depends on what stage of your class you're at but I'm guessing you've covered some basic data structures and algorithms etc? If you picture the problem as a directed graph, where each person is connected to the next one, you are trying to topologically sort the graph. Take a look at the relevant wiki article, which should point you in the right direction.

Hint:
Try mapping out the relations in the sample you have there as a graph and think about how you can correctly weight the nodes so that they are in order. That might help or might make it worse!
 

Lkr

Member
in a menu function i declare an object of class type Image that i made.

so

Code:
Image theimg;
// menu is printed
//select function 1
function1(theimg);
//menu prints again
//select function 3
function3(theimg);

both functions are
Code:
void function1(Image & img);
void function3(Image & img);

so function1 loads information into the array in my class, and function 3 should print it. however, function 3 doesn't have any of the information. i thought passing by reference let you retain changes
 

leroidys

Member
HOLY SHIT you guys. I have an assembly assignment due on Monday at midnight, and I don't feel like I have enough time to complete it if I work non stop until then. We also have the midterm in that class on Friday. The class is a total shitshow, nobody has any clue what's going on.

The assignment is this stupid "Dr. Evil Bomb" lab that he ripped off from some other school and did not explain at all beyond giving us a link to a one page gdb tutorial. I got past the first phase because it was just a string, but now have basically no clue what I'm looking for.

This post is 50% just venting and 50% begging for help. Does anyone have any experience with this sort of thing? Any great resources for c functions or debugging with gdb/ddd?
 

Slavik81

Member
in a menu function i declare an object of class type Image that i made.

so

Code:
Image theimg;
// menu is printed
//select function 1
function1(theimg);
//menu prints again
//select function 3
function3(theimg);

both functions are
Code:
void function1(Image & img);
void function3(Image & img);

so function1 loads information into the array in my class, and function 3 should print it. however, function 3 doesn't have any of the information. i thought passing by reference let you retain changes

It does. The problem must be in code you didn't include in this post.

HOLY SHIT you guys. I have an assembly assignment due on Monday at midnight, and I don't feel like I have enough time to complete it if I work non stop until then. We also have the midterm in that class on Friday. The class is a total shitshow, nobody has any clue what's going on.

The assignment is this stupid "Dr. Evil Bomb" lab that he ripped off from some other school and did not explain at all beyond giving us a link to a one page gdb tutorial. I got past the first phase because it was just a string, but now have basically no clue what I'm looking for.

This post is 50% just venting and 50% begging for help. Does anyone have any experience with this sort of thing? Any great resources for c functions or debugging with gdb/ddd?

bt = print stack trace

That's about the extent of my gdb knowledge. Good luck.
 
HOLY SHIT you guys. I have an assembly assignment due on Monday at midnight, and I don't feel like I have enough time to complete it if I work non stop until then. We also have the midterm in that class on Friday. The class is a total shitshow, nobody has any clue what's going on.

The assignment is this stupid "Dr. Evil Bomb" lab that he ripped off from some other school and did not explain at all beyond giving us a link to a one page gdb tutorial. I got past the first phase because it was just a string, but now have basically no clue what I'm looking for.

This post is 50% just venting and 50% begging for help. Does anyone have any experience with this sort of thing? Any great resources for c functions or debugging with gdb/ddd?

It's really hard to give any kind of help without seeing code you're dealing with directly, and my assembly is a bit rusty for anything that isn't 6502, but for the Dr. Evil binary bomb type thingies the key is usually to try to decipher some kind of pattern in the input. See how many times it's trying to read from stdin, which should give you some clue as to what it's looking for and then see if you can follow what it's trying to compare the input to with gdb. These assignments can be tough but if you are really methodical with stepping through every instruction in debugger it will bring you somewhere.
 
This post is 50% just venting and 50% begging for help. Does anyone have any experience with this sort of thing? Any great resources for c functions or debugging with gdb/ddd?

disp/i $pc

FTW. Otherwise some useful starter tips here:

http://www-numi.fnal.gov/offline_so...xt/WebDocs/Companion/intro_talks/gdb/gdb.html

In general gdb is pretty straightforward, and it's scriptable. It's more important to understand the idiomatic way of reading and debugging assembly, though. Until you can grasp the jist of a sequence you're just going to have to annotate everything as you step through it. I posted some ABI disassembly for a virtual instruction set up above if you're curious. The last few years I've spent a lot of time in SPU disassembly for PS3 so this is my bread and butter.
 

Lkr

Member
It does. The problem must be in code you didn't include in this post.
so this assignment is due in 3 hours. i saw someone on facebook say to someone else that one of their other functions we needed to use might be the problem. i thought mine was fine, but i went ahead and played around with it. sure enough it works fine now lol
 
Hey guys, I had a question that may or may not have a solution.

Essentialy, I'm making a mini lexer in Python, taking in strings of input, getting variable names from it and identifying whether they're an identifier, keyword, int, or float. That part of the project was simple, but then we have list out each identifier and list every line that they appear on.

I have code that stores every identifier in a dictionary and stores the line numbers in a list, but for some reason the line numbers are assigned to my lists randomly every time I run it. This is the same input file every time, so it's baffling me how this can even happen unless there's some type of weird interleaving going on. Any help would be appreciated.

For example, given
Line # Token Type Token
00 keyword int
01 identifier number18
02 Error No token found
03 identifier x1
04 integer 387
05 float 456.98
06 identifier number18
07 Error 13. Incorrect float format
08 identifier symbol
09 integer 49
10 identifier number18
11 identifier symbol
12 keyword for

the results of the 1st run were:
symbol [1, 6, 11]
number18 [3, 10]
x1 [8]

and for the 3rd run were:
number18 [1, 6, 10]
x1 [3]
symbol [8, 11]

Where could this randomness possibly come from?

Edit: I solved it using another approach, it had something to do with my use of the dictionary function that was causing the randomness. It's still a really strange bug that makes no sense though haha.
 

leroidys

Member
disp/i $pc

FTW. Otherwise some useful starter tips here:

http://www-numi.fnal.gov/offline_so...xt/WebDocs/Companion/intro_talks/gdb/gdb.html

In general gdb is pretty straightforward, and it's scriptable. It's more important to understand the idiomatic way of reading and debugging assembly, though. Until you can grasp the jist of a sequence you're just going to have to annotate everything as you step through it. I posted some ABI disassembly for a virtual instruction set up above if you're curious. The last few years I've spent a lot of time in SPU disassembly for PS3 so this is my bread and butter.

Thank you!

If anyone wants to throw me a bone, here's the function I'm currently in:

Code:
Dump of assembler code for function phase_2:
   0x0000000000400f6c <+0>:     push   %rbp
   0x0000000000400f6d <+1>:     push   %rbx
   0x0000000000400f6e <+2>:     sub    $0x28,%rsp
   0x0000000000400f72 <+6>:     mov    %rsp,%rsi
   0x0000000000400f75 <+9>:     callq  0x401875 <read_six_numbers>
   0x0000000000400f7a <+14>:    cmpl   $0x0,(%rsp)
   0x0000000000400f7e <+18>:    jns    0x400f85 <phase_2+25>
   0x0000000000400f80 <+20>:    callq  0x401714 <explode_bomb>
   0x0000000000400f85 <+25>:    lea    0x4(%rsp),%rbx
   0x0000000000400f8a <+30>:    mov    $0x1,%ebp
   0x0000000000400f8f <+35>:    mov    %ebp,%eax
   0x0000000000400f91 <+37>:    add    -0x4(%rbx),%eax
   0x0000000000400f94 <+40>:    cmp    %eax,(%rbx)
   0x0000000000400f96 <+42>:    je     0x400f9d <phase_2+49>
   0x0000000000400f98 <+44>:    callq  0x401714 <explode_bomb>
   0x0000000000400f9d <+49>:    add    $0x1,%ebp
   0x0000000000400fa0 <+52>:    add    $0x4,%rbx
   0x0000000000400fa4 <+56>:    cmp    $0x6,%ebp
   0x0000000000400fa7 <+59>:    jne    0x400f8f <phase_2+35>
   0x0000000000400fa9 <+61>:    add    $0x28,%rsp
   0x0000000000400fad <+65>:    pop    %rbx
   0x0000000000400fae <+66>:    pop    %rbp
   0x0000000000400faf <+67>:    retq   
End of assembler dump.

then the <read_six_numbers function is:

Code:
Dump of assembler code for function read_six_numbers:
   0x0000000000401875 <+0>:     sub    $0x18,%rsp
   0x0000000000401879 <+4>:     mov    %rsi,%rdx
   0x000000000040187c <+7>:     lea    0x4(%rsi),%rcx
   0x0000000000401880 <+11>:    lea    0x14(%rsi),%rax
   0x0000000000401884 <+15>:    mov    %rax,0x8(%rsp)
   0x0000000000401889 <+20>:    lea    0x10(%rsi),%rax
   0x000000000040188d <+24>:    mov    %rax,(%rsp)
   0x0000000000401891 <+28>:    lea    0xc(%rsi),%r9
   0x0000000000401895 <+32>:    lea    0x8(%rsi),%r8
   0x0000000000401899 <+36>:    mov    $0x402b45,%esi
   0x000000000040189e <+41>:    mov    $0x0,%eax
   0x00000000004018a3 <+46>:    callq  0x400c80 <__isoc99_sscanf@plt>
   0x00000000004018a8 <+51>:    cmp    $0x5,%eax
   0x00000000004018ab <+54>:    jg     0x4018b2 <read_six_numbers+61>
   0x00000000004018ad <+56>:    callq  0x401714 <explode_bomb>
   0x00000000004018b2 <+61>:    add    $0x18,%rsp
   0x00000000004018b6 <+65>:    retq   
End of assembler dump.

I really have almost no clue what is going on. I just see a bunch of addresses being swapped between registers, and have no idea how to suss out what numbers are actually meaningful.
 

CrunchyB

Member
This post is 50% just venting and 50% begging for help. Does anyone have any experience with this sort of thing? Any great resources for c functions or debugging with gdb/ddd?

Lots of programming classes have insane assignments :) This is somewhat normal.

Don't despair, but do put in a decent amount of time. Visual Studio Express is easier to use than GDB, you might want to try that.

And you can always post the assignments in this thread, there's probably someone who can help you. If it's an introductory course I can't imagine it being out of our league.

If anyone wants to throw me a bone, here's the function I'm currently in:

Wait, this is what all you've got to work with? Damn! Who in this age reads pure ASM? I can sorta see what's going on, but I'll need to know what the problem is to be of any help.
 

leroidys

Member
Lots of programming classes have insane assignments :) This is somewhat normal.

Don't despair, but do put in a decent amount of time. Visual Studio Express is easier to use than GDB, you might want to try that.

And you can always post the assignments in this thread, there's probably someone who can help you. If it's an introductory course I can't imagine it being out of our league.



Wait, this is what all you've got to work with? Damn! Who in this age reads pure ASM? I can sorta see what's going on, but I'll need to know what the problem is to be of any help.

It's a junior/sophomore level course, but we have never had any instruction in assembly language. We can only run this "bomb" on a few machines at school, or SSH into them, and they're all linux so I don't think I'll be able to use VS though I appreciate the suggestion :(

The premise is that there are 6 "stages". The first stage I completed by inputting a string, which I found by looking at where it stores my input and the address it compares it against, and then having it just dump all those characters.

Now in the second stage, it seems like its looking for 6 ints and building up the number it is looking for somehow.

Those textdumps were just the more relevant looking parts from DDD. The "bomb" is a c program where all the functions are hidden, so we have to dissasemble it and look at the machine code while we step through it :(
 

Jadedx

Banned
Can someone help me with this? I've been working on this for hours and I can't figure why it is not working.

The problems I am having is:
1. It does not show the symbols on the screen
2. The numbers don't seem to be random
3. The do while look does not work properly for some reason

Code:
#include <cstdlib>
#include <iostream>
#include <string>
#include <cmath>
#include <iomanip>
int spin(int,int, int);
void playGame();
float insertMoney(float);
int spin(int, int, int);
int showSpin(int, int, int);
int showSymbol(int);
int getMatches(int, int, int);
float calcWinnings(int, float);
char wantToPlayAgain(char);

using namespace std;

const int CHERRIES = 1;
const int ORANGES = 2;
const int PLUMS  = 3;
const int BELLS  = 4;
const int MELONS = 5;
const int BARS = 6;



int main()
{
    // The again variable is used to determine whether the
    // user wants to continue playing the game.
    char again;
    float money;
    
    // Start playing…
    do{
        // Pressing a key spins the machine.
        cout << "Press Enter to spin… " << endl;
        cin.get();
        
        

        // Simulate the spin.
         playGame();

        // Does the user want to play again?
        wantToPlayAgain(again);
      }while (wantToPlayAgain(again) == 'Y');

    
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

void playGame()
{
   int symbol1, symbol2, symbol3, matches;
   float money, winnings;      
   money = 0;
    
    
   money = insertMoney(money);    
   spin(symbol1,symbol2,symbol3);
   showSpin(symbol1,symbol2,symbol3);
   matches = getMatches(symbol1,symbol2,symbol3);
   winnings = calcWinnings(matches, money);
   
   cout << "You Won $" << setprecision(2) << fixed << winnings << endl; 
   
}


float insertMoney(float money)
{
    cout << "How much money do you want to insert?";
    cin >> money;
    return money;
}

int spin(int symbol1, int symbol2, int symbol3)
{         
    symbol1 = rand() % BARS + CHERRIES;
    symbol2 = rand() % BARS + CHERRIES;
    symbol3 = rand() % BARS + CHERRIES;
    
}

// The showSymbol module displays a slot symbol as a word.
int showSpin(int symbol1, int symbol2, int symbol3)
{     
     showSymbol(symbol1);
     showSymbol(symbol2);
     showSymbol(symbol3);
}

// The showSymbol module displays a slot symbol as a word.
int showSymbol(int symbol)
{
     switch (symbol)
     {
            
            case 1:
                 cout << "Cherries\n";
                 break;
            case 2:
                 cout << "Oranges\n";
                 break;
            case 3:
                 cout << "Plums\n";
                 break;
            case 4:
                 cout << "Bells\n";
                 break;
            case 5:
                 cout << "Melons\n";
                 break;
            case 6:
                 cout << "Bars\n";
                 break;            
     }    
     
     
}

// The getMatches function accepts arguments for the three
// slot symbols and returns the number of them that match.
int getMatches(int symbol1, int symbol2, int symbol3)
{
     int matches;

    // Do all three symbols match?
    if (symbol1 == symbol2 && symbol1 == symbol3) 
    {
        matches = 3;
    }
    // Else, do two of the symbols match?
    else if (symbol1 == symbol2 || symbol1 == symbol3 || symbol2 == symbol3)
    
        matches = 2;

    // Otherwise, there are no matches.
    else {
        matches = 0;
         }

    // Return the number of matches.
   return matches;
}

// The calcWinnings function accepts the number of matches and
// the amount of money inserted as arguments, and returns the
// amount of money won for this spin.
float calcWinnings(int matches, float money)
{
    return matches * money;
}

// The wantToPlayAgain module asks the user if he or she wants to
// play again and stores the input as "Y" or "N" in the reference
// parameter.
char wantToPlayAgain(char again)
{
    // Ask the user…
    cout << "Do you want to play again?\n";
    cin >> again;

    // Validate the input.
   while (again != 'Y' && again != 'N')
   {
        cout << "Enter either Y or N.\n";
        cin >> again;
   }
}
 

mike23

Member
Can someone help me with this? I've been working on this for hours and I can't figure why it is not working.

The problems I am having is:
1. It does not show the symbols on the screen
2. The numbers don't seem to be random
3. The do while look does not work properly for some reason

Code:
        // Does the user want to play again?
        wantToPlayAgain(again);
      }while (wantToPlayAgain(again) == 'Y');

You're calling wantToPlayAgain twice here. Ask once in the while condition.
Code:
void playGame()
{
   int symbol1, symbol2, symbol3, matches;
   float money, winnings;      
   money = 0;
    
    
   money = insertMoney(money);    
   spin(symbol1,symbol2,symbol3);
   showSpin(symbol1,symbol2,symbol3);
   matches = getMatches(symbol1,symbol2,symbol3);
   winnings = calcWinnings(matches, money);
   
   cout << "You Won $" << setprecision(2) << fixed << winnings << endl; 
   
}


float insertMoney(float money)
{
    cout << "How much money do you want to insert?";
    cin >> money;
    return money;
}

int spin(int symbol1, int symbol2, int symbol3)
{         
    symbol1 = rand() % BARS + CHERRIES;
    symbol2 = rand() % BARS + CHERRIES;
    symbol3 = rand() % BARS + CHERRIES;
    
}

// The showSymbol module displays a slot symbol as a word.
int showSpin(int symbol1, int symbol2, int symbol3)
{     
     showSymbol(symbol1);
     showSymbol(symbol2);
     showSymbol(symbol3);
}

// The showSymbol module displays a slot symbol as a word.
int showSymbol(int symbol)
{
     switch (symbol)
     {
            
            case 1:
                 cout << "Cherries\n";
                 break;
            case 2:
                 cout << "Oranges\n";
                 break;
            case 3:
                 cout << "Plums\n";
                 break;
            case 4:
                 cout << "Bells\n";
                 break;
            case 5:
                 cout << "Melons\n";
                 break;
            case 6:
                 cout << "Bars\n";
                 break;            
     }    
     
     
}

// The getMatches function accepts arguments for the three
// slot symbols and returns the number of them that match.
int getMatches(int symbol1, int symbol2, int symbol3)
{
     int matches;

    // Do all three symbols match?
    if (symbol1 == symbol2 && symbol1 == symbol3) 
    {
        matches = 3;
    }
    // Else, do two of the symbols match?
    else if (symbol1 == symbol2 || symbol1 == symbol3 || symbol2 == symbol3)
    
        matches = 2;

    // Otherwise, there are no matches.
    else {
        matches = 0;
         }

    // Return the number of matches.
   return matches;
}

// The calcWinnings function accepts the number of matches and
// the amount of money inserted as arguments, and returns the
// amount of money won for this spin.
float calcWinnings(int matches, float money)
{
    return matches * money;
}

// The wantToPlayAgain module asks the user if he or she wants to
// play again and stores the input as "Y" or "N" in the reference
// parameter.
char wantToPlayAgain(char again)
{
    // Ask the user&#8230;
    cout << "Do you want to play again?\n";
    cin >> again;

    // Validate the input.
   while (again != 'Y' && again != 'N')
   {
        cout << "Enter either Y or N.\n";
        cin >> again;
   }
}

The other big thing is that you mention reference parameters, but none of your parameters are actually references.

In wantToPlayAgain, for example. You don't return any char, and any assignment to the 'again' parameter will be lost because it is passed by value as it is a value type. It's the same for spin. In insertMoney, you don't need the money parameter because you return the value. In general, if your method only needs to return one value, just return it, don't use a reference parameter (although in this case, it isn't actually a reference parameter).

Oh, and showSymbol has a return type of int even though you never return a value. Should probably be void.

The reason the symbols aren't being printed is because the variables in the playGame method get initialized to random values, whatever happens to be in memory. Then the showSymbol method never hits any of the cases. Add a default case that prints the value of the symbol and you'll see what is happening.
 

Jadedx

Banned
You're calling wantToPlayAgain twice here. Ask once in the while condition.


The other big thing is that you mention reference parameters, but none of your parameters are actually references.

In wantToPlayAgain, for example. You don't return any char, and any assignment to the 'again' parameter will be lost because it is passed by value as it is a value type. It's the same for spin. In insertMoney, you don't need the money parameter because you return the value. In general, if your method only needs to return one value, just return it, don't use a reference parameter (although in this case, it isn't actually a reference parameter).

Oh, and showSymbol has a return type of int even though you never return a value. Should probably be void.

The reason the symbols aren't being printed is because the variables in the playGame method get initialized to random values, whatever happens to be in memory. Then the showSymbol method never hits any of the cases. Add a default case that prints the value of the symbol and you'll see what is happening.

Okay, I'm a little confused I can't return more than 1 value for each method so is there anyway for me to return all 3 values in spin? I have also changed it so the values should be between the ranges of 1 and 6, but when I print it out it usually gives me a very large number and 2 zeros.
 

mike23

Member
Okay, I'm a little confused I can't return more than 1 value for each method so is there anyway for me to return all 3 values in spin? I have also changed it so the values should be between the ranges of 1 and 6, but when I print it out it usually gives me a very large number and 2 zeros.

http://www.learncpp.com/cpp-tutorial/73-passing-arguments-by-reference/

You basically want to pass the three spin parameters by reference.
Code:
void spin(int &symbol1, int &symbol2, int &symbol3)
{         
    symbol1 = rand() % BARS + CHERRIES;
    symbol2 = rand() % BARS + CHERRIES;
    symbol3 = rand() % BARS + CHERRIES;
}

This should do the trick.
 

Jadedx

Banned
http://www.learncpp.com/cpp-tutorial/73-passing-arguments-by-reference/

You basically want to pass the three spin parameters by reference.
Code:
void spin(int &symbol1, int &symbol2, int &symbol3)
{         
    symbol1 = rand() % BARS + CHERRIES;
    symbol2 = rand() % BARS + CHERRIES;
    symbol3 = rand() % BARS + CHERRIES;
}

This should do the trick.

That gave me a linker error but I changed it to this:

Code:
void playGame()
{
   int symbol1, symbol2, symbol3, matches;
   float money, winnings;      
   money = 0;
    
    
   money = insertMoney(money);    
   symbol1 = spin(symbol1);
   symbol2 = spin(symbol2);
   symbol3 = spin(symbol3);
   showSpin(symbol1,symbol2,symbol3);
   matches = getMatches(symbol1,symbol2,symbol3);
   winnings = calcWinnings(matches, money);
   
   cout << "You Won $" << setprecision(2) << fixed << winnings << endl << endl; 
   
}

Code:
int spin(int symbol1)
{         
    symbol1 = rand() % BARS + CHERRIES;
    return symbol1;
}

And now it works!!!

Thank you so much for all your help Mike, if you weren't a monkey I'd kiss you.
 
Top Bottom