# How to write a program that simulates bouncing in C?

• (2 Pages)
• 1
• 2

## 28 Replies - 1870 Views - Last Post: 07 October 2013 - 05:38 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=330383&amp;s=cb4e85c4627d228b734d09ccd77ae056&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Stephenfrazier10

Reputation: 2
• Posts: 109
• Joined: 12-September 12

# How to write a program that simulates bouncing in C?

Posted 30 September 2013 - 09:17 AM

This is in C only, not C++

I am trying to teach myself C, and this problem is really driving me crazy. I have to write a program that will simulate dropping a ball and seeing where it bounces. The book says to bounce it on the number 0, and allow it to either bounce 1 number to the left or one number to the right ten times, with the numbers spanning -10 through 10.

I am completely clueless on how to begin coding this, Im guessing I would have to use a random number generator from -1 to 1 to get it to move either left or right?

Can someone explain to me how to start this program?

Is This A Good Question/Topic? 0

## Replies To: How to write a program that simulates bouncing in C?

### #2 Salem_c

• void main'ers are DOOMED

Reputation: 2030
• Posts: 3,997
• Joined: 30-May 10

## Re: How to write a program that simulates bouncing in C?

Posted 30 September 2013 - 09:27 AM

Or you could generate a random number between 0 and 20, then subtract 10 from it.

### #3 vividexstance

• D.I.C Lover

Reputation: 783
• Posts: 2,767
• Joined: 31-December 10

## Re: How to write a program that simulates bouncing in C?

Posted 30 September 2013 - 09:28 AM

Have you tried anything? If so, post what you've tried.

Sounds to me like you will need to use some kind of a loop, and a random number generator. What is it that you don't understand?

Here is a simple example of using a random number generator. First you must seed the random number generator using the srand() function. Then you use the rand() function to generate a number.

The trick is to use the modulus operator to define the range that the random numbers can be:
```// randomNumber.c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(void)
{
int number;
char ans = 'y';

// Seed the random number generator:
srand(time(NULL));

// Loop until the user enters 'n' or 'N' to not continue.
while(ans == 'y' || ans == 'Y')
{
// This will generate a number between 0-99:
number = rand() % 100;

// Display the number:
printf("number = %d\n", number);
fflush(NULL);

// Ask the user if they want to try again.
printf("\nTry Again? (y/n): ");
scanf(" %c", &ans);
}

return 0;
}

```

Let me know if there is anything in the code you don't understand.

This post has been edited by vividexstance: 30 September 2013 - 09:28 AM

### #4 Stephenfrazier10

Reputation: 2
• Posts: 109
• Joined: 12-September 12

## Re: How to write a program that simulates bouncing in C?

Posted 30 September 2013 - 09:34 AM

The entire program is supposed to ask the user how many balls to drop, make sure that number is at least one. And then simulate dropping the balls, and allowing them to "bounce" ten times each, and then keep track in an array how many balls landed in each array.

So I was thinking to create a function that starts a number at 10, since it would be the middle of 21, and then somehow (this is the part I cannot figure out) simulate the ball bouncing either one spot to the right or one spot to the left and increment the number of balls in that spot

This is all of the code I have right now...

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

int drop_balls(int); /* Prototype for drop_Balls, int parameter is number of balls being dropped */

int get_num_balls(); /* Prototype for the function that gets the number of balls that need to be dropped by the user */

short main()
{

}

int get_num_balls()
{
printf("How many balls should be dropped? ");
scanf("%d", num_balls);
while(num_balls <= 0)
{
printf("You have to drop at least one ball! \n ")
printf("How many balls should be dropped? ");
scanf("%d", num_balls);
}
return num_balls; /* Return the number of balls that will be dropped */
}

int drop_balls(num_balls)
{
int starting_point = 10;
int ball-count[21]; /* Keeps track of how many balls landed where */
}

}

