0

I was copy pasting code and for some reason this code does not work after being copy pasted? On the last input instead of the value being given to the array it changes the value of the for loop. It seems to work only when all the rest of the code is included but that makes no sense to me. Help!

    #include <stdio.h>
    #include <math.h>
    int main(void){
    
        int verts[7];
        int shapesquare;
        int shapequad;
        
        float AB;
        float BC;
        float CD;
        float DA;
        
        float area;
        
        float angleABC;
        float angleBCD;
        float angleCDA;
        float angleDAB;
    
        int i;
        
        char Yes;
        
        start:
            
        printf("Shape checker\n");
    
        for (i = 0; i < 8; i++){
            if(i == 0){
                printf("\nWhat is the x cord of vertice A?\n");
            }
            if(i == 1){
                printf("\nWhat is the y cord of vertice A?\n");
            }
            if(i == 2){
                printf("\nWhat is the x cord of vertice B?\n");
            }
            if(i == 3){
                printf("\nWhat is the y cord of vertice B?\n");
            }
            if(i == 4){
                printf("\nWhat is the x cord of vertice C?\n");
            }
            if(i == 5){
                printf("\nWhat is the y cord of vertice C?\n");
            }
            if(i == 6){
                printf("\nWhat is the x cord of vertice D?\n");
            }
            if(i == 7){
                printf("\nWhat is the y cord of vertice D?\n");
            }
                scanf( "%d", &verts[i]);
                printf("%d",i);
        }

    AB = sqrt(pow(verts[2]-verts[0],2)+pow(verts[3]-verts[1],2));
    BC = sqrt(pow(verts[4]-verts[2],2)+pow(verts[5]-verts[3],2));
    CD = sqrt(pow(verts[6]-verts[4],2)+pow(verts[7]-verts[5],2));
    DA = sqrt(pow(verts[0]-verts[6],2)+pow(verts[1]-verts[7],2));
    
    
    angleABC = acos((((verts[0]-verts[2])*(verts[4]-verts[2]))+((verts[1]-verts[3])*(verts[5]-verts[3])))/(AB*BC));
    angleBCD = acos((((verts[2]-verts[4])*(verts[6]-verts[4]))+((verts[3]-verts[5])*(verts[7]-verts[5])))/(BC*CD));
    angleCDA = acos((((verts[4]-verts[6])*(verts[0]-verts[6]))+((verts[5]-verts[7])*(verts[1]-verts[7])))/(CD*DA));
    angleDAB = acos((((verts[6]-verts[0])*(verts[2]-verts[0]))+((verts[7]-verts[1])*(verts[3]-verts[1])))/(AB*DA));
    
    
if((ceil(10000*angleABC) == 15708)&&(ceil(10000*angleBCD) == 15708)&&(ceil(10000*angleCDA) == 15708)&&(ceil(10000*angleDAB) == 15708)){

    shapesquare = 1;
    shapequad = 0 ;
}else{
    shapequad = 1;
    shapesquare = 0;
}
if(shapesquare == 1 && AB == BC && BC == CD && DA == AB){
    printf("Your shape is a square\n");
}else{if(shapequad == 0){
    printf("Your shape is a rectangle\n");
}
}
if(shapequad == 1 && ceil(10000*angleABC) == ceil(10000*angleCDA) && ceil(10000*angleBCD) == ceil(10000*angleDAB)){
if(AB == BC && BC == CD && DA == AB){
    printf("Your shape is a diamond\n");    
}else{
    printf("Your shape is a parallelogram\n");
}

}else{if(shapesquare == 0){
    printf("Your shape is a quadrilateral\n");
}

}
if(shapesquare == 1){
    area = AB * BC;
    printf("The area is %f \n", area);
}
     printf("\n\nWould you like to do another calculation? \n Y for continue \n");
    
    scanf(" %s", &Yes);

    if(Yes == 'Y'){
        goto start;
    }
    printf("goodbye!");

    return 0;
}

