≡ Menu

C Program Example to Generate Cluster of Dots

In this tutorial, we would write an example C program that will create a file which will serve as producer of testing data.

In another words, we will create an application in C, and your task is to input the number of clusters, number of dots for each of those clusters, and then to generate random numbers that are in those clusters.

The output file generated by this program can then be used by other programs as sample data.

A while back, I tried to write a simple weather prediction program, but I had no real life data to apply my algorithm and try out my application. Sometimes, it is very hard to generate the data that are required to test your application. That was the motivation for me to write some simple program like this, which will give you some idea on writing your own program to generate test data.

Problem Definition

In this program, you will be required to input the number of two dimensional dots. Those dots would have two double numbers as coordinates. Those dots would serve as centers of each cluster. Each cluster would have bubble that goes around each of the centers, in a way like you have the circle that surrounds the center of each cluster. In our solution we will allow clusters to overlap.

You could also modify this problem definition and improve on this code, which can do the following:

  • The input can be of multiple dimension (instead of the two dimension dots that we are using in our example)
  • You can also have an option to decide whether the dot would serve as a center of each cluster or not.
  • Modify the program in such a way that the clusters don’t overlap.

High Level Code Explanation

On a high-level our program should do the following:

  • Declare the variables etc.
  • Get the input from the user for the number of dots we are using
  • Get input for each of the centers and number of dots around each of the centers
  • Finally, generate required number of dots and write them into the file.

In the example C program given below, we’ve done the following:

  • First, we’ve defined few macros, that serve later in program and there is file name that could be changed very easy. Macros are very helpful, and explained very well in this book along with all important C features: C in a Nutshell by Peter Prinz and Tony Crawford.
  • Next we have the declaration of data type PC. This will store parameters that describe any center. If you would like to upgrade this to world of C++, there is a good candidate for a class. Then we have one function that will be used to input the values we need.
  • The x and y are used to input values for any of centers. Next three variables are used for generating random numbers.
  • The variable n is used to calculate number of dots in each of the clusters, and variable r sis used to keep value for bubble that goes around the center.
  • Variable i, j and k are used inside the loops as counters.
  • If you are new to Pointers in C program, this will help: C Pointers Fundamentals Explained with Examples

C Program Example

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

#define      THE_PROGRAM_IS_RUNNING   "***THE PROGRAM IS GENERATING OUTPUT***\n\n"
#define      MESSAGE_SUCCESS      "***THE PROGRAM IS GENERATING RESULTS***\n\n"


typedef struct
{
  double x;
  double y;
  int    r;
  int    n;
}PC;


void inputValuesForCenters( int, PC* );


int
main()
{
double  x, y, dNumberBefore, dNumberAfter, dNumber;

int n, iCounter = 0, i, r, j, k = 0,
    iNumberOfCenters, iHowManyDots=0;
int numberX, numberY, signX, signY;

FILE* fpOut = fopen("output.txt", "w");

PC*   dots, ptr;

printf( THE_PROGRAM_IS_RUNNING);
printf( "How many dots for the ceneters->" );
scanf("%d",&iNumberOfCenters);

dots = (PC *)malloc(iNumberOfCenters * sizeof(PC));


inputValuesForCenters(iNumberOfCenters,  dots);


printf(MESSAGE_SUCCESS);


fprintf(fpOut, "%d \n", iNumberOfCenters);

for(;;)
{
j=0;

   while( j < dots[iCounter].n )
   {
     numberX = (rand() % 2);
     signX = ( numberX == 0 )? 1: -1; 
     dNumberBefore = (rand()%dots[iCounter].r);
     dNumberAfter  = ((double)rand()/10000.0);
     dNumberAfter -= (int)dNumberAfter;
     dNumber       = signX*((double) dNumberBefore + dNumberAfter) + dots[iCounter].x;
     fprintf( fpOut, "%lf \n", dNumber);

    numberY = (rand() % 2);
    signY=( numberY == 0 )? 1: -1;
    dNumberBefore = (rand()%dots[iCounter].r);
    dNumberAfter  = ((double) rand()/10000.0);
    dNumberAfter -= (int)dNumberAfter;
    dNumber       = signY*((double)dNumberBefore + dNumberAfter) + dots[iCounter].y;
    fprintf(fpOut, "%lf \n",dNumber);
    j++; 
   }
   iCounter++; if( iCounter >= iNumberOfCenters ) { break; }
}


free (dots);

fclose(fpOut);

return 0;
}


void
inputValuesForCenters( int m, PC* dots)
{
double x, y;
int r;
int i;
int n;

for( i = 0; i < m; i++)
{
printf("\n input coordianates of the center x and y->"); scanf("%lf %lf",&x,&y);
printf("\n");

printf("\n What is the r->"); scanf("%d",&r); printf("\n");

printf("\n How many dots in the cluster->"); scanf("%d",&n); printf("\n");

dots[i].x = x; dots[i].y = y;
dots[i].r = r;
dots[i].n = n;
}

}

In our above program:

  • We will input the number of dots, used in our generator of random clusters.
  • Next, input all required values for each of the clusters. i.e we need: two values for center, number of dots in each of the clusters, and the circumference of bubble. Probably you might not like whole number for last variable, but it could be adjusted.
  • Then, we will open the file where we will place our dots. We’ll display a status message at this stage.
  • Next, we need to generate enough dots for each of our clusters. For that we will use rand function, but it will generate ints, so we would need to juggle for double numbers. First part will be used for whole part, and next part is used for part after the comma.

Enhancing the Example Code

You can enhance the above C program to do the following:

  • Instead of having dots around one point inside a circle, try to make dots to go around line.
  • Try to create two or more parallel lines.
  • Try to put dots outside the circle.
  • Place the dots outside one square.
  • Use some other type of curve in your program.
Add your comment

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

Comments on this entry are closed.

  • JJ October 5, 2016, 2:05 am

    Unfortunately your code seems to be full of errors. I tried to compile it under Ubuntu 12.04 but it fails.

    Some of the errors I could fix like missing semi-colons but function parameters are also wrong.

  • Bhagyaraj October 5, 2016, 10:55 pm

    It’s working, no issues

  • DuskoKoscica October 7, 2016, 4:19 am

    It takes more anticipation, from programmer.

    You have nice link that will help you resolve the issue you might have, and there is recommendation for a good book as well.

    After you figure out this, there will be no issues when you try to use pointers in C, and later in C++.

    Now I will look for some music: Frente, Fresno and Lana Del Rey…

    Have nice day…