读取值并将其用作钻石参数的C程序

时间:2016-09-26 22:32:30

标签: c for-loop

我需要编写一个程序来提示奇数正整数,并在由星号组成的页面上生成菱形;数字是高度,也是宽度......

我不确定在设置奇数和正数的参数后该怎么做...我不太确定如何使用imut值并设置钻石数字使用该值

我有一个基本的钻石代码,但我不确定它有什么问题。

#include <stdio.h>
#include <stdlib.h>

int main (void) 
{
    int row, col, numstars;
    int half, rate = 1;

    printf("Please enter a positive odd integer.\n");

    scanf("%d", &numstars);

    for (row = 1; row <= numstars; row++) 
    {
        half = numstars / 2;

        for (col = 0; col < half + 1 - numstars; col++)
            printf(" ");

        for (col = 1; col <= 2 * numstars - 1; col++)
            printf("*");

        if ((numstars == (half + 1)))
            rate = -rate;
        numstars = numstars + rate;

        printf("\n");       
    }
    return 0;
}

1 个答案:

答案 0 :(得分:0)

您在这里要求我们教您算法的基础知识。

  • 您必须对您遇到的问题更加具体
  • 您必须学习如何调试 - 互联网上存在大量资源,但最简单的方法是从初学者书开始。

最大的问题是,您在 <configSections>...</configsections> 循环中修改了numstar ...因此您获得了无限循环......

以下是我修复代码的方法:简化并区分两种情况

for