2

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;
}
Nik
  • 1,780
  • 1
  • 14
  • 23
kspruill
  • 31
  • 2
  • 1
    `i = 50;` should do it. – Nik Oct 28 '17 at 23:16
  • Where should it go, @Nik? – ifconfig Oct 28 '17 at 23:17
  • 1
    I missed the nested `while` loop. That suggestion won't work. You will need to do what @ifconfig is suggesting, except instead of `bool`, use `int`, as there is not such thing as `bool` in `C` – Nik Oct 28 '17 at 23:26
  • Thanks, @Nik I forgot about that. Used to C++.... :) – ifconfig Oct 28 '17 at 23:28
  • alternatively, if you declare `randNumA` above the while loop, you can also just do this: `while (randNumA != randNumB)`. That's probably the easiest. – Nik Oct 28 '17 at 23:29
  • @Nik if i declare randNumA above the while loop is seems to stop it from executing the for loop and it just displays the one output instead of 50. – kspruill Oct 28 '17 at 23:41
  • @Nik C does have `bool`, defined in . See http://en.cppreference.com/w/c/types/boolean – aschepler Oct 28 '17 at 23:57
  • @aschepler, C89 does not support it. See your own reference as well as [this](https://stackoverflow.com/a/1608350/7159784). And yes, you can also do it through `#define`, but I would think that is inappropriate given the asker's experience. – Nik Oct 29 '17 at 00:02
  • @kspruill see my answer. – Nik Oct 29 '17 at 00:09
  • Think about **how you decide when it's time to break out of the loop.** Use that condition as the loop termination condition, instead of the infinite `while (1)`. (Just be careful not to quit one round too soon...) – alexis Oct 29 '17 at 00:26
  • Thank you, everyone for your help! Made this a lot more clearer and even gave me several ways to look at the problem. – kspruill Oct 30 '17 at 01:42
  • @kspruill you're welcome of course. Since you're a new and by all accounts very courteous member, I thought you would like to know that [the best way to show your appreciation for people's help](https://meta.stackexchange.com/questions/17878/thanking-users-who-answered-my-question) is by [Upvoting answers that you thought helped you and accepting an answer that you thought answered your question best](https://stackoverflow.com/help/someone-answers). This will also help future readers find the best information. – Nik Oct 30 '17 at 01:57

3 Answers3

0

At the top, you can declare a condition variable: int isNumNeither = 0;, check for the variable to stay false in the while loop condition and then set it to 1 in the else block.

The next time the loop checks the condition, the loop will exit automatically since the condition evaluates to false.

For example:

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;
  }
}
ifconfig
  • 6,242
  • 7
  • 41
  • 65
0

Here's another take on your problem, with various bugs cleaned up.
Main Changes:

  1. Declaring variables more than once: i.e.int randNumA
  2. The while loop was needlessly complicated. See comments.
  3. Simplified variable declarations.

See comments in code:

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;
}
Nik
  • 1,780
  • 1
  • 14
  • 23
0

Check, does it do what you want. I can add comments, if you are needed.

I don't use some your variables, because they are unnecessary here and also, have added couple of new variables - cnt (iteration counter of the while loop), num_iteration (the number of iterations), instead of the literal 50 number.

#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;
}

Output

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
MiniMax
  • 983
  • 2
  • 8
  • 24