Download presentation
Presentation is loading. Please wait.
Published byArthur Bridges Modified over 8 years ago
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) 6d) 3
2
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) undefinedd) 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){ int i; for(i=0; i<num; i++){ printf("%d ", a[i]); } printf("\n"); } a) data, 5 b) *data,5 c) data, SIZEd) data[SIZE]
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? 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
6
What does this print out? #include 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, 7 b) 5, 11 a) 5, 2 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 ; 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
11
What does B store? >>A = [1 2 3; 3 5 6;1 2 1] >>B = max(A) c)6 5 6 b)3 6 2 a) 6 d)3 5 6
12
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 2 a) 6 d)3 5 6
13
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
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) c)D = 6 E = 2 b)D = 6 E = 3 a)D= 6 E = 4 d)D = 3 5 6 E = 3
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 example figure(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… 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;
21
Line, Color and Mark Style You can change the appearance of your plots by selecting user defined line styles mark styles color Try using help plot for 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 Table 5. 2 Line, Mark and Color Options Line TypeIndicatorPoint TypeIndicatorColorIndicator solid-point.blueb dotted:circleogreeng dash-dot-.x-markxredr dashed--plus+cyanc star*magentam squaresyellowy diamonddblackk triangle downv triangle up^ triangle left< triangle right> pentagramp hexagramh
24
specify the drawing parameters for each line after the ordered pairs that define the line
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.