Hi
What I'm trying to do is count the number of unique triangles I can construct with lines. The input I get are lenghts of the lines (int) and can range from 3 to 10000.
The way I went about it is: after getting the input and some incorrect input checks. The while loop is supposed to run for delky[0] and compare it first with [1] and [2] then with [2] and [3] and so on and if these combinations create a triangle it "pocettroj" is incremented by one. Then I count the perimeter and put it into another array, so that I can later discount duplicates (not done yet).
The problems are that it runs only for some numbers and that I dont know how to now if the loop runs and compares [0] with all the other numbers in the array I tried with the first if in the while loop but then how would I make it stop.
I know its "a bit" messy but any tips and explanations are much appreciated.
Code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int delky[10000];
int obvod[30000];
int temp;
int q = 0;
int o = 0;
int i = 0;
int a = 1;
int b = 2;
int c = 0;
int pocettroj = 0;
int pocetnosniku = 0;
printf("delky nosniku:\n");
while (scanf(" %d ", &delky[i]) == 1 && i < 10000)
{ /*if (scanf(" %d ", &delky[i]) == 0)
{
printf("Nespravny vstup.\n");
return;
} */
i++;
pocetnosniku++;
}
if (i < 2)
{
printf("Nespravny vstup.\n");
return (0);
}
printf("\nDelka nosniku pred while loopem [0,1,2] %d %d %d", delky[0], delky[1], delky[2]); /*these printfs are here only for my checks*/
printf("\npocet nosniku: %d", i);
while (a < pocetnosniku -1)
{
if (delky[b] == 0)
{
a = c + 1;
b = c + 2;
c++;
}
if (((delky[c] > delky[a]) && (delky[c] > delky[b]) && (delky[a] + delky[b] > delky[c])) || (delky[c] == delky[a] ) || delky[c] == delky[b] || delky[a] == delky[b])
{
pocettroj++;
temp = (delky[c] + delky[b] + delky[a]);
for (q=0;q<1;q++)
{
obvod[o] = temp;
o++;
}
a++;
b++;
}
}
printf("\n obvody: %d %d %d", obvod[0], obvod[1], obvod[2]); /*these printfs are also only for my check*/
printf("\n hodnota c: %d", c );
printf("\nPocet iteraci: %d", a);
printf("\nDelka nosniku [c,a,b] %d %d %d", delky[c], delky[a], delky[b]);
printf("\nPocet trojuhelniku: %d", pocettroj); /*this should be the final output that tells how many triangles are possible to create*/
return 0;
}