Jump to content


Check out our Community Blogs

Aereshaa

Member Since 07 Apr 2008
Offline Last Active Feb 19 2010 08:36 PM
-----

Topics I've Started

Incomprehensible error... any ideas?

10 February 2010 - 01:10 PM

inline void foo(){}
int main(){foo();}
That seemingly innocent code gives the following linker error:
$gcc -std=c99 -Wall -Wextra -pedantic inlinetest.c
/tmp/cc4A48Yu.o: In function `main':
inlinetest.c:(.text+0x12): undefined reference to `foo'
collect2: ld returned 1 exit status

The longest initializer ever!

18 December 2009 - 01:34 PM

Check out this initializer from my tetris implementation:
typedef struct {struct {int x,y;} block[4];} piece;
piece deck[7]={{{{4,0},{5,0},{6,0},{7,0}}},{{{4,0},{5,0},{6,0},{4,1}}},{{{4,0},{5,0},{6,0},{5,1}}},{{{4,0},{5,0},{6,0},{6,1}}},{{{6,0},{5,0},{5,1},{4,1}}},{{{4,0},{5,0},{5,1},{6,1}}},{{{4,0},{5,0},{5,1},{4,1}}}};
//ILTJSZO
Do you think it could be longer? ;)

Esoteric language: Timef**k

10 December 2009 - 08:31 PM

I wrote this interpreter in C for my esoteric language Timef**k
#include "stdio.h"
#include "stdlib.h"

char code[1000];
char data[10][100];
int ip[100];
int dx[100];
int dt[100];
char output[100];
int op[100];

int skip(int i){
	while(code[i] != ')')
		if(code[i] == '(') i = skip(i+1);
		else  i++;
	return i;
}

int exec(int ct,int ei){
	printf("exec %d %d\n",ct,ei);
	int i = ip[ct];
	int x = dx[ct];
	int t = dt[ct];
	int o = op[ct];
	while(i<ei){
		printf("%d %d %d %d : %c\n",ct,i,x,t,code[i]);
		if(code[i] == '>') if(x<9) x++; else x=0;
		if(code[i] == '<') if(x>0) x--; else x=9;
		if(code[i] == 'v'){
			exec(t,i+1);
			if(t<99) t++;
		}
		if(code[i] == '^') if(t>0) t--;
		if(code[i] == '(') if(data[x][t] == 0) i = skip(i+1);
		if(code[i] == '|') {
			int j;
			for(j=0;j<10;j++) data[j][ct+1] = data[j][ct];
			ip[ct]=i;
			dx[ct]=x;
			dt[ct]=t;
			op[ct]=o;
			ct++;
			t++;
		}
		if(code[i] == '+')data[x][t]++; 
		if(code[i] == '-')data[x][t]--;
		if(code[i] == '.'){
			output[o++] = data[x][t];
			printf("%d was %d or '%c' at %d\n",x,data[x][t],data[x][t],t);
		}
		i++;
	}
	printf("end exec\n");
}


int main(){
	int i;
	for(i=0;i<1000;i++)
		if((code[i]=getchar())=='#')
			break;
	code[i]=0;
	puts(code);
	exec(0,i);
	puts(output);
	for(i=0;i<10;i++)printf("%d\n",output[i]);
}

Really fast prime finder

14 October 2009 - 11:36 AM

This program outputs, for any number n, all primes < n. On my computer, it outputs all < 10000000 in 22 seconds wall clock time.:thumbup1:
#include "stdio.h"
#include "stdlib.h"

struct list {
	int i;
	struct list * next;
} *first, *last;

void add_prime(){
	int i=last->i+1;
	while(!check_prime(i))i++;
	last->next = malloc(sizeof(struct list));
	last=last->next;
	last->i=i;
	last->next=NULL;
	//printf("add %d",i);
}

int check_prime(int i){
	if(i>last->i*last->i)
		add_prime();
	struct list *p=first;
	while(i%p->i)
		if(p->next)
			p=p->next;
		else
			return 1;
	return 0;
}

int main(int argc,char ** argv){
	printf("%d\n",2);
	int lim;
	if(argc==1)lim=100;
	else sscanf(argv[1],"%d",&lim);
	first=last=malloc(sizeof(struct list));
	first->i=2;
	first->next = NULL;
	int i=2;
	while(++i<lim)
		if(check_prime(i))
			printf("%d\n",i);
}

It uses a linked list to store all primes that are necessary. In particular, to save space, it only stores primes that are actually necessary to prove the others' primacy. Please post comments and suggestions.

The final blow vs. Java

11 October 2009 - 11:47 AM

Ok, I've had enough. Java is not the best language for teaching programming. Here's why:
The first program you write is customarily the Hello World message. Here's C:
#include "stdio.h"
int main(){
        puts("Hello, world!");
}
4 lines. Easy. You just have to explain the main function, how to include a library, and the way the puts function works.
Next, C++:
#include "iostream"
int main(){
	std::cout << "Hello, world!\n";
}
Once again 4 lines. Pretty much the same things to explain, except you're using the output operator << instead.
Here's Python, Perl, BASIC, and Ruby:
print "Hello, world!"
1 line. That's the minimum, and it says what it does. Really easy.
Now, here's Java:
class Hello {
	public static void main() {
		System.out.println("Hello World!");
	}
}
A class. You have to define a class, and then...
public static void. What the **. I don't know what that means, but it shouldn't be necessary for the first program. And what's with the name of the puts-alike function? Why is it inside two of what I assume are packages? And, it's 5 lines.
Java sucks.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download