Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Khumalo_Submission_Commit #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions VehiclePositions/Instructions/makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Makefile to (1) compile and (2) clean
# - Rule for compilation should create solution.exe
# - Should only recompile code when any source code changed
# - Rule for clean should remove solution.exe
# - Should not show error if the file does not exist
# - Use GCC compiler toolchain
# - Specify the following options:
# - Disable optimization
# - Disable generation of debug information
# - Enable all general warnings
# - Enable conversion warnings
# Author: S Khumalo

CC = gcc
CFLAGS = -Wall -O2

all: solution

solution: solution.c
$(CC) $(CFLAGS) -o $@ $< -lm

clean:
rm -f solution
Binary file not shown.
94 changes: 94 additions & 0 deletions VehiclePositions/Instructions/solution.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
AUthor: S Khumalo
Project: Read a binary data file and find nearest positions

Approach:
Open and read .dat file
Apply structs on the file, specifically to the latitude, longtude and position ID
Use the K-Nearest alogorithm to find the nearest position to each of the 10
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

// Define a structure to hold the data
struct VehiclePosition {
int positionID;
char vehicleRegistration[256];
float latitude;
float longitude;
unsigned long long utcTimestamp;
};

// Define the 10 coordinates
struct Coordinate {
int positionID;
float latitude;
float longitude;
} coordinates[] = {
{1, 34.544909, -102.100843},
{2, 32.345544, -99.123124},
{3, 33.234235, -100.214124},
{4, 35.195739, -95.348899},
{5, 31.895839, -97.789573},
{6, 32.895839, -101.789573},
{7, 34.115839, -100.225732},
{8, 32.335839, -99.992232},
{9, 33.535339, -94.792232},
{10, 32.234235, -100.222222}
};

// Function to calculate the distance between two coordinates
float calculateDistance(float lat1, float lon1, float lat2, float lon2) {
float radlat1 = M_PI * lat1 / 180;
float radlat2 = M_PI * lat2 / 180;
float radlon1 = M_PI * lon1 / 180;
float radlon2 = M_PI * lon2 / 180;
float theta = lon1 - lon2;
float radtheta = M_PI * theta / 180;
float dist = sin(radlat1) * sin(radlat2) + cos(radlat1) * cos(radlat2) * cos(radtheta);
dist = acos(dist);
dist = dist * 180 / M_PI;
dist = dist * 60 * 1.1515;
return dist;
}

int main() {
// Open the binary file for reading
FILE *file = fopen("C:\\Users\\Refilwe\\Desktop\\Recruitment\\VehiclePositions\\Instructions\\VehiclePositions.dat", "rb");
if (!file) {
perror("Error opening file");
return 1;
}

// Read the file and find the closest positions
struct VehiclePosition closestPositions[10];
for (int i = 0; i < 10; i++) {
closestPositions[i].positionID = -1; // Initialize to an invalid ID
closestPositions[i].utcTimestamp = ULLONG_MAX; // Initialize to max timestamp
}

struct VehiclePosition currentPosition;

while (fread(&currentPosition, sizeof(struct VehiclePosition), 1, file) == 1) {
for (int i = 0; i < 10; i++) {
float distance = calculateDistance(currentPosition.latitude, currentPosition.longitude,
coordinates[i].latitude, coordinates[i].longitude);
if (distance < calculateDistance(closestPositions[i].latitude, closestPositions[i].longitude,
coordinates[i].latitude, coordinates[i].longitude)) {
closestPositions[i] = currentPosition;
}
}
}

// Close the file
fclose(file);

// Print the closest positions
for (int i = 0; i < 10; i++) {
printf("Closest Position ID %d: %d\n", i + 1, closestPositions[i].positionID);
}

return 0;
}