Basically my program takes a random number from a random number generator and calculates how many times it takes to go through and match that exact number with another randomly generated number(inner Loop). It then takes the count and adds it to the sum. The outer loop then runs this 50 times. The question I have is my professor does not want goto
or break
statements in the program. This being my first computer programming course I am not quite sure how I can change my program to remove the break statement and keep it working the way it should. The program works 100% fine with the break
statement. Thanks in advance for any help!
#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;
}