# Programming Exercises

Page

**1**of**1**## Programming Exercises

1. Create a program that will receive a set of numbers from the user. When the user has finished entering numbers, they will enter -1. The program must then display the sum and average of these numbers, not including the -1. As an extra, also track and display the smallest and largest numbers entered.

2. Make a player vs. player tic-tac-toe game. The first player will move, then the second, and back and forth until the game is either won or tied. The program will take a column and row number as input and will check to make sure the move is valid. It should properly detect a winner. If you want a challenge, try making a simple AI option. (C has a function, rand(), that returns a random double value. To get a random integer, cast it to int and modulus with the limit. For a number between 0 and 10,

(int)rand() % 11. For a number between 1 and 10, (int)rand() % 10 + 1)

3. Make a recursive exponentiation function (to calculate X^N where X can be any valid number and N can be any integer) AND the equivelant loop version (as in, using loops instead of recursion).

Try to do as many of these as you can. If you have a problem, I'll be willing to help as long as you actually have a well-defined algorithm. The next programming meeting will be all about trying to make changes to the robot code, so you need to be sure that you understand the language.

2. Make a player vs. player tic-tac-toe game. The first player will move, then the second, and back and forth until the game is either won or tied. The program will take a column and row number as input and will check to make sure the move is valid. It should properly detect a winner. If you want a challenge, try making a simple AI option. (C has a function, rand(), that returns a random double value. To get a random integer, cast it to int and modulus with the limit. For a number between 0 and 10,

(int)rand() % 11. For a number between 1 and 10, (int)rand() % 10 + 1)

3. Make a recursive exponentiation function (to calculate X^N where X can be any valid number and N can be any integer) AND the equivelant loop version (as in, using loops instead of recursion).

Try to do as many of these as you can. If you have a problem, I'll be willing to help as long as you actually have a well-defined algorithm. The next programming meeting will be all about trying to make changes to the robot code, so you need to be sure that you understand the language.

**Brendan van Ryn**- Posts : 95

Join date : 2010-09-30

Age : 30

## Re: Programming Exercises

1. Create a program that will take a number,

t

2. Create a program that receives a number,

The first is to calculate the slope between each pair of consecutive points. Given points A(25, 50), B(35, 55), C(50, 60), you would calculate the slope of line segments AB, and BC, using rise over run. You would then average these slopes to get the slope of the line of best fit. This slope would be substituted into the equation y = mx + b for every point and isolated for b. The average of these intercepts is the intercept of the line of best fit.

The second method is to use these two formulas and solve for a and b:

∑y = a∑x

∑xy = a∑x + b∑x

Where y = bx + a

If you need more exercises, post a message and I'll come up with some.

*n*, as input. It will then calculate the n^{th}term in the Fibonacci sequence and display it. Bonus 1: The user will enter a number,*n*, as input. This will be followed by*n*more numbers. For each number,*x*, entered, display t_{x}in the Fibonacci sequence. Bonus 2: Use a recursive function instead of a loop to calculate the sequence. Use this formula to help:t

_{n}= t_{n-1}+ t_{n-2}2. Create a program that receives a number,

*n*, as input. This will be followed by*n*coordinates entered in the form: x\ny\nx\ny\n... This program will then perform linear regression on these points. You can do this one of two ways:The first is to calculate the slope between each pair of consecutive points. Given points A(25, 50), B(35, 55), C(50, 60), you would calculate the slope of line segments AB, and BC, using rise over run. You would then average these slopes to get the slope of the line of best fit. This slope would be substituted into the equation y = mx + b for every point and isolated for b. The average of these intercepts is the intercept of the line of best fit.

The second method is to use these two formulas and solve for a and b:

∑y = a∑x

^{0}+ b∑x∑xy = a∑x + b∑x

^{2}Where y = bx + a

If you need more exercises, post a message and I'll come up with some.

**Brendan van Ryn**- Posts : 95

Join date : 2010-09-30

Age : 30

Page

**1**of**1****Permissions in this forum:**

**cannot**reply to topics in this forum