do while

hey pals,

have to write a code that :
identifies how many times variable b got incorrect values.
Var.b got randomly values from 0-100, but only values from 50-70 will be considered as correct values.

is it correct what I've done ?

1
2
3
4
     do{b=rand()%20+50;
           k++;
   } while(b=rand()%100);
       cout<<"b got incorrect values for "<<k<<endl;
1
2
do {
} while ( b < 50 || b > 70 );
do { ... } while(b=rand()%100); means
do { ... } while((b = rand()%100) != 0);
What does the result of rand()%100 not equaling zero have to do with your problem statement?
You should turn on compiler warnings (At least -Wall in g++)

It appears the random nature of your assignments makes them hard to test. I suggest going through your code by hand with particular selected known values to test what kind of a response they cause. Additionally, what you could do is temporarily comment out the call to srand you probably have, so that your program produces the same "random" number sequence each time for repeatability, and then use a debugger to understand what's happening.

identifies how many times variable b got incorrect values.
Your problem statement does not say how many times to loop, or equivalently, what causes the loop to terminate.
Last edited on
thanks
Topic archived. No new replies allowed.