• What do you need help with? V. 3.0
    4,884 replies, posted
What's so hard about it?
[QUOTE=Edvinas;32976936]I've been having a lot of trouble with this assignment which I have to finish until Friday: Anyone got any suggestions?[/QUOTE] Are you familiar with logarithms? The amount of digits in a number x is log(x) rounded up, with the base of the logarithm being the same as the number system. I.e. in the decimal system: ceiling(log10(x)). Any standard math library should contain the decimal logarithm and the ceiling function.
[QUOTE=Jookia;32977001]What's so hard about it?[/QUOTE] My programing knowledge is very low as I've just started learning C++ this month. [QUOTE=ThePuska;32977024]Are you familiar with logarithms? The amount of digits in a number x is log(x) rounded down, with the base of the logarithm being the same as the number system. I.e. in the decimal system: floor(log10(x)). Any standard math library should contain the decimal logarithm and the floor function.[/QUOTE] I'm familiar with: if, for, while loops and functions, but I'm willing to learn anything else necessary.
Write a function that counts the digits in a number. It's easiest to do with logarithms. Call that function for every room number and add the digit counts together.
[QUOTE=ThePuska;32977202]Write a function that counts the digits in a number. It's easiest to do with logarithms. Call that function for every room number and add the digit counts together.[/QUOTE] I'm not sure I understand the second part, I count the digit count with this function: [code]int dig(int x) { int X = 0; while (x != 0) { x = x / 10; X ++; } return X; }[/code] Anyway, I'm going to get some sleep, I'll get back to this tomorrow, thanks for the help.
[QUOTE=mechanarchy;32967678]You're using "System.out.print". I'd suggest System.out.println which will print each thing on a new line instead so it will become more clear. The actual issue with your code, however, is that you're not resetting the value of computerCards each time to the empty string (so = ""). Because you aren't doing this, each time the while block operates it simply adds more stuff to the end of your string. So first you'll have "Apple" then "AppleOrange" then "AppleOrangePotato" instead of "Apple", "Orange", "Potato". If you added [code] computerCards = ""; [/code] after the "count ++;" line I think this problem should be resolved.[/QUOTE] Ah, but the output is intended to show the three cards chosen by the computer, its meant to amend the string "computerCards" each time the loop runs. I think that having the random outside the loop would be worse wouldnt it? I want three different cards, if the random is outside the loop then wont the same seed be used for each card? whereas having it inside means a new seed is picked for each card...that is, if I understand random number generation in Java correctly, which I very well may not. To clarify, the output should look like: [CODE]Card of Suit Card of Suit Card of Suit[/CODE] All together on one line
[QUOTE=fylth;32980054]Ah, but the output is intended to show the three cards chosen by the computer, its meant to amend the string "computerCards" each time the loop runs. I think that having the random outside the loop would be worse wouldnt it? I want three different cards, if the random is outside the loop then wont the same seed be used for each card? whereas having it inside means a new seed is picked for each card...that is, if I understand random number generation in Java correctly, which I very well may not. To clarify, the output should look like: [CODE]Card of Suit Card of Suit Card of Suit[/CODE] All together on one line[/QUOTE] Well in that case you'd just need to add some spaces to the end or a tab character. So just say computerCards = computerCards + "Card of Suit" + "\t"; The "\t" is a tab character, and it's equivalent to putting a few spaces in. Using this method, you will probably end up with a spare tab character on the end of your string, so you'd need to trim that off later if it's important that it's not there.
[QUOTE=Edvinas;32976936]I've been having a lot of trouble with this assignment which I have to finish until Friday: There are from 1 to x offices in the building, a tablet is nailed on each office door with it's number, [i]each number must have a separate tablet[/i]. So, if there are 10 offices in the building, then 11 tablets will be required, if 100 then 192. Write a program which calculates how many tablets will be required for x offices. Anyone got any suggestions?[/QUOTE] I think you mean 'digit' rather than 'number'. Doesn't make sense otherwise.
[QUOTE=WTF Nuke;32964827]Quick maths question, how would I know if 2 entities are going to collide at the bottom or the top if I know their Y velocities and positions?[/QUOTE] Repost because bottom of page.
Could someone explain to me how to set the size of the LOVE engine window? Apparently using their conf.lua file suggestion on their wiki didn't work for me (it says this method works with only 0.8.0 and above and there's not a 0.8.0 that I can see). --Fixed, solved my own problem-- Edit: What I did was I accidently put the conf.lua in the LOVE folder instead of placing it in the actual game folder.
[QUOTE=WitheredGryphon;32984998]Could someone explain to me how to set the size of the LOVE engine window? Apparently using their conf.lua file suggestion on their wiki didn't work for me (it says this method works with only 0.8.0 and above and there's not a 0.8.0 that I can see). --Fixed, solved my own problem--[/QUOTE] For anyone else who searches for this later: [url]http://love2d.org/wiki/love.graphics.setMode[/url] [del]If you use that function in love.load it'll set up the default 800x600 pixel window first and then reset it to this one, causing the window to flicker out of existence momentarily and look kinda dodgy. You can fix this, but you'd need to write your own\copy and edit the existing [url=http://love2d.org/wiki/love.run]love.run[/url] function (I think).[/del]
I'm looking into making a pretty simple multiplayer game, where should I start? Can anyone throw me some tutorials? Having a hard time finding relevant ones. Basically it needs to be real-time and players would walk around and interact with eachother in various ways, if that helps at all.
I'm coding in C++, and I'm not handling my memory right. My code goes something along the lines of this: [cpp] class Particle; // with various stuff, it's instance-counted. int main() { std::vector<Particle*> particles; // Every so often particles.push_back(new Particle()); // Every so often, not at the same time Particle* p = 0; p = particles[some index] delete p; particles.erase(particles.begin() + whatever index); // At the very end of execution Particle* p = 0; while(particles.size() > 0) { p = *particles.end(); delete p; particles.pop_back(); } return 0; } [/cpp] After return 0, my instance counter invariably reports there's at least one undisposed Particle object. By my reckoning, all instances should have been cleaned up, should they not? On another note, when you pop_back() or erase() an entry in an std::vector, the object's destructor is called. Does this free the memory, or only delete the object that's using it? And if so, should I be using operator delete before or after the vector erase call, as the delete operator both calls the destructor and frees the memory? In my fiddling with the code it seems to make no difference in which order delete and erase are called.
I'm quite sure it should be delete p[] at [cpp]Particle* p = 0; p = particles[some index] delete p;[/cpp] [editline]26th October 2011[/editline] Don't take my word though, I am pretty bad at pointers.
[QUOTE=WTF Nuke;32985894]I'm quite sure it should be delete p[] at [cpp]Particle* p = 0; p = particles[some index] delete p;[/cpp] [editline]26th October 2011[/editline] Don't take my word though, I am pretty bad at pointers.[/QUOTE] particles is a vector of Particle*, so when you index it you'd just get a single particle pointer, hence without the []. I thought I'd learnt all of this stuff and it shouldn't have been an issue but apparently not :( [editline]27th October 2011[/editline] Well... I fixed it! but I don't actually know what I did.
[QUOTE=mechanarchy;32985787]I'm coding in C++, and I'm not handling my memory right. My code goes something along the lines of this:[/QUOTE] Your code looks correct to me, though the deletion at the end is inefficient. You delete each particle before discarding its pointer, so there shouldn't be any leaks. Make sure you aren't removing entries from the vector in any other places you forgot about. [QUOTE=mechanarchy;32985787]On another note, when you pop_back() or erase() an entry in an std::vector, the object's destructor is called. Does this free the memory, or only delete the object that's using it? And if so, should I be using operator delete before or after the vector erase call, as the delete operator both calls the destructor and frees the memory? In my fiddling with the code it seems to make no difference in which order delete and erase are called.[/QUOTE] pop_back() and erase() are responsible for destructing (if applicable) and freeing the memory for the value that's actually [i]in[/i] the vector. In this case that's a pointer, which has no destructor, so all the vector does is free the four or eight bytes occupied by the pointer. It does [i]not[/i] call the destructor on the object that the pointer points to; that object isn't stored in the vector, and its lifetime can legitimately be longer than the pointer stored in the vector (e.g. you might still have other pointers to it). It doesn't matter whether you delete the Particle before or after you remove the pointer from the vector. The two steps are freeing different, unrelated resources: a pointer whose lifetime is controlled by the vector, and a Particle object whose lifetime is controlled by your own code. Some additional notes: [cpp] Particle* p = 0; p = particles[some index]; [/cpp] is simpler to write as [cpp] Particle* p = particles[some index]; [/cpp] [cpp]p = *particles.end();[/cpp] is simpler to write as [cpp]p = particles.back();[/cpp] And instead of calling pop_back() repeatedly in a loop to remove all the elements, just use the loop to call delete on the pointers, then call particles.clear() when you're done. That way all the vector's storage can be freed at once. When you do it piecemeal, one item at a time with pop_back(), the vector might reallocate its internal array any number of times, copying all the remaining values over to a smaller region of memory, and that's wasteful.
[QUOTE=Wyzard;32986136]Your code looks correct to me, though the deletion at the end is inefficient. You delete each particle before discarding its pointer, so there shouldn't be any leaks. Make sure you aren't removing entries from the vector in any other places you forgot about. pop_back() and erase() are responsible for destructing (if applicable) and freeing the memory for the value that's actually [i]in[/i] the vector. In this case that's a pointer, which has no destructor, so all the vector does is free the four or eight bytes occupied by the pointer. It does [i]not[/i] call the destructor on the object that the pointer points to; that object isn't stored in the vector, and its lifetime can legitimately be longer than the pointer stored in the vector (e.g. you might still have other pointers to it). It doesn't matter whether you delete the Particle before or after you remove the pointer from the vector. The two steps are freeing different, unrelated resources: a pointer whose lifetime is controlled by the vector, and a Particle object whose lifetime is controlled by your own code. Some additional notes: [cpp] Particle* p = 0; p = particles[some index]; [/cpp] is simpler to write as [cpp] Particle* p = particles[some index]; [/cpp] [cpp]p = *particles.end();[/cpp] is simpler to write as [cpp]p = particles.back();[/cpp] And instead of calling pop_back() repeatedly in a loop to remove all the elements, just use the loop to call delete on the pointers, then call particles.clear() when you're done. That way all the vector's storage can be freed at once. When you do it piecemeal, one item at a time with pop_back(), the vector might reallocate its internal array any number of times, copying all the remaining values over to a smaller region of memory, and that's wasteful.[/QUOTE] Ah, thanks heaps for clarifying the operation of pop_back and erase! Makes much more sense now :smile: As for setting the particle pointer to 0, that was originally because I'd had the "Particle* p = particles[index];" in the middle of a loop and I thought it would be "more efficient" to move the local variable outside the loop. I wasn't aware of the .back() function, however I shall use that now when it's appropriate. Clearing the elements one-by-one, I've currently got going from end to start so it [i]shouldn't[/i] have to reallocate the space and shuffle all the elements along, however following your suggestion I'll delete the contents of each pointer then clear them all in one go at the end.
Going from back to front means it won't have to shift all the elements for every erasure, but it'll still reallocate periodically. Imagine if you had a vector with a million elements, and you used a loop like that to pop_back() all but the first one. You wouldn't want it to keep that million-element array allocated when the vast majority of the space is unused. So as you remove elements, the vector will occasionally allocate a smaller array, copy all the remaining values over to it, and free the old large array. BTW, you don't gain anything by moving the declaration of p out of the loop -- any decent compiler should effectively do that for you. (If it were an object with a constructor that'd have to run each time, that'd be different.)
[QUOTE=ROBO_DONUT;32982616]I think you mean 'digit' rather than 'number'. Doesn't make sense otherwise.[/QUOTE] Yes, I meant digit, thank you for correcting me.
[QUOTE=mechanarchy;32982427]Well in that case you'd just need to add some spaces to the end or a tab character. So just say computerCards = computerCards + "Card of Suit" + "\t"; The "\t" is a tab character, and it's equivalent to putting a few spaces in. Using this method, you will probably end up with a spare tab character on the end of your string, so you'd need to trim that off later if it's important that it's not there.[/QUOTE] I'm already doing that, the issue with the code I dont understand is that the output looks like [CODE]2 of Clubs2 of Clubs Jack of Clubs2 of Clubs Jack of Club9 of Hearts[/CODE] instead of [CODE]2 of Clubs Jack of Clubs 9 of Hearts[/CODE]
[QUOTE=fylth;32987484]I'm already doing that, the issue with the code I dont understand is that the output looks like [CODE]2 of Clubs2 of Clubs Jack of Clubs2 of Clubs Jack of Club9 of Hearts[/CODE] instead of [CODE]2 of Clubs Jack of Clubs 9 of Hearts[/CODE][/QUOTE] [code] int count = 1; [b]while (count <= 3) {[/b] Random r = new Random(); int cardRand = r.nextInt(12) + 1; int suitRand = r.nextInt(3) + 1; switch (suitRand) { case 1: suitName = "Hearts"; break; case 2: suitName = "Diamonds"; break; case 3: suitName = "Clubs"; break; case 4: suitName = "Spades"; break; } switch (cardRand) { case 1: computerCards = computerCards + " Ace of " + suitName; break; case 11: computerCards = computerCards + " Jack of " + suitName; break; case 12: computerCards = computerCards + " Queen of " + suitName; break; case 13: computerCards = computerCards + " King of " + suitName; break; default : computerCards = computerCards + Integer.toString(deck[suitRand][cardRand]) + " of " + suitName; break; } [b]System.out.print(computerCards);[/b] count ++; [b]}[/b] [/code] You're calling print inside the loop. So at the end of the first pass of the loop, you've got computerCards == "2 of Clubs\t". You print that, and see "2 of Clubs\t" on the screen. At the end of the second pass of the loop, you've got computerCards == "2 of Clubs\tJack of Clubs\t". You print that, and see "2 of Clubs\t2 of Clubs\tJack of Clubs\t", because you'll end up printing the thing twice. End of third pass, computerCards == "2 of clubs\tJack of Clubs\t9 of Hearts\t", print it, see "2 of Clubs\t2 of Clubs\tJack of Clubs\t2 of Clubs\tJack of Clubs\t9 of Hearts\t". Solution: Move print outside the loop.
[QUOTE=mechanarchy;32987621] Solution: Move print outside the loop.[/QUOTE] That would be the problem! Thanks, after looking at the code for so long you become convinced that you've thought of everything, cheers
I'm still stuck with this program, it's like, the more I think about it, the more mistakes I make. Anyone mind helping me out? :v:
How would I go about making a value start decreasing once it has reached another value? Say x starts at 0 and it increments up to 10, how would I make it start incrementing back to 0?
[QUOTE=Zard;32988760]How would I go about making a value start decreasing once it has reached another value? Say x starts at 0 and it increments up to 10, how would I make it start [b][del]incrementing[/del] decrementing[/b] back to 0?[/QUOTE] You'd need to keep track of what 'stage' you're in. First stage: increasing from zero to ten. Second stage: decreasing from ten to zero. From there, everywhere you have "number++" or "number += 1" or "number = number + 1" you'd instead need to increment or decrement depending on what stage you're in. [editline]27th October 2011[/editline] [QUOTE=Edvinas;32988708]I'm still stuck with this program, it's like, the more I think about it, the more mistakes I make. Anyone mind helping me out? :v:[/QUOTE] [QUOTE=Edvinas;32976936]There are from 1 to x offices in the building. Tablets are nailed on each office door with its number, one tablet for digit in the number. So, if there are 10 offices in the building, then 11 tablets will be required, if 100 then 192. Write a program which calculates how many tablets will be required for x offices.[/QUOTE] I really shouldn't be doing your homework for you, but considering the previous suggestions haven't helped... To work out how many digits are in a particular number, you use the floor function on the base-10 logarithm of that number. Basically the following snippet, except not in pseudo-code: [code] digits = floor(log10(number)) [/code] Once you have this, you simply need to loop between 1 and x (the number of floors) and add the result of that calculation each time to the total. [del]NB: ThePuska, it is the floor of the logarithm, not the ceiling: compare log10(34) == 2.53; floor(log10(34)) == 2. There aren't 3 digits in "34" :smile:[/del]
Thanks, it worked. But, I had some problems with floor(log10(x)). If lets say, I put x = 10, it gave me floor(log10(10)) = 1.
[QUOTE=Edvinas;32989096]Thanks, it worked. But, I had some problems with floor(log10(x)). If lets say, I put x = 10, it gave me floor(log10(10)) = 1.[/QUOTE] That shouldn't be happening. log10(10) == 2. floor(2) == 2? I guess it's a floating point issue. Or your floor function is broken.
I don't know a lot about programming but I figured this was the best place to ask, what is this error telling me? It's not from something I made # EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x000000006d8bf557, pid=2632, tid=828 I'm just curious on what this means
[QUOTE=BrQ;32989603]I don't know a lot about programming but I figured this was the best place to ask, what is this error telling me? It's not from something I made # EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x000000006d8bf557, pid=2632, tid=828 I'm just curious on what this means[/QUOTE] The program is trying to access memory that it hasn't allocated (think of it as requested for use). There's probably nothing you can do to fix it if it isn't your own program. EDIT: More in depth explanation is on wikipedia: [url]http://en.wikipedia.org/wiki/Segmentation_fault[/url].
[QUOTE=ZeekyHBomb;32961312]Sure. I and other people will still be around, provided this forum doesn't get closed down til then (which it probably won't). Sounds like you tried to output "j" instead of just j (without quotes) btw.[/QUOTE] never mind i changed it in class so now it works but i tried to put many numbers in one int but it didnt work :S
Sorry, you need to Log In to post a reply to this thread.