Functions and Recursions – Geometry – Problem #2

IIT Kharagpur CS10001 / CS11001 Programming and Data Structures Lab Question Archive

Difficulty: medium

Problem Statement

The task is to generate random points on the plane. For this, write a C program that has the following input/output behavior:  The program asks the user for a positive integer n . Assume that n<400 .  The program asks the user for a seed (a non-negative integer) for random number generation.  The program generates distinct points randomly on the 2D-plane, whose coordinates are integers in [0,20], and stores them in a 1D integer array The first two cells of should contain the (x,y) coordinates of the first point, the second two cells for the second point, and so on. The program starts with the array as empty. Each time it generates a point, it first searches to see if the point already exists in (Write a function to do this.) If the point exists, then it is not inserted in and a new point is generated. If the point does not exist in then it is inserted in It stops inserting points when the number of points in A becomes .  The program prints all the points in Each point is printed as a space-separated pair of integers in a new line (see sample output). To generate the random numbers, use the srand and rand functions defined in stdlib.h. Their prototypes are as follows. void srand (unsigned s); int rand (void); First, you have to set the seed to the user input (say, s) as follows. srand(s); Then call rand()to get a random integer in the interval[0, RAND_MAX ], and assign it to an integer variable, say r: r=rand(); Note: RAND_MAX is a large integer constant whose value need not concern us. Then you can do r = r%21; so as to get r in the interval [0,20].

Back to Lab Problems
Functions and Recursions/Geometry/Problem #2

Lab Problem Statement

Specifications & Problem DetailsIIT Kharagpur PDS Archive
Loading question...

Interactive C Compiler · Problem #2

WASM In-Browser

Runs directly on your machine · Zero remote server dependency

Compiler downloads on first Run (large, saved in your browser after that)
main.cCtrl + Enter to run
1
2
3
4
5
6
7
Input (stdin)given to scanf / cin
Output

Output appears here. Press Run to compile and execute.