Question

In: Computer Science

c++problem Amelia is a computational geometry scientist. Her research has a broad range of applications –...

c++problem

Amelia is a computational geometry scientist. Her research has a broad range of applications – from pure geometry applications, to applied fields such as computer mathematics, or physical simulations. As her research assistant, you’re going to help her analyze some triangles in 2d space that she generated from a sample program. Amelia is aware of the fact that you’re new to C++ so the task is easy this week (Based on what Amelia’s more experienced assistants say, this means ”the math is easy, and she doesn’t remember how hard it was to learn C++ for the first time). Your task in the program is to compute the average area of all the triangles. Because every time the triangles generated can be different, you’re going to read them from input and do computation accordingly. To compute the area of a triangle formed by 3 vertices (x1, y1),(x2, y2),(x3, y3):

1. Compute 2 helper vectors ~m = (mx, my) = (x2−x1, y2−y1), ~n = (nx, ny) = (x3−x1, y3−y1); 2. Compute the area A = 1 2 |~m × ~n| = 1 2 |mx · ny − nx · my|. (For those of us to whom vector math is a distant memory, or an exciting discovery for the future): Area = abs(x1y2 + x2y3 + x3y1 − y1x2 − y2x3 − y3x1)/ 2

Example 1

Next triangle: j

Error: the command has to be q or t.

Example 2

Next triangle: t 0 0 3 0 0 4

Next triangle: j

Error: the command has to be q or t.

Example 3

Next triangle: q

Error: there’s no triangle recorded.

Example 5

Next triangle: t 0.4 4.1 121.1 343.1 11.1 3

Next triangle: t 3 2312.1 12.55 66 6 9.654

Next triangle: t 4.1 131 323 1.1 1.2 22

Next triangle: q

The average is: 9024.49!

Example 6

Next triangle: t 0 0 0 1 0 -1

Error: the 3 points you’ve just typed cannot form a triangle.

Solutions

Expert Solution

Here is the complete program:

#include <bits/stdc++.h>
using namespace std;

int main(){
char c;
//vector for storing final area of valid triangle
vector<double> vec;
// variables for vertices and to store area
double x1,x2,x3,y1,y2,y3,area;
//run infinite till user wants to quit
while(true){
//ask user to input
cout<<"Next triangle: ";
cin>>c;   
//if user enters q then simply break from loop
if(c=='q'){cout<<"\n";   
break;
}
else if(c!='t'){cout<<"\n"; //if command is other then t or q
cout<<"Error: The command has to be q or t\n"; // show error message
return 0; //directly exit the program
}
else if(c=='t'){ //if command is t
cin>>x1>>y1>>x2>>y2>>x3>>y3; //scan all the vertices
/*check if the vertices forms a valid triangle */
area = abs((x1*y2)+(x2*y3)+(x3*y1)-(y1*x2)-(y2*x3)-(y3*x1));
area =(double) (area/2.0);
//give error message that 3 points does not form a triangle
if(area==0){ //if area is zero
cout<<"Error : The 3 points you've just typed cannot form a triangle";
return 0; //exit the program
}
vec.push_back(area); //if triangle is valid then store in vector
}
}

if(vec.size()==0){ //if there is no valid triangle entered
cout<<"Error : There's no triangle recorded";
return 0; //exit program
}

int i,n=vec.size(); //declare helping variables

double ans=0; // to store the sum of area of the triangle

for(i=0;i<n;i++){
ans+=vec[i]; //calculate total area to calculate average
}
  
//display average area
cout<<"The average is : "<<ans/(double)n<<"!";
return 0; //exit

}

The sample output as below:


Related Solutions

