Why not use a while loop for FIZZBUZZ in C? [closed] - c

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
Only been coding in C for about a month, but wondered why the while loop wasn't used more often for FIZZBUZZ. Is it not as clean or some other reason? Here's what I came up with:
int main()
{
int num=1;
while(num<=100)
{
if (num%3==0 && num%5==0)
{
printf("FIZZBUZZ!!!\n");
}
else if (num%3==0)
{
printf("FIZZ!!!\n");
}
else if (num%5==0)
{
printf("BUZZ!!!\n");
}
else
{
printf("%d\n", num);
}
num++;
}
return 0;
}

Your loop can be neatly folded into a for loop:
for(int num = 1; num <= 100; ++num)
There are two advantages here:
num is scoped inside the loop, when before it was bleeding into whatever scope followed the while. Restricting variables to the minimum possible scope is a good rule of thumb, because it minimizes the amount of variables to think about at any given point.
The range over which your program will operate is now summarized in a single place: you can see the bounds (1 to 100) and the step (1) inside the for. That kind of ranges is pretty well balanced to be read and memorized quickly before reading the body of the loop. For example, if you now wanted to only check odd numbers, it would be immediately clear from reading the num += 2 in the for header, rather than stumbling upon it at the very end of the loop's body.

Related

C - struggling to understand how to use int function within if/ while loops [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
So I'm doing CS50 and struggling with an error that occurs when I include the following two lines:
if (int i < (number -1))
if (int i =< number)
when compiling, 'error: expected expression' comes under 'int'. Am I using it incorrectly within while if statements?
No you can not declare variable in an if statement. But if you want your variable in a local scope you can create a block and declare your variable, initialize it and use. We have to use <= for checking less than or equal to instead of =<
example:
{
int i =5,number=10;
if(i < number-1)
{
//Add your code
}
if(i <= number)
{
//Add your code
}
}
int i; is a variable declaration/definition.
That confuses your compiler when it tries to understand your if condition.
I have to guess a little, but am confident. You probably want
if (i < (number -1)){ /* assuming code here */ }
if (i <= number) { /* assuming code here */ }
Note that is also changed =< to <=.

C - while(true) vs while(condition) - performance [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I would like to know if there is a difference in performance between :
while(true)
{
.....
}
And :
bool x;
x = true;
while(x)
{
.....
}
I need the best performance and a small difference between the two is important to my application.
Info from comment by OP:
The while(true) will at some point also be left, that is however rare.
If you need a truly endless loop, then why use a condition?
If you need a loop which can be left, then your while(true){...} will contain an if(!x) which your while(x) does not contain.
Any potential optimisation benefit of while(true) over while(x) will be lost at that point.
First of all...
If you want to make an infinite loop you (always) use:
while(true)
{
...
}
There would be absolutely no reason why defining a variable before that loop should speed up your "application". So there is no reason to use:
bool x = true;
while(x)
{
...
}

Variable Number of for loops with code in between [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
So the problem I have is that I want to implement N for loops in the following way (where N is a variable):
for i0=0:MAX
cOR[0] = initial + move[i0];
for i1=0:MAX
cOR[1] = cOR[0] + move[i1];
....
some other stuff inside the final loop
(cOR is a vector of length equal to the number of for loops)
So I found this solution that works when you just have the nested loops (https://stackoverflow.com/a/20577981/3932908) but have been struggling to modify it for my particular case which requires code in between the for loops. Is there a simple way to implement this or is a different approach needed?
The general approach is to
Write a recursive function.
If recursion is not for some reason appropriate for your code (e.g. very long recursion depth, or the ability to suspend the execution is required), then convert the recursive version to an iterative one by explicitly modeling the stack.
Doing №1 is easy:
void f(int depth, int initial, int *cOR)
{
if(your termination condition)
{
// some other stuff inside the final loop, and...
return;
}
for(int i = 0; i < MAX; ++i)
{
cOR[depth] = initial + move[i];
f(depth+1, cOR[depth]);
}
}
And call it like so:
f(0, initial, cOR);
Now we head to №2, i.e. converting to a non-recursive version. The extra state we need is what was stored on the stack before: the values of the i variables. So here we go:
int i[max_depth];
int depth = 0;
for(;;)
{
if(your termination condition)
{
// some other stuff inside the final loop, and...
do {
if(--depth < 0)
return;
} while(++i[depth] >= MAX);
}
else
i[depth] = 0;
cOR[depth] = (depth > 0 ? cOR[depth-1] : initial) + move[i[depth]];
++depth;
}
If you can't estimate max_depth a priori then you can switch to a dynamically allocated array that grows as you need.

Which is the best place for parameter restrictions - inside or outside function? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
Option 1: In function
int funcA(int a){
if(a < 0){
return -1;
}
else{
...
return 0;
}
}
Option 2: In main
int main(){
int a;
...
if(a < 0){
...
}
else{
funcA(a);
}
}
Option 3: Both places? If you have some suggestions I will be grateful.
There is no hard and fast rule as to what is better -- there's basically two cases you'd want to consider.
If the constraints are unique to this specific usage of the function, then it would be preferable to put them outside the function, to let the function be more generalized and limit only this specific call to it.
If the constraints are something integral to the function and are always going to be expected whenever the function is called, then it would be preferable to put them inside the function, so that they do not have to be duplicated every place that the function will be called.
It is the callers responsibility to make sure it calls a function with valid arguments - per the documentation.
It is the called functions interest to guard against invalid values when it can - and again, per the documentation.
There is no inherently "right" or "wrong" choice without a contract (documentation) for funcA which is highly dependent on what the function does and how it is expected to be used.

What are the cases in which a loop cannot be unrolled? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
What are the cases in which a loop cannot be unrolled? I've been reading a paper which shows a loop that it says is not able to be unrolled. I cannot actually post the specific code as it is private, however, I am wondering if there is something obvious I am missing in regards to not being able to unroll.
Thanks in advance. If there's any other info that I can try to provide, let me know.
well you can't unroll a loop with any type of recursion in it because it could be infinitely long, also you cant unroll an infinite loop or one with some kind of method for breaking out that isn't incremental
recursion:
method(int x){
if(x > 0)
return 0;
else
return method(x-1);
infinite loop:
while(true){
...
if(some condition)
break;
}
last one:
boolean somevar = true;
while(somevar){
...
if(some condition)
somevar = false;
}

Resources