0

The formula simply isn't executing. I tried using printf to debug and it printed out 0 for i at the end of the code

#include <stdio.h>

int main()
{
 int i, base, height;
 printf("Lumber  Cross-Sectional   Moment of Section\n");
 printf("Size  Area    Inertia  Modulus\n");
 for (i = 0; i > 35; i++)
 {
  if (i == 6 || i == 12 || i == 18|| i == 24 || i == 30)
  {
   base = base * 2;
   height = 2;
  }
  if (i != 6 || i != 12 || i != 18 || i != 24 || i != 30)
  {
   height = height * 2;
  }
  printf("%d x %d %d  %d   %d \n", base, height, base * height, base * 2 + height); 

 }//for  
 return (0);
}//main
2
  • 4
    Another bug -- don't assume base=0 and height=0 at the beginning of the loop. They could be anything. Commented Jul 10, 2010 at 2:02
  • Looking at your other questions, you seem to not understand how certain constructs work, and you don't know how to use a debugger (for stepping through code, checking variable values and showing you how it skips over the loop). Commented Jul 10, 2010 at 14:07

6 Answers 6

17

Should be

for (i = 0; i < 35; i++)

The body of the loop will only execute when the condition is true.

Sign up to request clarification or add additional context in comments.

Comments

4

It should be

for (i = 0; i < 35; i++)

as said by Bill the Lizard.

for (i = 0; i > 35; i++)

is never true in its condition since 0 < 35 and hence the loop body is never executed.

Comments

3

You need to think what your conditions mean. The reason the loop doesn't execute is mentioned by others. Let's look at the other condition:

if (i != 6 || i != 12 || i != 18 || i != 24 || i != 30)

When do you think the above condition will be false? We need to look at two cases:

  • i is equal to 6. So, i != 6 is false, but every other part of the condition (i != 12, etc.) is true. This is because 6 is only equal to 6, and no other number. So the overall condition is true.
  • i is not equal to 6. Now, i != 6 is true, and therefore the whole condition is true.

So, your if condition above is always true.

Also, think about what would happen if you were looping till a large number, let's say 215. Will you want to have a condition like:

if (i == 6 || i == 12 || ... || i == 210)

Surely, there has to be a better to do what you want. I think what you want is to multiply base by 2, and reset height when i is divisible by 6; otherwise multiply height by 2. The code structure becomes:

/* not legal C */
if (i is divisible by 6) {
} else {
}

To test divisibility by 6, remember that for numbers n and m, n % m gives you the remainder of n divided by m.

Comments

0

As said by the above two mates for loop will not execute until the condition is true. Before asking it here you should have debugged it yourself. :)

Comments

0

I think if you reshape the code and the conditions, it will be lot better. Can you change the for loop to for (i=1 ; i<36 ; i++)? This way you can simplify the if condition inside the loop,as you can see you are checking if its a multiple of 6 in the code. You can just think on those lines.

Some more pointers:

  1. Initialize all variables to default values.
  2. Print the output in better readable way. Delete x from it. Also space the values equally using \t.

Comments

0

The first loop will never execute.First time for checks for the (i>35), in your case i<35, so the loop will not execute.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.