• What do you need help with? V4 (January 2012)
    966 replies, posted
[QUOTE=swift and shift;34569264]that's not how it works, it goes by whatever macro is defined when lib.cpp is compiled. If you [i]really[/i] want to do what you're trying, do something like this: [b]lib.hpp[/b]: [cpp] _doStuffInt(); _doStuffBool(); static void doStuff() { #ifdef useint _doStuffInt(); #else _doStuffBool(); #endif } [/cpp] [b]lib.cpp[/b]: [cpp] void _doStuffInt() {int bloop;} void _doStuffBool() {bool bloop;} [/cpp] (also, please don't use .h for C++ header files)[/QUOTE] Thanks! (also everyone ever uses .h :P)
[QUOTE=flayne;34652327](also everyone ever uses .h :P)[/QUOTE] No, not "everyone ever" uses .h, quite a lot of people use .hpp. In fact, systems such as GitHub's language detection [B]depend[/B] on people using .hpp.
-snip- fixed
I have been off c++ for a couple of weeks now and want to pick it up again. But I have only programmed in console and I want to start to code programs with GUI's. My goal is a movie player, but I haven't found any tutorial that is 1. Updated 2. In the language c++ I've only found stuff from 2004 with c#. I am now asking you, if you have any good tutorials on Win32 programming to share! Thank you
[QUOTE=flayne;34652327]Thanks! (also everyone ever uses .h :P)[/QUOTE] .h is for C, not C++. Use .hpp or .hh instead.
Now you tell me. All my C++ projects use .h
I use hpp. Along with cpp.
When I enabled the MP3 player in my Source Mod, the MP3 player appears in the top left corner, rather than the middle.
[QUOTE=Jookia;34655725]I use hpp. Along with cpp.[/QUOTE] cpp is too mainstream so I use cc and hh
[QUOTE=swift and shift;34655784]cpp is too mainstream so I use cc and hh[/QUOTE] Gee, using the matching file formats is totally hipster isn't it?
I'm trying to do a problem from Project Euler, but my code is WAY too slow. Even after ten minutes, I have no results. I need help making run faster. I'm not asking for specifics, just enough to get me on the right track. [code]//Project Euler #3- GCF of 600851475143 #include <iostream> #include <cmath> #include <vector> using namespace std; int main() { double answer; vector<double> primes; primes.push_back(2); //finds all primes for(double i=3; i<=600851475143; i++) { for(double t=0; t<=primes.size(); t++) { if(fmod(i,primes[t])==0) break; if((fmod(i,t)!=0)&&(t==primes.size())) primes.push_back(i); } } //finds which prime is the largest factor for(double i=0; i<=primes.size(); i++) { if(fmod(600851475143,primes[i])==0) answer=primes[1]; } cout << answer << endl; }[/code]
[QUOTE=nick10510;34650225]I understand this could be done with a couple of google searches and maybe some digging around in the options, but how do I stop Visual Studio from changing this [code]private void Function(){ //code }[/code] into this [code]private void Function() { //code }[/code][/QUOTE] That's funny. That's kind of the exact opposite of what I want Eclipse to do.
Hey guys, I've got a little algorithmic problem for you here. I have a "hacky" solution which I am not confident with, since it doesn't work with less than 5 numbers (and I can't test all number combinations), so I'm here to see if you guys can think of a way to do this. No particular language, pseudo-code is fine. I need a function/program which will do the following: Has an input of 5 numbers Each number has a value between 1 and 5 (including 1 and 5) I would like the function to identify if any of the number's values have a difference of 2 or greater from any of the other numbers. If so, return which number meets the above criteria. Since there are 5 numbers, it's possible it may return 2 numbers. Example: An input of 12213 - would return nothing An input of 52111 - would return "1" (since number "1" has a value of 5, which differs from the other numbers by more than 2) An input of 54111 - would return "1" and "2" (because numbers "1" and "2" differ by more than 2 from the other numbers) Thanks very much for any help. Maybe some statistical analysis would be of use here?
[QUOTE=The Kakistocrat;34655964]I'm trying to do a problem from Project Euler, but my code is WAY too slow. Even after ten minutes, I have no results. I need help making run faster. I'm not asking for specifics, just enough to get me on the right track. [code]//Project Euler #3- GCF of 600851475143 #include <iostream> #include <cmath> #include <vector> using namespace std; int main() { double answer; vector<double> primes; primes.push_back(2); //finds all primes for(double i=3; i<=600851475143; i++) { for(double t=0; t<=primes.size(); t++) { if(fmod(i,primes[t])==0) break; if((fmod(i,t)!=0)&&(t==primes.size())) primes.push_back(i); } } //finds which prime is the largest factor for(double i=0; i<=primes.size(); i++) { if(fmod(600851475143,primes[i])==0) answer=primes[1]; } cout << answer << endl; }[/code][/QUOTE] You might want to avoid looping 600851475143 times, first :v: I'm no expert at factorization, but this is not a very big number as they go. So for what you're doing, which is trial division, you only need to test with numbers up to sqrt(600851475143) = 775146. Also, don't find all the primes first, first find if the trial number is a factor of the big number before seeing it it's prime. Hopefully that'll get you started.
Wait, why only test numbers up to the square root? Isn't the idea to find the largest prime factor, so wouldn't it be above the square root rather than below it?
[QUOTE=WTF Nuke;34656971]Wait, why only test numbers up to the square root? Isn't the idea to find the largest prime factor, so wouldn't it be above the square root rather than below it?[/QUOTE] Hmm, maybe you're right, but for this particular problem the largest prime factor is smaller than the square root so it works anyway. And it still holds true for testing if the divisors of the big number are prime or not.
You could also code the sieve of eratosthenes. take the output of that, then loop through it backwards for the largest prime < sqrt(n). first number that divides evenly will be your highest prime.
[QUOTE=Topgamer7;34657897]You could also code the sieve of eratosthenes. take the output of that, then loop through it backwards for the largest prime < sqrt(n). first number that divides evenly will be your highest prime.[/QUOTE] Still, storing 775146 ints takes a lot of space.
It's about ~3 megabytes. Not that huge of an amount. Time memory trade off and all that.
For the sieve, it's easier to store bools rather than ints. A true if its a prime and a false if not. Also the reason why you use the sqrted number is because any number above that you can find by division. [editline]11th February 2012[/editline] Also for the problem I only have [sp]8 [/sp] factors below the square root, including it. How are you going to store 775146 ints with just that many factors?
[QUOTE=Topgamer7;34658127]It's about ~3 megabytes. Not that huge of an amount. Time memory trade off and all that.[/QUOTE] I don't think the sieve is really worth it in this case, I mean I was able to find it with trial division in 28ms on a 1.6gHz processor. Although it doesn't [i]really[/i] matter because of course 3mb is fine for computers these days, but it seems like overkill to use that much memory for such a simple problem.
What I do if I need to check if a number is prime: check if it's even. Then a for loop from 3 to sqrt(n), incremented by two every time. Check if n % i == 0. Simple enough and should work for all cases. e: I'm not sure anymore. I can't think. e2: sqrt should work for checking if a number is a prime, but I don't think it works for finding factors.
Just use the sieve for primes, it's really useful and fast. And for finding factors, you only need to find half, and that half is going to be equal to and below the sqrt. If you need the other factor pair, just divide the number by the factor.
[QUOTE=ichiman94;34648956]By moving it as much as you need somewhere then rotating it? The moving vector can represent the center of your model. (I only know openGL specific code but you can do the same with xna)[/QUOTE] By default, the center of rotation always follows the model's origin. I'm wondering how you can actually change that origin from within the code.
[QUOTE=Octave;34656604]You might want to avoid looping 600851475143 times, first :v: I'm no expert at factorization, but this is not a very big number as they go. So for what you're doing, which is trial division, you only need to test with numbers up to sqrt(600851475143) = 775146. Also, don't find all the primes first, first find if the trial number is a factor of the big number before seeing it it's prime. Hopefully that'll get you started.[/QUOTE] Thank you, my new code solved it in no time. And thanks to everyone else for their suggestions.
[QUOTE=Trumple;34656563] Example: An input of 12213 - would return nothing An input of 52111 - would return "1" (since number "1" has a value of 5, which differs from the other numbers by more than 2) An input of 54111 - would return "1" and "2" (because numbers "1" and "2" differ by more than 2 from the other numbers) Thanks very much for any help. Maybe some statistical analysis would be of use here?[/QUOTE] [code]#include <stdio.h> #define NUMS 5 int calc(int in[], int out[]) { int i; int j; int out_i = 0; for (i = 0; i < NUMS; i++) { for (j = i+1; j < NUMS; j++) { if (abs(in[i] - in[j]) > 2) { if (out_i == 0 || i+1 != out[out_i-1]) { out[out_i] = i+1; out_i++; } j == NUMS; // go to next outer loop iteration } } } return out_i; } void print_array(int in[], int amt_nums) { int i; if (amt_nums == 0) { printf("nothing"); } else { printf("%d", in[0]); } for (i = 1; i < amt_nums; i++) { printf(", %d", in[i]); } printf("\n"); } int main(void) { { int nums[] = {1, 2, 2, 1, 3}; int out[NUMS] = {0}; int amt_nums = 0; amt_nums = calc(nums, out); print_array(out, amt_nums); } { int nums[] = {5, 2, 1, 1, 1}; int out[NUMS] = {0}; int amt_nums = 0; amt_nums = calc(nums, out); print_array(out, amt_nums); } { int nums[] = {5, 4, 1, 1, 1}; int out[NUMS] = {0}; int amt_nums = 0; amt_nums = calc(nums, out); print_array(out, amt_nums); } } [/code]
[QUOTE=Mr. Smartass;34659182]By default, the center of rotation always follows the model's origin. I'm wondering how you can actually change that origin from within the code.[/QUOTE] Apply a translation matrix before a rotation matrix (or after, depending on row-major or column-major ordering) and it should rotate along the origin defined by the translation matrix.
How do you swap from a Z up environment to a Y up environment. It looks like I can swap vertex-positions by this conversion: [cpp] old:new x:x y:z z:-y [/cpp] I'm not sure on normals, though swapping z for -y (as above) seems to work. I don't currently have a reliable way to view normals so I'm not sure if that works. The other thing would be positions and rotations. How do those need to be swapped? I should probably do it with a matrix, but I'm not sure how to generate a z+toy+ matrix.
[QUOTE=robmaister12;34661446]Apply a translation matrix before a rotation matrix (or after, depending on row-major or column-major ordering) and it should rotate along the origin defined by the translation matrix.[/QUOTE] Ah, thanks. I was doing it in the other order. Is there another method for moving the center of rotation, or is that it?
[QUOTE=Mr. Smartass;34661979]Ah, thanks. I was doing it in the other order. Is there another method for moving the center of rotation, or is that it?[/QUOTE] Actually it doesn't magically work with just one translation (I thought it did, but it doesn't). You have to translate the object's origin to the center of rotation, rotate, then translate back to the origin. [url]http://math.stackexchange.com/questions/62182/how-do-i-rotate-a-matrix-transformation-with-a-centered-origin[/url] [editline]12th February 2012[/editline] [QUOTE=Lord Ned;34661494]How do you swap from a Z up environment to a Y up environment. It looks like I can swap vertex-positions by this conversion: [cpp] old:new x:x y:z z:-y [/cpp] I'm not sure on normals, though swapping z for -y (as above) seems to work. I don't currently have a reliable way to view normals so I'm not sure if that works. The other thing would be positions and rotations. How do those need to be swapped? I should probably do it with a matrix, but I'm not sure how to generate a z+toy+ matrix.[/QUOTE] Flip the 2nd and 3rd rows, set the 3rd row negative. [1, 0, 0, 0] [0, 0, 1, 0] [0, -1, 0, 0] [0, 0, 0, 1]
Sorry, you need to Log In to post a reply to this thread.