
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ program to find cabs nearby using Great Circle Distance formula
In this article, we will be discussing a program to find the cabs near about (less than 50km) using the Great Circle Distance formula.
Let us suppose we have been given a JSON file which contains the name and coordinates of the people who need a cab and also the coordinates of the all the cabs available.
To solve this, we would convert the GPS coordinates into double. From the double form, we would finally convert them in degrees to radians. Then we can ultimately apply the Great Circle Distance formula to find the cabs available in 50km from the user’s position.
Note that due to a large amount of input data, we will be taking the JSON file as input in the program and giving the output in another JSON file as well.
Example
#include <bits/stdc++.h> using namespace std; #define pi 3.14159265358979323 #define earth_radius 6371.0 //defining the user's coordinates #define latitude1d 12.9611159 #define longitude1d 77.6362214 ifstream users ("input.access_file"); ofstream out ("output.access_file"); //converting degree to radian double to_radian(double degree) { return ( degree * (pi/180)); } //to calculate the distance double cal_distance(double latitude2d, double longitude2d) { double lat1, lon1, lat2, lon2, diff_lon, great_circle; lat1 = to_radian(latitude1d); lon1 = to_radian(longitude1d); lat2 = to_radian(latitude2d); lon2 = to_radian(longitude2d); diff_lon = lon2 - lon1; great_circle = acos( sin(lat1) * sin(lat2) + cos(lat1) *cos(lat2) * cos(diff_lon) ); return (earth_radius * great_circle); } //creating structure to access JSON file struct access_file { long long int user_length, i, j, x, y, m, n, f, friends,id[100000]; char latitude_string[1000], longitude_string[1000], id_string[1000], name[1000]; double latitude2d, longitude2d; string line; //to check the value of distance void check_distance() { if (cal_distance(latitude2d, longitude2d) <=50.0000) { id[i] = atoll(id_string); i++; out << "{\"User_id\": " << id[i - 1] << ", \"Name\": " << name << "}" << endl; } } void file_parser() { if (users.is_open()) { while (getline(users, line)) { f = 0; x = 0; y = 0; friends = 0; m = 0, n = 0; user_length = line.size(); for (j = 0; j < user_length; j++) { if (line[j] == '"') f++; else if (line[j] == ':') friends++; if (f == 3) { j++; while (line[j] != '"') { latitude_string[x] = line[j]; x++; j++; } j--; latitude_string[x] = '\0'; } else if (f == 13) { j++; while (line[j] != '"') { longitude_string[y] = line[j]; y++; j++; } j--; longitude_string[y] = '\0'; } if (friends == 2) { j += 2; while (line[j] != ',') { id_string[m] = line[j]; m++; j++; } j--; id_string[m] = '\0'; friends++; } else if (friends == 4) { j += 2; while (line[j] != ',') { name[n] = line[j]; n++; j++; } j--; name[n] = '\0'; friends++; f += 2; } } //converting string to float latitude2d = atof(latitude_string); longitude2d = atof(longitude_string); check_distance(); } } //closing input file users.close(); //closing output file out.close(); } }; int main() { access_file object; object.file_parser(); return 0; }
Output
(A file named output.json will be created at the same place where the code and the input.json file is stored.)
- Related Articles
- What is the formula to find the area of semi-circle?
- Program to find minimum distance to the target element using Python
- C program to find the area of circle and cylinder using structures.
- JAVA Program to Calculate Shortest Distance from Center of the Circle to Chord
- C++ program to implement Inverse Interpolation using Lagrange Formula
- JavaScript program to find area of a circle
- What is the formula of circle perimeter
- C Program for Program to find the area of a circle?
- Java program to find the area of a circle
- Java program to find the circumference of a circle
- Python Program to find the area of a circle
- Program to find Circumference of a Circle in C++
- Java Program to Find the Perimeter of a Circle
- Swift Program to Find the Perimeter of a Circle
- Swift Program to Find the Area of a Circle

Advertisements