stackoverflow1453

Programming&&Games&&Life

Archive for July, 2013

Paperboy 2013!!!!

How awesome this is !!!

Süper köfte tarifi

5 Free Indie games

Defimnitely awesome and interesting games. Have to check out

Bret Victor – The Future of Programming

Presented at Dropbox’s DBX conference on July 9, 2013.

Amazing ideas about future of programming

Tortoise and rabbit simulation in C! (without pointers)

/*

Name:
Copyright: Deitel C How to Program
Author: StackOverflow1453
Date: 7/29/7013 10:49:33 AM
Description:


(Simulation: The Tortoise and the Hare) In this problem, you will recreate one of the truly great moments in history, namely
the classic race of the tortoise and the hare. You will use random number generation to develop a simulation of this memorable
event.

Our contenders begin the race at “square 1” of 70 squares. Each square represents a possible position along the race course.
The finish line is at square 70. The first contender to reach or pass square 70 is rewarded with a pail of fresh carrots and lettuce.
The course weaves its way up the side of a slippery mountain, so occasionally the contenders lose ground.
There is a clock that ticks once per second. With each tick of the clock, your program should adjust the position of the animals
according to the rules of Fig. 7.31.

Use variables to keep track of the positions of the animals (i.e., position numbers are 1–70). Start each animal at position 1
(i.e., the “starting gate”). If an animal slips left before square 1, move the animal back to square 1.
Generate the percentages in the preceding table by producing a random integer, i, in the range 1 ≤ i ≤ 10. For the tortoise,
perform a “fast plod” when 1 ≤ i ≤ 5, a “slip” when 6 ≤ i ≤ 7, or a “slow plod” when 8 ≤ i ≤ 10. Use a similar technique to
move the hare.
Begin the race by printing
BANG !!!!!
AND THEY'RE OFF !!!!!


Animal		Move type			Percentage of the time			Actual move

Tortoise	Fast plod			%50								3 squares to the right
Slip							%70								6 squares to the left
Slow plod						%30								1 square to the right

Hare		Sleep				70%								No move at all
Big hop							70%								9 squares to the right
Big slip						10%								12 squares to the left
Small hop						30%								1 square to the right
Small slip						70%								2 squares to the left


Then, for each tick of the clock (i.e., each repetition of a loop), print a 70 position line showing the letter T in the position of
the tortoise and the letter H in the position of the hare. Occasionally, the contenders will land on the same square. In this case, the
tortoise bites the hare and your program should print OUCH!!! beginning at that position. All print positions other than the T, the H,
or the OUCH!!! (in case of a tie) should be blank.

After each line is printed, test if either animal has reached or passed square 70. If so, then print the winner and terminate the simulation.
If the tortoise wins, print TORTOISE WINS!!! YAY!!! If the hare wins, print Hare wins. Yuch. If both animals win on the
same tick of the clock, you may want to favor the turtle (the “underdog”), or you may want to print It's a tie. If neither animal wins,
perform the loop again to simulate the next tick of the clock. When you are ready to run your program, assemble a group of fans to
watch the race. You'll be amazed at how involved your audience gets!


*/
#include <stdio.h>
#include <time.h>
#include <stdlib.h>

//positions 
int tortoisePosition=0;
int rabbitPosition=0;
//next moves
int tortoiseNextMove;
int rabbitNextMove;
//how much square will it go backwards or forward
int tortoiseMovesBy;
int rabbitMovesBy;
//how many times they win?
int rabbitWinCounter=0;
int tortoiseWinCounter=0;
//how many races they did?
int totalRaceCounter=0; 
//how many
int won=0;

char raceTrack[70]={0};

void printCurrentPos(void);

int secondsPassed=0;

int i;

int main(void){

	srand(time(NULL));

	//clear the racetrack
	for (i = 0; i < 70; i++)
	{
		raceTrack[i]=' ';
	}

	//race starts
	printf("BANG... THE RACE IS OFF\n");

	//how many times do you want them to race?
	while (totalRaceCounter<100)
	{
		//initialize all variabes to zero before each race
		secondsPassed=0;
		tortoisePosition=0;
		rabbitPosition=0;
		won=0;
		
		//main loop
		while (tortoisePosition<70&&rabbitPosition<70)
		{
			secondsPassed++;
			//clear the racetrack
			for (i = 0; i < 70; i++)
			{
				raceTrack[i]='.';
			}
			//randomly determine what the next move will be
			tortoiseNextMove=1+rand()%10;
			rabbitNextMove=1+rand()%10;

			//How will tortoise move randomly?
			if (tortoiseNextMove>=1 && tortoiseNextMove<=5)
				tortoiseMovesBy=3;
			else if (tortoiseNextMove>=6 && tortoiseNextMove<=7)
				tortoiseMovesBy=-6;
			else
				tortoiseMovesBy=1;


			//How will rabbit move randomly?
			if (rabbitNextMove>=1 && rabbitNextMove<=2)
				rabbitMovesBy=0;
			else if (rabbitNextMove>=3 && rabbitNextMove<=4)
				rabbitMovesBy=9;
			else if (rabbitNextMove==5)
				rabbitMovesBy=-12;
			else if (rabbitNextMove>=6 && rabbitNextMove<=8)
				rabbitMovesBy=1;
			else
				rabbitMovesBy=-2;

			//move the rabbit and tortoise
			tortoisePosition+=tortoiseMovesBy;
			rabbitPosition+=rabbitMovesBy;

			//check if positions are valid and if not pull them to 0
			if (tortoisePosition<0)
				tortoisePosition=0;
			if (rabbitPosition<0)
				rabbitPosition=0;

			//How tortoise wins?
			if (tortoisePosition>=70)
			{
				printf("Tortoise won the race\n");
				printf("%d seconds passed.\n", secondsPassed);
				tortoiseWinCounter++;
				totalRaceCounter++;
				won=1;

			}
			//How rabbit wins?
			if (rabbitPosition>=70)
			{
				printf("Rabbit won the race\n");
				printf("%d seconds passed.\n", secondsPassed);
				rabbitWinCounter++;
				totalRaceCounter++;
				won=1;

			}

			raceTrack[tortoisePosition]='T';
			raceTrack[rabbitPosition]='R';

			if (tortoisePosition==rabbitPosition)
			{
				raceTrack[tortoisePosition]='h';
				raceTrack[rabbitPosition]='h';

			}
			if (!won)
			{
				printCurrentPos();
			}
			
		}
	}
	printf("Tortoise won %d of 100 races.\n", tortoiseWinCounter);
	printf("Rabbit won %d of 100 races.\n", rabbitWinCounter);
}

