This has to be programmed in C, please make it as simple as possible, I need to
ID: 3857735 • Letter: T
Question
This has to be programmed in C, please make it as simple as possible, I need to understand what each part does thank you
Problem Statement Your company manufactures electrically conductive metal bars with custom polygonal cross- sections. The metal extrusion machine is limited to producing metal bars with a four-sided polygon as the cross-section. You are given the assignment to calculate the electrical resistance of the bar given its cross-section shape, length, and type of metal Background P3 P4 Pl P2 Arbitrary Cross-section The DC resistance of a conductor having a length L and a uniform cross-sectional area A is given by this equation: where is the electrical conductivity of the conductor. Table 1 shows the conductivity of common metals used in electrical wires. Table 1: Metal electrical conductivity Conductor Silver Copper Gold Aluminum Platinum Conductivity (S/m) 6.30x10 5.96x107 4.10x10 3.50x10 0.943x107Explanation / Answer
Detailed explanation has been given using comments. Area has been calculated using Herons formula. The commented part shows area calculation using shoelace formula
CODE:
#include<stdio.h>
#include<math.h>
int main(){
//p1(x1,y1),p2(x2,y2) and so on
int x1=6,y1=4,x2=17,y2=4; //Coordinates of P1 and P2 are initialized
int x3,y3,x4,y4;
char choice='Y';
while(choice!='N'){
printf("Enter the x and y coordinates of P3 in mm ");
scanf("%d %d",&x3,&y3);
printf("Enter the x and y coordinates of P4 in mm ");
scanf("%d %d",&x4,&y4);
//Constraints: p3 and p4 must be distinct
if((x3==x4) && (y3==y4)){
printf("Error: P3 and P4 are having same coordinate ");
return 0;
}
//Constraints: p3 and p4 must be located within the defined grid
if((x3>25) || (x3<0) || (x4>25) || (x4<0) || (y3<5) || (y3>25) || (y4<5) || (y4>25) ){
printf("Error: P3 and P4 are outside the defined grid ");
return 0;
}
//Constraints: P4 must be located strictly to the left of P3
if(x4>=x3){
printf("Error: P3 is to the left of P4 ");
return 0;
}
//Constraints: P3 and P4 must be above P1 and P2
if( (y1>=y3) || (y1>=y4) || (y2>=y3) ||(y2>=y4) ){
printf("Error: Either P1 or P2 is located above P3 or P4 ");
return 0;
}
double a=0,l=0;//a=area,l=length
/*
// Calculating the area of cross section
a=abs(x1*y2+x2*y3+x3*y4+x4*y1-x2*y1-x3*y2-x4*y3-x1*y4)/2;// Calculated using shoelace formula
cout<<"Area A="<<a<<" mm²"<<endl;
*/
//Area as per Herons formula
//Using distance formula
double p1p2,p1p4,p3p4,p3p2,p1p3;
//Length of p1 to p2
p1p2=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
//Length of p1 to p4
p1p4=sqrt((x1-x4)*(x1-x4)+(y1-y4)*(y1-y4));
//Length of p3 to p4
p3p4=sqrt((x3-x4)*(x3-x4)+(y3-y4)*(y3-y4));
//Length of p3 to p2
p3p2=sqrt((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2));
//Length of p1 to p3
p1p3=sqrt((x1-x3)*(x1-x3)+(y1-y3)*(y1-y3));
//Area of traingle a1=p1p2p3
double a1,a2;
double s1=(p1p2+p3p2+p1p3)/2;
a1=sqrt(s1*(s1-p1p2)*(s1-p3p2)*(s1-p1p3));
double s2=(p1p4+p3p4+p1p3)/2;
a2=sqrt(s2*(s2-p1p4)*(s2-p3p4)*(s2-p1p3));
a=a1+a2;
printf("Area A= %lf mm² ",a);
//Length in meters
printf("Enter the length in meters ");
scanf("%lf",&l);
//Constraints: Length is valid or not
if(l<=0){
printf("Error: Length is invalid ");
return 0;
}
//Check the metal type and assign conductivity using switch case
char type;
double c=0;//c is for conductivity
printf("Enter the desired metal type(initials) ");
scanf(" %c",&type);
switch(type){
case 's':
case 'S':
c=6.30*10000000;
break;
case 'c':
case 'C':
c=5.96*10000000;
break;
case 'g':
case 'G':
c=4.10*10000000;
break;
case 'a':
case 'A':
c=3.50*10000000;
break;
case 'p':
case 'P':
c=0.943*10000000;
break;
default:
printf("Error: Invalid type ");
return 0;
}
//Calculation of resistance R
double R=0;
// We need to convert area from mm² to m² to get resistance in Ohms
a=a/1000000;//1m²=1000000mm²
R=l/(c*a);
printf("Resistance R= %lf ohms ",R);
printf("Enter N to quit or input Y to start with the same process ");
scanf(" %c",&choice);
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.