I have structure of triangles, user must enter number of triangles that he wants to have and then fill data for each one - length of edges. Then program writes edge with biggest length. First I allocate memory for my triangle array, and then I fill each triangle with data, but my program never saves last entered triangle in my array and I get some weird number as biggest edge. Here is my code
struct triangle
{
int a;
int b;
int c;
};
int getBiggestEdge(int a, int b, int c)
{
int max = a;
if(b>max) {max=b;}
if(c>max) {max=c;}
return max;
}
int maxEdge(struct triangle niz [], int br)
{
int i, najduza;
najduza = getBiggestEdge(niz[0].a, niz[0].b, niz[0].c);
for(i=1; i<=br; i++)
{
if(getBiggestEdge(niz[i].a, niz[i].b, niz[i].c) > najduza)
{
najduza = getBiggestEdge(niz[i].a, niz[i].b, niz[i].c);
}
}
return najduza;
}
int main()
{
int i, n, edge;
struct triangle* niz;
printf("Insert number of triangles: ");
scanf("%d", &n);
niz= (struct triangle*) malloc(n * sizeof(struct triangle));
if (niz == NULL)
{
printf("Error!");
return 0;
}
printf("Insert data for every triangle (a b c):\n");
for(i=0;i<n;i++)
{
printf("Triangle: ");
scanf("%d %d %d", &niz[i].a, &niz[i].b, &niz[i].c);
}
edge = maxEdge(niz, n);
printf("Biggest edge is: %d\n",edge);
free(niz);
return 0;
}