Trending February 2024 # C++ Program To Find Cabs Nearby Using Great Circle Distance Formula # Suggested March 2024 # Top 6 Popular

You are reading the article C++ Program To Find Cabs Nearby Using Great Circle Distance Formula updated in February 2024 on the website Moimoishop.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested March 2024 C++ Program To Find Cabs Nearby Using Great Circle Distance Formula

using namespace std; #define pi 3.14159265358979323 #define earth_radius 6371.0 #define latitude1d 12.9611159 #define longitude1d 77.6362214 ifstream users (“input.access_file”); ofstream out (“output.access_file”); double to_radian(double degree) {    return ( degree * (pi/180)); } 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); } 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] = ”;                }                else if (f == 13) {                   j++;                   while (line[j] != ‘”‘) {                      longitude_string[y] = line[j];                      y++; j++;                   }                   j–; longitude_string[y] = ”;                }                if (friends == 2) {                   j += 2;                   while (line[j] != ‘,’) {                      id_string[m] = line[j];                      m++; j++;                   }                   j–; id_string[m] = ”;                   friends++;                }                else if (friends == 4) {                   j += 2;                   while (line[j] != ‘,’) {                      name[n] = line[j];                      n++; j++;                   }                   j–; name[n] = ”;                   friends++; f += 2;                }             }                         latitude2d = atof(latitude_string);             longitude2d = atof(longitude_string);             check_distance();          }       }             users.close();             out.close();    } }; int main() {    access_file object;    object.file_parser();    return 0; }

Output (A file named chúng tôi will be created at the same place where the code and the chúng tôi file is stored.)

You're reading C++ Program To Find Cabs Nearby Using Great Circle Distance Formula

C++ Program To Find The Product Of Two Numbers Using Recursion

Recursion is a technique where we call a function from the same function itself. There must be some base or terminating condition to end the recursive call. Recursive procedures are very much helpful to perform complex iterative solution with fewer codes and easier solution approach through sub-operation finding.

In this article we shall discuss a recursive approach to perform product (multiplication) between two numbers in C++. Initially we will understand the basic principle, the syntax for recursive function call, the algorithm and the source code.

Multiplication using Recursion

In high-level languages, there is the multiplication operators available to directly perform multiplication. However we know that the multiplication is actually a repetitive addition. So the result of A * B is repetitive addition of A, B number of times, or we can say repetitive addition of B, A number of times. Whenever there is a repetition, we can do this using recursion. Let us see the recursive function definition syntax first.

Syntax

if ( base condition ) { terminate recursive call } recursive function call: function_name ( updated parameter list ) }

define a function multiply() which takes two numbers A and B

if A < B, then

return multiply( B, A )

otherwise when B is not 0, then

return A + multiply( A, B – 1 )

otherwise

return 0

end if

end of function definition

Read two inputs A and B

res = multiply( A, B )

display res

Example

using

namespace

std

;

int

multiply

(

int

A

,

int

B

)

{

if

(

A

<

B

)

{

return

multiply

(

B

,

A

)

;

}

else

if

(

B

!=

0

)

{

return

A

+

multiply

(

A

,

B

1

)

;

}

else

{

return

0

;

}

}

int

main

(

)

{

cout

<<

“Multiplication of 5, 7 is: “

<<

multiply

(

5

,

7

)

<<

endl

;

cout

<<

“Multiplication of 8, 0 is: “

<<

multiply

(

8

,

0

)

<<

endl

;

cout

<<

“Multiplication of 25, 3 is: “

<<

multiply

(

25

,

3

)

<<

endl

;

cout

<<

“Multiplication of 9, 1 is: “

<<

multiply

(

9

,

1

)

<<

endl

;

}

Output Multiplication of 5, 7 is: 35 Multiplication of 8, 0 is: 0 Multiplication of 25, 3 is: 75 Multiplication of 9, 1 is: 9

