Engineering Computation Workshop 2 === ###### tags: `comp20005` `workshop` `c` --- ### Trace through the execution of the following program. What do you think the output will be? Discuss your answer. ```cpp= #include<stdio.h> int main(int argc, char *argv[]) { int i, j; i = 3; j = 4; if (i<j && j<6) { i = i+j; } else { j = i+j; } printf("i=%d, j=%d", i, j); return 0; } ``` --- ### Output ``` i=7 j=4 ``` --- ### Trace through the execution of the following program. What do you think the output will be? Discuss your answer. ```cpp= #include<stdio.h> int main(int argc, char *argv[]) { int i, j, k; i = 3; j = 4; k = 7; if ((i<j || j < k) && j<i) { i = i+1; if (i*i>k) { k = k+1; } } else { j = j+1; if (i*i>k) { k = k+2; } } printf("i=%d, j=%d, k=%d\n", i, j, k); return 0; } ``` --- ### Output ``` i= 3, j=5, k=9 ``` --- ### Trace through the execution of the following program. What do you think the output will be? Discuss your answer. ```cpp= #include<stdio.h> int main(int argc, char *argv[]) { int month, days; month = 7; if (month == 2) { days = 28; } else if (month == 4 || 6 || 9 || 11) { days = 30; } else { days = 31; } printf("days=%d\n", days); return 0; } ``` --- ### Output ``` main.c:8:26: warning: use of logical '||' with constant operand [-Wconstant-logical-operand] } else if (month == 4 || 6 || 9 || 11) { ^ ~ main.c:8:26: note: use '|' for a bitwise operation } else if (month == 4 || 6 || 9 || 11) { ^~ | main.c:8:31: warning: use of logical '||' with constant operand [-Wconstant-logical-operand] } else if (month == 4 || 6 || 9 || 11) { ^ ~ main.c:8:31: note: use '|leg' for a bitwise operation } else if (month == 4 || 6 || 9 || 11) { ^~ | main.c:8:36: warning: use of logical '||' with constant operand [-Wconstant-logical-operand] } else if (month == 4 || 6 || 9 || 11) { ^ ~~ main.c:8:36: note: use '|' for a bitwise operation } else if (month == 4 || 6 || 9 || 11) { ^~ | 3 warnings generated. ``` --- ### Output ``` days=30 ``` --- ### Trace through the execution of the following program. What do you think the output will be? Discuss your answer. ```cpp= #include<stdio.h> int main(int argc, char *argv[]) { int x, y; x = 1; y = 2; if (x>y) printf("x=%d, y=%d\n", x, y); x = x+1; if (x<y) printf("x=%d, y=%d\n", x, y); y = y+2; printf("x=%d, y=%d", x, y); return 0; } ``` --- ### Output ``` x=2, y=4 ``` --- ### Trace through the execution of the following program. What do you think the output will be? Discuss your answer. ```cpp= #include<stdio.h> int main(int argc, char *argv[]) { int x, y; x = 1; y = 2; if (x>y); { printf("x=%d, y=%d\n", x, y); x = x+1; } if (x<y); { printf("x=%d, y=%d\n", x, y); y = y+2; } printf("x=%d, y=%d\n", x, y); return 0; } ``` --- ### Output ``` x=1, y=2 x=2, y=2 x=2, y=4 ``` --- ### Trace through the execution of the following program. What do you think the output will be? Discuss your answer. ```cpp= #include<stdio.h> int main(int argc, char *argv[]) { int x, y; x = 0; y = 0; if (y<x) { printf("y is smaller\n"); } else if (y=x) { printf("x and y are equal\n"); } else { printf("y is greater\n"); } return 0; } ``` --- ### Output ``` y is greater ``` --- ### Giving Change >Suppose that coins are available in denominations of 50c, 20c, 10c, 5c, 2c and 1c. Write a program that reads an integer amount of cents between 0 and 99 (your program might check that the input value falls within this range) and prints out the coins necessary to make up that amount of money. Here's an example of how your program should work: ``` Enter amount in cents: 67 The coins required to make 67 cents are: give a 50c coin give a 10c coin give a 5c coin give a 2c coin amount remaining: 0c ``` --- ### Solution (no loops) ```cpp= #include <stdio.h> int main(int argc, char **argv) { int cents; // read in cents required printf("Enter amount in cents: "); scanf("%d", &cents); printf("The coins required to make %d cents are:\n", cents); if (cents > 99 || cents < 0){ printf("Invalid input %d must be between 0-99", cents); return 0; } if (cents >= 50) { printf("give a %2dc coin\n", 50); cents -= 50; } if (cents >= 20) { printf("give a %2dc coin\n", 20); cents -= 20; } if (cents >= 20) { printf("give a %2dc coin\n", 20); cents -= 20; } if (cents >= 10) { printf("give a %2dc coin\n", 10); cents -= 10; } if (cents >= 5) { printf("give a %2dc coin\n", 5); cents -= 5; } if (cents >= 2) { printf("give a %2dc coin\n", 2); cents -= 2; } if (cents >= 2) { printf("give a %2dc coin\n", 2); cents -= 2; } if (cents >= 1) { printf("give a %2dc coin\n", 1); cents -= 1; } if (cents != 0) { printf("An error has occurred, expected cents == 0 but is %d\n",cents); return 0; } printf("amount remaining: %dc\n", cents); return 0; } ``` --- ### Temperature Conversion >In the previous lab, you were asked to write a program that converted temperatures from Fahrenheit to Celsius. Extend that program by adding in the reverse transformation: ``` Enter a temperature: 212F The temperature 212.0F converts to 100.0C ``` ``` Enter a temperature: 212C The temperature 212.0C converts to 413.6F ``` --- ### solution ```cpp= #include <stdio.h> int main(int argc, char **argv) { double temp, conv_temp; char type, conv_type; printf("Enter a temperature: "); scanf("%lf %c", &temp, &type); if (type == 'C' || type == 'c'){ conv_temp = temp * (9.0 / 5.0) + 32; conv_type = 'F'; } else if (type == 'F' || type == 'f') { conv_temp = (temp - 32) * 5.0 / 9.0; conv_type = 'C'; } else { printf("Error, please enter C or F after temp"); return 1; } printf("The temperature %.1f%c converts to %.1f%c\n", temp, type, conv_temp, conv_type); return 0; } ``` ---
{"metaMigratedAt":"2023-06-15T04:32:42.481Z","metaMigratedFrom":"Content","title":"Engineering Computation Workshop 2","breaks":true,"contributors":"[{\"id\":\"097a8b2e-1817-41aa-b11f-65c49c54dbaf\",\"add\":6622,\"del\":76}]"}
    242 views