Question

In: Computer Science

Write a program in C or in Java, that takes an integer value N from the...

Write a program in C or in Java, that takes an integer value N from the command line, generates N random points in the unit square, and computes the distance separating the closest pair of points.

A unit square is a square with sides of length 1, at points (0, 0), (0, 1), (1, 0), and (1, 1).

If you wish to avoid the command-line processing, you can just assume you will generate a fixed number of points, say between 10 and 50).

Solutions

Expert Solution

C code:

#include <stdio.h>
#include <math.h>
#include <stdlib.h>

int main(void) {
   int n;
   scanf("%d",&n);
   double arr[n][2];
   int i=0;
   while(i<n){
   double x = (rand()%1001)/1000.0;
   double y = (rand()%1001)/1000.0;
   arr[i][0] =x;
   arr[i][1] = y;
   i++;
   }
   double mn = 2.0;
   for(int i=0;i<n;i++){
   for(int j=i+1;j<n;j++){
   double dis = sqrt((arr[i][0]-arr[j][0])*(arr[i][0]-arr[j][0]) + (arr[j][1]-arr[i][1])*(arr[j][1]-arr[i][1]));
   //printf("%f \n",dis);
   if(mn>dis){
   mn = dis;
   }
   }
   }
   printf("Minimum distance between two random points : %f",mn);
   return 0;
}

code screenshot:


Related Solutions

Write a program in Objective C that takes an integer keyed in from the terminal and...
Write a program in Objective C that takes an integer keyed in from the terminal and extracts and displays each digit of the integer in Eglish. So if the user types 647, the program should display the following: six four seven
Write a program that takes an integer N from the command line and uses StdRandom.uniform() to...
Write a program that takes an integer N from the command line and uses StdRandom.uniform() to generate a random sequence of integers be- tween 0 and N – 1. Run experiments to validate the hypothesis that the number of integers generated before the first repeated value is found is ~√?N/2.
Write a program that takes two integer arrays a and b of size n from the...
Write a program that takes two integer arrays a and b of size n from the user, the use a method product to find the product of a and b and return the results after storing them in an array c, then prints the returned results to the screen. (Note: c[i] = a[i] * b[i], for i = 0, ..., n-1) Sample Output: Enter the size of your arrays: 5 Enter the integer values of the first array a: 1...
Write a C program called test that takes one command line argument, an integer N. When...
Write a C program called test that takes one command line argument, an integer N. When we run test: ./test N the program will do this: the parent process forks N child processes each child process prints its process ID, exits the parent process waits for all child processes to exit, then exits
(8%) Write a C/C++ program that takes an input (array) from 1 to n (say n...
(8%) Write a C/C++ program that takes an input (array) from 1 to n (say n = 50) and displays the string representations of those numbers with following conditions If the current number is divisible by 2, then print CSU If the current number is divisible by 5, then print SB If the current number is divisible by both 2 and 5, then print CSUSB If the number is neither divisible by 2 nor 5, then print the number Example:...
Write a Java program that takes an ArrayList<Integer>,  adds k copies of it at the end, and...
Write a Java program that takes an ArrayList<Integer>,  adds k copies of it at the end, and returns the expanded ArrayList.  The total size will be (k+1) * n.   public ArrayList<Integer> makeCopies(ArrayList<Integer>, int k) { } Example: ArrayList<Integer> has (3,7,4) and k = 2, then the returned, expanded ArrayList will have (3,7,4,3,7,4,3,7,4).  The total size is (k+1)*n = (2+1)*3= 9.
Write a program in C or C++ that takes a number series of size n (n...
Write a program in C or C++ that takes a number series of size n (n integers) as input from the user, push all the numbers to the stack, and reverse the stack using recursion. Please note that this is not simply popping and printing the numbers, but the program should manipulate the stack to have the numbers stored in reverse order. In addition to the provided header file, the students can use the following function to print the content...
C++ Write a program that takes a string and integer as input, and outputs a sentence...
C++ Write a program that takes a string and integer as input, and outputs a sentence using those items as below. The program repeats until the input string is "quit". If the input is: apples 5 shoes 2 quit 0 the output is: Eating 5 apples a day keeps your doctor away. Eating 2 shoes a day keeps your doctor away.
Write a java code that: 1) Takes as an argument an integer number, say N 2)...
Write a java code that: 1) Takes as an argument an integer number, say N 2) Creates an array of size N 3) Populates the array with random numbers between 0 and 10 * N. This is, the values of the elements of the array should be random numbers between 0 and 10 * N. 4) Finally, the code outputs the index of the smallest element and the index of the largest element in the array
Write a C++ program that randomly generates N integer numbers (such that N is entered by...
Write a C++ program that randomly generates N integer numbers (such that N is entered by the user) and then stores them to a text file (myNumbers.txt) sorted in increasing (non-decreasing) order. Again, please notice that the size of the data (N) is known during the run time, not the compile-time (needs to be entered by the user after running the program).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT