Multiple Bouncing Balls Program in C

Multiple Bouncing Balls Program in C
0

#1

I am trying to make a program in C with multiple bouncing balls. I know it is a very known program and I have already searched the internet and seen different versions of it. However, because I am new in programming, I didn’t manage to understand them and answer my questions. The problem is that I have created a code, but my program doesn’t run properly. To be more exact, I think the part where it reads the data is fine, but then there is a problem in the code of the graphics.h library. I cannot understand where the problem is, as it is the first time I use this library. Please help me and be as lenient as possible.

Thank you in advamce.

Here is my code:

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

int main()
{
    FILE *input;
    int n, *num, i, total, j;
    if((input=fopen("data.txt", "r"))!=NULL)
    {
        fscanf(input, "%d", &n);
        total=n*7;
        num=(int *) malloc(total*sizeof(int));
        if(num!=NULL)
        {
           for(i=0; i<total; i++)
           {
               fscanf(input, "%d", &num[i]);
           }
           fclose(input);
           clock_t start,finish, previous;
           double step;
           int gdriver = DETECT, gmode, errorcode;
           initgraph(&gdriver, &gmode, "");
           errorcode = graphresult();
           if (errorcode != grOk) 
           { 
               printf("Graphics error: %s\n", grapherrormsg(errorcode));
               system ("pause");
               exit(1);               
           }
           start=clock();
           previous=start;
           j=0;               
           do
           {
               for(i=0; i<=n; i++)
               {
                   finish = clock();
                   step = (finish-previous)*1.0/CLOCKS_PER_SEC;
                   if (step >= 0.03)
                   {
                    previous = finish;
                    setfillstyle(SOLID_FILL,BLACK);
                    setcolor(BLACK);
                    fillellipse(num[j],num[j+1],num[j+4],num[j+4]);
                    num[j]+= num[j+5]*step;
                    num[j+1]+= num[j+6]*step;
                    if (num[j]+num[j+4]>=getmaxx() || num[j]-num[j+4]<=0)
                    num[j+5] *= -1;
                    if (num[j+1]+num[j+4]>=getmaxy() || num[j+1]-num[j+4]<=0)
                    num[j+6] *= -1;
                    setfillstyle(SOLID_FILL,RED);
                    setcolor(RED);
                    fillellipse(num[j],num[j+1],num[j+4],num[j+4]);
                   }
               }
         }
     while (!kbhit());
     closegraph();
    }
   } 
  }
    else
    {
        printf("Could not allocate memory\n\n");
    }
}
else
{
    printf("Could not open file\n\n");
}
system("pause");
return 0;
}```

#2

since you’re suspicious of the graphics.h library - i would start by isolating that and just to see if it is working or not.


#3

Yes, that is what I did and I believe that there is a problem in that. However, I cannot find what and where it is.


#4

so it didnt work when you isolated it? perhaps an update - could check the syntax or methods you’re using and make sure theyre correct - i dont know much about C# so im just giving my thoughts here


#5

No, it didn’t. I don’t know what it might be. I really appreciate your help. Thank you.


#6

a quick check on msdn https://msdn.microsoft.com/en-us/library/system.drawing.graphics(v=vs.110).aspx
shows FillEllipse() with caps - perhaps something like that - not sure if caps matter there


#7

Nope. Same problem. I think that it is probably something which has to do with the functions of the program and not the syntax. I say that because I use CodeBlocks and it doesn’t show me any errors at the debugging. It just starts the program and then it crushes.