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 theqsort
function.
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_info
structures based on theirn
values.generic_fizz_buzz
: Prints numbers from 1 tomax
, replacing numbers divisible by specifiers ininfo
with the corresponding strings.
main
Function:
-
Declares an array
info
of threereplace_info
structures:- {5, "Buzz"} replaces multiples of 5.
- {7, "Baxx"} replaces multiples of 7.
- {3, "Fizz"} replaces multiples of 3.
-
Sorts the
info
array by then
values usingqsort
and a custom comparison functioncompare
. -
Calls
generic_fizz_buzz
with 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_info
structures based on theirn
values. - Returns negative if
a
'sn
is less thanb
'sn
, positive ifa
'sn
is 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_word
to 0, indicating that no replacement word has been found. - Loops through the
info
array to check for matches:- If
i
is divisible by anyinfo[it].n
, it printsinfo[it].text
and setsfound_word
to 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 Stirling numbers of the second kind step by step in the Tcl programming language
You may also check:How to resolve the algorithm Descending primes step by step in the Perl programming language
You may also check:How to resolve the algorithm Bitmap/Histogram step by step in the Common Lisp programming language
You may also check:How to resolve the algorithm Sorting algorithms/Insertion sort step by step in the VBScript programming language
You may also check:How to resolve the algorithm Count in octal step by step in the JavaScript programming language