C++ Problems

Can anyone spot what wrong with my code here?
I got problem with the loop and Q(remains same = Q1 in output). How to make the random number to be different for x and y???

#include<stdio.h>
#include<time.h>
#include<math.h>
#include<stdlib.h>
int Random();
void Position(int z,int y);
float Distance(int t, int i);
int Decision();
int main()
{
int a,b,d=0;
float c;
while(d!=-1)
{
a=Random();
printf("x is %d\n",a);
b=Random();
printf("y is %d\n",b);
Position(a,b);
c=Distance(a,b);
printf("The distance from (%d,%d) to (-15,20) is %.2f\n",a,b,c);
d=Decision();
}

system("pause");
return 0;
}

int Random()
{
int randNo;
srand(time(NULL));
randNo = -9 + rand() % (9+9+1 );
return randNo;
}

void Position(int z,int y)
{


if(0<z<=9 && 0<y<=9)
{
printf("(%d,%d) is in QI\n",z,y);
}

else if(-9<=z<0 && 0<y<=9)
{
printf("(%d,%d) is in QII\n",z,y);
}
else if(-9<=z<0 && -9<=y<0)
{
printf("(%d,%d) is in QIII\n",z,y);
}
else if(0<z<=9 && -9<=y<0)
{
printf("(%d,%d) is in QIV\n",z,y);
}
else if(z=0)
{
printf("(%d,%d) is on the y-axis\n",z,y);
}
else if(y=0)
{
printf("(%d,%d) is on the x-axis\n",z,y);
}
}

float Distance(int t, int i)
{
float w;
w=sqrt(pow(-15-t,2)+pow(20-i,2));
return w;
}

int Decision()
{
int d;
printf("continue: ");
scanf("%d",d);
return d;

}
Last edited on
put your code in [ code ][ /code ] btw it help us out. You have an infinate loop mate, it crashed my browser.
The code tags will make it easier to read the code :) Edit: You can edit the post, highlight just the code part, then click the <> button in the Format palette on the right side of the post.

You only need to seed the random function once, not every time you call the random function. You could move the srand statement to the beginning of the main function.

if(0<z<=9 && 0<y<=9) etc.

These if statements aren't working the way you might think. You'd need to separate the conditions - if (z > 0 && z <= 9)

I'm getting a compiler warning message on these.
In function 'void Position(int, int)': 40:13: warning: comparisons like 'X<=Y<=Z' do not have their mathematical meaning



1
2
else if(z=0)
else if(y=0)

The = operator is for assignment, I think you mean to check whether these variables are equal with the == operator?
Last edited on
what do you mean by [ code ][ /code ]?
if i use this conditions - if (z > 0 && z <= 9), can i write like this - if ((z > 0 && z <= 9)&&(y > 0 && y <= 9))

You could use the multiple conditions or maybe something like this to make the conditions less complicated?

And just a thought - what if the random point ends up to be (0,0) - would you want to say that it's at the origin instead of a single axis?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
    if (x == 0)
        std::cout << "(" << x << "," << y << ") is on the y-axis\n";
    else if (y == 0)
        std::cout << "(" << x << "," << y << ") is on the x-axis\n";        
    else if (x > 0)
    {
        if (y > 0)
            std::cout << "("x << "," << y << ") is in ..\n";
        else if (y < 0)
            std::cout << "("x << "," << y << ") is in ..\n";
    }
    else if (x < 0)
    {
        if (y > 0)
            std::cout << "("x << "," << y << ") is in ..\n";
        else if (y < 0)
            std::cout << "("x << "," << y << ") is in ..\n";
    }
Last edited on
yaa.. i want to say that it's at the origin.
Topic archived. No new replies allowed.