```

This post has been edited by Stephenfrazier10: 30 September 2013 - 09:34 AM

### #5 vividexstance

• D.I.C Lover

Reputation: 783
• Posts: 2,767
• Joined: 31-December 10

## Re: How to write a program that simulates bouncing in C?

Posted 30 September 2013 - 09:35 AM

The main() function returns an int not a short.

### #6 Stephenfrazier10

Reputation: 2
• Posts: 109
• Joined: 12-September 12

## Re: How to write a program that simulates bouncing in C?

Posted 30 September 2013 - 09:38 AM

My stupidity, the book said to have a short main by implementing other functions, not an actual short

### #7 Stephenfrazier10

Reputation: 2
• Posts: 109
• Joined: 12-September 12

## Re: How to write a program that simulates bouncing in C?

Posted 30 September 2013 - 09:59 AM

Still cannot get it working, but this is where Ive gotten so far.

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

int drop_balls(int); /* Prototype for drop_Balls, int parameter is number of balls being dropped */

int get_num_balls(); /* Gets the number of balls that need to be dropped by the user */

int main()
{
get_num_balls();
drop_balls(num_balls);
}

int get_num_balls()
{
printf("How many balls should be dropped? ");
scanf("%d", num_balls);
while(num_balls <= 0)
{
printf("You have to drop at least one ball! \n ")
printf("How many balls should be dropped? ");
scanf("%d", num_balls);
}
return num_balls; /* Return the number of balls that will be dropped */
}

int drop_balls(num_balls)
{
int ball_count[21]; /* Keeps track of how many balls landed where */
int ball_num = 0; /* What number ball are we on */
srand(time(NULL)); /* Seed the generator */
for(ball_num; ball_num < num_balls; ball_num++ ) /* Do the correct # of balls */
{
int starting_point = 10; /* Start at 10 since its the middle of 21 */
for(starting_point; starting_point >=0; starting_point--) /* Bounce each ball 10 times */
{
int number;
number = rand() % 100;
if(number > 50)
{
starting_point++;
}
else
{
starting_point--;
}
}
ball_count[starting_point] += "o"

}
}

}

```

### #8 vividexstance

• D.I.C Lover

Reputation: 783
• Posts: 2,767
• Joined: 31-December 10

## Re: How to write a program that simulates bouncing in C?

Posted 30 September 2013 - 10:15 AM

You must seed the random number generator only once, and typically it's done at the beginning of main().

*EDIT*:
Where is num_balls declared/defined?
You need to save the number returned by the get_num_balls() function and pass it to the drop_balls() function.

*EDIT2*:
Another way to do it, would be to pass the function call result of get_num_balls() to the drop_balls() function:
```drop_balls(get_num_balls());

```

This post has been edited by vividexstance: 30 September 2013 - 10:18 AM

### #9 Stephenfrazier10

Reputation: 2
• Posts: 109
• Joined: 12-September 12

## Re: How to write a program that simulates bouncing in C?

Posted 30 September 2013 - 10:22 AM

I took the 2nd way to do it. And I did only seed it once, right?

### #10 vividexstance

• D.I.C Lover

Reputation: 783
• Posts: 2,767
• Joined: 31-December 10

## Re: How to write a program that simulates bouncing in C?

Posted 30 September 2013 - 10:29 AM

Technically you do, but if that function gets called more than once you will call it multiple times. That's why it's best to just put the srand() call at the beginning of main() right after any variable declarations.

### #11 Stephenfrazier10

Reputation: 2
• Posts: 109
• Joined: 12-September 12

## Re: How to write a program that simulates bouncing in C?

Posted 30 September 2013 - 10:33 AM

Once it is all done, I want the results to be displayed like this. I still do not know why its not doing that...
-10:
-8:
-6: o
-4: ooooooooo
-2: oooooooooooooooooooooo
0: ooooooooooooooooooooooooo
2: oooooooooooooooooooooo
4: ooooooooooooo
6: ooooooo
8:
10: o

### #12 Stephenfrazier10

Reputation: 2
• Posts: 109
• Joined: 12-September 12

## Re: How to write a program that simulates bouncing in C?

Posted 30 September 2013 - 10:41 AM

This is all of it now

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

int drop_balls(); /* Prototype for drop_Balls, int parameter is number of balls being dropped */

int get_num_balls(); /* Gets the number of balls that need to be dropped by the user */

int main()
{
drop_balls(get_num_balls());
}

int get_num_balls()
{
printf("How many balls should be dropped? ");
scanf("%d",&num_balls);
while(num_balls <= 0)
{
printf("You have to drop at least one ball! \n ");
printf("How many balls should be dropped? ");
scanf("%d", num_balls);
}
return num_balls; /* Return the number of balls that will be dropped */
}

