All pastes #964636 Raw Edit

Anonymous

public c v1 · immutable
#964636 ·published 2008-03-31 12:36 UTC
rendered paste body
#include	<stdio.h>   // printf and such#include	<stdlib.h> // calloc(), random()#include	<conio.h> // clrsrc()#define	QLEN	30#define	QARLEN	5#define	CLEN	30typedef	enum{False,True}bool;typedef	struct Competitor{unsigned int answer; /* 0,1,2 */bool	     playing; /* Have we used the person ? */}Competitor;typedef	struct	Helper{	/* Helper is basically the same as Competitor	 * but it just holds the index number of the player picked	 * to help us	 */unsigned int answer; /* 0,1,2 */int	     location; // index}Helper;typedef	struct User{long int		money;unsigned int	answer; // dunno, could be more..}User;typedef	struct Question{char		*QuestStr;unsigned int	picked; /* The only reason I'm using unsigned			 *  int is to "save space" */unsigned int	answer;}Question;unsigned int	pickA();int		pickQ();void		PickTwoPersons(unsigned int answer, Competitor *src, Helper *target);void	main(){int		i = 0;Question 	q[QARLEN];Competitor      Players[CLEN];Helper		Helpers[2];User		u;// Clean the screen nicely	clrscr();/* too lazy to write an init function * writing it here.. */	printf("Q/Answers board\n");	for (i =  0;i < 5;i++) // Initialize the array	{		q[i].QuestStr	= (char *)calloc(sizeof(char),QLEN); // Calloc zeroes the array out, and does the same as malloc		q[i].picked	= 0; // We haven't picked this one yet		q[i].answer	= pickA();		// I'm only printing it nicely, ignore that		((i+1)%3) ? printf("%4u",q[i].answer) : printf("%4u\n",q[i].answer);	}	puts("\n"); // two newlines..	for(i=0;i<CLEN;i++)	{		/* We're automatically picking an		 * answer since I'm mega-lazy to initialize		 * questions, etc		 */		Players[i].answer 	= random(3);		Players[i].playing	= True; // We can't use 0 since I've declared it as enum, the compiler gets bitchy		// Everyone is playing at start		// I'm also using this loop to print the board nicely		// So I could compare my answers/locations I got from the function		// ignore this line if you'd like to ;)		((i+1) % 3) ? printf("%4u",Players[i].answer) : printf("%4u\n",Players[i].answer);	}	u.money		= 660; // I'm only filling the struct	u.answer	= 1;  // to prepare it for the next stage	// The real thing...	// Since an array is actually a pointer	// we don't need the '&' opreator	PickTwoPersons(u.answer,Players,Helpers);	// No error checking, be careful !	printf("Got - %d %d\n",Helpers[0].location,Helpers[1].location);}int	pickQ(){	return	(random(QARLEN+1)); //  0...5}unsigned int	pickA(){	return	random(3); // 0..3}void	PickTwoPersons(unsigned int answer, Competitor *src, Helper *target){int	i,flag = 0;	for (i=0;i<CLEN && !flag;i++)	{		printf("Comparing src[%d].answer(%d) with %u\n",i,src[i].answer,answer);		if (src[i].answer == answer)		{//		printf("i == %d\n",i); debugging			flag = 1;		}	}	if (flag)	{		target[0].answer	= src[i].answer;//		printf("i == %d\n",i-1); debugging		target[0].location	= i-1;  // i is incremented once more						// before we exit the loop						// so we gotta decrease it	}	else	{		printf("We couldn't find anyone who has done this q right, this sucks..exitting..");		return; // Returning to the calling function(main)	}	flag	= 0;	for (i=0;i<CLEN && !flag ;i++)	{		if (src[i].answer != answer)		     flag	= 1;	}	if (flag)	{		target[1].answer	= src[i].answer;		target[1].location	= i-1;//		printf("%u location\n",target[1].location); debugging	}	else	{		printf("We couldn't find anyone who got wrong in this question...this sucks..\n");	}}