+1 480 409 0818 

Spaceship Navigating Simulator

Simulating a spaceship navigating through an asteroid field using C assignment help

Solution /* ----------------------------------------------- Submitted By: ????????? Homework Number: ????????? Credit to: ???????????? Submitted On: December 3, 2018 By submitting this program with my name, I affirm that the creation and modification of this program is primarily my work. ------------------------------------------------ */ #include #include #include #include #define MAX_IDENTITIES 20 #define MAX_ASTEROIDS 20 #define MAX_NAME_LEN 20 #define MAX_LINE_LEN 100 #define DIMENSIONS 3 /*3 dimensions: x, y and z*/ #define WARNING_DISTANCE 750.0 #define ASTEROID_SPEED 25.0 #define CHART_WIDTH 70 /* Definition of the ident structure */ struct Ident { char Name[MAX_NAME_LEN]; int PIN; }; /* Load identity data from the file "filename", saves the loaded identities in the array identities, returns the number of identities loaded */ int loadIdentities(char *filename, struct Ident *identities) { char buffer[MAX_LINE_LEN]; /* buffer for reading lines from the file */ FILE *file; int pin; char name[MAX_NAME_LEN]; int nIdentities; int n; file = fopen(filename, "rt"); /* open file for reading */ if (file == NULL) /* if there's an error opening the file */ { printf("Error: file %s can't be opened!\n", filename); return -1; /* return with error */ } nIdentities = 0; /* star identity counter to zero */ while(fgets(buffer, MAX_LINE_LEN, file)) /* repeat reading lines until end of file */ { n = sscanf(buffer, "%s %d", name, &pin); /* get the name and pin */ if (n == 2) /* if the scanf found the 2 fields, name and pin */ { identities[nIdentities].PIN = pin; /* copy the pin to the structure array */ strcpy(identities[nIdentities].Name, name); /* copy the name to the structure array */ nIdentities++; /* increment the number of loaded identities */ } } fclose(file); return nIdentities; /* return the number of loaded identities */ } /* Ask the user for his/her name and pin and uses the identities array to validate the entry to the program, if the data was not valid, returns 0, otherwise the user is valid and returns 1 */ int validateUser(struct Ident *identities,int nIdentities) { int pin; char name[MAX_NAME_LEN]; int i, found; int tries; int valid; valid = 0; /* by default the user is not valid */ for (tries = 0; tries < 3 && !valid; tries++) { printf("Please enter your name: "); scanf("%s", name); /* read name from user (no spaces) */ printf("Please enter your PIN number: "); scanf("%d", &pin); /* read pin from user */ /* look for the provided name and pin in the array of identities */ found = -1; /* change to found index if we found the name and pin */ for (i = 0; i < nIdentities && found == -1; i++) /* repeat for all identities while not found */ { if (!strcmp(name, identities[i].Name)) /* if we found the name at position i*/ found = i; } if (found != -1) /* if we found the name */ { if (pin == identities[found].PIN) /* if the pin is correct */ valid = 1; /* mark user as valid */ else printf("Invalid PIN. Please try again.\n"); } else /* we didn't find the name */ printf("Invalid Name. Please try again.\n"); } if (!valid) /* if the name was not valid */ { printf("\nInvalid user. Terminating program.\n"); return 0; } else { printf("\nWelcome to the program, %s!\n\n", name); return 1; } } /* Load the asteroid data from the file "filename", saves the loaded asteroid names in the array names, and the positions in the array positions, returns the number of asteroids loaded */ int loadAsteroids(char *filename, char names[][MAX_NAME_LEN], int positions[][DIMENSIONS]) { char buffer[MAX_LINE_LEN]; /* buffer for reading lines from the file */ FILE *file; int nAsteroids; char name[MAX_NAME_LEN]; int n, x, y, z; file = fopen(filename, "rt"); /* open file for reading */ if (file == NULL) /* if there's an error opening the file */ { printf("Error: file %s can't be opened!\n", filename); return -1; /* return with error */ } nAsteroids = 0; /* star asteroid counter to zero */ while(fgets(buffer, MAX_LINE_LEN, file)) /* repeat reading lines until end of file */ { n = sscanf(buffer, "%s %d %d %d", name, &x, &y, &z); /* get the name and x,y,z position */ if (n == 4) /* if the scanf found the 4 fields, name and x,y,z */ { positions[nAsteroids][0] = x; /* copy the x,y,z position to the array */ positions[nAsteroids][1] = y; positions[nAsteroids][2] = z; strcpy(names[nAsteroids], name); /* copy the name to the array */ nAsteroids++; /* increment the number of loaded asteroids */ } } fclose(file); return nAsteroids; /* return the number of loaded asteroids */ } /* Calculates distances to every asteroid position in the array and saves the result in the distances array */ void calculate distances(int positions[][DIMENSIONS], double *distances, int steroids) { FILE *file; int x, y, z; int i; for (i = 0; i < nAsteroids; i++) { x = positions[i][0]; y = positions[i][1]; z = positions[i][2]; distances[i] = sqrt(x*x + y*y + z*z); /* calculate distance and save result in distance array */ } } /* Calculates distances to every asteroid position in the array and saves the result in a file, if there are no errors returns 1, otherwise, it returns 0 */ int saveDistances(char *filename, char names[][MAX_NAME_LEN], int positions[][DIMENSIONS], double *distances, int nAsteroids) { FILE *file; int x, y, z; int i; file = fopen(filename, "wt"); /* open file for writing */ if (file == NULL) /* if there's an error opening the file */ { printf("Error: file %s can't be created!\n", filename); return 0; /* return with error */ } for (i = 0; i < nAsteroids; i++) { x = positions[i][0]; y = positions[i][1]; z = positions[i][2]; /* save data in file */ fprintf(file, "%s\t%d\t%d\t%d\t%.2lf\n", names[i], x, y, z, distances[i]); } fclose(file); return 1; } /* Sort asteroids from the smallest distances to the largest using bubble sort */ void sortAsteroidIndices(double *distances, int *sorted indices, int steroids) { int i , j; int temp; for (i = 0; i < nAsteroids; i++) sortedIndices[i] = i; /* fill indices with the sequential numbers */ for (i = 0; i < nAsteroids; i++) for (j = 0; j < nAsteroids - 1; j++) if (distances[sortedIndices[j]] > distances[sortedIndices[j + 1]]) { temp = sortedIndices[j]; /* swap indices */ sortedIndices[j] = sortedIndices[j + 1]; sortedIndices[j + 1] = temp; } } /* Display a warning for all the asteroids at less than 750km of distance */ void displayClosestAsteroids(char names[][MAX_NAME_LEN], int positions[][DIMENSIONS], double *distances, int *sortedIndices, int nAsteroids) { int i, j; double timeToImpact; int x, y, z; printf("\nWarning - Warning - Warning\n\n"); j = sortedIndices[0]; if (distances[j] < WARNING_DISTANCE) { x = positions[j][0]; y = positions[j][1]; z = positions[j][2]; printf(" Nearest asteroid %s at %d, %d, %d: %.2lf km away\n", names[j], x, y, z, distances[j]); timeToImpact = distances[j] / ASTEROID_SPEED; printf(" Time to impact %.2lf seconds\n\n", timeToImpact); } for (i = 0; i < nAsteroids && distances[sortedIndices[i]] < 750; i++) { j = sortedIndices[i]; printf(" Asteroid %s at %d, %d, %d: %.2lf km away\n", names[j], x, y, z, distances[j]); } printf("\n------- - ------- - -------\n\n"); } /* Display a distance chart for all the asteroids */ void displayAsteroidChart(char names[][MAX_NAME_LEN], double *distances, int *sortedIndices, int nAsteroids) { int i, j; double timeToImpact; int x, y, z; double max, scale; int n; max = distances[sortedIndices[nAsteroids - 1]]; scale = CHART_WIDTH / max; printf("Asteroid Distance Bar Chart: \n\n"); for (i = 0; i < nAsteroids; i++) { printf("%3.3s|", names[i]); /* print axis label */ n = distances[i] * scale; /* calculate the number of asterisks required */ for (j = 0; j < n; j++) /* print the distance as asterisks */ printf("*"); printf("\n"); } max = (int)((max + 9) / 100) * 100; /* get max rounded to 100 */ n = max / 100; /* get number of ticks */ printf(" 0"); for (i = 0; i < n; i++) /* print ticks */ { printf("----|"); } printf("-----\n\n"); } /* Display the asteroid ranking by distance */ void displayRanking(char names[][MAX_NAME_LEN], double *distances, int *sortedIndices, int nAsteroids) { int i; int *ptr; printf("\nAsteroid ranking: \n"); ptr = &sortedIndices[0]; /* point to initial index used to transverse the asteroid array */ for (i = 0; i < nAsteroids; i++) { printf(" Asteroid %s %.2lf\n", names[*ptr], distances[*ptr]); ptr++; /* advance to next index */ } } int main() { struct Ident identities[MAX_IDENTITIES]; /* space to save identities */ char asteroidNames[MAX_ASTEROIDS][MAX_NAME_LEN]; /* space to save asteroid names */ int asteroidPos[MAX_ASTEROIDS][DIMENSIONS]; /* space to save asteroid positions */ double distances[MAX_ASTEROIDS]; /* space to save asteroid distances */ int sortedIndices[MAX_ASTEROIDS]; /* space to save sorted indices */ int nIdentities; /* number of loaded identities */ int nAsteroids; /* number of loaded asteroids */ int i,j; printf("=== Maher ===\n\n"); /* Print my name as the first output */ /* load the identities from the ident.txt file, save in "identities"*/ nIdentities = loadIdentities("ident.txt", identities); if (nIdentities < 0) /* if there was an error, exit the program */ return 1; /* validate user using the identity info */ if(!validateUser(identities, nIdentities)) return 1; /* if the user can't be validated, exit the program */ /* load the asteroid data from the asteroids.txt file, save in "asteroidNames" and asteroidPos */ nAsteroids = loadAsteroids("asteroids.txt", asteroidNames, asteroidPos); if (nAsteroids < 0) /* if there was an error, exit the program */ return 1; /* calculate the distances and save them in the distances array */ calculateDistances(asteroidPos, distances, nAsteroids); /* save distance data in the distance.txt file */ if(!saveDistances("distance.txt", asteroidNames, asteroidPos, distances, nAsteroids)) return 1; /* if there was an error, exit the program */ /* sort the distances and save the sorted indices in the sortedIndices array */ sortAsteroidIndices(distances, sortedIndices, nAsteroids); /* display warning about the closest asteroids */ displayClosestAsteroids(asteroidNames, asteroidPos, distances, sortedIndices, nAsteroids); /* display a bar chart of asteroid distances */ displayAsteroidChart(asteroidNames, distances, sortedIndices, nAsteroids); /* display the asteroid ranking using the sorted distances */ displayRanking(asteroidNames, distances, sortedIndices, nAsteroids); return 0; }