See, in this program, the function is taking parameters A and B both are two integer numbers. Now after each step it reduces the second parameter B by 1 and adding A with A itself. Like this the function is performing multiplication procedure.

Conclusion

Recursion is a process to call the same function from the function itself. While calling a function recursively, we update or change the parameter set a little so that the same effect is not coming again and again, then it divides the problem into smaller subproblems and solve the problem by solving these smaller problems in a bottom-up manner. Almost anything which can be implemented using loops, can also be implemented using recursion. In this article we have seen simple process to multiply two integers using recursion. The integers are added multiple times to get the final multiplication result.

Java Program To Find The Sum Of Natural Numbers Using Recursion

In this article, we will understand how to find the sum of natural numbers using recursion. All possible positive numbers from 1 to infinity are called natural numbers. A recursive function is a function that calls itself multiple times until a particular condition is satisfied.

Recursion is the process of repeating items in a self-similar way. In programming languages, if a program allows you to call a function inside the same function, then it is called a recursive call of the function.

Many programming languages implement recursion by means of stacks. Generally, whenever a function (caller) calls another function (callee) or itself as callee, the caller function transfers execution control to the callee. This transfer process may also involve some data to be passed from the caller to the callee.

Below is a demonstration of the same −

Input

Suppose our input is −

Enter the number : 25

Output

The desired output would be −

The sum of natural numbers up to 25 is 325 Algorithm Step 1 - START Step 2 - Declare 2 integer values namely my_input and my_sum. Step 3 - Read the required values from the user/ define the values Step 4 - A recursive function ‘Add’ is defined which takes an integer as input and returns the sum of the input value and its previous value until the input value is reduced to 0. Step 5 - The recursive function is called and the value ‘my_input’ is passed to it. Store the return value. Step 6 - Display the result Step 7 - Stop Example 1

Here, the input is being entered by the user based on a prompt. You can try this example live in ourcoding ground tool .

import java.util.Scanner; public class NaturalNumbers {    public static void main(String[] args) {       int my_input, my_sum;       System.out.println("Required packages have been imported");       Scanner my_scanner = new Scanner(System.in);       System.out.println("A reader object has been defined ");       System.out.print("Enter the number : ");       my_input = my_scanner.nextInt();       System.out.println("The number is defined as " +my_input);       my_sum = Add(my_input);       System.out.println("The sum of natural numbers up to " + my_input + " is " + my_sum);    }    public static int Add(int my_input) {          return my_input + Add(my_input - 1);       else          return my_input;    } } Output Required packages have been imported A reader object has been defined Enter the number : 25 The number is defined as 25 The sum of natural numbers up to 25 is 325 Example 2

Here, the integer has been previously defined, and its value is accessed and displayed on the console.

public class NaturalNumbers {    public static void main(String[] args) {       int my_input, my_sum;       my_input = 25;       System.out.println("The number is defined as " +my_input);       my_sum = Add(my_input);       System.out.println("The sum of natural numbers up to " + my_input + " is " + my_sum);    }    public static int Add(int my_input) {       return my_input + Add(my_input - 1);    else       return my_input;    } } Output The number is defined as 25 The sum of natural numbers up to 25 is 325

C++ Program To Implement The Solovay

Solovay-Strassen Primality Test is used to test a number whether it is a composite or possibly prime number.

