Let’s stay fresh with C. What does this print?

# Let’s stay fresh with C. What does this print?

## Let’s stay fresh with C. What does this print?

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
##### Presentation Transcript

1. Let’s stay fresh with C. What does this print? void mystery(int * x); main(){ int a = 5; mystery(&a); printf("%d\n",a); } void mystery(int * x){ int y; y = 2 * *x; *x = 3; } a) 5 b) 10 c) 6 d) 3

2. What is the final value of a[4]? inti; float a[10]={1.0, 5.3, -2.1, 2.0}; for(i=1; i<10; i++){ a[i] = a[i-1]+a[i]; } a) 2 b) 6.2 c) undefined d) 0.0

3. Which of the following is false about a function being passed an array? a)it knows the size of the array it was passed b) it is passed the address of the first element in the array c) it is able to modify the values stored in the array d) all of the above are true

4. Fill in the blank to print 1 2 3 4 5? #define SIZE 10 void mystery(int a[], int num); main(){ int data[SIZE] = {1,2,3,4,5,6,7,8,9,10}; mystery(____________); } void mystery(int a[], int num){ inti; for(i=0; i<num; i++){ printf("%d ", a[i]); } printf("\n"); } c) data, SIZE d) data[SIZE] a) data, 5 b) *data,5

5. Assume the variable x is stored at memory location 1000 and y is stored at memory location 1004. What are the values of x, y, and *y after executing the following block of C code? intx = 5; int *y; y = &x; x = 7; a)x = 7, y = 1004, *y = 7 b) x = 7, y = 1000, *y = 5 c) x = 7, y = 1000, *y = 7 d) x = 7, y = 5, *y = 5

6. What does this print out? #include <stdio.h> intmyFunction(int x[], int num); int main(void){ int result=0; int array[3] = {5, 2, -3}; result = myFunction(array,3); printf(“%d, %d\n”, array[1], array[2]); } intmyFunction(int x[], int num){ x[1] = 4; x[2] = 7; return(x[1]+x[2]); } a) 5, 2 b) 5, 11 c) 4, 7 d) 2, 11

7. SWITCHING GEARS TO MATLAB…

8. Will this piece of code work? If not, why? >>A = [1 2 3; 3 5 6] >>B = [2 4 6; 6 10 18] >>C = B*A This code will not work because ‘*’ is used for matrix multiplication and in matrix multiplication you need the same number of columns in B as rows in A.

9. Will this piece of code work? If not, why? >>A = [1 2 3; 3 5 6] >>B = [2 4 6; 6 10 18] >>C = B.*A This code will work because ‘.*’ means multiply each element of B with the same element of A. In order to use ‘.*’, A and B need to be the same dimensions.

10. What does C store? >>A = [1 2 3; 3 5 6;1 2 1] >>B = [2 4 ; 61] >>C = B.*A(2:3,1:2) 2 8 18 5 a) 6 20 6 2 10 24 12 1 4 12 30 6

11. What does B store? >>A = [1 2 3; 3 5 6;1 2 1] >>B = max(A) 3 6 2 a) 6 3 5 6 6 5 6

12. What does C store? >>A = [1 2 3; 3 5 6;1 2 1] >>B = max(A) >>C=max(B) 3 6 2 a) 6 3 5 6 6 5 6

13. What does B and C store? >>A = [1 2 3; 3 5 6;1 2 1] >>[B,C] = max(A) B = 3 6 2 C = 3 3 2 B= 6 C = 4 B = 3 5 6 C = 2 2 2 B = 6 5 6 C = 1 1 1

14. What does D and E store? >>A = [1 2 3; 3 5 6;1 2 1] >>[B,C] = max(A) >>[D,E] = max(B) D = 6 E = 3 D= 6 E = 4 D = 3 5 6 E = 3 D = 6 E = 2

15. TOPIC CHANGE:Exploring data

16. Define x and y and call the plot function

17. Engineers always add … • Title title(‘y = cos(x)’) • X axis label, complete with units xlabel(‘x-axis’) • Y axis label, complete with units ylabel(‘y-axix’) • Often it is useful to add a grid grid on Single quotes are used.

18. Creating multiple plots MATLAB overwrites the figure window every time you request a new plot To open a new figure window use the figure function – for examplefigure(2)

19. Create multiple lines on a single graph Each set of ordered pairs will produce a new line

20. If you want to create multiple plots, all with the same x value you can… x = 0:pi/100:2*pi; y1 = cos(x); y2 = cos(x)*2; y3 = cos(x)*4; y4 = cos(x)*6; • Use alternating sets of ordered pairs • plot(x,y1,x,y2,x,y3,x,y4) • Or group the y values into a matrix • z=[y1;y2;y3;y4] • plot(x,z)

21. Line, Color and Mark Style • You can change the appearance of your plots by selecting user defined • line styles • mark styles • color • Try usinghelp plotfor a list of available styles

22. Specify your choices in a string • For example plot(x, y, ':ok') • strings are identified with single quotes • the : means use a dotted line • the o means use a circle to mark each point • the letter k indicates that the graph should be drawn in black • (b indicates blue)

23. Available choices

24. specify the drawing parameters for each line after the ordered pairs that define the line