Unofficial Programming Thread II

Post » Tue May 17, 2011 7:38 am

Previous thread(s):
http://www.gamesas.com/index.php?/topic/1092028-unofficial-programming-thread/page__p__15933926__fromsearch__1&#entry15933926

So I think I will start looking into Android development but am unsure where to even start. As said in thelast thread is mostly Java which luckily enough seems close to C# which I have been working with most so it should not be too huge of an issue. Anyone know where to get started with the development?
User avatar
Silencio
 
Posts: 3442
Joined: Sun Mar 18, 2007 11:30 pm

Post » Tue May 17, 2011 6:01 am

From my last post that people helped with - how would I use the switch case thing? I tried it on that exact program before I did the "if" thing and I could not get it to work, even copying examples from websites. Kept getting some error about something being under something else.
User avatar
Jesus Duran
 
Posts: 3444
Joined: Wed Aug 15, 2007 12:16 am

Post » Tue May 17, 2011 8:17 am

From my last post that people helped with - how would I use the switch case thing? I tried it on that exact program before I did the "if" thing and I could not get it to work, even copying examples from websites. Kept getting some error about something being under something else.

You were using C# correct?

Here is an examples to replace the if statements (using generic variables and comments):
string Input = Console.ReadLine();switch (Input){     case "Yes":         //do something here         break;     case "No":         //do something here         break;     case "Cancel":         //do something here         break;     default:         //do something here if no other case is matched or just write a break         break;}


Let me know if that helps you at all.
User avatar
Laura Mclean
 
Posts: 3471
Joined: Mon Oct 30, 2006 12:15 pm

Post » Tue May 17, 2011 1:24 am

Previous thread(s):
http://www.gamesas.com/index.php?/topic/1092028-unofficial-programming-thread/page__p__15933926__fromsearch__1&#entry15933926

So I think I will start looking into Android development but am unsure where to even start. As said in thelast thread is mostly Java which luckily enough seems close to C# which I have been working with most so it should not be too huge of an issue. Anyone know where to get started with the development?

http://developer.android.com/guide/topics/fundamentals.html.

I've been doing a lot of Android stuff lately, including helping a professor develop a course to teach it. At this point there seem to be no decent books available to recommend. There are a handful to heartily recommend avoiding (two by the authors Conder and Darcy come to mind) unless you want a broad overview of the now relatively obsolete 1.5 version with little actually useful information. The developer docs linked above are your best source, though you need to dig around at times to find what you're looking for because they are vast - they also include documentation for non-Android-specific Java stuff which may be helpful if you're new to Java.

Maybe some new books have come out but TBH it's the kind of thing you learn by doing, not by reading about. It's possible to get a functional app running with minimal effort and you can add and modify things from there.

The main hurdle most beginners face is understanding things like Activity lifecycle. There are several tutorials on the site to get you started and show you how the basic pieces fit together.

The other hurdle for some is getting everything set up. The easiest way is probably to use the Eclipse IDE as described http://developer.android.com/sdk/index.html.
User avatar
Lovingly
 
Posts: 3414
Joined: Fri Sep 15, 2006 6:36 am

Post » Tue May 17, 2011 2:01 pm

I am having a huge issue even installing the SDK. When I launch the SDK manager to download the stuff it will stop at a random percentage and then does nothing when I click cancel. I tried numerous times and cannot seem to get it. I am on a good connection so I wanted to do it now, my connection at home is too slow.
User avatar
clelia vega
 
Posts: 3433
Joined: Wed Mar 21, 2007 6:04 pm

Post » Mon May 16, 2011 11:46 pm

Try http: instead of https: for the remote update site.
User avatar
Steven Nicholson
 
Posts: 3468
Joined: Mon Jun 18, 2007 1:24 pm

Post » Tue May 17, 2011 3:59 am

Would anyone else happen to be familiar with ARM at all?
User avatar
Peter P Canning
 
Posts: 3531
Joined: Tue May 22, 2007 2:44 am

Post » Tue May 17, 2011 1:38 pm

Try http: instead of https: for the remote update site.

Tried that and no luck, will give it a shot again tomorrow. But that is off topic so lets return to the actually programming. Once that issue is resolved then I can bring it back on topic.
User avatar
darnell waddington
 
Posts: 3448
Joined: Wed Oct 17, 2007 10:43 pm

Post » Tue May 17, 2011 7:22 am

ok, this is driving me nuts. I'm in an intro to C class, and I've got to make a program that determines whether or not an integer entered by the user is prime, and list a factor that it's divisible by if it isn't.

This is what I've got so far:

#include int main(){	int a=2, b;	float d, e;	printf("Enter your number:");	scanf("%d", &B);            	d=b/2;      	e=b%a;   	for(a; a>=1 && a<=d; a++){		if(e==0){			printf("Your number is not prime.  It is divisible by %d.", a);			break;		}		else if(e!=0 && a>=d){			printf("Your number is prime.");			break;		}		else continue;	}		return 0;}


Thing is, it doesn't seem to be working properly. It works well enough on determining if a number's prime, but has trouble finding if a number isn't prime if the user entered an odd number(like 27). It seems to be behaving as if && a>=d doesn't exist.

Any ideas?

p.s. I know that it's freaking out if you enter in 1, 2 or 3, just haven't been bothered to fix that yet. Also, I'm a newbie at C, so be gentle!
User avatar
saharen beauty
 
Posts: 3456
Joined: Wed Nov 22, 2006 12:54 am

Post » Tue May 17, 2011 5:29 am

The main problem I see from my tipsy state is that e needs moving into the loop. It is only calculating b mod a once, for the initial value of a (So it only checks if the number is divisible by 2)

The rest of my comments are regarding style and are probably not required to get a working program...

Also, why are you using a value of d of half b? Wouldn't sqrt ( b ) would be better by some huge order of magnitude?

Rather than trying to work out when the loop stops, why not just have a flag? Start by assuming b is prime, then test this assumption for all values in N+ less than or equal to sqrt( b ) , and if you find a value where it isn't set the flag to false (0) and break from the loop. Then you can give output at the end of the program depending on the value of the flag. This simplifies everything.

The continue is redundant.

Also, this line
	for(a; a>=1 && a<=d; a++){
You know a is initially 2. a++ (The only write operation on a) ensures a is increasing. Hence a>=1 is redundant as it is always true. True && a <= d can be simplified to a <= d

IMHO try not to use letters a b c et al that don't describe what the variable does. It is really confusing as to what they do when the scope of the variables (In this case, the function that the variables are defined in) is more than a few lines. The exception is i,j,k when used in loops over an array as it is very common to do so. (Including in maths, which is I assume where it stemmed from)

Example of something closer to what I would write:
#include #include int main(){	int divisor, possible;	float limit;        int prime = 1;	printf("Enter your number:");	scanf("%d", &possible);	limit=sqrt(possible);      		for( divisor = 2; divisor <= limit; divisor++){            if ( possible % divisor == 0 ){                prime = 0;                break;            }	}               if ( prime ){        	printf("Your number is prime.");        }else{            printf("Your number is not prime.  It is divisible by %d.", divisor);        }		return 0;}

User avatar
Marine Arrègle
 
Posts: 3423
Joined: Sat Mar 24, 2007 5:19 am

Post » Tue May 17, 2011 3:19 am

Stupid forum software <_<
User avatar
meghan lock
 
Posts: 3451
Joined: Thu Jan 11, 2007 10:26 pm

Post » Tue May 17, 2011 4:30 am

The main problem I see from my tipsy state is that e needs moving into the loop. It is only calculating b mod a once, for the initial value of a (So it only checks if the number is divisible by 2)


:facepalm:

Thanks. I could have sworn I tried that a while back, and got a compiler error about e not being initialized, but I guess that I was thinking of a different program I'm working on.


Also, why are you using a value of d of half b? Wouldn't sqrt ( b ) would be better by some huge order of magnitude?


It's just what came to mind as a decent limit for stopping the program if the number's prime(half of a number being the highest factor of that number AFAIK). I didn't know that the square root of a number would work too.


Rather than trying to work out when the loop stops, why not just have a flag? Start by assuming b is prime, then test this assumption for all values in N+ less than or equal to sqrt( b ) , and if you find a value where it isn't set the flag to false (0) and break from the loop. Then you can give output at the end of the program depending on the value of the flag. This simplifies everything.

The continue is redundant.


We simply haven't gotten there yet. Like I said, I'm a beginner.


Also, this line
	for(a; a>=1 && a<=d; a++){
You know a is initially 2. a++ (The only write operation on a) ensures a is increasing. Hence a>=1 is redundant as it is always true. True && a <= d can be simplified to a <= d


Ah, thanks! I initially had it set up to decrement instead of increment when I first wrote the program, and forgot to cut that bit out of my code.


IMHO try not to use letters a b c et al that don't describe what the variable does. It is really confusing as to what they do when the scope of the variables (In this case, the function that the variables are defined in) is more than a few lines. The exception is i,j,k when used in loops over an array as it is very common to do so. (Including in maths, which is I assume where it stemmed from)


lol, yeah sorry about that. My instructor would hang me if I turned in a program with variables named like that; I just find it easier to keep the variables like that until my code's actually up and running, and then change them to descriptive names in one fell swoop.

Thank you for the help! :D
User avatar
Bryanna Vacchiano
 
Posts: 3425
Joined: Wed Jan 31, 2007 9:54 pm

Post » Tue May 17, 2011 4:22 am



Why did you name the variable containing the number whose primality is to be checked "possible"? 0_o

Why not "number"?

Anyway, I know this is totally unimportant in a code that's supposed to be one of the first examples of C for someone else, but comparing integer to float instead of integer to integer with each step of the loop? Dude...

#include #include int main(void){    int number, divisor, limit, prime = 1;        printf("Enter your number: ");    scanf("%d", &number);        limit = floor(sqrt(number));        if(number % 2){        for(divisor = 3; divisor <= limit; divisor += 2){            if (number % divisor == 0){                prime = 0;                break;            }        }        if(prime) printf("Your number is prime.\n");        else printf("Your number is not prime. It is divisible by %d.\n", divisor);    } else printf("Your number is not prime. It is divisible by 2.");        return 0;}

Yeah I couldn't help myself, had to throw that in as well...

Could've done much better though, could've parallelised it...

Hm...
User avatar
daniel royle
 
Posts: 3439
Joined: Thu May 17, 2007 8:44 am

Post » Tue May 17, 2011 6:31 am

Why did you name the variable containing the number whose primality is to be checked "possible"? 0_o

Why not "number"?

It was 12AM, I had been awake for 18 hours as well as having drunk quite a lot and was trying to avoid getting some work done? I am fairly sure I made a load of mistakes.

My program does have the bonus of working though, which is always a plus :P

(Give your program a try with a composite number that isn't divisible by 2)

Could've done much better though, could've parallelised it...

Although, using a better algorithm (Sieve of Erathosthenes or Sieve of Atkin) is probably a better idea to start with.
User avatar
Queen of Spades
 
Posts: 3383
Joined: Fri Dec 08, 2006 12:06 pm

Post » Tue May 17, 2011 6:17 am

Although, using a better algorithm (Sieve of Erathosthenes or Sieve of Atkin) is probably a better idea to start with.

How exactly would you apply sieves to checking whether a single number is prime?


edit:
(Give your program a try with a composite number that isn't divisible by 2)

Uh... like 9?

I don't see, what did I screw up? :unsure:

edit: Ah of course. :shakehead:
User avatar
Ezekiel Macallister
 
Posts: 3493
Joined: Fri Jun 22, 2007 12:08 pm

Post » Tue May 17, 2011 7:22 am

How exactly would you apply sieves to checking whether a single number is prime?

Well, if you are running into speed issues with that function like that (With a maximum of about 23000 iterations), you have two possibilities. Your CPU is far too slow, in which case it wouldn't have multiple cores so multiple threads would just introduce overhead. Or you are trying to compute a lot of primes, in which case a sieve would prove useful.
User avatar
Johanna Van Drunick
 
Posts: 3437
Joined: Tue Jun 20, 2006 11:40 am

Post » Tue May 17, 2011 5:00 am

Or you are trying to do it multiple times, in which case a sieve would prove useful.

Ah, so you mean like top-down dynamic programming?
User avatar
Farrah Barry
 
Posts: 3523
Joined: Mon Dec 04, 2006 4:00 pm

Post » Tue May 17, 2011 2:52 am

Ah, so you mean like top-down dynamic programming?

:shrug: Memorization. I would imagine in some cases, if you wanted a lot of primes below 5 000 000 it would be faster to pecompute them (or better, do so in a lazy way) . I would suppose it really depends on the range of the primes you want and how many of them you want.

IDK. I am going to bed.
User avatar
Devils Cheek
 
Posts: 3561
Joined: Sun Aug 13, 2006 10:24 pm

Post » Tue May 17, 2011 12:56 pm

:shrug: Memorization.

Uh yes that's kinda what I said...

Anyway, I prefer doing parallel algorithms. :lol: It's not like I need the primes at the moment anyway, it's just coding for the sheer heck of it. ^_^

Yeah I'm a geek...


edit:
IDK. I am going to bed.

Yeah, me too. Good night.
User avatar
Dina Boudreau
 
Posts: 3410
Joined: Thu Jan 04, 2007 10:59 pm

Post » Tue May 17, 2011 5:32 am

I was thinking something like this, in C++:

#include #include #include #include #include #include using namespace std;unsigned long long n, limit, divisor = 0;pthread_mutex_t stop_control = PTHREAD_MUTEX_INITIALIZER;bool stop = false;void *threadfunc(void *argument){    int start = *(reinterpret_cast(argument));        for(unsigned long long i = static_cast(start); i <= limit; i += 10){        if(stop) return NULL;        if(!(n % i)){            if(pthread_mutex_trylock(&stop_control)) return NULL;            stop = true;            divisor = i;            return NULL;        }    }        return NULL;}int main(int argc, char **argv){    if(argc != 2){        cout << "Invalid number of parameters.\n";        exit(EXIT_FAILURE);    }        n = strtoull(argv[1], NULL, 0);    if(n < 1){        cout << "Unexpected command-line argument format.\n";        exit(EXIT_FAILURE);    }        if(n == 1){        cout << "1 is not a prime number.\n";        return EXIT_SUCCESS;    }    if(n == 2){        cout << "Your number is prime.\n";        return EXIT_SUCCESS;    }    if(!(n % 2)){        cout << "Your number is divisible by 2.\n";        return EXIT_SUCCESS;    }    if(n == 3){        cout << "Your number is prime.\n";        return EXIT_SUCCESS;    }    if(!(n % 3)){        cout << "Your number is divisible by 3.\n";        return EXIT_SUCCESS;    }    if(n == 5){        cout << "Your number is prime.\n";        return EXIT_SUCCESS;    }    if(!(n % 5)){        cout << "Your number is divisible by 5.\n";        return EXIT_SUCCESS;    }    if(n == 7){        cout << "Your number is prime.\n";        return EXIT_SUCCESS;    }    if(!(n % 7)){        cout << "Your number is divisible by 7.\n";        return EXIT_SUCCESS;    }        pthread_t threads[4];    int thread_args[4];    thread_args[0] = 11;    thread_args[1] = 13;    thread_args[2] = 17;    thread_args[3] = 19;        limit = static_cast(floor(sqrt(static_cast(n))));        for(int i = 0; i < 4; ++i){        if(pthread_create(&threads[i], NULL, threadfunc, reinterpret_cast(&thread_args[i]))){            cout << "Error creating threads.\n";            exit(EXIT_FAILURE);        }    }        for(int i = 0; i < 4; ++i){        if(pthread_join(threads[i], NULL)){            cout << "Error joining threads.\n";            exit(EXIT_FAILURE);        }    }        if(divisor) cout << "Your number is divisible by " << divisor << ".\n";    else cout << "Your number is prime.\n";        return EXIT_SUCCESS;}


First it sequentially checks whether n is divisible by 2, 3, 5 and 7, after that it uses four threads each going through one column in the following grid of numbers and checking whether n is divisible by the current number:

11 | 13 | 17 | 1921 | 23 | 27 | 2931 | 33 | 37 | 3941 | 43 | 47 | 4951 | 53 | 57 | 5961 | 63 | 67 | 69.  | .  | .  | ..  | .  | .  | ..  | .  | .  | .


It seems relatively fast for this sort of algorithm. On AMD Phenom II X4 955, checking whether 32748365856155783 is prime (http://www.wolframalpha.com/input/?i=32748365856155783) takes 0.439 seconds. Checking whether 18446744073709551557 is prime (http://www.wolframalpha.com/input/?i=18446744073709551557 and is also the largest prime storable as unsigned long long) takes 11.453 seconds. And http://img267.imageshack.us/img267/8040/htopc.png.
User avatar
K J S
 
Posts: 3326
Joined: Thu Apr 05, 2007 11:50 am

Post » Tue May 17, 2011 9:17 am

Or a bit better:

#include #include #include #include #include #include using namespace std;unsigned long long n, limit, divisor = 0;pthread_mutex_t stop_control = PTHREAD_MUTEX_INITIALIZER;bool stop = false;void *threadfunc(void *argument){    int threadID = *(reinterpret_cast(argument));    unsigned long long current;        switch(threadID){                case 0:        for(unsigned long long i = 1; ; i += 2){            if(stop) return NULL;            current = 6 * i - 1;            if(current > limit) return NULL;            if(!(n % current)){                if(pthread_mutex_trylock(&stop_control)) return NULL;                stop = true;                divisor = i;                return NULL;            }        }        break;                case 1:        for(unsigned long long i = 1; ; i += 2){            if(stop) return NULL;            current = 6 * i + 1;            if(current > limit) return NULL;            if(!(n % current)){                if(pthread_mutex_trylock(&stop_control)) return NULL;                stop = true;                divisor = i;                return NULL;            }        }        break;                case 2:        for(unsigned long long i = 2; ; i += 2){            if(stop) return NULL;            current = 6 * i - 1;            if(current > limit) return NULL;            if(!(n % current)){                if(pthread_mutex_trylock(&stop_control)) return NULL;                stop = true;                divisor = i;                return NULL;            }        }        break;                case 3:        for(unsigned long long i = 2; ; i += 2){            if(stop) return NULL;            current = 6 * i + 1;            if(current > limit) return NULL;            if(!(n % current)){                if(pthread_mutex_trylock(&stop_control)) return NULL;                stop = true;                divisor = i;                return NULL;            }        }        break;    }}int main(int argc, char **argv){    if(argc != 2){        cout << "Invalid number of parameters.\n";        exit(EXIT_FAILURE);    }        n = strtoull(argv[1], NULL, 0);    if(n < 1){        cout << "Unexpected command-line argument format.\n";        exit(EXIT_FAILURE);    }        if(n == 1){        cout << "1 is not a prime number.\n";        return EXIT_SUCCESS;    }    if(n == 2){        cout << "Your number is prime.\n";        return EXIT_SUCCESS;    }    if(!(n % 2)){        cout << "Your number is divisible by 2.\n";        return EXIT_SUCCESS;    }    if(n == 3){        cout << "Your number is prime.\n";        return EXIT_SUCCESS;    }    if(!(n % 3)){        cout << "Your number is divisible by 3.\n";        return EXIT_SUCCESS;    }        pthread_t threads[4];    int thread_args[4];    thread_args[0] = 0;    thread_args[1] = 1;    thread_args[2] = 2;    thread_args[3] = 3;        limit = static_cast(floor(sqrt(static_cast(n))));        for(int i = 0; i < 4; ++i){        if(pthread_create(&threads[i], NULL, threadfunc, reinterpret_cast(&thread_args[i]))){            cout << "Error creating threads.\n";            exit(EXIT_FAILURE);        }    }        for(int i = 0; i < 4; ++i){        if(pthread_join(threads[i], NULL)){            cout << "Error joining threads.\n";            exit(EXIT_FAILURE);        }    }        if(divisor) cout << "Your number is divisible by " << divisor << ".\n";    else cout << "Your number is prime.\n";        return EXIT_SUCCESS;}


This one takes 10.172 seconds to check the primality of 18446744073709551557. :P
User avatar
Yama Pi
 
Posts: 3384
Joined: Wed Apr 18, 2007 3:51 am

Post » Tue May 17, 2011 11:12 am

Finally got the Android SDK installed and working with Eclipse. Anyone know a good place to start learning how to go about making some stuff? I know they have a Hello World and Notepad tutorial on the main site with the SDK but was looking for other tutorials. I do have some knowledge of C# so the syntax seems similar but beyond that I am lost with where to start.
User avatar
Mrs Pooh
 
Posts: 3340
Joined: Wed Oct 24, 2007 7:30 pm

Post » Tue May 17, 2011 6:33 am

Finally got the Android SDK installed and working with Eclipse. Anyone know a good place to start learning how to go about making some stuff? I know they have a Hello World and Notepad tutorial on the main site with the SDK but was looking for other tutorials. I do have some knowledge of C# so the syntax seems similar but beyond that I am lost with where to start.

As far as I can understand from googling around, the main language is Java, right? In that case, http://math.hws.edu/eck/cs124/downloads/javanotes5-linked.pdf.
User avatar
Matt Terry
 
Posts: 3453
Joined: Sun May 13, 2007 10:58 am

Post » Mon May 16, 2011 11:01 pm

I'm currently writing a program that is causing me a lot of headaches, now, the Java itself isn't too complicated, it's more so the maths, so I figured it was worth asking you guys.

Basically, I've got a triangular based pyramid, and I have to find the volume. So I'm using the formula for the volume of a pyramid, of course. I worked out the base using Heron's formula but the issue is that I can't for the life of me figure out the height for when it's an irregular pyramid with the 4th vertex off centre.

edit: Venzar, let me know if you ever figure out how to configure Eclipse so it's not terrible, I am stuck using it at Uni.
User avatar
Kat Lehmann
 
Posts: 3409
Joined: Tue Jun 27, 2006 6:24 am

Post » Tue May 17, 2011 12:34 am

I'm currently writing a program that is causing me a lot of headaches, now, the Java itself isn't too complicated, it's more so the maths, so I figured it was worth asking you guys.

Basically, I've got a triangular based pyramid, and I have to find the volume. So I'm using the formula for the volume of a pyramid, of course. I worked out the base using Heron's formula but the issue is that I can't for the life of me figure out the height for when it's an irregular pyramid with the 4th vertex off centre.

Which input data do you have?
User avatar
danni Marchant
 
Posts: 3420
Joined: Sat Oct 07, 2006 2:32 am

Next

Return to Othor Games