• What do you need help with? Version 5
    5,752 replies, posted
[cpp]for(I=0; I <= (sizeof(x)/4)-2; I++) { for(J=I+1; J <= (sizeof(x)/4)-1; J++) { if(x[I] > x [J]) { //temporary variables for array values int tempI = x[I]; int tempJ = x[J]; //... just a hint :) } } }[/cpp] I've started the process, now can you deduce what you need to do? I can tell you in words if you want, but I want you to learn something :) [editline]16th November 2012[/editline] Also, you just need -2 and -1 because it's already accounted for in the pseudocode.
I changed the -2 and -1. I can see how a temp variable might help, gonna ponder on this for a while and see if I can work it out. [editline]16th November 2012[/editline] [cpp] for (I = 0; (sizeof(x)/4)-2; I++) { for (J = (I + 1); (sizeof(x)/4)-1; J++) { if (x[I] > x[J]) { int tempI = x[I]; int tempJ = x[J]; x[I] == tempJ; x[J] == tempI; } [/cpp] Does this make any sense? If it does, I'm not sure how to output the swapped array.
[QUOTE=177cheese;38469099]I changed the -2 and -1. I can see how a temp variable might help, gonna ponder on this for a while and see if I can work it out. [editline]16th November 2012[/editline] [cpp] for (I = 0; (sizeof(x)/4)-2; I++) { for (J = (I + 1); (sizeof(x)/4)-1; J++) { if (x[I] > x[J]) { int tempI = x[I]; int tempJ = x[J]; x[I] == tempJ; x[J] == tempI; } [/cpp] Does this make any sense? If it does, I'm not sure how to output the swapped array.[/QUOTE] Just output the array like you did before. See, the array has changed.
Hmm, something's not working right, it's crashing every time I try to run it. [editline]16th November 2012[/editline] doing some debugging, might get it to work in a few minutes [editline]16th November 2012[/editline] It's no longer crashing, but it still isn't displaying the sorted array. [cpp] #include <iostream> using namespace std; int main () { int x[] = { 28, 87, -3, 45, 19 }; int I = 0; int J = 1; int i = 0; cout << "The unsorted array is: " << endl; for (i = 0; i <= (sizeof(x)/4) - 1; i++) //This prints the array before being sorted { cout << x[i] << endl; } cout << "Once sorted, the array is: " << endl; int tempI = x[I]; int tempJ = x[J]; for (I = 0; (sizeof(x)/4)-2; I++) //This sorts the array { for (J = (I + 1); (sizeof(x)/4)-1; J++) { if (x[I] > x[J]) { x[I] == tempJ; x[J] == tempI; } } } for (i = 0; i <= (sizeof(x)/4) - 1; i++) { cout << x[i] << endl; } system("pause"); return 0; } [/cpp] It makes sense in my head, but I don't think in C++ just yet.
These lines: [cpp] int tempI = x[I]; int tempJ = x[J];[/cpp] Need to be in the if statement (I think, just do this to make your code easier to read.) [editline]16th November 2012[/editline] Also why the system("pause")?
When they're in the if statement, it causes the program to crash. Moving them out fixed it but it doesn't print the sorted array still. [editline]16th November 2012[/editline] My instructor says to include it, otherwise DevCPP just runs the program and closes. I heard there are less resource-draining ways to accomplish the same thing but I'm just gonna stick with what he says for now haha.
Oh, I got it. The reason is because in the second argument of your for statement in the sorting algorithm, you need to specify that the loop stops when I or J is less than or equal to than the value you presented. Every for loop needs to have this. [editline]16th November 2012[/editline] e.g. [cpp] for (int I = 0; I <= (sizeof(x)/4)-2; I++) //This sorts the array { for (int J = (I + 1); J <= (sizeof(x)/4)-1; J++) { if (x[I] > x[J]) { int tempI = x[I]; int tempJ = x[J]; x[I] = tempJ; x[J] = tempI; } } }[/cpp] When I put that in, the code works fine. [editline]16th November 2012[/editline] Here's the final code, cleaned up. Something you can learn from this is that you can declare integers in the for loop, instead of declaring them at the beginning. [cpp]#include <iostream> using namespace std; int main () { int x[] = { 28, 87, -3, 45, 19 }; cout << "The unsorted array is: " << endl; for (int i = 0; i <= (sizeof(x)/4) - 1; i++) //This prints the array before being sorted { cout << x[i] << endl; } cout << "Once sorted, the array is: " << endl; for (int I = 0; I <= (sizeof(x)/4)-2; I++) //This sorts the array { for (int J = (I + 1); J <= (sizeof(x)/4)-1; J++) { if (x[I] > x[J]) { int tempI = x[I]; int tempJ = x[J]; x[I] = tempJ; x[J] = tempI; } } } for (int i = 0; i <= (sizeof(x)/4) - 1; i++) { cout << x[i] << endl; } system("pause"); return 0; }[/cpp]
Oops, that was a silly mistake. Nice catch haha. It doesn't seem to be sorting when I output it, if it's working for you the logic on my output is probably messed up, gonna see if I can find the error. [editline]16th November 2012[/editline] Oh sweet you even cleaned it up! Thanks a ton for the assistance! And thanks for not giving me the answer directly, I learn a lot better that way.
Okay, I think I have a very special problem here: I want to detect whenever a SD-Card get plugged in into a Cardreader. I know the WndProc-Event and how to filter it for WM_DEVICECHANGED Messages, but the Problem is: The Cardreader (Multi-Cardreader in particular) creates 4 empty drives. So when I plug in the card, Windows recognizes it, but no Event is triggered. I found something about an undocumented "WM_MEDIA_CHANGED" MessageID (WM_USER + 666 = 1690?) but this doesn't work. Any ideas? (I could make a timer and let it check every 2 Seconds if a new removable and ReadyForUse-Device got add, but this is unperformant and feels unclean)
In python 2.5, how do I check an input by every digit to print out an output? let's say I enter 123456789 and I want to find the 2nd digit of that output, how can I do this? And if I try to get a digit beyond the left-most digit in the number, then 0 (zero) is returned. For example, get_digit(123456789, 9) returns 0. How can I avoid this?
[code]def get_digit(i, p): s = str(i) # Just convert the integer to a string L = len(s) if ((p < 0) | (p >= L)): return 0 # Out of bounds return int(s[L - 1 - p]) # Return the p-th digit from the right[/code] Yeah okay it's ugly and converts the entire number and does some extra encoding to convert it to an ASCII/UTF string (whichever Python uses) But who cares, it's Python
[QUOTE=ThePuska;38477052][code]def get_digit(i, p): s = str(i) # Just convert the integer to a string L = len(s) if ((p < 0) | (p >= L)): return 0 # Out of bounds return int(s[L - 1 - p]) # Return the p-th digit from the right[/code] Yeah okay it's ugly and converts the entire number and does some extra encoding to convert it to an ASCII/UTF string (whichever Python uses) But who cares, it's Python[/QUOTE] Well it does work, anyone else have a more efficient one?
[QUOTE=ThePuska;38477052][code]def get_digit(i, p): s = str(i) # Just convert the integer to a string L = len(s) if ((p < 0) | (p >= L)): return 0 # Out of bounds return int(s[L - 1 - p]) # Return the p-th digit from the right[/code] Yeah okay it's ugly and converts the entire number and does some extra encoding to convert it to an ASCII/UTF string (whichever Python uses) But who cares, it's Python[/QUOTE] You return a 0 on error (which is a valid return value too)? Why not throw an exception or something?
I'm not too familiar with python, so here's my go in Lua without using strings. [lua] function getdigit(n,p) n = math.floor(n / math.pow(10, p)) / 10 n = n - math.floor(n) return math.floor(n * 10) end [/lua] [IMG]http://i.imgur.com/4vsPi.png[/IMG] It mathematically returns 0 if using an invalid decimal place. [editline]16th November 2012[/editline] It also seems to work with decimals, but it doesn't like the second decimal place for some reason :v: [img]http://i.imgur.com/r5mfn.png[/img] I wouldn't trust it with non-integers.
[QUOTE=ArgvCompany;38477713]You return a 0 on error (which is a valid return value too)? Why not throw an exception or something?[/QUOTE] The specs said it should return 0
[QUOTE=ThePuska;38478679]The specs said it should return 0[/QUOTE] Oh, missed that. Sorry.
[QUOTE=ThePuska;38478679]The specs said it should return 0[/QUOTE] Here's the full spec get_digit(number, position) This function accepts two integer parameters. The first is a number to get a digit from, and the second is the position (from the right) of the required digit. That is, the one's digit of the number is position 0, the ten's digit is position 1, the hundreds digit is position 2, and so forth. The function returns the digit (as an integer) at the specified position. For example, get_digit(123456789, 3) returns 6. Note: If you try to get a digit beyond the left-most digit in the number, then 0 (zero) is returned. For example, get_digit(123456789, 9) returns 0.
I'm surprised nobody's mentioned this: [code]get_digit(number, digit): front_chopped = number % (10 ^ (digit - 1)) back_chopped = front_chopped % 10 return back_chopped[/code] It's off the top of my head, but it returns a number. Also replace ^ with pow or something. Edit: Oh somebody did. Ugh.
Suggest me one C# game engine for graphics and physics.
[QUOTE=garychencool;38479287]Here's the full spec get_digit(number, position) This function accepts two integer parameters. The first is a number to get a digit from, and the second is the position (from the right) of the required digit. That is, the one's digit of the number is position 0, the ten's digit is position 1, the hundreds digit is position 2, and so forth. The function returns the digit (as an integer) at the specified position. For example, get_digit(123456789, 3) returns 6. Note: If you try to get a digit beyond the left-most digit in the number, then 0 (zero) is returned. For example, get_digit(123456789, 9) returns 0.[/QUOTE] This fits that spec completely: [lua] function get_digit(number, position) return math.floor(number / 10^position) % 10 end [/lua]
[QUOTE=HeatPipe;38486163]Suggest me one C# game engine for graphics and physics.[/QUOTE] XNA + Jitter. If you don't mind having a really low-level graphics library, try OpenTK + Jitter (It's straight OpenGL bindings)
[QUOTE=robmaister12;38489844]XNA + Jitter. If you don't mind having a really low-level graphics library, try OpenTK + Jitter (It's straight OpenGL bindings)[/QUOTE] Or MonoGame for XNA on Linux or Mac.
How would I go about reversing a bool through a keypress? Say I want to increment r when the left key is pressed, but I want to decrement it when it's been pressed a second time. Here's my code so far (GLFW and C++) [cpp] if(glfwGetKey(GLFW_KEY_LEFT)) { if(keytoggle && GLFW_PRESS) r += 0.01f * time / 100; if(keytoggle && GLFW_RELEASE) keytoggle = true; if(!keytoggle && GLFW_PRESS) r -= -0.01f * time / 100; if(!keytoggle && GLFW_RELEASE) keytoggle = false; } [/cpp]
[cpp] if(glfwGetKey(GLFW_KEY_LEFT)) { if(GLFW_RELEASE) keytoggle = !keytoggle; if(GLFW_PRESS) { if(keytoggle) r += 0.01f * time / 100; else r -= -0.01f * time / 100; } } [/cpp]
Doesn't work. I'm also declaring and defining keytoggle outside of the running loop, so it's not that.
Surely you should be comparing GLFW_PRESS and GLFW_RELEASE to something?
[QUOTE=Dr Magnusson;38491673]Surely you should be comparing GLFW_PRESS and GLFW_RELEASE to something?[/QUOTE] Those are both in the if(glfwGetKey(GLFW_KEY_LEFT), so they are already.
[QUOTE=Meatpuppet;38492092]Those are both in the if(glfwGetKey(GLFW_KEY_LEFT), so they are already.[/QUOTE] Either I am missing something obvious, or you're a little confused about how if statements work. Could you post the entire function? GLFW_PRESS and GLFW_RELEASE are both members of an enumeration table, and so their value is constant. Evaluating your if statements will always render the same result. Furthermore, glfwGetKey returns the state of the input key identifier, so comparing the return value of this statement to GLFW_PRESS/RELEASE is what you [I]meant[/I] to do. Something like this: [cpp] if (glfwGetKey(GLFW_KEY_LEFT) == GLFW_PRESS) ... [/cpp]
In Java, how do I reference/use data from an instantiated object in my driver class within one of my support classes? I made a reference in one of my methods in the support class to an instantiated object in my driver class, but it "can't resolve" it.
[QUOTE=Dr Magnusson;38492559]Either I am missing something obvious, or you're a little confused about how if statements work. Could you post the entire function? GLFW_PRESS and GLFW_RELEASE are both members of an enumeration table, and so their value is constant. Evaluating your if statements will always render the same result. Furthermore, glfwGetKey returns the state of the input key identifier, so comparing the return value of this statement to GLFW_PRESS/RELEASE is what you [I]meant[/I] to do. Something like this: [cpp] if (glfwGetKey(GLFW_KEY_LEFT) == GLFW_PRESS) ... [/cpp][/QUOTE] That's what I was doing earlier. I just had trouble finding a place to toggle whether the key was pressed before, so I put it all in one loop. Thanks for telling me I can't do that.
Sorry, you need to Log In to post a reply to this thread.