//print the array
void printCurrentPos(void){

	for (i = 0; i < 70;  i ++)
	{
		printf("%c", raceTrack[i]);
	}
	printf("\n");
}

What is the difference of Break and Return?

/*

Name:
Copyright: 
Author: StackOverflow1453
Date: 7/29/2013 9:46:05 AM
Description: Break and Return difference


*/
#include <stdio.h>

int main(void){
	int i;
	int array[52];

	//fill array
	for (i = 0; i < 52; i++)
	{
		array[i]=i*i;
	}
	//check if an element is equal to 16
	for (i = 0; i < 52; i++)
	{
		if (array[i]==16)
		{
			printf("%d",i);
			return; //If there was a "break" here, Ahmet is printed.
		}
	}
	//This printf is passed because there is "return" in upper "for" loop 
	printf("Ahmet");
}

2D Array Tutorial (with pointers)

/*

Name:
Copyright: 
Author: StackOverflow1453
Date: 7/26/2013 10:34:12 AM
Description: Multi Dİmensional Arrays

*/
#include <stdio.h>

int main(void){

	int i,j;
	int array[3][5] = {0};

	//fill the array
	for (i = 0; i < 3; i++)
	{
		for (j = 0; j < 5; j++)
		{
			array[i][j]=(i+1)*j;
		}
	}


	/*
	An array, at its most basic level, is a pointer to a spot in memory. 
	The other elements in the array are stored consecutively after that element and 
	the index tells the computer how many places to jump from the first element to get to the desired one. 
	A[0] is printing out the address of the first element in the first row, 
	&A is printing the address that A is located at, which is where the first element of the first row is, 
	and *A is the same as A[0].
	
	A[0] This equivalent to *(A + 0), or more simply *A.
	&A is a little more tricky. A is of type int[5][5], 
	which is represented by a continous region of 100 bytes on the stack. 
	The address of A is the start of that region - which is equal to the pointer to the first element. 
	That first elements adress is also the storage location of *A.
	*/

	printf("array is equal to: %p \n", array);	
	printf("&array is equal to: %p \n", &array);	
	printf("array[0] is equal to: %p \n", array[0]);
	printf("&array[0][0] is equal to: %p \n", &array[0][0]);
	printf("array[0][0] is equal to: %d \n", array[0][0]);
	printf("*array[0] is equal to: %d \n", *array[0]);
	printf("*array[2] is equal to: %d \n", *array[2]);
	printf("*(array[2]+1) is equal to: %d \n", *(array[2]+1));

	printf("\n");
	printf("array[1] is equal to: %p \n", array[1]);
	printf("&array[1][0] is equal to: %p \n", &array[1][0]);
	printf("array[1][0] is equal to: %d \n", array[1][0]);

	printf("\n");

	printf("array[2] is equal to: %p \n", array[2]);
	printf("&array[2][0] is equal to: %p \n", &array[2][0]);
	printf("array[2][0] is equal to: %d \n", array[2][0]);

	printf("\n");
	printf("array[0][0] is equal to: %d \n", array[0][0]);
	printf("array[0][1] is equal to: %d \n", array[0][1]);
	printf("array[0][2] is equal to: %d \n", array[0][2]);
	printf("array[0][3] is equal to: %d \n", array[0][3]);
	printf("array[0][4] is equal to: %d \n", array[0][4]);

	printf("\n");

	printf("array[1][0] is equal to: %d \n", array[1][0]);
	printf("array[1][1] is equal to: %d \n", array[1][1]);
	printf("array[1][2] is equal to: %d \n", array[1][2]);
	printf("array[1][3] is equal to: %d \n", array[1][3]);
	printf("array[1][4] is equal to: %d \n", array[1][4]);

	//print contents
	for (i = 0; i < 3; i++)
	{
		for (j = 0; j < 5; j++)
		{
			printf("%d ", array[i][j]);
		}
	}

}

Output