Programming Practice

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

User avatar
Arce
Jealous Self-Righteous Prick
Jealous Self-Righteous Prick
Posts: 2153
Joined: Mon Jul 10, 2006 9:29 pm

Programming Practice

Post 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.
<qpHalcy0n> decided to paint the office, now i'm high and my hands hurt
User avatar
Arce
Jealous Self-Righteous Prick
Jealous Self-Righteous Prick
Posts: 2153
Joined: Mon Jul 10, 2006 9:29 pm

Re: Programming Practice

Post 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.
<qpHalcy0n> decided to paint the office, now i'm high and my hands hurt
User avatar
eatcomics
ES Beta Backer
ES Beta Backer
Posts: 2528
Joined: Sat Mar 08, 2008 7:52 pm
Location: Illinois

Re: Programming Practice

Post 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:
Image
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: Programming Practice

Post 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. ;)
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Programming Practice

Post by MarauderIIC »

Ginto, read beej's guide to network programming linked from here http://elysianshadows.com/phpBB3/viewto ... ?f=6&t=148
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
KuramaYoko10
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 55
Joined: Fri Oct 31, 2008 8:02 pm

Re: Programming Practice

Post 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;
}
Type a message here!!
[b][color=#BF0000]MarauderIIC[/color][/b] wrote:"Never" is never true in programming.
User avatar
Arce
Jealous Self-Righteous Prick
Jealous Self-Righteous Prick
Posts: 2153
Joined: Mon Jul 10, 2006 9:29 pm

Re: Programming Practice

Post 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
<qpHalcy0n> decided to paint the office, now i'm high and my hands hurt
User avatar
M_D_K
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 1087
Joined: Tue Oct 28, 2008 10:33 am
Favorite Gaming Platforms: PC
Programming Language of Choice: C/++
Location: UK

Re: Programming Practice

Post 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.
Gyro Sheen wrote:you pour their inventory onto my life
IRC wrote: <sparda> The routine had a stack overflow, sorry.
<sparda> Apparently the stack was full of shit.
User avatar
KuramaYoko10
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 55
Joined: Fri Oct 31, 2008 8:02 pm

Re: Programming Practice

Post 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
Type a message here!!
[b][color=#BF0000]MarauderIIC[/color][/b] wrote:"Never" is never true in programming.
User avatar
M_D_K
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 1087
Joined: Tue Oct 28, 2008 10:33 am
Favorite Gaming Platforms: PC
Programming Language of Choice: C/++
Location: UK

Re: Programming Practice

Post 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) ;)
Gyro Sheen wrote:you pour their inventory onto my life
IRC wrote: <sparda> The routine had a stack overflow, sorry.
<sparda> Apparently the stack was full of shit.
User avatar
M_D_K
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 1087
Joined: Tue Oct 28, 2008 10:33 am
Favorite Gaming Platforms: PC
Programming Language of Choice: C/++
Location: UK

Re: Programming Practice

Post 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.
Gyro Sheen wrote:you pour their inventory onto my life
IRC wrote: <sparda> The routine had a stack overflow, sorry.
<sparda> Apparently the stack was full of shit.
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: Programming Practice

Post 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:
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
User avatar
M_D_K
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 1087
Joined: Tue Oct 28, 2008 10:33 am
Favorite Gaming Platforms: PC
Programming Language of Choice: C/++
Location: UK

Re: Programming Practice

Post 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.
Gyro Sheen wrote:you pour their inventory onto my life
IRC wrote: <sparda> The routine had a stack overflow, sorry.
<sparda> Apparently the stack was full of shit.
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Programming Practice

Post by dandymcgee »

Can I buy a T-Shirt with the points? If so I might consider trying. Interesting problems indeed :P.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
KuramaYoko10
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 55
Joined: Fri Oct 31, 2008 8:02 pm

Re: Programming Practice

Post 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;
}
Type a message here!!
[b][color=#BF0000]MarauderIIC[/color][/b] wrote:"Never" is never true in programming.
Post Reply