Page 1 of 4

Programming Practice

Posted: Tue Feb 10, 2009 11:29 am
by Arce
Are you new to programming? Or perhaps you're just starting off with a new language? Trying to master syntax of a scripting language you're unfamiliar with?

Well, the best way to get there is to practice. Those crappy little apps that exist simply for the sake of practice are sometimes the correct route to go.

I'm officially dedicating this topic to practice problems and solutions. Have an interesting problem that will help to focus on a specific aspect of a language? Or just a great logical problem? Feel free to post here.

Need help answering one? Want to post some you've done at work, school, etc? Go for it.

There are tons of interesting, brainfuck programs that require logic you probably won't need in daily programming. And there are others that are practically a simulation of the brain activity required to during a long night of devving.

Re: Programming Practice

Posted: Tue Feb 10, 2009 11:35 am
by Arce
Aight, I'll start off with some, and post more when I have time (and shouldn't be studying thermodynamics).

If this topic increases, I may even edit my first post and create a problem archieve.

Problem 1: (short app)
Click here to see the hidden message (It might contain spoilers)
Write a program that prompts the user to input a positve amount of cents (US currency, 1/100th a dollar). Then have the program display every possible way of creating the given amount using quarters (25 cents), dimes (10 cents), nickles (5 cents), and pennies (1 cent).


Most common approaches to this one are iteration or recursion...So it'll probably make heavy use of looping. If you want some correct output values, I'll post them later.

The more languages this is answered in, the cooler. XD

I can do it in Java, Lua, Blitz, and C++, if anyone needs help.

Re: Programming Practice

Posted: Tue Feb 10, 2009 6:47 pm
by eatcomics
I might be able to do it in just basic... But I really don't want to and it probably wouldn't help anyone... :nono:

Re: Programming Practice

Posted: Tue Feb 10, 2009 8:20 pm
by Ginto8
Here's an idea for the up-and-coming network programmer:
Click here to see the hidden message (It might contain spoilers)
Create a console-based chat program (both server and client). Make it read the server's IP from a configuration file

Also helps with some file I/O. ;)

Re: Programming Practice

Posted: Tue Feb 10, 2009 9:57 pm
by MarauderIIC
Ginto, read beej's guide to network programming linked from here http://elysianshadows.com/phpBB3/viewto ... ?f=6&t=148

Re: Programming Practice

Posted: Wed Feb 11, 2009 8:21 am
by KuramaYoko10
Hey Arce,

Very nice idea of yours ... I got pretty excited about it and with how I could improve my programming with those excercises!!

I have done the "coin handler" one, it works and shows every possibility, but it is not effective I guess, since I made it divided in several functions...
So, I ask you to look at my code and see if there is a way to put those 4 functions (checkQuarter, checkDime, checkNickle, checkPenny) in only one, using recursion maybe?

Ok here goes the code... bug free until now (I have fixed 2 since my first reply here) :)
Click here to see the hidden message (It might contain spoilers)

Code: Select all

#include <stdio.h>
#include <stdlib.h>



void printPossibility();

//One function for each coin type check, since the loop is getting smaller and different for each coin value 
//going from 25 to 1
void checkPenny();
void checkNickle();
void checkDime();
void checkQuarter();

const int PENNY = 1;
const int NICKLE = 5;
const int DIME = 10;
const int QUARTER = 25;

//The input cents is the value the user have typed ... constant value
//The cents acquires the value of input, but it will be manipulated in the checking loop 
int inputCents;
int cents;
int pennyCoin = 0;
int nickleCoin = 0;
int dimeCoin = 0;
int quarterCoin = 0;

//It was necessary to set 'q' and 'qq' and so on, so that the 'q--' wouldnt interfere on the loop (for a = 0; a < qq ...)
int q = 0;
int qq = 0;

int d = 0;
int dd = 0;

int n = 0;
int nn = 0;

int p = 0;
int pp = 0;





void checkPenny()
{
	p = (int)(cents / PENNY);
	pp = p;

	if(pp >= 1)
	{
		for(int a = 0; a <= pp; a++)  //the equal sign in  'a<='  ...  fix bug 1
		{
			quarterCoin = q;
			dimeCoin = d;
			nickleCoin = n;
			pennyCoin = p;
			cents = inputCents - ((q*QUARTER) + (d*DIME) + (n*NICKLE) + (p*PENNY));

			if(cents == 0)
			{
				printPossibility();
			}

			p--;
		}
	}
}


