Casrtesian plane. How to store points that lie on the X axis:I think I need arrays.

Hello everyone! I am writing a program that is supposed to get input of n points' descartes coordinates and then outputs something like "Most number of points lie in the first/second/third/fourth quadrant." And if there are points that lie on the x axis it outputs: "The x,y point(s) lie on the X axis."
Here are my problems:
1.) I defined a maximum relation, but if there are e.g. 5 points in the first quadrant and 5 points in the second (and third and fourth have less points in them) then it says: "The maximum amount of points lie in the first quadrant." and also: "The maximum amount of points lie in the second quadrant." Is there any way to make it say that these two quadrants have the most points in a better way? Do I have to put it in manually like: if first=second, then write: ...etc.
2.) Normally, the program waits till the input is over to write something on the screen, but the only way I could solve the second part of the question was by making the program write: "The 0,y points lies on the X axis." as soon as that point is input. Is there any way I can store this and in the end list the points that lie on the X axis?
So here is my code and thank you in advance for your help!!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <iostream>
#include <cstdlib>
using namespace std;
//define type:
struct TPont{double x,y;} ;
//define the maximum relation:
int max(int a,int b,int c,int d)
{
int max_val=a;
if (b>max_val)
max_val=b;
if (c>max_val)
max_val=c;
if (d>max_val)
max_val=d;
return max_val;
}

int main()
{
    //input
    TPont p;
    int n;
    //output
    //checking the validity of input
    bool hiba;
    string tmp;
    //intializing quadrants
    int first=0;
    int second=0;
    int third=0;
    int fourth=0;

    //inputting
    do{
    cout << "How many points will you be checking for?" << endl; cin >> n;
    error=cin.fail();
        if (error)
        {
            cout << "Wrong input!" <<endl;
            cin.clear(); getline(cin,tmp,'\n');

        }
    }while (error);
    for(int i=0;i<n;i++){
        do{
        cout << "Please type the x and y coordinates of point p! " << endl; cin >> p.x >> p.y;
        error=cin.fail();
        if (error)
        {
            cout << "Wrong input!" <<endl;
            cin.clear(); getline(cin,tmp,'\n');

        }
    }while (error);

    //counting how many points lie in each quadrant
    if (p.x>0)
    {
        if (p.y>0) first++;
            else if (p.y<0) fourth++;
    }else if (p.x<0){
        if (p.y>0) second++;
            else if (p.y<0) third++;
    }
    //If any points lie on the x axis then the program writes that
    if (p.x==0)
   {
        cout << "The (" << p.x << ", "<< p.y <<")" << "point lies on the X axis." <<endl;
    }

    }

    //checking for maximum and writing it on the screen

    if (first==max(first, second, third, fourth))
    {
         cout << "Most points lie in the first quadrant." <<endl;

    }
    if (second==max(first, second, third, fourth))
    {
         cout << "Most points lie in the second quadrant." <<endl;

    }
    if (third==max(first, second, third, fourth))
    {
         cout << "Most points lie in the third quadrant." <<endl;

    }
    if (fourth==max(first, second, third, fourth))
    {
         cout << "Most points lie in the fourth quadrant." <<endl;

    }

    system("pause");
    return 0;
}


Use an array, and store all points that lie on the x-axis (you need to store only the x-coordinates)... Then display them in the last..!

Also, just as a suggestion for your max function, why don't you use something like this:

1
2
3
4
5
6
7
8
int max(int a, int b, int c, int d)
{
   int max_val;
   max_val=(a>b)?a:b;
   max_val=(max_val>c)?max_val:c;
   max_val=(max_val>d)?max_val:d;
   return max_val;
}


I feel it would be faster than using four iterations of the "if" loop... Just a suggestion...
Hi!
Thank you for your reply, the problem is, I tried using an array to store only the y-coordinates (I think you meant y instead of x, because if they lie on the x axis all their x coordinates are 0) but it didn't work, could you help me with that? I'll copy paste what I tried, I put the following after the last if statement, but it doesn't do anything...


if (fourth==max(first, second, third, fourth))
{
cout << "Most points lie in the fourth quadrant." <<endl;

}

if (p.x==0)
{
int xaxis[100]
p.y=xaxis[100]
}
cout << "The 0 " << xaxis[100] << "points lie on the X axis." << endl;
system("pause");
return 0;
}
Umm.. you have some basic problems in your geometry... When a point lies on the x-axis, its y-coordinates are 0, think about it for a while and clear your concept...

Your coding problem is in the fact that you declare the array inside the if loop. Normal variables have a scope only inside the block they are declared in, which means that your array ceases to exist outside the if loop.
Solution? Declare the array at the very beginning, along with your other variables. Also, the method of your assignment of the values as well as output is wrong. It'll keep overwriting the 100th memory block in your array, as a result of which, you'll end up having only one point, defeating the purpose of the array. Here is what it should look like:

1
2
3
4
5
6
7
int i=1, xaxis[100], j;   //This should be with the other variables, outside any loop.

if (p.y==0)         //Check if the y-coordinate is 0...
{
   p.x=xaxis[i];    //Store the x-coordinate.
   i++;               //Increment i, so it will store the next point in the next memory block of the array.
}


This should solve your problem of storing the points... For display, do this in the last, outside all loops:

1
2
for(j=1; j<=i; j++)
   cout<<"\nThe point ("<<xaxis[j]<<", 0) lies on the x-axis.";


So that's it then.. Post if you have any other problems...
Last edited on
I thought I replied to this, but I can't see it here anymore, so I'll reply again:

First off, I can't believe how I could make such a dumb mistake, I am so embarrassed, I am blaming it on sleep deprivation :)

Second, thank you so much for your help! I wanted to do a for loop for the array, but failed miserably at it, so I really appreciate your help.

I tried what you said, and I put your first line with my other variables, and I put the if and for loops underneath each other under my last if statement in my original post. But when I input something like (35, 0) and (4, 0) it says:
"The point (4658024, 0) lies on the X axis."

Do yo have any idea what I might be messing up? I'll try different variations in the meantime!
Yeah, that's coz you've put the loop at the wrong point. See, after you've taken a point as input, you check for quadrant. RIGHT AFTER THAT, you should check if it lies on the x-axis, and store it in the array if it does... That'll solve your prob...

EDIT: Also, you're welcome! :) And don't sweat it about the x-axis thing, honest mistake! Happens to a lot of people! :)
Last edited on
Topic archived. No new replies allowed.