Answer the question
In order to leave comments, you need to log in
Why does the program for counting the number of triangles not work?
Hello dear users. I have to turn to you for help, because I myself simply do not understand what the trouble is.
I have been given the following task: The input to the program is a set of pairs of numbers - the coordinates of points on the plane. It is necessary to write a program that finds the triangle with the largest area among all the triangles given by these points. Note that not every triple of given points can define a triangle.
Here's what I got:
#include <iostream>
#include <string>
#include <math.h>
using namespace std;
int main() {
char place[10][10];
int num,temp_x,temp_y,check,num_wrong;
double place_x[10],place_y[10],max_squ;
setlocale(LC_ALL,"Russian");
cout << "Введите количество точек: ";
cin >> num; // считываем общее количество точек
for (int i=0;i<num;i++) {
cout << "Введите координаты точки в формате x,y: ";
cin >> place[i]; // координаты каждой точки заносим в строковой массив
}
check=0; temp_x=0; temp_y=0;
/*
Работаем с каждой строкой массива с координатами. Проверяем строку посимвольно. Если символ является числом, то заносим его в переменную. После этого цикла мы получаем два массива, с координатами x и y. i элменет массива place_x соответствует i элементу массива place_y
*/
for (int i=0;i<num;i++) {
for (int j=0;j<strlen(place[i]);j++) {
if (isdigit(place[i][j]) && check==0) {temp_x=temp_x*10+(place[i][j]-'0');}
if (isdigit(place[i][j]) && check==1) {temp_y=temp_y*10+(place[i][j]-'0');}
if (!isdigit(place[i][j])) {check=1;place_x[i]=temp_x;temp_x=0;}
if (j==strlen(place[i])-1) {check=0;place_y[i]=temp_y;temp_y=0;}
}
}
double len1,len2,len3;
max_squ=0;
num_wrong=0;
/*
С помощью 3х циклов прогоняем все точки друг с другом. Высчитываем длину между этими точками по формуле. Затем, провеяем существование треугольника по основному неравенству, и если треугольник существует, то считыем его площадь.
*/
for (int i=0;i<num-2;i++) {
for (int j=1;j<num-1;j++) {
len1 = pow((place_x[j]-place_x[i]),2)+pow((place_y[j]-place_y[i]),2); len1 = sqrt(len1);
for (int k=2;k<num;k++) {
len2 = pow((place_x[k]-place_x[j]),2)+pow((place_y[k]-place_y[j]),2); len2 = sqrt(len2);
len3 = pow((place_x[k]-place_x[i]),2)+pow((place_y[k]-place_y[i]),2); len3 = sqrt(len3);
if ((len1+len2)>len3 && (len2+len3)>len1 && (len1+len3)>len2) {
double p=(len1+len2+len3)/2;
double check=p*(p-len1)*(p-len2)*(p-len3);
double squ=sqrt(check);
if (squ>max_squ) {max_squ=squ;}
}
else {num_wrong=num_wrong++;}
}
}
}
num_wrong=num_wrong/3;
cout << "Площадь самого большого треугольника равна " << max_squ << "\n";
cout << "Всего может быть составлено "<< (num*(num-1)*(num-2))/6 <<", из которых "<< num_wrong << " треугольник(-ов) не существует\n";
}
Answer the question
In order to leave comments, you need to log in
num_wrong=num_wrong++
What's this? What is this for?
In general, here UB. Correct the line.
You know how to write code, it's time to learn how to use a debugger...
(3.5)(5.7)(7.9)(9.11) - do the points lie on the same line? Is there a check for this? The length of the longer side must be less than the sum of the lengths of the other two. If it is equal to the sum, then the points lie on the same straight line and the triangle does not exist.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question