Friday, January 28, 2011

Naive prime finder

I'm going to try to tackle a few of the first Euler Project tasks. Some of the first ones involve finding prime numbers, so I want to make a generic "is this number prime?" function to handle that.

The basic definition of a prime number is a number that only has two divisors - one and itself - that fit right (no remainder).

Ok well, that's what mod (%) is for. Here is my first run at a program that finds the prime numbers in 1 to 20. I'll explain why this is a naive implementation in a second:



This program is a pretty brute force counter of how many times (index % i) returns 0 (meaning no remainder). Index is the number we want to test, and i loops from 1 to index testing (index % i).

I call this a naive implementation because I know better than to loop through every number up to the index but I want to make it basically work before I "optimize" it.

*EDIT*

I pretty much wrote this in one shot, and it didn't work the first time. Everything was coming up prime. Turned out I fumbled out a = instead of == when checking to see if divisors was 2. "something = something else" is a "true" statement regardless of what both are. "something == something else" is a condition that is true if equal, false if not.

Thursday, January 27, 2011

int division

int i = 11/2;

printf("i is %d\n",i);

Result is "i is 5".

I expected this, but just making sure. Changing %d to %f makes it print "i is 0.000000", which I also expected since any int you're trying to print as a float comes out as 0 like that. There's probably a reason for this, but I'd have not guessed it not knowing any better (meaning I had tried it before).

Mleh

Busy busy at work. This weekend is booked solid with me not being at the apartment. I'm worried I won't get my hands on a keyboard for two days straight which is going to probably lead to me forgetting everything I've learned.

100th post. Hooray.

But for real, I make the biggest leaps in knowledge when I code every day, and I haven't done anything significant since Sunday.

Tuesday, January 25, 2011

helping hand

I finally had the opportunity to tell someone in /r/learnprogramming something I wish I had known last summer.

http://www.reddit.com/r/learnprogramming/comments/f8zbc/need_help_with_kr_exercise/c1e7q2g

I'm seriously considering writing up a "Learning C with K&R" post full of things I wish I had known six months ago.

A looming problem

It seems like GTK callback functions (functions that are run in response to events like buttons being pressed) have very specific prototypes, and the last parameter is always a gpointer to user data.

But here's the problem.

Lets say I need to pass a handful of variables to a callback function. Two integers and an array. I only have ONE space (my gpointer) for this in my callback function prototype.

The only solution I've found online is to "pack" my variables into a structure and then pass the address of the structure to my callback function, but this seems like a really roundabout way of doing it. PLUS it forces you to gear the internal workings of a program around the GUI library you want to use. Is this normal?

I mean say I have an old CLI based program that I want to make a GUI for and this program is just a collection of functions with a whole mess of parameters required. I'd have to redesign the entire variable flow just to crowbar in a GUI. That seems crude!

In my last program (exercise 2-1) I was forced to declare my character arrays outside of main() because I didn't know any way to pass them to my callback function otherwise. My gpointer was taken up by label, anyways. What if I wanted to change the text on two labels, called label1 and label2? How do I pass the second one? Am I allowed to assign another callback to deal with label2? Maybe if I make my callback function a gboolean that returns FALSE to indicate that I didn't finish dealing with the event.

Ok - tonight's homework is to try that. I'll make two callbacks in response to 1 event and return false on the first callback and true on the second. Maybe that will work.

I'll keep an eye out on how to gracefully deal with a plethora of variables needing to be passed to a callback function.

useful to note

if (g_ascii_strcasecmp (gtk_window_get_title(GTK_WINDOW (window)),text2) == 0

g_ascii_strcasecmp returns 0 if the two arguments (each a char*) are the same. So in my function I'm asking "hey are you already text2? Oh g_ascii_strcasecmp returned 0 so you are text2? Ok then you're text1 now.

WHY it's not doing this consistently is screwing with my head. What I might do is be really assertive and do that condition on both the window and the label. It's redundant, but it might alter the behavior.

Exercise 2-1 in the GTK book



The exercise was to make a window with a title and a label (the text in the window). When you press a keyboard key the text in the window title and label should swap.

Didn't take too long to figure out how to get a property of a widget (the title text status is how I determine which way to swap things), and there is a GDK function for testing if strings are identical, or if the first is larger than the second, or vice versa. Also wasn't too hard to find.

Here's the problem - it doesn't always work. Sometimes I'll press a button and both texts will swap. Sometimes only the label will change (making both texts the same), and very rarely only the window title will change. I need to run this on my Windows box to see if the behavior is the same. If it is then I'll do some searching. If I had more time this morning I'd rewrite the swap text function just to see if that would alter the behavior.

I'm happy with this because I started from scratch and only had to reference the book or the API doc for new stuff that the book hadn't covered yet, like getting a widget attribute. That's what this book is good about. It says "do this exercise but be warned that you'll need functions regarding getting attributes we haven't covered yet and you need to look in the API docs to get that function".

The next exercise expands this program by using a button you click on instead of a keypress. Maybe tonight or tomorrow morning I'll work on that. Having two hours to myself in the morning (since Ana gets up so early and I wind up waking up, too) is a blast. Crashing at 3:30pm isn't, however.

*EDIT*

Also, I've started standardising how I use spaces. Spaces after commas in function calls, spaces in between function name and parameters list.