Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2014
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.41 KB | None | 0 0
  1. /*
  2. Filename: p1v1_1
  3.  
  4. Synopsis: practical 0 - A First C Program
  5.  
  6. Author:
  7. Connor Campbell, Username: dsb13140
  8.  
  9. Group: Thu, 10-12
  10.  
  11. Promise: i can confirm this submission is my own work
  12.  
  13. Signed:
  14. */
  15. /****************************REVISION HISTORY:****************************
  16. *
  17. * v0.1 15/10/2014: Initial version.
  18. * v0.2 15/10/2014/10/2014: Added the documentation and revision history to original version
  19. * and removed examples
  20. * v0.3a 15/10/2014: initialised variables for the speed on X and Y using scanf and
  21. * Calculated how much faster X is
  22. * v0.3b 15/10/2014: Answer was giving a rounded value, revised the equation to include double
  23. * to fix this problem
  24. * v0.4a 15/10/2014: initialised variables for the clock speed and the CPU time of X and Y
  25. * Also calculated the necessary clock speed needed in Y
  26. * v0.4b 16/10/2014: Added cycle speed and CPI variables for X and Y then used these values calculate
  27. * the CPU time of each before accounting for instruction count
  28. * v0.5a 16/10/2014: Inserted the equation to calculate how much faster X is compared to Y
  29. * v0.5b 16/10/2014: Answer was not being rounded correctly, inserted the correct specifier to fix the issue
  30. * v0.5c 16/10/2014: Answer was rounding again, so revised the equation to include double
  31. * v0.6 16/10/2014: If speed of X an Y is the same, shows that X is 1 times faster so added an if and else
  32. * statement to make it look clearer
  33. * v1.1 16/10/2014: Completed version
  34. *
  35. **************************************************************************/
  36. #define VERSION "p1v1_1 by Connor Campbell. Last update: 16/10/2014\n\n"
  37.  
  38. #include <stdio.h> /* Include standard library functions for I/O */
  39.  
  40. #include <stdio.h>
  41.  
  42.  
  43.  
  44. int main(void)
  45. {
  46. printf(VERSION);
  47.  
  48. int speedX;
  49. printf("Speed of X: ");
  50. scanf("%d",&speedX);
  51.  
  52. int speedY;
  53. printf("Speed of Y: ");
  54. scanf("%d",&speedY);
  55.  
  56. float z = speedY/speedX;
  57.  
  58. if (z == 1){
  59. printf("%d's on X %d's on Y. So X is the same speed as Y\n", speedX, speedY);
  60. }
  61. else{
  62. printf("%d's on X, %d's on Y. So X is %.2f times faster than Y\n", speedX, speedY, z);
  63. }
  64.  
  65. float clockX;
  66. printf("Clock Speed of X: ");
  67. scanf("%f",&clockX);
  68.  
  69. int cpuX;
  70. printf("CPU time of X: ");
  71. scanf("%d",&cpuX);
  72.  
  73. float clockY;
  74. printf("Clock Speed of Y: ");
  75. scanf("%f",&clockY);
  76.  
  77. int cpuY;
  78. printf("CPU time of Y: ");
  79. scanf("%d",&cpuY);
  80.  
  81.  
  82. float clockZ = (clockX*cpuX*1.2)/cpuY;
  83.  
  84.  
  85. printf("X: %.2fGHz clock, %ds CPU time; Y: ?GHz clock, %ds CPU time\n", clockX, cpuX, cpuY);
  86. printf(" Therefore clock rate of Y needs to be %.2fGHz\n", clockZ);
  87.  
  88. int cycleX;
  89. printf("Cycle time of X: ");
  90. scanf("%d",&cycleX);
  91.  
  92. float cpiX;
  93. printf("CPI of X: ");
  94. scanf("%f", &cpiX);
  95.  
  96. int cycleY;
  97. printf("Cycle time of Y: ");
  98. scanf("%d",&cycleY);
  99.  
  100. float cpiY;
  101. printf("CPI of Y: ");
  102. scanf("%f", &cpiY);
  103.  
  104. int cpuTimeX = cycleX*cpiX;
  105. int cpuTimeY = cycleY*cpiY;
  106.  
  107. float faster = (cpuTimeY)/cpuTimeX;
  108.  
  109. if(faster == 1){
  110. printf("X: Cycle Time:%dps, CPI=%0.2f; Y: Cycle Time=%dps, CPI=%0.1f\n" , cycleX, cpiX, cycleY, cpiY);
  111. printf(" So, Cpu Time_X is IC x CPI_X x Cycle Time_X => IXC x %dps\n", cpuTimeX);
  112. printf(" So, Cpu Time_Y is IC x CPI_Y x Cycle Time_Y => IXC x %dps\n", cpuTimeY);
  113. printf(" Therefore X is the same speed as Y\n");
  114. }
  115. else{
  116. printf("X: Cycle Time:%dps, CPI=%0.2f; Y: Cycle Time=%dps, CPI=%0.1f\n" , cycleX, cpiX, cycleY, cpiY);
  117. printf(" So, Cpu Time_X is IC x CPI_X x Cycle Time_X => IXC x %dps\n", cpuTimeX);
  118. printf(" So, Cpu Time_Y is IC x CPI_Y x Cycle Time_Y => IXC x %dps\n", cpuTimeY);
  119. printf(" Therefore X is %0.2f times faster than Y\n", faster);
  120. }
  121.  
  122. }
  123.  
  124.  
  125. /* int j = 3, k = 2;
  126. int *p = &j;
  127. float f = 3.0;
  128. double d = 2.0;
  129.  
  130.  
  131. The following code illustrates (some of) the examples from Lecture 5.
  132. printf("Examples based on CS210_01_sw/72:\n"\
  133. "5 * 6 + 13/3.0 => %f\n\t\'z\' => %d\n\t\'0\' => %d\n\n",
  134. 5 * 6 + 13/3.0, 'z', '0');
  135.  
  136. printf("Examples based on CS210_01_sw/73:\n"\
  137. "\tj => %d, k => %d\n\tj/k + 3 => %d\n\t3 + (int) 5.0/k => %d\n\n",
  138. j, k, j/k + 3, 3 + (int) 5.0/k);
  139.  
  140. printf("Examples based on CS210_01_sw/74:\n\t"\
  141. "f => %f, d => %f\n\t"\
  142. "(float) j => %f\n\t"\
  143. "f/d + 3 => %f\n\t"\
  144. "(double) 3/4 + d => %f\n\n",
  145. f, d, (float) j, f/d + 3, (double) 3/4 + d);
  146.  
  147. printf("Examples based on CS210_01_sw/75:\n\t"\
  148. "p => %p\n\t"\
  149. "&j => %p\n\t"\
  150. "p + 1 => %p\n\t"\
  151. "\"a string literal\" => %p\n\t"\
  152. "(char *) 0x000ffff => %p\n\n",
  153. (void *)p, (void *)&j, (void *)(p + 1), "a string literal",
  154. (char *) 0x000ffff);
  155.  
  156. printf("Examples based on the BUG ALERT of CS210_01_sw/88:\n"\
  157. "\t5/2 => %d\n\t1/3 => %d\n\t"\
  158. "5/-2 => %d\n\t-1/3 => %d\n\t"\
  159. "-5%%2 => %d\n\t5%%-2 => %d\n\n",
  160. 5/2, 1/3, 5/-2, -1/3, -5%2, 5%-2);
  161.  
  162. return 0;
  163. }
  164.  
  165. /* Notes/Hints:
  166. See CS210_01_sw/34-35 for an overview of printf() and format specifiers.
  167.  
  168. The output when the above code is run is:
  169.  
  170. p1 v0.1 by Duncan. Last update: 05/10/2014
  171.  
  172. Examples based on CS210_01_sw/72:
  173. 5 * 6 + 13/3.0 => 34.333333
  174. 'z' => 122
  175. '0' => 48
  176.  
  177. Examples based on CS210_01_sw/73:
  178. j => 3, k => 2
  179. j/k + 3 => 4
  180. 3 + (int) 5.0/k => 5
  181.  
  182. Examples based on CS210_01_sw/74:
  183. f => 3.000000, d => 2.000000
  184. (float) j => 3.000000
  185. f/d + 3 => 4.500000
  186. (double) 3/4 + d => 2.750000
  187.  
  188. Examples based on CS210_01_sw/75:
  189. p => 0x7fff5fbff79c
  190. &j => 0x7fff5fbff79c
  191. p + 1 => 0x7fff5fbff7a0
  192. "a string literal" => 0x100000e0e
  193. (char *) 0x000ffff => 0xffff
  194.  
  195. Examples based on the BUG ALERT of CS210_01_sw/88:
  196. 5/2 => 2
  197. 1/3 => 0
  198. 5/-2 => -2
  199. -1/3 => 0
  200. -5%2 => -1
  201. 5%-2 => 1
  202.  
  203. */
  204. /* The output when the demonstrated version of p1v1_1 is executed:
  205.  
  206. p1 v1.1 by Duncan. Last update: 05/10/2014
  207.  
  208. 10s on X, 15s on Y. So X is 1.50 times faster than Y
  209.  
  210. X: 2.00GHz clock, 10s CPU time; Y: ?GHz clock, 6s CPU time
  211. Therefore clock rate of Y needs to be 4.00GHz
  212.  
  213. X: Cycle time=250ps, CPI=2.0; Y: Cycle time=500ps, CPI=1.2
  214. So, CPU Time_X is IC x CPI_X x Cycle Time_X => IC x 500ps
  215. So, CPU Time_Y is IC x CPI_Y x Cycle Time_Y => IC x 600ps
  216. Therefore X is 1.20 times faster than Y
  217.  
  218. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement