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

24
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

description

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. What is the final value of a[4]?. int i ; - PowerPoint PPT Presentation

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

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

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

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

What is the final value of a[4]?

int i;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

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

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 arrayc) it is able to modify the values stored in the array

d) all of the above are true

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

Fill in the blank to print 1 2 3 4 5?

#define SIZE 10void 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){ int i;  for(i=0; i<num; i++){ printf("%d ", a[i]); } printf("\n");}

a) data, 5 b) *data,5 c) data, SIZE d) data[SIZE]

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

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?

int x = 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

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

What does this print out?#include <stdio.h>int myFunction(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]); } int myFunction(int x[], int num){ x[1] = 4; x[2] = 7; return(x[1]+x[2]);}

c) 4, 7b) 5, 11a) 5, 2

d) 2, 11

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

SWITCHING GEARS TO MATLAB…

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

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 matrixmultiplication and in matrix multiplication you need the same number of columns in B as rows in A.

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

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 eachelement of B with the same element of A. In order touse ‘.*’, A and B need to be the same dimensions.

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

What does C store?

>>A = [1 2 3; 3 5 6;1 2 1]>>B = [2 4 ; 6 1]>>C = B.*A(2:3,1:2)

c) 4 12 30 6

b) 2 8 18 5

a) 6 20 6 2

d) 10 24 12 1

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

What does B store?

>>A = [1 2 3; 3 5 6;1 2 1]>>B = max(A)

c) 6 5 6

b) 3 6 2a) 6

d) 3 5 6

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

What does C store?

>>A = [1 2 3; 3 5 6;1 2 1]>>B = max(A)>>C=max(B)

c) 6 5 6

b) 3 6 2a) 6

d) 3 5 6

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

What does B and C store?

>>A = [1 2 3; 3 5 6;1 2 1]>>[B,C] = max(A)

c) B = 6 5 6 C = 1 1 1

b) B = 3 6 2 C = 3 3 2

a) B= 6 C = 4

d) B = 3 5 6 C = 2 2 2

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

What does D and E store?

>>A = [1 2 3; 3 5 6;1 2 1]>>[B,C] = max(A)>>[D,E] = max(B)

c) D = 6 E = 2

b) D = 6 E = 3

a) D= 6 E = 4

d) D = 3 5 6 E = 3

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

TOPIC CHANGE: Exploring data

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

Define x and y and call the plot function

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

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.

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

Creating multiple plotsMATLAB overwrites the figure window every time you request a new plotTo open a new figure window use the figure function – for example figure(2)

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

Create multiple lines on a single graph

Each set of ordered pairs will produce a new line

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

If you want to create multiple plots, all with the same x value you can…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)

x = 0:pi/100:2*pi; y1 = cos(x); y2 = cos(x)*2; y3 = cos(x)*4; y4 = cos(x)*6;

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

Line, Color and Mark StyleYou can change the appearance of your plots by selecting user defined line styles mark styles colorTry using

help plotfor a list of available styles

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

Specify your choices in a string

For exampleplot(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)

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

Available choicesTable 5. 2 Line, Mark and Color Options

Line Type Indicator Point Type Indicator Color Indicator

solid - point . blue b

dotted : circle o green g

dash-dot -. x-mark x red r

dashed -- plus + cyan c

star * magenta m

square s yellow y

diamond d black k

triangle down v

triangle up ^

triangle left <

triangle right >

pentagram p

hexagram h

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

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