Algorithms Begin    Declare a function modulo to the long datatype to perform binary calculation.       Declare m_base, m_exp, m_mod of long datatype and pass them as a parameter.       Declare two variables a, b of long datatype.          Initialize a = 1, b = m_base.          if (m_exp % 2 == 1) then             a = (a * b) % m_mod.          b = (b * b) % m_mod.          m_exp = m_exp / 2.       Return a % m_mod. End Begin    Declare a function Jecobian of the int datatype to calculate Jacobian symbol of a given number.    Declare CJ_a, CJ_n of the long datatype and pass them as a parameter.    if (!CJ_a) then       return 0.    Declare answer of the integer datatype.       Initialize answer = 1.    if (CJ_a < 0) then       CJ_a = -CJ_a.       if (CJ_n % 4 == 3) then          answer = -answer.    if (CJ_a == 1) then       return answer.    while (CJ_a) do       if (CJ_a < 0) then          CJ_a = -CJ_a.          if (CJ_n % 4 == 3) then          answer = -answer.    while (CJ_a % 2 == 0) do       CJ_a = CJ_a / 2;          answer = -answer.    swap(CJ_a, CJ_n)    if (CJ_a % 4 == 3 && CJ_n % 4 == 3) then       answer = -answer.          CJ_a = CJ_a % CJ_n;       CJ_a = CJ_a - CJ_n.    if (CJ_n == 1) then       return answer. End Begin    Declare a function Solovoystrassen to the Boolean datatype to perform the Solovay-Strassen Primality Test.       Declare SS_p to the long datatype and pass as a parameter.       Declare itr to the integer datatype and pass them as a parameter.       if (SS_p < 2) then          return false.       if (SS_p != 2 && SS_p % 2 == 0) then          return false.       for (int i = 0; i < itr; i++)          long a = rand() % (SS_p - 1) + 1;          long jacob = (SS_p + Jacobian(a, SS_p)) % SS_p;          long mod = modulo(a, (SS_p - 1) / 2, SS_p);          return false       return true. End Begin    Declare iter of the integer datatype.       Initialize iter = 50.    Declare num1, num2 of the long datatype.    Print “Enter the first number:”    Input the value of num1.    if (Solovoystrassen(num1, iter)) then       print the value of num1 and ”is a prime number”.    Else       print the value of num1 and ”is a composite number”.    Print “Enter another number:”       Input the value of num2.    if (Solovoystrassen(num2, iter)) then       print the value of num2 and ”is a prime number”.    Else       print the value of num2 and ”is a composite number”. using namespace std; long modulo(long m_base, long m_exp, long m_mod) function to perform binary calculation {    long a = 1;    long b = m_base;       if (m_exp % 2 == 1)          a = (a * b) % m_mod;       b = (b * b) % m_mod;       m_exp = m_exp / 2;    }    return a % m_mod; } int Jacobian(long CJ_a, long CJ_n) {    if (!CJ_a)       return 0;// (0/n) = 0    int answer = 1;    if (CJ_a < 0) {       CJ_a = -CJ_a;       if (CJ_n % 4 == 3)          answer = -answer;    }    if (CJ_a == 1)       return answer;    while (CJ_a)  {       if (CJ_a < 0) {          CJ_a = -CJ_a;          if (CJ_n % 4 == 3)             answer = -answer;       }       while (CJ_a % 2 == 0) {          CJ_a = CJ_a / 2;             answer = -answer;       }       swap(CJ_a, CJ_n);       if (CJ_a % 4 == 3 && CJ_n % 4 == 3)          answer = -answer;       CJ_a = CJ_a % CJ_n;             CJ_a = CJ_a - CJ_n;    }    if (CJ_n == 1)       return answer;    return 0; } bool Solovoystrassen(long SS_p, int itr) {    if (SS_p < 2)       return false;    if (SS_p != 2 && SS_p % 2 == 0)       return false;    for (int i = 0; i < itr; i++) {             long a = rand() % (SS_p - 1) + 1;       long jacob = (SS_p + Jacobian(a, SS_p)) % SS_p;       long mod = modulo(a, (SS_p - 1) / 2, SS_p);          return false;    }    return true; } int main() {    int iter = 50;    long num1;    long num2;    cout<< "Enter the first number: ";    cout<<endl;    if (Solovoystrassen(num1, iter))       cout<<num1<<" is a prime numbern"<<endl;    else       cout<<num1<<" is a composite numbern"<<endl;       cout<<"Enter another number: ";    cout<<endl;    if (Solovoystrassen(num2, iter))       cout<<num2<<" is a prime numbern"<<endl;    else       cout<<num2<<" is a composite numbern"<<endl;    return 0; } Output Enter the first number: 24 24 is a composite number Enter another number: 23 23 is a prime number

C Program To Insert A Node At Any Position Using Double Linked List

struct node {    int num;    struct node * preptr;    struct node * nextptr; }*stnode, *ennode; void DlListcreation(int n); void DlLinsertNodeAtBeginning(int num); void DlLinsertNodeAtEnd(int num); void DlLinsertNodeAtAny(int num, int pos); void displayDlList(int a); int main(){    int n,num1,a,insPlc;    stnode = NULL;    ennode = NULL;    printf(” Input the number of nodes : “);    scanf(“%d”, &n);    DlListcreation(n);    a=1;    displayDlList(a);    printf(” Input the position ( 1 to %d ) to insert a new node : “,n+1);    scanf(“%d”, &insPlc);    printf(” Input data for the position %d : “, insPlc);    scanf(“%d”, &num1);    DlLinsertNodeAtAny(num1,insPlc);    a=2;    displayDlList(a);    return 0; } void DlListcreation(int n){    int i, num;    struct node *fnNode;       stnode = (struct node *)malloc(sizeof(struct node));       if(stnode != NULL){          printf(” Input data for node 1 : “);          scanf(“%d”, &num);          ennode = stnode;          for(i=2; i<=n; i++){             fnNode = (struct node *)malloc(sizeof(struct node));             if(fnNode != NULL){                printf(” Input data for node %d : “, i);                scanf(“%d”, &num);                ennode = fnNode;             }             else{                printf(” Memory can not be allocated.”);                break;             }          }       }       else{          printf(” Memory can not be allocated.”);       }      } } void DlLinsertNodeAtAny(int num, int pos){    int i;    struct node * newnode, *tmp;    if(ennode == NULL){    }    else{       tmp = stnode;       i=1;       while(i<pos-1 && tmp!=NULL){          i++;       }       if(pos == 1){          DlLinsertNodeAtBeginning(num);       }       else if(tmp == ennode){          DlLinsertNodeAtEnd(num);       }       else if(tmp!=NULL){          newnode = (struct node *)malloc(sizeof(struct node));          }       }       else{       }    } } void DlLinsertNodeAtBeginning(int num){    struct node * newnode;    if(stnode == NULL){    }    else{       newnode = (struct node *)malloc(sizeof(struct node));       stnode = newnode;    } } void DlLinsertNodeAtEnd(int num){    struct node * newnode;    if(ennode == NULL){    }    else{       newnode = (struct node *)malloc(sizeof(struct node));       ennode = newnode;    } } void displayDlList(int m){    struct node * tmp;    int n = 1;    if(stnode == NULL) {       printf(” No data found in the List yet.”);    }    else{       tmp = stnode;       if (m==1) {       }       else{       }       while(tmp != NULL){          n++;       }    } }