int drop_balls(num_balls)
{
int ball_count[21]; /* Keeps track of how many balls landed where */
int ball_num = 0; /* What number ball are we on */
srand(time(NULL)); /* Seed the generator */
for(; ball_num < num_balls; ball_num++ ) /* Do the correct # of balls */
{
int starting_point = 10; /* Start at 10 since its the middle of 21 */
for(starting_point; starting_point >=0; starting_point--) /* Bounce each ball 10 times */
{
int number;
number = rand() % 100; /* Create a random integer between 1-100 */
if(number > 50) /* If its less than 50 move to the right once */
{
starting_point++;
}
else /* If its greater than 50, move to the left once */
{
starting_point--;
}
}
ball_count[starting_point] += "o";  /* Add one 'o' to simulate one ball landing there */
}
int j;
for(j = 0; j < 21; j++) /* Go through each spot in the array */
{
printf("'%d :'", ball_count[j]);
}
}

```

This is the error messages I keep getting

main.c: In function 'get_num_balls':
main.c:18:17: error: 'num_balls' undeclared (first use in this function)
scanf("%d",&num_balls);
^
main.c:18:17: note: each undeclared identifier is reported only once for each function it appears in
main.c: In function 'drop_balls':
main.c:51:36: warning: assignment makes integer from pointer without a cast [enabled by default]
ball_count[starting_point] += "o"; /* Add one 'o' to simulate one ball landing there */
^

### #13 vividexstance

• D.I.C Lover

Reputation: 783
• Posts: 2,767
• Joined: 31-December 10

## Re: How to write a program that simulates bouncing in C?

Posted 30 September 2013 - 10:49 AM

The error is because you never declare the "num_balls" variable you just start using it right away without it being declared.

The last warning looks like it has to do with this line:
```ball_count[starting_point] += "o";  /* Add one 'o' to simulate one ball landing there */

```

What exactly are you trying to do here? You do know that ball_count is an array of integers correct?

### #14 Stephenfrazier10

Reputation: 2
• Posts: 109
• Joined: 12-September 12

## Re: How to write a program that simulates bouncing in C?

Posted 30 September 2013 - 10:54 AM

I want it to display the numbers -10 through 10, and a 'o' representing a ball that landed on that spot. This is the example in the book

How many balls to drop? 150
-10: o
-8:
-6: ooooo
-4: ooooooooooooooooo
-2: oooooooooooooooooooooooooooooooooo
0: oooooooooooooooooooooooooooooooo
2: oooooooooooooooooooooooooooo
4: oooooooooooooooooooooooo
6: oooooooo
8: o
10:

### #15 Stephenfrazier10

Reputation: 2
• Posts: 109
• Joined: 12-September 12

## Re: How to write a program that simulates bouncing in C?

Posted 30 September 2013 - 11:21 AM

This is what I have now. I keep getting a bunch of deceleration errors, what am I not defining?

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

int drop_balls(); /* Prototype for drop_Balls, int parameter is number of balls being dropped */

int get_num_balls(); /* Gets the number of balls that need to be dropped by the user */

int main()
{
drop_balls(get_num_balls());
}

int get_num_balls()
{
int num_balls;
printf("How many balls should be dropped? ");
scanf("%d",&num_balls);
while(num_balls <= 0)
{
printf("You have to drop at least one ball! \n ");
printf("How many balls should be dropped? ");
scanf("%d", num_balls);
}
return num_balls; /* Return the number of balls that will be dropped */
}

int drop_balls()
{
int ball_count[21]; /* Keeps track of how many balls landed where */
int ball_num = 0; /* What number ball are we on */
srand(time(NULL)); /* Seed the generator */
for(; ball_num < num_balls; ball_num++ ) /* Do the correct # of balls */
{
int starting_point = 10; /* Start at 10 since its the middle of 21 */
for(starting_point; starting_point >=0; starting_point--) /* Bounce each ball 10 times */
{
int number;
number = rand() % 100; /* Create a random integer between 1-100 */
if(number > 50) /* If its less than 50 move to the right once */
{
starting_point++;
}
else /* If its greater than 50, move to the left once */
{
starting_point--;
}
}
ball_count[starting_point]++;  /* Add one to simulate one ball landing there */
}
int j;
for(j = 0; j < 21; j++) /* Go through each spot in the array */
{
printf("%d: ", j); /* Print out the number that its on */
if(ball_count[j] > 0) /* If there are balls there */
{
int a;
for(a = 0; a < ball_count[j]; a++) /* Print out all the balls there */
{
printf("o");
}
}
}
}

```