Guest User

Untitled

a guest
Feb 20th, 2018
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.83 KB | None | 0 0
  1. /******************************************************************************
  2. filename array.c
  3. author Lucas Carpenter
  4. DP email lucas.c@digipen.edu
  5. course CS120
  6. assignment 3
  7. due date 11/1/2011
  8.  
  9. Brief Description:
  10. does various operations on arrays, including reversal, addition, dotting, and corssing.
  11. ******************************************************************************/
  12.  
  13. /******************************************************************************
  14. function reverse_array
  15. author Lucas Carpenter
  16. DP email lucas.c@digipen.edu
  17. course CS120
  18. assignment 3
  19. due date 11/1/2011
  20.  
  21. Brief Description:
  22. reverses an array
  23. ******************************************************************************/
  24.  
  25. void reverse_array(int a[], int size)
  26. {
  27. /*initialize variables*/
  28. int iter,
  29. begin,
  30. end,
  31. hold;
  32. /* begin is start of array, and end is... end*/
  33. begin = 0;
  34. end = (size - 1);
  35.  
  36. /*perfrom element reversal (# of elements)/2 */
  37. for(iter = 0; iter < (size / 2); iter++)
  38. {
  39. /* hold left element value */
  40. hold = a[begin];
  41. /*assign left element the value of right element */
  42. a[begin] = a[end];
  43. /* assign right element the former value of left element*/
  44. a[end] = hold;
  45. /* move towards the middle of the array */
  46. --end;
  47. ++begin;
  48. }
  49.  
  50. }
  51.  
  52. /******************************************************************************
  53. function add_arrays
  54. author Lucas Carpenter
  55. DP email lucas.c@digipen.edu
  56. course CS120
  57. assignment 3
  58. due date 11/1/2011
  59.  
  60. Brief Description:
  61. adds the elements of two arrays
  62. ******************************************************************************/
  63.  
  64.  
  65. void add_arrays(const int a[], const int b[], int c[], int size)
  66. {
  67. /* initialize variables */
  68. int i;
  69. /*perform once for each element */
  70. for (i = 0; i <= size; i++)
  71. {
  72. /* add each incrementing element from a and b, assign it to c */
  73. c[i] = a[i] + b[i];
  74. }
  75. }
  76.  
  77. /******************************************************************************
  78. function scalar_multiply
  79. author Lucas Carpenter
  80. DP email lucas.c@digipen.edu
  81. course CS120
  82. assignment 3
  83. due date 11/1/2011
  84.  
  85. Brief Description:
  86. multiplies each element of an array by a scalar
  87. ******************************************************************************/
  88.  
  89. void scalar_multiply(int a[], int size, int multiplier)
  90. {
  91. /* initialize variables */
  92. int i;
  93. /* perform once for each element */
  94. for (i = 0; i <= size; i++)
  95. {
  96. /*multiply element by multipler */
  97. a[i] *= multiplier;
  98. }
  99. }
  100.  
  101. /******************************************************************************
  102. function dot_product
  103. author Lucas Carpenter
  104. DP email lucas.c@digipen.edu
  105. course CS120
  106. assignment 3
  107. due date 11/1/2011
  108.  
  109. Brief Description:
  110. dots two arrays
  111. ******************************************************************************/
  112.  
  113. int dot_product(const int a[], const int b[], int size)
  114. {
  115. /* initialize variables*/
  116. int result,
  117. i;
  118. i = 0;
  119. result = 0;
  120. /* perfrom once for each element */
  121. while(i < size)
  122. {
  123. /*take the sum total of each product */
  124. result += a[i] * b[i];
  125. ++i;
  126. }
  127. return result;
  128. }
  129.  
  130. /******************************************************************************
  131. function cross_product
  132. author Lucas Carpenter
  133. DP email lucas.c@digipen.edu
  134. course CS120
  135. assignment 3
  136. due date 11/1/2011
  137.  
  138. Brief Description:
  139. crosses two arrays
  140. ******************************************************************************/
  141.  
  142. void cross_product(const int a[], const int b[], int c[])
  143. {
  144. /* assign each cross to each c element */
  145. c[0] = a[1] * b[2] - a[2] * b[1];
  146. c[1] = a[2] * b[0] - a[0] * b[2];
  147. c[2] = a[0] * b[1] - a[1] * b[0];
  148. }
Add Comment
Please, Sign In to add comment