How to resolve the algorithm General FizzBuzz step by step in the C programming language
How to resolve the algorithm General FizzBuzz step by step in the C programming language
Table of Contents
Problem Statement
Write a generalized version of FizzBuzz that works for any list of factors, along with their words. This is basically a "fizzbuzz" implementation where the user supplies the parameters. The user will enter the max number, then they will enter the factors to be calculated along with the corresponding word to be printed. For simplicity's sake, assume the user will input an integer as the max number and 3 factors, each with a word associated with them.
For example, given: In other words: For this example, print the numbers 1 through 20, replacing every multiple of 3 with "Fizz", every multiple of 5 with "Buzz", and every multiple of 7 with "Baxx". In the case where a number is a multiple of at least two factors, print each of the words associated with those factors in the order of least to greatest factor. For instance, the number 15 is a multiple of both 3 and 5; print "FizzBuzz". If the max number was 105 instead of 20, you would print "FizzBuzzBaxx" because it's a multiple of 3, 5, and 7.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm General FizzBuzz step by step in the C programming language
Explanation:
This C code implements a generic FizzBuzz program, which prints a sequence of numbers from 1 to a given maximum, replacing certain numbers based on user-provided information. The "FizzBuzz" part of the name comes from a traditional programming challenge where multiples of 3 are replaced with "Fizz" and multiples of 5 with "Buzz".
Header Inclusions:
<stdio.h>: For input and output functions.<stdlib.h>: For theqsortfunction.
Data Structure:
struct replace_info holds information about a number to be replaced. It has two members:
n: The number to be replaced.text: The string to replace the number with.
Function Declarations:
compare: Compares tworeplace_infostructures based on theirnvalues.generic_fizz_buzz: Prints numbers from 1 tomax, replacing numbers divisible by specifiers ininfowith the corresponding strings.
main Function:
-
Declares an array
infoof threereplace_infostructures:- {5, "Buzz"} replaces multiples of 5.
- {7, "Baxx"} replaces multiples of 7.
- {3, "Fizz"} replaces multiples of 3.
-
Sorts the
infoarray by thenvalues usingqsortand a custom comparison functioncompare. -
Calls
generic_fizz_buzzwith the following arguments:max: Maximum number to print.info: Information about numbers to be replaced.info_length: Number of elements ininfo.
compare Function:
- Compares two
replace_infostructures based on theirnvalues. - Returns negative if
a'snis less thanb'sn, positive ifa'snis greater, or zero if they are equal.
generic_fizz_buzz Function:
- Loops through numbers from 1 to
max, excludingmax. - For each number
i:- Initializes
found_wordto 0, indicating that no replacement word has been found. - Loops through the
infoarray to check for matches:- If
iis divisible by anyinfo[it].n, it printsinfo[it].textand setsfound_wordto 1.
- If
- If no replacement word is found, it prints
i. - Prints a newline.
- Initializes
Output:
The output of the program with the given info is as follows:
1
2
Fizz
4
Buzz
Fizz
Baxx
8
Fizz
Buzz
11
Fizz
13
Baxx
Fizz
Buzz
17
Fizz
Baxx
Fizz
Source code in the c programming language
#include <stdio.h>
#include <stdlib.h>
struct replace_info {
int n;
char *text;
};
int compare(const void *a, const void *b)
{
struct replace_info *x = (struct replace_info *) a;
struct replace_info *y = (struct replace_info *) b;
return x->n - y->n;
}
void generic_fizz_buzz(int max, struct replace_info *info, int info_length)
{
int i, it;
int found_word;
for (i = 1; i < max; ++i) {
found_word = 0;
/* Assume sorted order of values in the info array */
for (it = 0; it < info_length; ++it) {
if (0 == i % info[it].n) {
printf("%s", info[it].text);
found_word = 1;
}
}
if (0 == found_word)
printf("%d", i);
printf("\n");
}
}
int main(void)
{
struct replace_info info[3] = {
{5, "Buzz"},
{7, "Baxx"},
{3, "Fizz"}
};
/* Sort information array */
qsort(info, 3, sizeof(struct replace_info), compare);
/* Print output for generic FizzBuzz */
generic_fizz_buzz(20, info, 3);
return 0;
}
You may also check:How to resolve the algorithm Unicode strings step by step in the C programming language
You may also check:How to resolve the algorithm Compiler/syntax analyzer step by step in the C programming language
You may also check:How to resolve the algorithm Terminal control/Coloured text step by step in the C programming language
You may also check:How to resolve the algorithm Julia set step by step in the C programming language
You may also check:How to resolve the algorithm Use another language to call a function step by step in the C programming language