void checkNickle()
{
	n = (int)(cents / NICKLE);
	nn = n;

	if(nn >= 1)
	{
		for(int a = 0; a < nn; a++)
		{
			quarterCoin = q;  //redefining the coin values for the coins checked before fix bug 2
			dimeCoin = d;
			nickleCoin = n;
			cents = inputCents - ((q*QUARTER) + (d*DIME) + (n*NICKLE) + (p*PENNY));

			if(cents == 0)
			{
				printPossibility();
			}
			else
			{
				checkPenny();
			}

			n--;
		}
	}
}


void checkDime()
{
	d = (int)(cents / DIME);
	dd = d;

	if(dd >= 1)
	{
		for(int a = 0; a < dd; a++)
		{
			quarterCoin = q;
			dimeCoin = d;
			cents = inputCents - ((q*QUARTER) + (d*DIME) + (n*NICKLE) + (p*PENNY));

			if(cents == 0)
			{
				printPossibility();
			}
			else
			{
				checkNickle();

				dimeCoin = d;
				cents = inputCents - (d * DIME);

				checkPenny();
			}

			d--;
		}
	}
}


void checkQuarter()
{
	q = (int)(cents / QUARTER);
	qq = q;

	if(qq >= 1)
	{
		for(int a = 0; a < qq; a++)
		{
			quarterCoin = q;
			cents = inputCents - ((q*QUARTER) + (d*DIME) + (n*NICKLE) + (p*PENNY));

			if(cents == 0)
			{
				printPossibility();
			}
			else
			{
				checkDime();

				quarterCoin = q;
				cents = inputCents - (q * QUARTER);

				checkNickle();

				quarterCoin = q;
				cents = inputCents - (q * QUARTER);

				checkPenny();
			}

			q--;
		}
	}
}


//The coins are checked from the highest (quarter) to the smallest (penny)
//*Try putting it in only one function
void handleCoins()
{
	checkQuarter();

	cents = inputCents;

	checkDime();
	
	cents = inputCents;

	checkNickle();

	cents = inputCents;

	checkPenny();
}


void printPossibility()
{
	printf("%d pennies , %d nickles , %d dimes , %d quarters \n\n", pennyCoin, nickleCoin, dimeCoin, quarterCoin);

	pennyCoin = 0;
	nickleCoin = 0;
	dimeCoin = 0;
	quarterCoin = 0;
}


int main(int argc, char *argv[])
{
	printf("Please enter an amount of cents that you want me to handle: \n");
	scanf("%d", &inputCents);
	system("CLS");
	

	while(inputCents > 0)
	{
		cents = inputCents;

		handleCoins();

		inputCents = 0;
	}
	
	system("PAUSE");
	return 0;
}

Re: Programming Practice

Posted: Wed Feb 11, 2009 5:32 pm
by Arce
Haha, great job! As per usua1, I'm somewhat pressed for time...But I'11 definate1y take a 1ook at your code when I can and try to come up with a recursive so1ution.

Atm, though, I'd recommend trying to use compounded, nested "for" statements to try and so1ve it? That was my origina1 approach. If you need he1p with it I'd g1ad1y paste some code snippets.

And, if you guys 1ike this type of thing, I can post tons and tons more. Just 1emme know what you want.

A1so, I've got a fu11 competitive program even that I can post as we11 as samp1e and graded test data. It was the 2nd competitive program, to be done in any 1anguage, of the 2OO9 ACS1 senior division computer science competition. If anybody here is interested (honest1y, i thought it was god-easy, but some of you may not...) I can post it and some initia1 test va1ues. Then, when you're done, I'11 give you 'rea1 va1ues' to test it with, you give me your program's output, and I'11 score ya! (x/5) =P

Re: Programming Practice

Posted: Thu Feb 12, 2009 8:03 am
by M_D_K
OK I have one for yout all. Now it seems alot of programmers(experienced ones too) have problems figuring this one out.

OK so here it is
Click here to see the hidden message (It might contain spoilers)
Print the numbers from 1 to 100 if the number is a multiple of 3 print foo, if it is a multiple of 5 print bar, if its both print foobar. BTW you'll be printing foo,bar,etc instead of the number

Seems easy but I know people who have been scratching their respective heads on this. When I heard of it I figured it out in like 10 seconds ;)

EDIT: You don't have to make it recursive but bonus points if you do.
Good Luck.

Re: Programming Practice

Posted: Thu Feb 12, 2009 9:35 am
by KuramaYoko10
I tried to work it out real quick and here is what I got :)

Click here to see the hidden message (It might contain spoilers)

Code: Select all

