I wrote a FizzBuzz program to get comfortable with C++. I haven't learned any languages before this, but I'm doing this on my own. I learned if
, for
, std::cout
, and some operators. I wrote several questions below, but if that isn't allowed I can revise.
Program uses nested for loops to evaluate whether a quotient is a equivalent to an integer.
Questions
Any problems with my variable names? Commenting? White space usage?
Instead of evaluating comparisons every time the main counter increments, would it be faster to store all possible increments in a "list" of some kind and then do my comparisons with elements in that list? Collating multiples of multiples within a list would require searching for that element within a list, which could be costly.
Would a straight forward counting approach or a list approach be faster for n counter values greater than a few digits? i.e. finding all multiples up to 100,000 or 1,000,000.
I realize after writing this that I could have used modulus
%
. However, modulus would only account for integer multiples, correct? (i.e. I would not be able to use "3.5" as one of my multiples.)To add to the above, I want to run this algorithm using floats instead of integers (e.g. multiples of fractions, or PI to several digits out), and I want to count out to some arbitrary limit using appropriate intervals (e.g. +0.5, +0.001). Regardless of my approach, are these types of evaluations inextricably linked to issues with float precision (imprecision)?
This question is off-topic (sorry), but how much of a problem in coding is the issue of organization in code? I ask because I didn't need to use the variables
int passFizz
andint passBuzz
because I could have usedint checkFizzBuzz
in their places instead. However, if I were to add dozens of multiples to check for, I'd think that using a single global variable would be organizationally poor.
#include <iostream>
// Prints numbers "1" through "100" as integers.
// Every multiple of "3" is printed as "Fizz".
// Every multiple of "5" is printed as "Buzz".
// Every common multiple of "3" and "5" is printed as "FizzBuzz".
// for loops evaluate whether a quotient is an integer.
int main()
{
float counterMain = 1;
// factorMain will be set to the multiples for Fizz or "Buzz.
float factorMain = 0;
// factorTest "tests" factorMain to see if it is an integer factor of counterMain.
float factorTest = 1;
// factorLimit sets the divisor (factor) upper limit (only one other factor in this case).
// factorLimit is a quotient of factorMain and factorTest.
float factorLimit = 0;
int passFizz = 0;
int passBuzz = 0;
// counterMain gets printed at the end if nothing is passed to checkFizzBuzz.
for(counterMain = 1; counterMain <= 100; ++counterMain)
{
// Resets pass check.
passFizz = 0;
passBuzz = 0;
// "Fizz" value set here.
factorMain = 3;
// Sets the divisor upper limit as a quotient of counterMain.
factorLimit = counterMain / factorMain;
//factorTest counts to the divisor upper limit.
for(factorTest = 1; factorTest <= factorLimit; ++factorTest)
{
// Tests if factorTest is an integer quotient of counterMain divided by factorMain.
if(factorTest == factorLimit)
{
// Passes 1 to passFizz if factorMain is an integer.
passFizz = 1;
// Sets factorTest to for loop exit condition.
factorTest = factorLimit;
}
}
// "Buzz" value is set here.
factorMain = 5;
factorLimit = counterMain / factorMain;
// Copy of the above for loop but passes 2 to passBuzz instead.
for(factorTest = 1; factorTest <= factorLimit; ++factorTest)
{
if(factorTest == factorLimit)
{
passBuzz = 2;
factorTest = factorLimit;
}
}
// Fizz-Buzz pass check, then prints out.
int checkFizzBuzz = (passFizz + passBuzz);
// Value of 0 means no factors were found, prints counterMain value instead.
if(checkFizzBuzz == 0)
{
std::cout << counterMain;
}
if(checkFizzBuzz == 1)
{
std::cout << "Fizz";
}
if(checkFizzBuzz == 2)
{
std::cout << "Buzz";
}
if(checkFizzBuzz == 3)
{
std::cout << "FizzBuzz";
}
std::cout << "\n";
// Return to counterMain for loop.
}
return 0;
}
I realize after writing this that I could have used modulus %. However, modulus would only account for integer multiples, correct? (i.e. I would not be able to use "3.5" as one of my multiples.)
You realize that point of FizzBuzz is to give a simple test to screen out programmers who can't code at all? Your added complication seems to miss that point entirely. blog.codinghorror.com/why-cant-programmers-program \$\endgroup\$ – markspace Sep 28 '19 at 6:43