C ++嵌套while循环菱形图案的翻译算法 [英] translating algorithm for C++ nested while loop diamond pattern
本文介绍了C ++嵌套while循环菱形图案的翻译算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的C ++作业问题:
Here is my homework problem for C++:
以下算法是使用嵌套循环显示菱形图案的问题的解决方案。将下面的算法翻译成C ++程序。生成,运行和测试程序。
The following algorithm is a solution to a problem that uses nested loops to display a diamond pattern. Translate the algorithm below in to a C++ program. Build, run and test the program.
算法解决方案:
开始
Declare numRows as constant integer = 7
Declare maxPlus as constant integer = 7
Declare numSpaces as integer
Declare numPluses as integer
Declare row as integer
Declare space as integer
Declare plus as integer
Set row = 1
Repeat while row >= 1 AND row <= numRows
Set numPluses = 2 * row - 1
if(numPluses > maxPlus) then
Set numPluses = 14 - numPluses
endif
Set numSpaces = (maxPlus - numPluses) / 2
Set space = 1
Repeat while space >= 1 AND space <= numSpaces
Display( ' ')
Set space = space + 1
End Repeat
Set plus = 1
Repeat while plus >= 1 AND plus <= numPluses
Display( '*')
Set plus = plus + 1
End Repeat
Set row = row + 1
Display a new line
End Repeat
停止
我的代码:
#include <iostream>
using namespace std;
int main() {
const int numRows = 7;
const int maxPlus = 7;
int numSpaces;
int numPluses;
int row;
int space;
int plus;
row = 1;
while((row >=1) && (row <= numRows)){
numPluses = 2 * row - 1;
if(numPluses > maxPlus){
numPluses = 14 - numPluses;
}
numSpaces = (maxPlus - numPluses)/ 2;
space = 1;
while((space >= 1) && (space <= numSpaces)){
cout << " ";
space++;
}
while((plus >= 1) && (plus <= numPluses)){
cout << "*";
plus++;
}
row++;
cout << endl;
}
return 0;
}
我的问题是为什么我没有得到菱形图案?我觉得我正确地翻译了算法,但是得到的只是一堆空白。我看错问题还是编码错误? C ++代码的屏幕截图
My question is why am I not getting the diamond pattern? I feel like I translated the algorithm correctly, but all I get is a bunch of blank space. Did I read the problem wrong or did I code wrong? screenshot of C++ code
推荐答案
在最后一个while循环之前:
Before the last while loop:
Set plus = 1
您在翻译中错过了它。
这篇关于C ++嵌套while循环菱形图案的翻译算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文