Marie Curie was a famous French-Polish scientist known for her pioneering research on radioactivity. Her work...
Marie Curie was a famous French-Polish scientist known for her pioneering research on radioactivity. Her work not only brought her fame but her death as well; she developed aplastic anemia due to radiation exposure. She experienced recurrent and prolonged infections (viral, bacterial, parasitic, and fungal). Explain why she suffered from recurrent infections. Be sure to mention the different types of WBC and the relation to the various infections, and the reasons why she lacked the cell-mediated and the humoral response....
Marie Curie was a famous French-Polish scientist known for her pioneering research on radioactivity. Her work...
Marie Curie was a famous French-Polish scientist known for her pioneering research on radioactivity. Her work not only brought her fame but her death as well; she developed aplastic anemia due to radiation exposure. She experienced recurrent and prolonged infections (viral, bacterial, parasitic, and fungal). Explain why she suffered from recurrent infections. Be sure to mention the different types of WBC and the relation to the various infections, and the reasons why she lacked the cell-mediated and the humeral response.
The concept of time value of money has numerous "real-world" applications. Some of the applications range...
The concept of time value of money has numerous "real-world" applications. Some of the applications range from calculating the payment for a car or mortgage to estimating what interest rate is needed on an investment to send your child to college in 20 years. In your discussion, respond to the following two questions: Do you believe the concept of time value money is important in ordinary business relationships? Explain. How would you use a concept of time value to determine...
Penny Lawrence is a research scientist in Miama, Florida. Her husband Lary Lawrence stays home to...
Penny Lawrence is a research scientist in Miama, Florida. Her husband Lary Lawrence stays home to take care of their two young children, George (age 11) and Allie (age 9). Penny's total wages for 2019 were $73,023 from which $6,418 of federal income tax was withheld.  Florida does not have a state income tax. They had $213 in interest income. Penny received dividends from Exxon in the amount of $434 and Lary received dividends from First National Bank in the amount...
Quality Products Company has a broad range of products it sells in a four-state area. Sales...
Quality Products Company has a broad range of products it sells in a four-state area. Sales for the four quarters of 2019 are expected to be $575,000, $412,000, $749,000, and $638,000, for an annual total of $2,374,000. Cost of goods sold averages 57% of sales. Ending inventory for each quarter should be 10% of cost of goods sold for the following quarter. Inventory at January 1 is expected to be $40,000. Required: Show calculations for ending inventory, purchases, and cost...
Hrudka Corp. has manufactured a broad range of quality products since 1988. The following information is...
Hrudka Corp. has manufactured a broad range of quality products since 1988. The following information is available for the company’s fiscal year ended February 28, 2014. Hrudka follows ASPE. 1. The company has $4 million of bonds payable outstanding at February 28, 2014, that were issued at par in 2003. The bonds carry an interest rate of 7%, payable semi-annually each June 1 and December 1. 2. Hrudka has several notes payable outstanding with its primary banking institution at February...
Hrudka Corp. has manufactured a broad range of quality products since 1988. The following information is...
Hrudka Corp. has manufactured a broad range of quality products since 1988. The following information is available for the company’s fiscal year ended February 28, 2014. Hrudka follows ASPE. 1. The company has $4 million of bonds payable outstanding at February 28, 2014, that were issued at par in 2003. The bonds carry an interest rate of 7%, payable semi-annually each June 1 and December 1. 2. Hrudka has several notes payable outstanding with its primary banking institution at February...
Hrudka Corp. has manufactured a broad range of quality products since 1988. The following information is...
Hrudka Corp. has manufactured a broad range of quality products since 1988. The following information is available for the company’s fiscal year ended February 28, 2011. 1. The company has $4 million of bonds payable outstanding at February 28, 2011, that were issued at par in 2000. The bonds carry an interest rate of 7%, payable semi-annually each June 1 and December 1. 2. Hrudka has several notes payable outstanding with its primary banking institution at February 28, 2011. In...
Hrudka Corp. has manufactured a broad range of quality products since 1988. The following information is...
Hrudka Corp. has manufactured a broad range of quality products since 1988. The following information is available for the company’s fiscal year ended February 28, 2014. Hrudka follows ASPE. 1. The company has $4 million of bonds payable outstanding at February 28, 2014, that were issued at par in 2003. The bonds carry an interest rate of 7%, payable semi-annually each June 1 and December 1. 2. Hrudka has several notes payable outstanding with its primary banking institution at February...
(Computational) Applied Statistics Problem 1: A noted medical researcher has suggested that a heart attack is...
(Computational) Applied Statistics Problem 1: A noted medical researcher has suggested that a heart attack is less likely to occur among adults who actively participate in athletics. A random sample of 300 adults is obtained. Of that total, 100 are found to be athletically active. Within this group, 10 suffered heart attacks; among the 200 athletically inactive adults, 26 had suffered heart attacks.                    a) Test the hypothesis that the proportion of adults who are active and sufferedheart attacks is different...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT