这个c ++ while循环的不变性是什么?

问题描述

我在C ++教科书中写了一个问题的答案,该教科书要求我们编写一个绘制矩形,正方形和三角形的程序。我编写的代码可以很好地完成所有这些工作,但是我一直在努力确定循环不变式。有些似乎直截了当,有些则没有。

循环根据三角形的高度扫描20x20空间的列,然后将当前列号与高度和行号之和以及差值进行比较。

它行得通,我只是不确定我能确定不变式。

//draws triangle 
int row = 0;
int triHeight = 20;

// invariant: we have written 'row' rows of the triangle
while (row != triHeight)
{
    int col = 0;
    
    //What is the invariant of this while loop? 
    while (col < triHeight + row) {
        
        ++col;
        
        if (col == triHeight - row || col == triHeight + row || row == triHeight - 1)
            cout << '*';
        else {

                cout << ' ';
        }
    }
    
    cout << endl;
    ++row;
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...