Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Filename: p1v1_1
- Synopsis: practical 0 - A First C Program
- Author:
- Connor Campbell, Username: dsb13140
- Group: Thu, 10-12
- Promise: i can confirm this submission is my own work
- Signed:
- */
- /****************************REVISION HISTORY:****************************
- *
- * v0.1 15/10/2014: Initial version.
- * v0.2 15/10/2014/10/2014: Added the documentation and revision history to original version
- * and removed examples
- * v0.3a 15/10/2014: initialised variables for the speed on X and Y using scanf and
- * Calculated how much faster X is
- * v0.3b 15/10/2014: Answer was giving a rounded value, revised the equation to include double
- * to fix this problem
- * v0.4a 15/10/2014: initialised variables for the clock speed and the CPU time of X and Y
- * Also calculated the necessary clock speed needed in Y
- * v0.4b 16/10/2014: Added cycle speed and CPI variables for X and Y then used these values calculate
- * the CPU time of each before accounting for instruction count
- * v0.5a 16/10/2014: Inserted the equation to calculate how much faster X is compared to Y
- * v0.5b 16/10/2014: Answer was not being rounded correctly, inserted the correct specifier to fix the issue
- * v0.5c 16/10/2014: Answer was rounding again, so revised the equation to include double
- * 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
- * statement to make it look clearer
- * v1.1 16/10/2014: Completed version
- *
- **************************************************************************/
- #define VERSION "p1v1_1 by Connor Campbell. Last update: 16/10/2014\n\n"
- #include <stdio.h> /* Include standard library functions for I/O */
- #include <stdio.h>
- int main(void)
- {
- printf(VERSION);
- int speedX;
- printf("Speed of X: ");
- scanf("%d",&speedX);
- int speedY;
- printf("Speed of Y: ");
- scanf("%d",&speedY);
- float z = speedY/speedX;
- if (z == 1){
- printf("%d's on X %d's on Y. So X is the same speed as Y\n", speedX, speedY);
- }
- else{
- printf("%d's on X, %d's on Y. So X is %.2f times faster than Y\n", speedX, speedY, z);
- }
- float clockX;
- printf("Clock Speed of X: ");
- scanf("%f",&clockX);
- int cpuX;
- printf("CPU time of X: ");
- scanf("%d",&cpuX);
- float clockY;
- printf("Clock Speed of Y: ");
- scanf("%f",&clockY);
- int cpuY;
- printf("CPU time of Y: ");
- scanf("%d",&cpuY);
- float clockZ = (clockX*cpuX*1.2)/cpuY;
- printf("X: %.2fGHz clock, %ds CPU time; Y: ?GHz clock, %ds CPU time\n", clockX, cpuX, cpuY);
- printf(" Therefore clock rate of Y needs to be %.2fGHz\n", clockZ);
- int cycleX;
- printf("Cycle time of X: ");
- scanf("%d",&cycleX);
- float cpiX;
- printf("CPI of X: ");
- scanf("%f", &cpiX);
- int cycleY;
- printf("Cycle time of Y: ");
- scanf("%d",&cycleY);
- float cpiY;
- printf("CPI of Y: ");
- scanf("%f", &cpiY);
- int cpuTimeX = cycleX*cpiX;
- int cpuTimeY = cycleY*cpiY;
- float faster = (cpuTimeY)/cpuTimeX;
- if(faster == 1){
- printf("X: Cycle Time:%dps, CPI=%0.2f; Y: Cycle Time=%dps, CPI=%0.1f\n" , cycleX, cpiX, cycleY, cpiY);
- printf(" So, Cpu Time_X is IC x CPI_X x Cycle Time_X => IXC x %dps\n", cpuTimeX);
- printf(" So, Cpu Time_Y is IC x CPI_Y x Cycle Time_Y => IXC x %dps\n", cpuTimeY);
- printf(" Therefore X is the same speed as Y\n");
- }
- else{
- printf("X: Cycle Time:%dps, CPI=%0.2f; Y: Cycle Time=%dps, CPI=%0.1f\n" , cycleX, cpiX, cycleY, cpiY);
- printf(" So, Cpu Time_X is IC x CPI_X x Cycle Time_X => IXC x %dps\n", cpuTimeX);
- printf(" So, Cpu Time_Y is IC x CPI_Y x Cycle Time_Y => IXC x %dps\n", cpuTimeY);
- printf(" Therefore X is %0.2f times faster than Y\n", faster);
- }
- }
- /* int j = 3, k = 2;
- int *p = &j;
- float f = 3.0;
- double d = 2.0;
- The following code illustrates (some of) the examples from Lecture 5.
- printf("Examples based on CS210_01_sw/72:\n"\
- "5 * 6 + 13/3.0 => %f\n\t\'z\' => %d\n\t\'0\' => %d\n\n",
- 5 * 6 + 13/3.0, 'z', '0');
- printf("Examples based on CS210_01_sw/73:\n"\
- "\tj => %d, k => %d\n\tj/k + 3 => %d\n\t3 + (int) 5.0/k => %d\n\n",
- j, k, j/k + 3, 3 + (int) 5.0/k);
- printf("Examples based on CS210_01_sw/74:\n\t"\
- "f => %f, d => %f\n\t"\
- "(float) j => %f\n\t"\
- "f/d + 3 => %f\n\t"\
- "(double) 3/4 + d => %f\n\n",
- f, d, (float) j, f/d + 3, (double) 3/4 + d);
- printf("Examples based on CS210_01_sw/75:\n\t"\
- "p => %p\n\t"\
- "&j => %p\n\t"\
- "p + 1 => %p\n\t"\
- "\"a string literal\" => %p\n\t"\
- "(char *) 0x000ffff => %p\n\n",
- (void *)p, (void *)&j, (void *)(p + 1), "a string literal",
- (char *) 0x000ffff);
- printf("Examples based on the BUG ALERT of CS210_01_sw/88:\n"\
- "\t5/2 => %d\n\t1/3 => %d\n\t"\
- "5/-2 => %d\n\t-1/3 => %d\n\t"\
- "-5%%2 => %d\n\t5%%-2 => %d\n\n",
- 5/2, 1/3, 5/-2, -1/3, -5%2, 5%-2);
- return 0;
- }
- /* Notes/Hints:
- See CS210_01_sw/34-35 for an overview of printf() and format specifiers.
- The output when the above code is run is:
- p1 v0.1 by Duncan. Last update: 05/10/2014
- Examples based on CS210_01_sw/72:
- 5 * 6 + 13/3.0 => 34.333333
- 'z' => 122
- '0' => 48
- Examples based on CS210_01_sw/73:
- j => 3, k => 2
- j/k + 3 => 4
- 3 + (int) 5.0/k => 5
- Examples based on CS210_01_sw/74:
- f => 3.000000, d => 2.000000
- (float) j => 3.000000
- f/d + 3 => 4.500000
- (double) 3/4 + d => 2.750000
- Examples based on CS210_01_sw/75:
- p => 0x7fff5fbff79c
- &j => 0x7fff5fbff79c
- p + 1 => 0x7fff5fbff7a0
- "a string literal" => 0x100000e0e
- (char *) 0x000ffff => 0xffff
- Examples based on the BUG ALERT of CS210_01_sw/88:
- 5/2 => 2
- 1/3 => 0
- 5/-2 => -2
- -1/3 => 0
- -5%2 => -1
- 5%-2 => 1
- */
- /* The output when the demonstrated version of p1v1_1 is executed:
- p1 v1.1 by Duncan. Last update: 05/10/2014
- 10s on X, 15s on Y. So X is 1.50 times faster than Y
- X: 2.00GHz clock, 10s CPU time; Y: ?GHz clock, 6s CPU time
- Therefore clock rate of Y needs to be 4.00GHz
- X: Cycle time=250ps, CPI=2.0; Y: Cycle time=500ps, CPI=1.2
- So, CPU Time_X is IC x CPI_X x Cycle Time_X => IC x 500ps
- So, CPU Time_Y is IC x CPI_Y x Cycle Time_Y => IC x 600ps
- Therefore X is 1.20 times faster than Y
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement