为什么下面的代码会产生这个输出?

huangapple go评论48阅读模式
英文:

Why the code below is giving this output?

问题

The first code you provided prints a rhombus of stars with spaces before each line, while the second code attempts to achieve the same result but does not produce the desired output.

The issue in the second code is in the inner for loop condition:

for (int j = n - i + 1; 2 * n - i >= j > n - i; j++)

The condition 2 * n - i >= j > n - i is not valid in C. In C, you need to use separate comparisons and logical operators to achieve this. You can rewrite the inner loop condition as follows:

for (int j = n - i + 1; j <= 2 * n - i; j++)

This change ensures that the inner loop correctly iterates over the required number of '*' characters in each line. After making this adjustment, your second code should produce the desired rhombus pattern.

Here's the corrected inner loop condition:

for (int j = n - i + 1; j <= 2 * n - i; j++)

By making this change, your code should work as expected and produce the desired rhombus pattern.

英文:

I want to write a code for printing a rhombus of stars in c Language
I have written a code that gives the desired output. :

#include &lt;stdio.h&gt;
int main()
{
    int n, i, j, K;
    printf(&quot;Enter no. of rows; &quot;);
    scanf(&quot;%d&quot;, &amp;n);
    for (int i = 1; i &lt;= n; i++)
    {
        for (int j = 1; j &lt;= n - i + 1; j++)
        {
            printf(&quot; &quot;);
        }
        for (int k = 1; k &lt;= n; k++)
        {
            printf(&quot;*&quot;);
        }
        printf(&quot;\n&quot;);
    }
    return 0;
}

But, I want to get the same result another way. So, I wrote the another code expecting the same result which is below.

I tried this code:

#include &lt;stdio.h&gt;
int main()
{
    int n, a, i, j;
    printf(&quot;Enter no. of rows: &quot;);
    scanf(&quot;%d&quot;, &amp;n);
    for (int i = 1; i &lt;= n; i++)
    {
        for (int j = n - i + 1; 2 * n - i &gt;= j &gt; n - i; j++)
        {
            printf(&quot;*&quot;);
        }
        printf(&quot;\n&quot;);
    }
    return 0;
}

I was expecting the desired output which came for the code written above.
But, it resulted in this output.
So, I am confused. Why did this happen?
Please explain.

答案1

得分: 0

Your test expression in the inner for loop is not in the correct format.
Mathematically correct but yields undesired results in c.

It should be:

for (int j = n - i + 1; 2 * n - i >= j && j > n - i; j++)

The reason is that boolean operators have arity of 2 and are evaluated left to right.

How your code is actually evaluated is this (2 * n - i >= j) > n - i

So you first evaluate (2 * n - i >= j) which becomes 1 (True), then you evaluate 1 > n - i

That is why you see ***** only on i=5 row.

(You still have a problem that your asterisks are not properly indented, for that you need to print spaces in front.)

英文:

Your test expression in the inner for loop is not in the correct format.
Mathematically correct but yields undesired results in c.

It should be:

for (int j = n - i + 1; 2 * n - i &gt;= j &amp;&amp; j &gt; n - i; j++)

The reason is that boolean operators have arity of 2 and are evaluated left to right.

How your code is actually evaluated is this (2 * n - i &gt;= j) &gt; n - i

So you first evaluate (2 * n - i &gt;= j) which becomes 1 (True), then you evaluate 1 &gt; n - i

That is why you see ***** only on i=5 row.

(You still have a problem that your asterisks are not properly indented, for that you need to print spaces infront.)

huangapple
  • 本文由 发表于 2023年5月7日 17:38:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76193128.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定