#include <stdio.h>



int number = 1;
int divided3 = 0;
int divided5 = 0;


int main()
{
	for(int a = 1; a < 101; a++)
	{
		divided3 = number / 3;
		divided5 = number / 5;


		if(number == divided3 * 3  &&  number == divided5 * 5)
		{
			printf("FooBar \n");
		}
		else if(number == divided3 * 3)
		{
			printf("Foo \n");
		}
		else if(number == divided5 * 5)
		{
			printf("Bar \n");
		}
		else
			printf("%d \n", number);

		number++;
	}


	return 0;
}


Later on, when I have more time, I will try polishing the "coin handler" code and this one too xD

Re: Programming Practice

Posted: Thu Feb 12, 2009 9:40 am
by M_D_K
KuramaYoko10 wrote:I tried to work it out real quick and here is what I got :)

Click here to see the hidden message (It might contain spoilers)
code


Later on, when I have more time, I will try polishing the "coin handler" code and this one too xD
Close but there is a better solution. and its one operator(thats a clue) ;)

Re: Programming Practice

Posted: Thu Feb 12, 2009 9:42 am
by M_D_K
KuramaYoko10 wrote:I tried to work it out real quick and here is what I got :)

Click here to see the hidden message (It might contain spoilers)
code


Later on, when I have more time, I will try polishing the "coin handler" code and this one too xD
Close but there is a better solution. and its one operator(thats a clue) ;)
Also think whats the lowest common denominator for both a multiple of 3 and 5.

Re: Programming Practice

Posted: Thu Feb 12, 2009 2:08 pm
by Ginto8
M_D_K wrote:OK I have one for yout all. Now it seems alot of programmers(experienced ones too) have problems figuring this one out.

OK so here it is
Click here to see the hidden message (It might contain spoilers)
Print the numbers from 1 to 100 if the number is a multiple of 3 print foo, if it is a multiple of 5 print bar, if its both print foobar. BTW you'll be printing foo,bar,etc instead of the number

Seems easy but I know people who have been scratching their respective heads on this. When I heard of it I figured it out in like 10 seconds ;)

EDIT: You don't have to make it recursive but bonus points if you do.
Good Luck.
:lol: I was bored...
Click here to see the hidden message (It might contain spoilers)

Code: Select all

#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
    for(int i = 1; i <= 100; ++i)
    {
            bool divBy3 = ((i % 3) == 0);
            bool divBy5 = ((i % 5) == 0);
            if(divBy3 && !divBy5)
                 cout << "foo\n";
            else if(!divBy3 && divBy5)
                 cout << "bar\n";
            else if(divBy3 && divBy5)
                 cout << "foobar\n";
            else
                 cout << i << endl;
    }
    getch();
}

and what do you mean by "recursive"? :shock:

Re: Programming Practice

Posted: Thu Feb 12, 2009 3:07 pm
by M_D_K
6 out of 10 for that Ginto. You must work harder ;) Not that its bad or anything.

Recursion is where a function is the algorithm and it calls itself over and over(basically). For example

Code: Select all

void PlusPlus(int end, int num)
{
        num++;
        printf("num:%d\n", num);
        if(num < end)
                PlusPlus(end, plus);
};
Extremely basic but thats the idea of it. Look here for more info.
BTW recursion will get you 10 bonus points.

Re: Programming Practice

Posted: Thu Feb 12, 2009 3:26 pm
by dandymcgee
Can I buy a T-Shirt with the points? If so I might consider trying. Interesting problems indeed :P.

Re: Programming Practice

Posted: Thu Feb 12, 2009 3:59 pm
by KuramaYoko10
Nice... thanks to this problem and Ginto8's answers I got to know about the modulus operator '%' and how to implement it O.o :)

Now, what was the grade of my first trial ?? and the grade of this trial now?? xD

Here it is:

P.S.: I dont know if this is a "recursion" but I have tried, and looking to your example now, it looks alike xD

Click here to see the hidden message (It might contain spoilers)

Code: Select all

#include <stdio.h>


void recursiveSolution(int number);

int number = 1;


void recursiveSolution(int number)
{
	if(number <= 100)
	{
		if(number % 15 == 0)
			printf("FooBar \n");

		else if(number % 3 == 0)
			printf("Foo \n");

		else if(number % 5 == 0)
			printf("Bar \n");
		
		else
			printf("%d \n", number);

		number++;
		
		recursiveSolution(number);
	}
	else
		printf("\nProgram finished \n\n");
}


int main()
{
	recursiveSolution(number);


	return 0;
}