04-27-2015, 08:28 AM
(04-20-2015, 08:21 AM)GISED_Link Wrote:(04-18-2015, 07:59 AM)Tommyand Wrote:Code:#include <stdio.h>
void FizzBuzz(int x); //Header of the function
int main(void) {
int x; //cannot declare a variable in the condition of the loop (valid in C++, java but not i C(95 ?)
for (x=1; x<=10 ;x++){
FizzBuzz(x);
}
return 0;
}
void FizzBuzz(int x) { //the function
if (x % 3 == 0){ // Or (!(x % 3))
printf("%d : Fizz\n", x);
}
if (x % 5 == 0){ // Or (!(x % 5))
printf("%d : Buzz\n", x);
}
}
I correct it... It miss a lot of {} (after the if and the for), I show x when fizz or buzz is found and add the stdio library
I specifically left out the brackets because they are unnecessary, because it's a single line of code inside the if / for, and that is allowed in C. I was thinking about that, and it does look nicer without them...
The initialization of the variable inside the for loop? Like I said, weak C-foo. I did C++, not C.
However, switching around the order of the functions made little sense, and you actually messed up the FizzBuzz function. That final printf('/n') is seperate for a reason... So, my suggustion would be to put the number in the final printf statement, along with the newline. So...
Code:
if (blah)
printf("Fizz");
if (blah2)
printf("Buzz");
printf(": %d\n, x);