c programming - exercise d

Download C Programming - Exercise D

If you can't read please download the document

Upload: theo

Post on 24-Sep-2015

7 views

Category:

Documents


2 download

DESCRIPTION

C Programming - Exercise D

TRANSCRIPT

//Exercise D#include #include #define N 5typedef struct yfalos yf;struct yfalos {char id[10];float coor[2];float safe_dis;};int navigator(float coor[], yf yfalos[], float* dis);int main(void) {yf yfalos[N];float radioC[2] = {0.0f}, R = 0.0, shipC[2] = {0.0f}, kontinoteros_yf = 0.0f;int i = 0, nav;printf("Please type the coordinates of the radiopharos (first x and then y: ");scanf(" %f %f", radioC, radioC+1);printf("And the radius of the \"safe\" circle around it: ");scanf(" %f", &R);for (; i < N; i++) {printf("\nPlease type the id of the %d%s coral: ", i, (!i) ? "st" : (i == 2) ? "nd" : (i == 3) ? "rd" : "th");scanf(" %s", yfalos[i].id);printf("\nNow type the coordinates of %s (first x and then y): ", yfalos[i].id);scanf(" %f %f", &yfalos[i].coor[0], &yfalos[i].coor[1]);printf("\nAnd finaly the safe distance for %s: ", yfalos[i].id);scanf(" %f", &yfalos[i].safe_dis);}while (1) {printf("\n\nPlease type the coordinates of the ship (x first and then y): ");scanf(" %f %f", shipC, shipC+1);if (sqrt(pow(shipC[0] - radioC[0], 2.0f) + pow(shipC[1] - radioC[1], 2.0f)) < R) {nav = navigator(shipC, yfalos, &kontinoteros_yf);if (nav == 1)printf("\n\tRed alert");else if (nav == 2)printf("\n\tYellow alert");printf("\nThe closest coral has the id '%s' and is %.3f miles far from here!", ,kontinoteros_yf);}}return 0;}int navigator(float coor[], yf yfalos[], float* dis) {int i = 1, pos = 0;float d = 0.0f;*dis = sqrt(pow(coor[0] - yfalos[0].coor[0], 2.0f) + pow(coor[1] - yfalos[0].coor[1], 2.0f));for (; i < N; i++) {if ((d = sqrt(pow(coor[0] - yfalos[i].coor[0], 2.0f) + pow(coor[1] - yfalos[i].coor[1], 2.0f))) < *dis) {*dis = d;pos = i;}}return (*dis > yfalos[i].safe_dis) ? 0 : (*dis > yfalos[i].safe_dis*0.7) ? 2 : 1;}