Output

When the above program is executed, it produces the following result −

Doubly Linked List : Insert node at any position: ----------------------------------------------------------------------------------- Input the number of nodes : 5 Input data for node 1 : 23 Input data for node 2 : 12 Input data for node 3 : 11 Input data for node 4 : 34 Input data for node 5 : 10 Data entered in the list are : node 1 : 23 node 2 : 12 node 3 : 11 node 4 : 34 node 5 : 10 Input the position ( 1 to 6 ) to insert a new node : 5 Input data for the position 5 : 78 After insertion the new list are : node 1 : 23 node 2 : 12 node 3 : 11 node 4 : 34 node 5 : 78 node 6 : 10

C# Program To Delete Empty And Nonempty Directory

Introduction

On the computer, we can store files in a directory, also known as a folder. A directory also contains shortcuts to other directories and files. Here, we will discuss the C# Program to Delete Empty and Non-empty Directories. A static class called Directory offers static methods for dealing with directories. A DirectoryInfo object offers details on a particular directory.

Delete Empty and Non-empty Directory

Now that we have a directory (either empty or not), we must delete it. An empty directory indicates that there are no files or subdirectories present in the directory. A directory can be thought of as a grouping of files and subdirectories; it may or may not contain data. A directory that contains files or other directories is referred to as non-empty. Using the Delete() method of the Directory class, we can remove the directory. There are two ways in which this method is overloaded −

