Thursday, December 12, 2019

Array of Possible for Artillery Programming

Question: Define the Data types based upon Array of Possible for Artillery Programming. Answer: #include #include #include struct Projectile { char Pname[50]; double blastradius; }; struct Artillery { char Aname[50]; double mVelocity; double maxElevation; double minElevation; }; void PrintArtillery(struct Artillery artillery[]) { strcpy(artillery[0].Aname, "Cannon"); artillery[0].mVelocity=200; artillery[0].minElevation= 0; artillery[0].maxElevation=45; //Populating the Artillery array strcpy(artillery[1].Aname, "Howitzer"); artillery[1].mVelocity=900; artillery[1].minElevation= 20; artillery[1].maxElevation=65; strcpy(artillery[2].Aname, "Mortar"); artillery[2].mVelocity=805; artillery[2].minElevation= 50; artillery[2].maxElevation=85; //display menu for artillery printf("Artillery Namett Muzzle Velocityt Minimum Elevationt Maximum Elevation"); int i; for(i=0; i3; i++) printf("n%d.%stt %1.fm/s tt %1.f degreestt %1.f degrees",i+1,artillery[i].Aname,artillery[i].mVelocity,artillery[i].minElevation,artillery[i].maxElevation); } void PrintProjectile(struct Projectile projectile[]) { strcpy(projectile[0].Pname,"High Explosive Anti -Tank"); projectile[0].blastradius= 5; //Populating the Projectile array strcpy(projectile[1].Pname,"M549 HERA "); projectile[1].blastradius= 15; strcpy(projectile[2].Pname," W19 Katie AFAP"); projectile[2].blastradius= 4200; //display menu for projectile printf("nnProjectile Namettt Blast Radius"); int i; for(i=0; i3; i++) printf("n%d.%stt %1.f meters",i+1,projectile[i].Pname, projectile[i].blastradius); } double CalculateAirTime(double velocity, double angle) { double t; t = (2 * velocity * sin(angle)) / 9.8; return t; } double CalculateRange(double velocity, double angle) { double max; max = (velocity * velocity * sin(2 * angle)) / 9.8; return max; } void SimulateProjectile(double * height, double * distance, double time, double velocity, double angle) // using pass by reference function { * height = velocity * time * sin(angle) - 0.5 * 9.8 * time * time; //formula to calculate height * distance = velocity * time * cos(angle); // distance calculation } //main function int main() { //local variables int angleofelevation, muzzleVelocity, minelevation, maxelevation, flag = 0, choice_artillery = 0, choice_projectile=0, targetdistance; double convert, angleRadians, distancetravelled, maxtime, airtime = 1; double distance = 0; double height = 0; struct Artillery A[3]; struct Projectile P[3]; PrintArtillery(A); do { printf("nEnter choice for Artillery (1 to 3): "); //enter choice for artillery scanf("%d", choice_artillery); if (choice_artillery == 1) { angleofelevation = 45; minelevation = 0, maxelevation = 45; //set minimum and maximum elevation muzzleVelocity = 200; //set initial velocity angleRadians = 3.14159 / 180 * angleofelevation; //converting angle of elevation from degree into radian maxtime = CalculateAirTime(muzzleVelocity, angleRadians); distancetravelled = CalculateRange(muzzleVelocity, angleRadians); printf("nnThe maximum range of the selected gun: %.2f N n", distancetravelled); //giving the output (distance travelled) after calculation in screen } else if (choice_artillery == 2) { angleofelevation = 65; minelevation = 20, maxelevation = 65; //set minimum and maximum elevation muzzleVelocity = 900; //set initial velocity angleRadians = 3.14159 / 180 * angleofelevation; //converting angle of elevation from degree into radian maxtime = CalculateAirTime(muzzleVelocity, angleRadians); distancetravelled = CalculateRange(muzzleVelocity, angleRadians); printf("nnThe maximum range of the selected gun: %.2f N n", distancetravelled); //giving the output (distance travelled) after calculation in screen } else if (choice_artillery == 3) { angleofelevation = 85; minelevation = 50, maxelevation = 85; //set minimum and maximum elevation muzzleVelocity = 805; //set initial velocity angleRadians = 3.14159 / 180 * angleofelevation; //converting angle of elevation from degree into radian maxtime = CalculateAirTime(muzzleVelocity, angleRadians); distancetravelled = CalculateRange(muzzleVelocity, angleRadians); printf("nnThe maximum range of the selected gun: %.2f N n", distancetravelled); //giving the output (distance travelled) after calculation in screen } } while ((choice_artillery 3) || (choice_artillery 1)); PrintProjectile(P); do{ printf("nEnter choice for Projectile (1 to 3): "); //enter choice for Projectile scanf("%d", choice_projectile); }while(choice_projectile3); //enter target distance printf("nEnter the value of target distance in meters: "); scanf("%d", targetdistance); //loop to enter angle of elevation do { //enter angle of elevation printf("nEnter the value of angle of elevation in degrees: "); scanf("%d", angleofelevation); //check condition if (angleofelevation = maxelevation angleofelevation = minelevation) { flag = 0; } else { printf("nInvalid velocity!!! please enter valid angle of elevation"); flag = 1; } } while (flag == 1); angleRadians = 3.14159 / 180 * angleofelevation; //converting angle of elevation from degree into radian do { SimulateProjectile( height, distance, airtime, muzzleVelocity, angleRadians);// calling printf("n At second %.2f , the shell is %.2f meters in the air and has travelled %.2f meters", airtime, height, distance); //giving the output (distance travelled) after calculation in screen airtime++; }while (height 0 ); airtime = (muzzleVelocity * sin (angleRadians)) / (0.5 * 9.8); // time when shell is in the ground distance = muzzleVelocity * airtime * cos(angleRadians); // total distance travelled by the shell printf("n At second %.2f , the shell has hit the ground and has travelled %.2f meters", airtime, distance); printf("n stimulation completen"); printf("n Total Air Time: %.2f N n", airtime); printf("n Total Distance : %.2f N n", distance); if(P[choice_projectile-1].blastradius = targetdistance) //check if target distance was less than or equal to the bladius radius or not { printf("n Hitn "); } else { printf("n Too long n"); } return 0; }

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.