Engineering Computation Workshop 5 === ###### tags: `comp20005` `workshop` `c` --- ### More Functions & Pointers* --- ### Function Review ```cpp #include <stdio.h> int main(int argc, char **argv) { printf("argc is %d\n", argc); for (int i=0;i<argc;i++) { printf("arg %d is: %s\n", i, argv[i]); } return 0; } ``` --- ### Prototypes ```cpp= #include <stdio.h> int function(); int main(int argc, char **argv) { function(); return 0; } int function() { // actual implementation return 5; } ``` --- ### Recursion What happens when a function calls itself? ```cpp int plus(int a, int b){ if (b == 0) { return a; } return 1 + plus(a, b-1); } ``` --- ### New Stuff (but not really) ##### Pointers ```cpp int *ip; /* pointer to an integer */ double *dp; /* pointer to a double */ float *fp; /* pointer to a float */ char *ch /* pointer to a character */ ``` --- ##### How to get the value out of a pointer? dereference ```cpp *<pointer>; ``` e.g. ```cpp #include <stdio.h> int main() { int a = 5; int *d = &a; printf("The address of a is: %p, the value is %d", d, *d); } ``` ---- ### Pointers Continued What can we do with them? ```cpp void swap(int *a, int *b) { int tmp = *a; *a = *b; *b = tmp; } ``` --- ### * , & , *variable Pointer Type ```cpp int *ip; /* pointer to an integer */ ``` Type to Pointer Conversion ```cpp int num = 5; int *ip = &num; ``` Pointer to Type Conversion ```cpp int *ip = <some address that has a value>; int num = *ip; ``` --- #### NULL pointers Whats the default value for a pointer? ``` int *ptr = NULL; ``` --- ### Questions --- ### Mid-Semester Practice Test **Q1: The best description of the programming language C is that it is:** --- A widely-used language that is defined by international standards and available on a broad range of general-purpose and specialized computing hardware --- **Q2: According to the precedence rules, what is the value of the C expression** ```cpp (1<=2<=3) + (9>=8>=7) ``` 1. -1 2. 2 3. 1 4. The expression is not well formed 5. 0 --- **Q2: According to the precedence rules, what is the value of the C expression** ```cpp (1<=2<=3) + (9>=8>=7) ``` 1. -1 2. 2 **3. 1** 4. The expression is not well formed 5. 0 --- **Q3: Which of the following statements about while loops is not true:** 1. A while loop can execute zero times 2. A while loop can have an empty "guard" expression 3. A while loop will iterate if the "guard" expression is non-zero 4. A while loop does not have to have a control variable associated with it 5. A while loop can have an empty body --- **Q3: Which of the following statements about while loops is not true:** 1. A while loop can execute zero times **2. A while loop can have an empty "guard" expression** 3. A while loop will iterate if the "guard" expression is non-zero 4. A while loop does not have to have a control variable associated with it 5. A while loop can have an empty body --- **Q4: The following code fragment manipulate two int variables, jill and jack:** ```cpp jack = 1; for (jill=1; jill<5; jill++) { if (jack!=jill) { jack += jill; } } ``` 1. 6 and 13 respectively 2. It is not possible to tell, as not enough information has been provided 3. 5 and 13 respectively 4. 5 and 7 respectively 5. 6 and 14 respectively --- **Q4: The following code fragment manipulate two int variables, jill and jack:** ```cpp jack = 1; for (jill=1; jill<5; jill++) { if (jack!=jill) { jack += jill; } } ``` 1. 6 and 13 respectively 2. It is not possible to tell, as not enough information has been provided 3. 5 and 13 respectively **4. 5 and 7 respectively** 5. 6 and 14 respectively --- **Q5: The following code fragment was written by a student to test whether a year is a "good" year, where variable year has been declared as an int:** ```cpp if (year == 2016 || 2017 || 2018 || 2019) { printf("This year is okay\n"); } else if (year == 2020) { printf("This is not a good year\n"); } ``` If the code fragment is executed with variable year assigned the value 2020, the output will be: --- 1. There would be no output generated by the code fragment 2. The line "This year is okay", followed by the line "This is not a good year" **3. The line "This year is okay"** 4. The line "This is not a good year" 5. The code fragment contains a syntax error and would not be compiled --- **Q6: Write a function** int median3(int x1, int x2, int x3) --- **Q6: Write a function** ```cpp int median3(int x1, int x2, int x3) { if ((x1 <= x3 && x1 >= x2) || (x1 <= x2 && x1 >= x3)) { return x1; } else if ((x2 <= x1 && x2 >= x3) || (x2 <= x3 && x2 >= x1)){ return x2; } else { return x3; } } ``` --- ```cpp= int bill(int jack, int jane); double jane(double dick, int fred, double dave); int trev; int main(int argc, char *argv[]) { double beth; int pete, bill; /* point #1 */ return 0; } int bill(int jack, int jane) { int mary; double zack; /* point #2 */ return 0; } double jane(double dick, int fred, double dave) { double trev; /* point #3 */ return 0.0; } ``` --- Point 1: ```cpp int trev, int argc, char **argv, int bill, int pete; ``` Point 2: ```cpp int trev, jack, jane, mary; double zack; ``` Point 3: ```cpp double dick, dave, trev; int fred; ``` --- ### 6.5 Sorting Two Integers Write a function int_sort2(int *p1, int*p2) that orders its two arguments so that the numerically smaller value is placed into the underlying variable corresponding to the first pointer argument, and the larger into the variable corresponding to the second argument pointer. Do not use the int_swap function discussed in Section 6.7. Write a main function that reads two integers into two variables v1 and v2, calls int_sort2 to sort them, and prints v1 and v2 before and after the function call. --- ### 6.6 Sorting Three Integers Write a function int_sort3(int *p1, int *p2, int *p3) that orders its three arguments from smallest to largest. This time you may make use of the int_swap function. If you do, be sure to think carefully about the types, and what the arguments should be when it is called. You may also make use of int_sort2 if you wish. Write a main function that reads three integers into three variables v1, v2, and v3, calls int_sort3 to sort them, and prints the three variables before and after the function call. --- ### 6.7 Write a main program to test the function read_num shown in Figure 6.9. What does the function do when lo is greater than hi? How could it be altered to better handle this case? --- ### 6.9 Calculating Change using Functions Consider again the problem described in Exercise 3.6 on page 43. Write a function int try_one_coin(int *cents, int coin) that reduces the current cents amount by the value of the current coin as many times as is possible, altering the value of cents appropriately and returning the number of coins of that denomination that should be issued. Then write a function void print_change(int cents) that tests each different coin denomination in the correct ordering, and uses try_one_coin to tell it how to generate the required change. In the late 1980s in Australia, 1c and 2c coins were abolished, and $1 and $2 were introduced. Write a further function int round_to_5(int cents) that returns a value rounded off to the nearest multiple of five. For example, 12c of change is rounded to 10c; whereas 23c is rounded to 25c. ---
{"metaMigratedAt":"2023-06-15T04:32:41.970Z","metaMigratedFrom":"Content","title":"Engineering Computation Workshop 5","breaks":true,"contributors":"[{\"id\":\"097a8b2e-1817-41aa-b11f-65c49c54dbaf\",\"add\":7924,\"del\":411}]"}
    216 views