output Shape checker

What is the x cord of vertice A? 3 0

What is the y cord of vertice A? 3 1

What is the x cord of vertice B? 3 2

What is the y cord of vertice B? 3 3

What is the x cord of vertice C? 3 4

What is the y cord of vertice C? 3 5

What is the x cord of vertice D? 3 6

What is the y cord of vertice D? 3 3

What is the x cord of vertice C? 3 4

What is the y cord of vertice C? 3 5

What is the x cord of vertice D? 3 6

What is the y cord of vertice D? 3 3

What is the x cord of vertice C? 3

pm100
  • 48,078
  • 23
  • 82
  • 145
  • 2
    You array can fit `7` numbers. How many do you try to write to it? – Yksisarvinen Apr 06 '22 at 23:42
  • array can fit 8 numbers including zero. This code works but only when the code after it is included. I can include the rest. – Samuel Armstrong Apr 06 '22 at 23:45
  • https://en.cppreference.com/w/c/language/array – pm100 Apr 06 '22 at 23:47
  • No, `int verts[7];` can only fit `7` numbers, that's exactly how you declared it. It may appear to work, but you cannot really put 8 numbers in an array of length 7. Accessing `verts[i]` when `i == 7` is Undefined Behaviour - changing value of another variable would fit in the range of possible outcomes of such code. – Yksisarvinen Apr 06 '22 at 23:48
  • You can combine all those `printf` statements. `'x'` and `'y'` can be tied to `i` being odd or even. And `'A','B','C','D'` can be tied to `x / 2`. – 001 Apr 06 '22 at 23:48
  • 1
    The reason it seems to work sometimes is becuase you have entered Undefined Behavior land. Your code after you assign to `verts[7]` can do anything it wants, including appearing to work. – pm100 Apr 06 '22 at 23:49

1 Answers1

1

You have

int verts[7];

with

 for (i = 0; i < 8; i++){
     ....
      scanf( "%d", &verts[i]);

You cannot fit 8 numbers in a 7 entry array. Either change the arrays size or the for loop number


verts[7] creates an array, that array has seven slots in it. THey are

verts[0]
verts[1]
verts[2]
verts[3]
verts[4]
verts[5]
verts[6]

Count them, there are 7 slots numbered 0 to 6. Your loop tries to assign to verts[7]

pm100
  • 48,078
  • 23
  • 82
  • 145
  • you can. Verts[0] is used – Samuel Armstrong Apr 06 '22 at 23:49
  • @SamuelArmstrong humor me - just try changing it to 8 and see if it works. – pm100 Apr 06 '22 at 23:50
  • @SamuelArmstrong I will add more explanation to the answer – pm100 Apr 06 '22 at 23:50
  • @SamuelArmstrong I'm sorry, but you need to read [a good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). An array of length 7 can only store 7 elements. Valid indices for `verts` are from `0` to `6` (inclusive). `verts[7]` is already one past the end, trying to access that element is Undefined Behaviour. – Yksisarvinen Apr 06 '22 at 23:51
  • @pm100 Yea you are right. I still dont understand how it could work in the first place however. – Samuel Armstrong Apr 06 '22 at 23:55
  • @SamuelArmstrong UB, the worst kind of UB is a program that seems to work, but fails in production on the busiest day of the year. . C++ vectors in debug mode would tell you too (you had this tagged c++ even though its not - I changed it to c) – pm100 Apr 06 '22 at 23:59
  • Terminology note: UB -> Undefined Behaviour. Code is a description of program behaviour, not a list of instructions for the computer to run. The compiler takes this description of behaviour and converts it into a list of instructions. If your code describes something that is consistent with the rules of the language, it compiles, but has no language-defined behaviour the compiler does... whatever it wants to do. Usually it just goes on its merry way and does whatever would normally happen, no matter how stupid a thing this might be, and in this case rolls off the end of the array. – user4581301 Apr 07 '22 at 00:05