在程序

时间:2017-10-28 23:10:22

标签: c while-loop conditional

基本上我的程序从随机数生成器中获取一个随机数,并计算通过并将该确切数与另一个随机生成的数字(内循环)匹配所需的次数。然后它计算并将其加到总和上。然后外环运行50次。我的问题是我的教授不希望程序中有gotobreak个陈述。这是我的第一个计算机编程课程,我不太确定如何更改程序以删除break语句并保持其工作方式。该程序使用break语句100%正常工作。在此先感谢您的帮助!

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

int main()
{
    int randNumA = 0; int randNumB = 0; int i = 0.0;
    int iterationa = 0; int iterationb = 0;
    float avgIteration = 0.0;
    int sumIteration = 0;

    srand(time(NULL));

    for(i = 0; i < 50; i++)
    {
        int randNumB = rand() % 100;
        printf("The random number selected is %d\n", randNumB);
        int iterationb = 0;
        while (1)
        {
            int randNumA = rand() % 100;
            if (randNumA < randNumB)
            {
                iterationa++;
                iterationb++;
            }
            else if(randNumA > randNumB)
            {
                iterationa++;
                iterationb++;
            }
            else
            {
                iterationa++;
                iterationb++;
                printf("The final iteration was %d\n\n", iterationb);
                break;
            }
        }
    }
    sumIteration = sumIteration + iterationa;
    avgIteration = (float)sumIteration / (float)50;

    printf("The average amount of iterations for each attempt were %f\n", 
    avgIteration);
    return 0;
}

3 个答案:

答案 0 :(得分:0)

在顶部,您可以声明条件变量:int isNumNeither = 0;,检查变量在while循环条件中保持为false,然后在1块中将其设置为else

下一次循环检查条件时,循环将自动退出,因为条件的计算结果为false。

例如:

while (isNumNeither == 0)
{
  int randNumA = rand() % 100;
  if (randNumA < randNumB)
  {
    iterationa++;
    iterationb++;
  }
  else if(randNumA > randNumB)
  {
    iterationa++;
    iterationb++;
  }
  else
  {
    iterationa++;
    iterationb++;
    printf("The final iteration was %d\n\n", iterationb);
    isNumNeither = 1;
  }
}

答案 1 :(得分:0)

这是对你的问题的另一种看法,清理了各种错误。
主要变化:

  1. 多次声明变量:即int randNumA
  2. while循环不必要地复杂化。见评论。
  3. 简化变量声明。
  4. 请参阅代码中的注释:

    int main()
    {
        //It's great that you're initializing your variables.
        //You can do it as a comma separated list as such.
        int randNumA = -1,//so it definitely goes into the while loop the first time
            randNumB = 0, i = 0, iterationa = 0, iterationb = 0,  sumIteration = 0;
        float avgIteration = 0;
        srand(time(NULL));
    
        for(i=0; i < 50; i++)
        {
            randNumB = rand() % 100;//You've already declared, don't need int in front
            printf("The random number selected is %d\n", randNumB);
            iterationb = 0;
    
            while (randNumA != randNumB)//this will break the loop as is required
            {
                randNumA = rand() % 100;
                //next two lines occur in all three conditions, so bring them outside
                iterationa++;
                iterationb++;
                //No need for all three cases. Either equal or not!
                if (randNumA == randNumB)
                {
                    printf("The final iteration was %d\n\n", iterationb);
                }
            }
        }
    
        sumIteration += iterationa;//shorthand notation
        avgIteration = (float)sumIteration / 50;//casing one of these will suffice
    
        printf("The average amount of iterations for each attempt were %f\n", avgIteration);
        return 0;
    }
    

答案 2 :(得分:0)

检查,它能做你想做的事。如果需要,我可以添加评论。

我没有使用你的变量,因为它们在这里是不必要的,并且还添加了几个新变量 - cnt(while循环的迭代计数器),num_iteration(数量)迭代),而不是文字50数字。

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

int main() {
    float sum_iteration = 0;
    int num_iteration = 50; 
    int cnt, i;
    int rand_num_a = 0;
    int rand_num_b = 0;

    srand(time(NULL));

    for(i = 0; i < num_iteration; i++) {
        rand_num_a = rand() % 100;

        printf("The random number selected is %d\n", rand_num_a);
        cnt = 0;

        do {
            rand_num_b = rand() % 100;
            cnt++;  
        }
        while(rand_num_a != rand_num_b); 

        printf("The final iteration was %d\n\n", cnt);
        sum_iteration += cnt;
    }   

    printf("The average amount of iterations for each attempt were %f\n", sum_iteration / num_iteration);
    return 0;
}

<强>输出

The random number selected is 39
The final iteration was 71

The random number selected is 55
The final iteration was 119

The random number selected is 25
The final iteration was 10

... skip lines

The random number selected is 81
The final iteration was 132

The random number selected is 37
The final iteration was 300

The random number selected is 22
The final iteration was 19

The average amount of iterations for each attempt were 89.580002