Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /******************************************************************************
- filename array.c
- author Lucas Carpenter
- DP email lucas.c@digipen.edu
- course CS120
- assignment 3
- due date 11/1/2011
- Brief Description:
- does various operations on arrays, including reversal, addition, dotting, and corssing.
- ******************************************************************************/
- /******************************************************************************
- function reverse_array
- author Lucas Carpenter
- DP email lucas.c@digipen.edu
- course CS120
- assignment 3
- due date 11/1/2011
- Brief Description:
- reverses an array
- ******************************************************************************/
- void reverse_array(int a[], int size)
- {
- /*initialize variables*/
- int iter,
- begin,
- end,
- hold;
- /* begin is start of array, and end is... end*/
- begin = 0;
- end = (size - 1);
- /*perfrom element reversal (# of elements)/2 */
- for(iter = 0; iter < (size / 2); iter++)
- {
- /* hold left element value */
- hold = a[begin];
- /*assign left element the value of right element */
- a[begin] = a[end];
- /* assign right element the former value of left element*/
- a[end] = hold;
- /* move towards the middle of the array */
- --end;
- ++begin;
- }
- }
- /******************************************************************************
- function add_arrays
- author Lucas Carpenter
- DP email lucas.c@digipen.edu
- course CS120
- assignment 3
- due date 11/1/2011
- Brief Description:
- adds the elements of two arrays
- ******************************************************************************/
- void add_arrays(const int a[], const int b[], int c[], int size)
- {
- /* initialize variables */
- int i;
- /*perform once for each element */
- for (i = 0; i <= size; i++)
- {
- /* add each incrementing element from a and b, assign it to c */
- c[i] = a[i] + b[i];
- }
- }
- /******************************************************************************
- function scalar_multiply
- author Lucas Carpenter
- DP email lucas.c@digipen.edu
- course CS120
- assignment 3
- due date 11/1/2011
- Brief Description:
- multiplies each element of an array by a scalar
- ******************************************************************************/
- void scalar_multiply(int a[], int size, int multiplier)
- {
- /* initialize variables */
- int i;
- /* perform once for each element */
- for (i = 0; i <= size; i++)
- {
- /*multiply element by multipler */
- a[i] *= multiplier;
- }
- }
- /******************************************************************************
- function dot_product
- author Lucas Carpenter
- DP email lucas.c@digipen.edu
- course CS120
- assignment 3
- due date 11/1/2011
- Brief Description:
- dots two arrays
- ******************************************************************************/
- int dot_product(const int a[], const int b[], int size)
- {
- /* initialize variables*/
- int result,
- i;
- i = 0;
- result = 0;
- /* perfrom once for each element */
- while(i < size)
- {
- /*take the sum total of each product */
- result += a[i] * b[i];
- ++i;
- }
- return result;
- }
- /******************************************************************************
- function cross_product
- author Lucas Carpenter
- DP email lucas.c@digipen.edu
- course CS120
- assignment 3
- due date 11/1/2011
- Brief Description:
- crosses two arrays
- ******************************************************************************/
- void cross_product(const int a[], const int b[], int c[])
- {
- /* assign each cross to each c element */
- c[0] = a[1] * b[2] - a[2] * b[1];
- c[1] = a[2] * b[0] - a[0] * b[2];
- c[2] = a[0] * b[1] - a[1] * b[0];
- }
Add Comment
Please, Sign In to add comment