Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C 

FizzBuzz - a VERY simple challange for newbie C++'ers

Checkout https://www.youtube.com/watch?v=QPZ0pIK_wsc

Instructions stated in video however,
For each number from 1 to 100, if that number is a multiple of 3, print 'Fizz'. If that number is a multiple of 5, print 'Buzz'. If the number is a multiple of both 3 and 5, print 'FizzBuzz'. Otherwise print that number

Are you a nub? Would you get the job? Post your solutions below!
Code
int main()
{
for(int i = 0; i < 100; i++)
{
if(i % 3 = 0)
{
std::cout << "Fizz";
}
if(i % 5 = 0)
{
std::cout << "Buzz" << std::endl;
}
}
}
[Image: hfnmhEI.jpg]
For fun:
Code
#include <iostream>

int main()
{
    int indexes[] = { 0, 0, 1, 0, 2, 1, 0, 0, 1, 2, 0, 1, 0, 0, 3 };
    const char *values[] = { 0, "Fizz", "Buzz", "FizzBuzz" };
    for (int i = 1, n = 0; i <= 100; i++, n = (n + 1) % 15)
    {
        if (indexes[n]) {
            std::cout << values[indexes[n]] << '\n';
        } else {
            std::cout << i << '\n';
        }
    }
}

^ Wow look at that shitty highlighting..

Code
#include <iostream>
#include <string>

int main()
{
    std::string value = "FizzBuzz";
    for (int i = 1; i <= 100; i++)
    {
        int n = i % 3 != 0 ? 4 : 0;
        int length = i % 5 == 0 ? 4 : 0;
        if (!n) length += 4;
        if (length) std::cout << value.substr(n, length) << '\n';
        else std::cout << i << '\n';
    }
}
Getting Started: C/C++
[C/C++/.NET/x86 ASM - Automation Programmer]
Probably not the fastest or most elegant way, but this is the first thing that came to my mind:
Code
#include <iostream>

int main() {
    for(size_t i = 1; i <= 100; i++) {
        if i % 15 == 0 std::cout << "FizzBuzz" << std::endl;
        else if i % 3 == 0 std::cout << "Fizz" << std::endl;
        else if i % 5 == 0 std::cout << "Buzz" << std::endl;
        else std::cout << i << std::endl;
    }


(May 11, 2020 10:14 PM)0xDEAD10CC Wrote:  For fun:
Code
#include <iostream>

int main()
{
   int indexes[] = { 0, 0, 1, 0, 2, 1, 0, 0, 1, 2, 0, 1, 0, 0, 3 };
   const char *values[] = { 0, "Fizz", "Buzz", "FizzBuzz" };
   for (int i = 1, n = 0; i <= 100; i++, n = (n + 1) % 15)
   {
       if (indexes[n]) {
           std::cout << values[indexes[n]] << '\n';
       } else {
           std::cout << i << '\n';
       }
   }
}

^ Wow look at that shitty highlighting..

Code
#include <iostream>
#include <string>

int main()
{
   std::string value = "FizzBuzz";
   for (int i = 1; i <= 100; i++)
   {
       int n = i % 3 != 0 ? 4 : 0;
       int length = i % 5 == 0 ? 4 : 0;
       if (!n) length += 4;
       if (length) std::cout << value.substr(n, length) << '\n';
       else std::cout << i << '\n';
   }
}

Cool idea to save just one string and build the substring based off that!
(May 12, 2020 06:03 AM)4Txe4y33C9zvNhMj Wrote:  Probably not the fastest or most elegant way, but this is the first thing that came to my mind:
Code
#include <iostream>

int main() {
   for(size_t i = 1; i <= 100; i++) {
       if i % 15 == 0 std::cout << "FizzBuzz" << std::endl;
       else if i % 3 == 0 std::cout << "Fizz" << std::endl;
       else if i % 5 == 0 std::cout << "Buzz" << std::endl;
       else std::cout << i << std::endl;
   }



Cool idea to save just one string and build the substring based off that!

Probably not the most maintainable code in the world but this problem has been out for years, so I figured I'd use some out of the box thinking and come up with some alternatives. Lookup tables are fast, and with the one method, you don't have to check for divisibility of larger numbers, just keep track of the multiples up to 15 and reset. :) It will be faster once it reaches the larger numbers.
Getting Started: C/C++
[C/C++/.NET/x86 ASM - Automation Programmer]
did this just for fun. here's a solution that only uses O(1) operations (addition/comparison). Still runs in O(n) time like the rest

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

int main() {
   int i = 0, x = 0, y = 0;
   while (++i <= 100) {
       if (++x >= 3) {
           x = 0;
       }
       if (++y >= 5) {
           y = 0;
       }
       printf("%d\t%s%s\n", i, x==0 ? "Fizz" : "", y==0 ? "Buzz" : "");
   }
}
(May 12, 2020 07:23 PM)SIM-3991524 Wrote:  did this just for fun. here's a solution that only uses O(1) operations (addition/comparison). Still runs in O(n) time like the rest

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

int main() {
   int i = 0, x = 0, y = 0;
   while (++i <= 100) {
       if (++x >= 3) {
           x = 0;
       }
       if (++y >= 5) {
           y = 0;
       }
       printf("%d\t%s%s\n", i, x==0 ? "Fizz" : "", y==0 ? "Buzz" : "");
   }
}

That's not C++.. 🤐

RE: a VERY simple challange for newbie C++'ers

But it also does not work as the problem was intended. It writes out i for all the numbers in the range, and writing the words beside the numbers changes the requirements and makes the problem easier...

The first solution written by "Sмℯґƒ" in post #2 is also invalid... Where are the newlines (not to mention == and not =)?

Both fails IMHO.
Getting Started: C/C++
[C/C++/.NET/x86 ASM - Automation Programmer]
(May 12, 2020 11:20 PM)0xDEAD10CC Wrote:  That's not C++.. 🤐

RE: a VERY simple challange for newbie C++'ers

But it also does not work as the problem was intended. It writes out i for all the numbers in the range, and writing the words beside the numbers changes the requirements and makes the problem easier...

The first solution written by "Sмℯґƒ" in post #2 is also invalid... Where are the newlines (not to mention == and not =)?

Both fails IMHO.

Its fine bro its just to have fun
Code
#include <iostream>

void fizzBuzz(int n)
{
    for (int i = 1; i <= n; i++)
    {
        if (i % 3 == 0 && i % 5 == 0)
        {
            std::cout << "FizzBuzz";
        }
        else if (i % 3 == 0)
        {
            std::cout << "Fizz";
        }
        else if (i % 5 == 0)
        {
            std::cout << "Buzz";
        }
        else
        {
            std::cout << i;
        }

        std::cout << std::endl;
    }
}

This was my solution. According to the video, it's "dodgy." Guess I am a nub. :(

Jokes aside, his final solution was actually pretty impressive.
A quick code that I can think of.

Code
int main()
{
    for(int i=0;i<100;i++)
    {
        if(i%15==0)
        {
            return "FizzBuzz";
        }
        else if(i%3==0)
        {
            cout << "Fizz";
        }
        else if(i%5==0)
        {
            cout << "Buzz";
        }
        else
        {
            cout << i;
        }
    }
}


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C] by saltycrackerMay 29, 2020 04:36 AM 0 140 May 29, 2020 04:36 AM
Last Post: saltycracker
[C] by saltycrackerMay 12, 2020 07:47 AM 0 127 May 12, 2020 07:47 AM
Last Post: saltycracker
[C] by saltycrackerMay 1, 2020 01:21 AM 11 290 May 1, 2020 01:21 AM
Last Post: Dostoevsky