# Thread: while loop giving me a hard time once again

1. ## while loop giving me a hard time once again

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;
}```

2. i have re-thought the approach and tried it like this, but it still only takes the same numbers as input otherwise it stops in the middle of the run.

Code:
```#include <stdio.h>
#include <stdlib.h>

int main()
{
int delky[10000];
int obvod[30000];
int temp;
int q = 0;
int o = 0;
int a = 1;
int b = 2;
int i = 0;
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 < 3)
{
printf("\nNespravny vstup.\n");
return (0);
}

while (delky[c] != 0)
{

if (((delky[c] > delky[c + a]) && (delky[c] > delky[c + b]) && (delky[c + a] + delky[c + b] > delky[c])) || (delky[c] == delky[c + a] ) || delky[c] == delky[c + b] || delky[c + a] == delky[c + b])
{
pocettroj++;
temp = (delky[c] + delky[c + a] + delky[c + b]);

for (q=0;q<1;q++)
{
obvod[o] = temp;
o++;
}
a++;
b++;
if (delky[c + b] == 0)
{
c++;
}

}
}
printf("\n obvody: %d %d %d", obvod[0], obvod[1], obvod[2]); /*these printfs are also only for my check*/
printf("\n pocet iteraci hodnota c,a: %d,%d", c, a );
printf("\nDelka nosniku %d %d %d", delky[c], delky[c + a], delky[c + b]);
printf("\nPocet trojuhelniku: %d", pocettroj); /*this should be the final output that tells how many triangles are possible to create*/
return 0;
}```

3. Tips:
* Use a text editor that auto-indents code based on the filetype (ie vi/vim). Indentation should be consistent and add spacing around logical groupings of code
* Break down complex logic conditions into smaller manageable pieces, which helps with maintenance and modification. You can use variables to store intermediate logical conditions that are then used in a an actual if statement such as if (condition1 || condition2 && condition3) where condition* variables are set prior to some other complex logic result. You can also use a function to hide the logic and give it a suitable name to describe what it might be checking.
* Use for loops if you're planning on using a initialize, condition, increment pattern as this brings everything right to top so it can't be lost inside an if condition (hint: problem with your c++ increment). Use while loops for pretty much everything else or if you have some exotic initialization/increment conditions. Use do ... while() loops for error catches and loops that require at least one iteration always.
* Use aliases for intermediate values like 'arr[a + b]' instead of referring to them over and over again using the index notation which can be error prone and become a maintenance nightmare.