problem understanding a program

Dec 2, 2008 at 10:47pm
closed account (2wv7M4Gy)
i just begun i C++ and i can not understand what is this program for :

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

const int MaxRandomValue=20;

int main() {

int x1, y1;
int x2, y2;
int rx, ry;


int count=0;







cout<<"max random value is "<<MaxRandomValue<<endl<<endl;
//input
do{
cout<<"x1: "; cin>>x1;
}
while (x1<0);
do{
cout<<"y1: "; cin>>y1;
}
while (y1<0);
do{
cout<<"x2: "; cin>>x2;
}
while (x2<x1);
do{
cout<<"y2: "; cin>>y2;
}
while (y2<y1);

//random loop
do {

rx = rand() % MaxRandomValue;
ry = rand() % MaxRandomValue;
count ++;
cout<<"x= "<<rx<<endl<<"y= "<<ry<<endl<<endl;
}
while (!((rx>=x1)&&(rx<=x2)&&(ry>=y1)&&(ry<=y2))); //(!( ( ((rx>=x1)&&(rx<=x2)) || ((rx<=x1)&&(rx>=x2)) ) && ( ((ry>=y1)&&(ry<=y2)) || ((ry<=y1)&&(ry>=y2)) ) ));

cout<<"took me "<<count<<" tries."<<endl;




system("pause");
return 0;
}


If anyone could help me i would appreciate it !
Last edited on Dec 2, 2008 at 10:47pm
Dec 2, 2008 at 10:59pm
It ask for x1 and y1 to be greater than 0, x2 and y2 to be greater respectively to x1 and y1.
Than it try to find rx and ry to be in between of x1, x2 and y1, y2 assigning them random values, eg:
input:
x1 = 2
x2 = 7
y1 = 3
y2 = 6
output:
rx = 5
ry = 4
Dec 4, 2008 at 1:34am
the (x1,y1), (x2,y2) outline a rectangle area, the (rx,ry) is a coordinate 'guess' when the guess comes inside the area, the loop ends.
Topic archived. No new replies allowed.