Home > Net >  user inputed array on last input changes for loop value instead of array
user inputed array on last input changes for loop value instead of array

Time:04-07

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

CodePudding user response:

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]

  •  Tags:  
  • c
  • Related