英文:
What's wrong with my approach to printing a hollow diamond pattern in C++ causing an infinite loop?
问题
I am trying to print a hollow diamond pattern. When I printed a solid diamond it goes perfectly. But when I tried to code for a hollow diamond, I stucked. The code is running for infinity. I know another approaches for this problem but I want to know about the flaws in my approach.
我正在尝试打印一个空心菱形图案。当我打印实心菱形时,一切都进行得很顺利。但是当我尝试编写空心菱形的代码时,它陷入无限循环。我知道另一种解决这个问题的方法,但我想了解我方法中的缺陷。
This is my code for solid diamond (upper half). It is running perfectly.
这是我的实心菱形(上半部分)的代码。它运行得很好。
Below is the code for hollow diamond:
以下是空心菱形的代码:
When I run this code for the upper half of the hollow diamond, it runs for infinity. I am unable to understand why. I think the problem lies in the nested for loop. According to me, when I run from j=n to j=1 and apply the conditional statement that it should print "*" only when j=i, it should work fine. But instead, it runs for infinity. PLEASE HELP!!!
当我运行这段代码来打印空心菱形的上半部分时,它会无限运行。我无法理解为什么会这样。我认为问题出现在嵌套的for循环中。按照我的理解,当我从j=n运行到j=1并应用条件语句,即只有当j=i时才打印"*",它应该正常工作。但是实际上,它无限运行。请帮忙解决!
英文:
I am trying to print a hollow diamond pattern. When I printed a solid diamond it goes perfectly. But when I tried to code for a hollow diamond, I stucked. The code is running for infinity. I know another approaches for this problem but I want to know about the flaws in my approach.
I am trying to print a hollow diamond in c++. First I print a solid diamond easily. But when I tried to print a hollow diamond, it runs for infinity. I am unable to understand why.
cin>>n;
for(i=1; i<=n; i++){
for(j=n; j>i; j--){
cout<<" ";
}
for(j=i; j>=1; j--){
cout<<"* ";
}
for(j=1; j<i; j++){
cout<<"* ";
}
cout<<endl;
}
This is my code for solid diamond (upper half). It is running perfectly.
Below is the code for hollow diamond:
cin>>n;
for(i=1; i<=n; i++){
for(j=n; j>=1; j--){
(j=i)?cout<<"* ":cout<<" ";
}
for(j=1; j<i; j++){
cout<<"* ";
}
cout<<endl;
}
When I run this code for upper half of the hollow diamond, it runs for infinity. I am unable to understand why. I think, the problem lies in the nested for loop. According to me when I run from j=n to j=1 and applies the conditional statement that it should print "*" only when j=i. It should work fine. But instead, it runs for infinity. PLEASE HELP!!!
答案1
得分: 0
You are assigning the value of i to j in your ternary expression. Use ==
for equality testing.
(j=i)?cout<<"* ":cout<<" ";
Also, you'd do better with an if
statement: ternaries aren't the best habit to get into (though they have their place). Stylistically, your code will mesh better with other C/C++ if you get into the habit of using zero-based loops (and, indeed, for arrays, you will have to anyway) rather than 1-based as here.
英文:
You are assigning the value of i to j in your ternary expression. Use ==
for equality testing.
(j=i)?cout<<"* ":cout<<" ";
Also, you'd do better with an if
statement: ternaries aren't the best habit to get into (though they have their place). Stylistically, your code will mesh better with other C/C++ if you get into the habit of using zero based loops (and, indeed, for arrays, you will have to anyway) rather than 1 based as here.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论