Help me! C language

Asked

Viewed 85 times

-1

I’m starting now and wanted to know what’s wrong, the program opens, I put the values and when you click 'enter' nothing happens, please help me.

#include <stdio.h>
#include <stdlib.h>

main() {
    float bc, ab, ac, a, b, c, d;
    printf("\nEntre com quatro numero:\n\n");
    scanf("%f %f %f %f", & a, & b, & c, & d);
    bc = b - c;
    if (bc < 0)
        bc = bc * -1;
    ab = a - b;
    if (ab < 0)
        ab = ab * -1;
    ac = a - c;
    if (ac < 0)
        ac = ac * -1;
    if ((bc < a && a < b + a) && (ab < b && b < a + b) && (ac < c && c < a + c))
        if (a == b && b == c)
            printf("\n Trinagulo \n\n");
        else if (a == b || b == c || c == a)
        printf("\n Triangulo isosceles\n\n");
    else printf("\n Triangulo escaleno\n\n");
    system("pause");
}
  • 1

    Could you explain the code? Why enter 4 numbers if you only use three? And what values you reported? Tried with different values? Research how to do a table test.

  • 1

    Pay attention to how you create the question title. Avoid words like help please, or urgent! Moderators generally negatively ask questions with this type of title. Be straight and write the full code.

2 answers

1

I believe it’s more a matter of familiarity with programming. Pay attention to edentation and clasps. Otherwise, congratulations on a good job.

Code in execution

Code:

#include <stdio.h>
#include <stdlib.h>

int main() {

    float bc, ab, ac, a, b, c;

    a = 0.0f;
    b = 0.0f;
    c = 0.0f;

    printf("\nEntre com tres numero:\n\n");
    scanf("%f %f %f", &a, &b, &c);

    bc = b - c;

    if (bc < 0)  bc = bc * -1;

    ab = a - b;

    if (ab < 0)  ab = ab * -1;

    ac = a - c;

    if (ac < 0)  ac = ac * -1;

    if ( (bc < a && a < b + a) && (ab < b && b < a + b) && (ac < c && c < a + c) ){

        if (a == b && b == c)
            printf("\n Trinagulo \n\n");
        else if (a == b || b == c || c == a)
            printf("\n Triangulo isosceles\n\n");
       else 
        printf("\n Triangulo escaleno\n\n");
    }
}
  • 1

    I think you have not tested this code, it is not producing the correct results, you are declaring a variable that is not used and scanf is reading an extra variable in the list.

  • 1

    Correction performed.

1


When you have an if/Else chaining inside another if/Else you need to use keys to avoid the ambiguous Else problem, where the compiler doesn’t know if Else belongs to the internal or external if, the keys fix it.

There is no need to create so many variables for this algorithm, and their logic is not correct.

To verify whether the segments can form a triangle it is necessary to know if each segment is smaller than the sum between the other two segments. If yes, check whether the segments are equal (equilateral), different (scalene) or equal with a difference (isosceles). If not, the segments cannot form a triangle.

#include <stdio.h>

int main() {
  printf("Insira 3 valores: ");

  // C não inicializa as variáveis, portanto elas devem ser inicializadas manualmente.
  float a = 0.0f, b = 0.0f, c = 0.0f;
  scanf("%f %f %f", &a, &b, &c);

  // Cada segmento precisa ser menor que a soma dos outros dois.
  if(a < b + c && b < a + c && c < a + b) {
    printf("Triangulo: ");

    if(a == b && b == c) {
      // Segmentos iguais: Equilátero.
      printf("Equilátero\n");
    }else if (a != b && b != c && c != a) {
      // Segmentos diferentes: Escaleno.
      printf("Escaleno\n");
    }else {
      // Por dedução se não é Equilátero e nem Escaleno é Isósceles.
      printf("Isósceles\n");
    }
  }else {
    printf("Não é possível formar um triangulo!\n");
  }

  return 0;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.