473,569 Members | 2,880 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How can I make a multiple bouncing balls program in C?

2 New Member
Hello,

I am trying to make a program in C with multiple bouncing balls. 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.

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*si zeof(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(&gdri ver, &gmode, "");
errorcode = graphresult();
if (errorcode != grOk)
{
printf("Graphic s error: %s\n", grapherrormsg(e rrorcode));
system ("pause");
exit(1);
}
start=clock();
previous=start;
do
{
j=0;
for(i=0; i<=n; i++)
{
finish = clock();
step = (finish-previous)*1.0/CLOCKS_PER_SEC;
if (step >= 0.03)
{
previous = finish;
setfillstyle(SO LID_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(SO LID_FILL,RED);
setcolor(RED);
fillellipse(num[j],num[j+1],num[j+4],num[j+4]);
j=j+7;
}
}
}
while (!kbhit());
closegraph();
}
else
{
printf("Could not allocate memory\n\n");
}
}
else
{
printf("Could not open file\n\n");
}


system("pause") ;
return 0;
}
Jan 19 '18 #1
3 2379
weaknessforcats
9,208 Recognized Expert Moderator Expert
Debugging is a necessary part of coding.

In this case I would execute the program step-by-step using your debugger. This will let you see ofter each line of code what the contents of your variables is.

If you have not used your debugger, this is an excellent opportunity to learn how.

Let me know what happens.
Jan 19 '18 #2
jackwld
2 New Member
Well, I used it and it showed me this:

https://mega.nz/#!NRwEAJKR!KI5cTeFhA...RYEgUkF4lc8Atc
Jan 20 '18 #3
weaknessforcats
9,208 Recognized Expert Moderator Expert
I am not sure what that's about. MEGA is a New Zealand based cloud service. I have no idea why it's part of a debugger service.

What compiler are you using?
Jan 21 '18 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
4594
by: bdj | last post by:
Hello! Can anyone tell me where to read more about best practices about this: Should I put data in a seperate scheme for tables, packages in anoter schema and create a lot of users that have access to (some) packages. When the tables are in anoter schema than the packages, where should the table-API (generated from Designer) be?
2
2466
by: steve | last post by:
I'm trying to create a PHP script to send mail to a list of addresses. I'm sending mail okay, because the messages arrive in an account that I can check, but messages to a known bad address aren't being bounced back to the sending address. Here's the line of code I'm using: $mailok = mail("me@goodaddress.com,me@badaddress.com","this is a...
5
1811
by: Jozef | last post by:
Does anyone know if there is some sort of catch all installation program that will determine if dependencies were installed properly? I get the feeling that the Windows boys like to throw curve balls to the Access boys over there at Microsoft (or the Access boys just like shooting themselves in the foot by not properly developing/testing). ...
3
1336
by: OJ | last post by:
It's easy to control one ball (a graphic) to move and bounce inside a square. But I don't know how to create and control a large number of balls (10 or 100) and count how many times they hit a wall of the square. Anyone can help?? I have little experience in VB.net. Thanks in advance. OJ
1
2114
by: clickncook | last post by:
I'm currently working on a website for recipes that allows users to search by ingredients they have and I'm experiencing a problem with my filtering. My site allows users to just check the checkboxes of ingredients they have. I've been in and out of every forum I can find on the internet any solution and this forum has been pretty helpful! I'm...
2
1763
by: SilentCodingOne | last post by:
I have a application where each time you click the add ball button a new randomly colored ball is added to the jpanel. It works fine except that the balls suddenly stop at the same spot. I want them to continue to bounce for a longer period of time instead of like 8 bounces. It was going around withoutstopping prior to my getting the add new ball...
5
1407
by: gobblegob | last post by:
I would like to know some code to check if my program is running, if so then not load. Thanks in advanced, Gobble.
4
2814
by: iriggi | last post by:
I need to draw red balls that bounce from left to right and a ball which rotates (always the same uniform mouvement, a little circle in the middle). the illusion is that all the balls are bouncing, but it is not true. The problem is that I don't know how to made a python program who shows that.
1
1525
by: Goldy T Joseph | last post by:
#include<iostream.h> #include<conio.h> class doctor { char name,dp,speci; public: void read(); void print(); }; class patient
0
7619
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8138
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7681
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7983
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5228
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3662
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2118
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1229
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.