Page 2 of 4
Re: Programming Practice
Posted: Thu Feb 12, 2009 4:17 pm
by M_D_K
Your first attempt was a total fail
It printed "FooBar" before 1 and your app stopped at 99 which is an off by one error. This make it a fail.
You second attempt much better you successfully did a recursive version and no off by one error. So 10 points for the recursive and I gonna assume that you also fixed the for loop version so total 18
Also other scores:
BOMERMAN: 16 points did both the for loop version and recursive gained the full 16.
MakazeGamer: 20 points and beat BOMBERMAN to completion.
M_D_K(thats me): 30 points since I made the challenge
OMFG EDIT!!!!2 BOMERMAN has 16 points total.
Re: Programming Practice
Posted: Thu Feb 12, 2009 4:23 pm
by KuramaYoko10
M_D_K wrote:Your first attempt was a total fail
It printed "FooBar" before 1 and your app stopped at 99 which is an off by one error. This make it a fail.
You second attempt much better you successfully did a recursive version and no off by one error. So 10 points for the recursive and I gonna assume that you also fixed the for loop version so total 18
Also other scores:
BOMERMAN: 20 points did both the for loop version and recursive gained the full 20.
MakazeGamer: 20 points and beat BOMBERMAN to completion.
M_D_K(thats me): 30 points since I made the challenge
Nice... I am happy I got better and a 18 out of 20!!
However I dont see those two errors in the first program attempt... I copied and paste the code I posted here in the forum and I got it all right - starting on 1 , and finishing on Bar (100 is a multiple of 5)!!
lol but it is ok... send more challenges =D
Re: Programming Practice
Posted: Thu Feb 12, 2009 4:50 pm
by M_D_K
Sparda: 19 out of 20. So close but no cookie for you sparda.
Re: Programming Practice
Posted: Thu Feb 12, 2009 4:51 pm
by Ginto8
OK then, with recursion:
Click here to see the hidden message (It might contain spoilers)
Code: Select all
#include <iostream>
#include <conio.h>
using namespace std;
void divisible(int number, int max)
{
bool divBy3 = ((number % 3) == 0);
bool divBy5 = ((number % 5) == 0);
if(divBy3 && !divBy5)
cout << "foo\n";
else if(!divBy3 && divBy5)
cout << "bar\n";
else if(divBy3 && divBy5)
cout << "foobar\n";
else
cout << number << endl;
int nextNumber = number + 1;
if(nextNumber <= max)
divisible(nextNumber, max);
}
int main()
{
divisible(1, 100);
getch();
}
Re: Programming Practice
Posted: Thu Feb 12, 2009 4:59 pm
by M_D_K
Ginto8 wrote: OK then, with recursion:
Click here to see the hidden message (It might contain spoilers)
Code: Select all
#include <iostream>
#include <conio.h>
using namespace std;
void divisible(int number, int max)
{
bool divBy3 = ((number % 3) == 0);
bool divBy5 = ((number % 5) == 0);
if(divBy3 && !divBy5)
cout << "foo\n";
else if(!divBy3 && divBy5)
cout << "bar\n";
else if(divBy3 && divBy5)
cout << "foobar\n";
else
cout << number << endl;
int nextNumber = number + 1;
if(nextNumber <= max)
divisible(nextNumber, max);
}
int main()
{
divisible(1, 100);
getch();
}
Umm you did pretty bad. riddle me this batman do you ever see a foobar in your output? I doubt it. Since you put it at the bottom of an else if chain. no bonus points for you
Re: Programming Practice
Posted: Thu Feb 12, 2009 5:50 pm
by Arce
MDK, i did yours since it's simple and easy...For those of you who're having trouble with it, I'd recommend acquainting yourselves with the modulus operator (% in most languages). It's very useful, and I use it several times in the editor.
Click here to see the hidden message (It might contain spoilers)
Recursion:
Java:
Code: Select all
// base case: O - Do nothing.
// if you call foobar(100), it will print out starting at 1 and ending at 100.
public static void fooBar(int n) {
if(n!=0) {
fooBar(n-1);
if(n % 3 == 0 && n % 5 == 0)
System.out.println("foobar");
else if(n % 3 == 0)
System.out.println("foo");
else if(n % 5 == 0)
System.out.println("bar");
}
}
C++
Code: Select all
// base case: O - Do nothing.
// if you call foobar(100), it will print out starting at 1 and ending at 100.
void fooBar(int n) {
if(n!=0) {
fooBar(n-1);
if(n % 3 == 0 && n % 5 == 0)
std::cout << "foobar\n";
else if(n % 3 == 0)
std::cout << "foo\n";
else if(n % 5 == 0)
std::cout << "bar\n";
}
}
Blitz:
Code: Select all
Function fooBar(num)
if num > 0 Or num < 0 then
if(num mod 3 = 0) and (num mod 5 = 0) then print "foobar"
elseif (num mod 3 = 0) print "foo"
elseif (num mod 5 = 0) print "bar"
endif
End Function
Iteration:
Java:
Code: Select all
public static void foobar() {
for(int n =0;n<100;++n) {
if(n % 3 == 0 && n % 5 == 0)
System.out.println("foobar");
else if(n % 3 == 0)
System.out.println("foo");
else if(n % 5 == 0)
System.out.println("bar");
}
}
C++
Code: Select all
void foobar() {
for(int n =0;n<100;++n) {
if(n % 3 == 0 && n % 5 == 0)
std::cout << "foobar");
else if(n % 3 == 0)
std::cout << "foo";
else if(n % 5 == 0)
std::cout << "bar";
}
}
Blitz:
Code: Select all
Function fooBar()
for num = 1 to 100
if(num mod 3 = 0) and (num mod 5 = 0) then print "foobar"
elseif (num mod 3 = 0) print "foo"
elseif (num mod 5 = 0) print "bar"
next
End Function
Also, has nobody else been able to do the coin one? I wasn't very specific, but what I wanted was this output:
10 cents = 0 quarters + 0 dimes + 0 nick1es + 1O pennies
10 cents = 0 quarters + 0 dimes + 1 nick1es + 5 pennies
10 cents = 0 quarters + 0 dimes + 2 nick1es + O pennies
10 cents = 0 quarters + 1 dimes + 0 nick1es + O pennies
For every correct combination of change to create the given amount. I'm NOT testing for efficiency, and no huge numbers will be tested. Again, I have some sample outputs if you need them...
Anyone want to try it? Doing it iteratively really presses your brain with compound loops, haha.
Re: Programming Practice
Posted: Thu Feb 12, 2009 6:08 pm
by KuramaYoko10
Hey Arce.. have you tried my code yet?? ... it is there on the first page!!
BTW, can you pm me your solution for the coin problem written in C/C++ ??
thanks in advance
Re: Programming Practice
Posted: Thu Feb 12, 2009 6:12 pm
by Arce
I'11 give your code a try nexttime I'm at schoo1. Current1y don't have a C++ dev environment setup! (working on it right now). Uninsta11ed my i11egit MVC++ too1s in favor of freeware so1utions (in the event that I create something worthwhi1e and don't want to get sued...)
Current1y have Jcreator for java and will upgrade to NetBeans when I get time. Will likely give codeblocks another try, or *shudder*Bloodshed DevC++...If I don't like either, I may revert to my legit MVC++ 6 (outdated, but I liked it...)
Re: Programming Practice
Posted: Thu Feb 12, 2009 8:24 pm
by Amarant
Here's my attempt at M_D_K's challenge.
Since I have already seen all the other solutions I tried to solve it using a different approach.
- Not using % operator.
- No special code to print foobar when the number is a multiple of 3 and 5.
Click here to see the hidden message (It might contain spoilers)
Code: Select all
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char * argv[])
{
int i, three = 1, five = 1;
for(i = 1; i <= 100; i++, three++, five++)
{
if(three == 3){ printf("foo"); three = 0; }
if(five == 5){ printf("bar"); five = 0; }
if(five != 0 && three != 0) printf("%d",i);
printf("\n");
}
return EXIT_SUCCESS;
}
Re: Programming Practice
Posted: Thu Feb 12, 2009 9:06 pm
by wearymemory
I'm not that content with my code for a dirth of eloquence, but here's my solution to the coin handling problem in Java using nested for loops:
Click here to see the hidden message (It might contain spoilers)
Code: Select all
public class CoinHandler {
public static void main(String[] args) {
double val = 1.50;
for(int quarters = 0; quarters * 25 <= val * 100; quarters++) {
int newVal = (int) ((val * 100) - quarters * 25);
for(int dimes = 0; dimes * 10 <= newVal; dimes++) {
newVal -= dimes * 10;
for(int nickels = 0; nickels * 5 <= newVal; nickels++) {
int pennies = (newVal - nickels * 5);
System.out.println("Quarters: " + quarters + ", Dimes: " + dimes + ", Nickles: " + nickels + ", Pennies:"+ pennies);
}
}
}
}
}
I'd like to have a go at this using recursion tomorrow.
I'd also like to see yours, Arce.
Re: Programming Practice
Posted: Fri Feb 13, 2009 5:20 am
by Ginto8
M_D_K wrote:Ginto8 wrote: OK then, with recursion:
Click here to see the hidden message (It might contain spoilers)
Code: Select all
#include <iostream>
#include <conio.h>
using namespace std;
void divisible(int number, int max)
{
bool divBy3 = ((number % 3) == 0);
bool divBy5 = ((number % 5) == 0);
if(divBy3 && !divBy5)
cout << "foo\n";
else if(!divBy3 && divBy5)
cout << "bar\n";
else if(divBy3 && divBy5)
cout << "foobar\n";
else
cout << number << endl;
int nextNumber = number + 1;
if(nextNumber <= max)
divisible(nextNumber, max);
}
int main()
{
divisible(1, 100);
getch();
}
Umm you did pretty bad. riddle me this batman do you ever see a foobar in your output? I doubt it. Since you put it at the bottom of an else if chain. no bonus points for you
There are definitely foobar's in there. I put it at the bottom of an if else chain, but that doesn't matter because I was using && every time.
Re: Programming Practice
Posted: Fri Feb 13, 2009 7:18 am
by KuramaYoko10
Ginto8 wrote:M_D_K wrote:Ginto8 wrote: OK then, with recursion:
Click here to see the hidden message (It might contain spoilers)
Code: Select all
#include <iostream>
#include <conio.h>
using namespace std;
void divisible(int number, int max)
{
bool divBy3 = ((number % 3) == 0);
bool divBy5 = ((number % 5) == 0);
if(divBy3 && !divBy5)
cout << "foo\n";
else if(!divBy3 && divBy5)
cout << "bar\n";
else if(divBy3 && divBy5)
cout << "foobar\n";
else
cout << number << endl;
int nextNumber = number + 1;
if(nextNumber <= max)
divisible(nextNumber, max);
}
int main()
{
divisible(1, 100);
getch();
}
Umm you did pretty bad. riddle me this batman do you ever see a foobar in your output? I doubt it. Since you put it at the bottom of an else if chain. no bonus points for you
There are definitely foobar's in there. I put it at the bottom of an if else chain, but that doesn't matter because I was using && every time.
That should matter, since if it is in the bottom of the loop, it will check first for the first 'if' and if it that works it will skip the others 'else if' ...
But since Ginto8 have added a new argument in his 'if', denying other possibilities, it should work:
Re: Programming Practice
Posted: Fri Feb 13, 2009 3:59 pm
by M_D_K
OOOOPS I skimmed it since I was up for 30+ hrs
10 points for Ginto.
Re: Programming Practice
Posted: Sat Feb 14, 2009 5:33 am
by Venom
KuramaYoko10 wrote: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;
}
Hey Kurama...
I Remade your code with recursion (just learned with the foobar-Arce`s post up there...so i don`t know if that`s the best way ^^)
Click here to see the hidden message (It might contain spoilers)
Code: Select all
#include <stdio.h>
#include <stdlib.h>
void Check(int b);
const int CoinVal[4] = {25,10,5,1};
int Coinqnt[4];
int InputCents;
int Cents;
int main(int argc, char *argv[])
{
printf("Please enter an amount of cents that you want me to handle: ");
scanf("%d", &InputCents);
system("CLS");
Cents = InputCents;
if (InputCents > 0){
Check(0);
}
else{
printf("Invalid Number \n");
}
system("PAUSE");
return 0;
}
void Check(int b)
{
for(Coinqnt[b] = Cents / CoinVal[b]; Coinqnt[b] >= 0; Coinqnt[b]--)
{
Cents = InputCents - ((Coinqnt[0]*CoinVal[0]) + (Coinqnt[1]*CoinVal[1]) + (Coinqnt[2]*CoinVal[2]) + (Coinqnt[3]*CoinVal[3]));
if(Cents == 0)
{
printf("%d cents = %d pennies , %d nickles , %d dimes , %d quarters \n\n", InputCents, Coinqnt[3], Coinqnt[2], Coinqnt[1], Coinqnt[0]);
}
else if(b!=3)
{
Check(b+1);
}
}
}
Re: Programming Practice
Posted: Tue Mar 10, 2009 12:55 pm
by KuramaYoko10
Ok... I dont know if I should bring this topic up again, but I like the idea of it, and I will post a very simple challenge which I remember doing when I was learning the basics of C (which I still am... kinda lol)
You are requested to print on screen X numbers of the fibonacci sequence (starting from 0), being X the variable that the user inputs.
The fibonacci sequence is an interesting sequence of numbers in which you get the previous number, add to your current number to get the result of the next number of the sequence ... here is how it starts:
*more info at:
http://en.wikipedia.org/wiki/Fibonacci_sequence
I have the code done, but I will try thinking on different ways of solving it, so it will be an exercise to me as well ... I will wait for some replys and then post my code here (and because i am not on my computer right now =P)