Delete(String)

Delete(String,Boolean)

Delete(String)

This method is contained in the Directory Class. This technique is used to delete an empty directory. This method deletes the directory from the defined address or location.

public static void Delete (string DirLocation);

Where DirLocation is the address of the directory that has been provided and that we want to delete, and this parameter’s type is a string. Now, there can be some errors that can happen after the execution of this command.

When a file with the same name and address specified by DirLocation already exists, an IO Exception is thrown. Alternatively, the subfolder is read-only. An unauthorized Access Exception is another type of error that can happen. If the caller does not possess the required authorization, this exception will be thrown. Argument Null Exception happens when the DirLocation is empty, this error will be thrown. Path Too Long Exception is another error that can happen when the given DirLocation, file name, or both exceed the system-defined maximum length, this exception will arise. The Directory Not Found Exception will appear if the DirLocation is missing or cannot be located. or the indicated route is erroneous.

Algorithm

The algorithm below will give a step by step procedure to write a program which deletes the directory. In this algorithm we will be using Delete(String) method.

For example, we are going to consider an empty directory named “csk”. Now, we are going to use Delete(String) method and will delete the “csk” directory.

Step 1 − We’ll be using the Directory.delete method for deleting the directory by passing the directory address.

Step 2 − By using Console.Writeline() we write that the deletion of the directory is completed.

Example using System; using System.IO; class TutPoint { static void Main(){ Directory.Delete("D:/csk"); Console.WriteLine("Deleted"); } } Output Deleted Delete(String, Boolean)

This method is also contained in the Directory Class. The specified directory, as well as, if specified, any subdirectories and files within the directory, are deleted using this technique.

public static void Delete (string DirLocation, bool recursive);

This technique is used to remove the specified directory as well as any subdirectories and files contained within it. Now, there can be some errors that can happen after the execution of this command.

When a file with the same name and address specified by DirLocation already exists, an IO Exception is thrown. Alternatively, the subfolder is read-only. An unauthorized Access Exception is another type of error that can happen. If the caller does not possess the required authorization, this exception will be thrown. Argument Null Exception happens when the DirLocation is empty, this error will be thrown.

Path Too Long Exception is another error that can happen when the given DirLocation, file name, or both of them surpass the maximum length that is set in the system then this exception will arise. The Directory Not Found Exception will appear if the DirLocation is missing or cannot be located. or the indicated route is erroneous.

Algorithm

The algorithm below will give a step by step procedure to write a program which deletes the directory. In this algorithm we will be using Delete(String, Boolean) method.

For example, we are going to consider a non empty directory named “csk” with a file named “msd” in the D drive. Now, we are going to use Delete(String, Boolean) method and will delete the “csk” directory.

Step 1 − We’ll be using the Directory.delete(String, Boolean) method for deleting the directory by passing the directory address.

Step 2 − Here true is the boolean being passed as it checks for subdirectories’ existence.

Step 3 − By using Console.Writeline() we write that the deletion of the directory is completed.

Example using System; using System.IO; class TutPoint { static void Main() { Directory.Delete("D:/csk",true); Console.WriteLine("Deleted"); } } Output Deleted Time Complexity

In the algorithm we are using a single function of the Directory class. Here, the time complexity for Delete(String) will be O(1) as well as for Delete(String, Boolean) will be O(1) too.

Conclusion

We have extensively discussed the C# Program to Delete Empty and Non-empty Directories. Firstly, we talked about the directory definition and its use. Then we talked about deleting the directory in two different ways. Finally, we saw the algorithm and the example code. We hope that this article has helped you enhance your knowledge regarding C#.

Update the detailed information about C++ Program To Find Cabs Nearby Using Great Circle Distance Formula on the Moimoishop.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!