-
November 30th, 2011, 09:47 AM
#1
Very newbie question about a program concerning cartesian plane and 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!!
Code:
#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;
}
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|