Engineering H 192 Computer Programming Arrays and Pointers

  • Slides: 26
Download presentation
 Engineering H 192 - Computer Programming Arrays and Pointers Lecture 15 The Ohio

Engineering H 192 - Computer Programming Arrays and Pointers Lecture 15 The Ohio State University Gateway Engineering Education Coalition 1

 Engineering H 192 - Computer Programming Arrays and Pointers • So far, we

Engineering H 192 - Computer Programming Arrays and Pointers • So far, we have looked at pointer variables which were declared with statements like: FILE *fptr ; int *aptr ; • We have also used pointer constants when we sent the address of a variable to a function. We did this by placing an ampersand before the variable name. For instance, the address of variable a is &a, which is a pointer constant. Winter Quarter The Ohio State University Gateway Engineering Education Coalition 2

 Engineering H 192 - Computer Programming Arrays and Pointers • The name of

Engineering H 192 - Computer Programming Arrays and Pointers • The name of an array without a subscript (index number) is also a pointer constant. When the name is used without the subscript it references the address of element 0 of the array. int myarray[10]; /* Declare an array */ int *myptr; /* Declare a pointer (not initialized)*/ printf (“%dn”, myarray); /* print address of myarray[0] */ scanf (“%d”, myarray); /* get value from keyboard and store in myarray[0] */ scanf (“%d”, &myarray[0]); /* same thing as above */ Winter Quarter The Ohio State University Gateway Engineering Education Coalition 3

 Engineering H 192 - Computer Programming Arrays and Pointers myptr = &myarray[2]; /*

Engineering H 192 - Computer Programming Arrays and Pointers myptr = &myarray[2]; /* Assign the address of the third element of myarray to myptr */ printf(“%d”, *myptr); /* Print the value of what myptr is pointing to, i. e. , the value of myarray[2] */ • Note that the * in front of myptr de-references the pointer. That is, it says “use the value in the address that is pointed to. ” • The following shows a small program and its output. Winter Quarter The Ohio State University Gateway Engineering Education Coalition 4

 Engineering H 192 - Computer Programming Arrays and Pointers /* Printing array values

Engineering H 192 - Computer Programming Arrays and Pointers /* Printing array values and addresses */ #include <stdio. h> int main ( ) { int k; float a[4] = {1000, 2000, 3000, 4000}; printf ("k a &a[k]n"); for (k=0; k<4; k++) printf ("%d %ld %fn", k, a, &a[k], a[k]); } Winter Quarter The Ohio State University Gateway Engineering Education Coalition 5

 Engineering H 192 - Computer Programming Running the Program r 1 tel (~)

Engineering H 192 - Computer Programming Running the Program r 1 tel (~) millerm 52> a. out k 0 1 2 3 a 2147462916 &a[k] 2147462916 2147462920 2147462924 2147462928 a[k] 1000. 000000 2000. 000000 3000. 000000 4000. 000000 r 1 tel (~) millerm 53> Winter Quarter The Ohio State University Gateway Engineering Education Coalition 6

 Engineering H 192 - Computer Programming Arrays and Pointers • We have seen

Engineering H 192 - Computer Programming Arrays and Pointers • We have seen how to pass a pointer for a single valued variable to a function. • Sometimes we want to pass an entire array to a function. The name of an array without a subscript is a pointer constant that contains the address of element [ 0 ] of the array. Therefore, if we pass the array name with no subscript to a function, we are passing a pointer to that array. • The following program illustrates this. Winter Quarter The Ohio State University Gateway Engineering Education Coalition 7

 Engineering H 192 - Computer Programming Arrays and Pointers /* Passing an array

Engineering H 192 - Computer Programming Arrays and Pointers /* Passing an array to a function */ #include <stdio. h> #include <string. h> void myfunct (int , char [ ]); int main ( ) { char name[20] = "Michael J. Miller"; myfunct (20, name); } Winter Quarter The Ohio State University Gateway Engineering Education Coalition 8

 Engineering H 192 - Computer Programming Arrays and Pointers void myfunct (int len

Engineering H 192 - Computer Programming Arrays and Pointers void myfunct (int len , char text[ ]) { int k ; printf ("%dn", strlen(text)) ; for (k = 0 ; k < len ; k++) printf ("%c", text[k]) ; } /*Program Output */ 17 Michael J. Miller Winter Quarter The Ohio State University Gateway Engineering Education Coalition 9

 Engineering H 192 - Computer Programming Arrays and Pointers • Given the declaration

Engineering H 192 - Computer Programming Arrays and Pointers • Given the declaration int a[3] = { 1, 2, 3 } ; a is a pointer to (the address of) a[0] &a[0] is a pointer to a[0] is the value 1 ( *a is also the value 1) &a[1] is a pointer to a[1] is the value 2 &a[2] is a pointer to a[2] is the value 3 &a[0]+1 is a pointer to a[1] &a[0]+2 is a pointer to a[2] Winter Quarter The Ohio State University Gateway Engineering Education Coalition 10

 Engineering H 192 - Computer Programming Arrays and Pointers Given the declaration int

Engineering H 192 - Computer Programming Arrays and Pointers Given the declaration int b[3][3] = {{1, 3, 5}, {7, 9, 11}, {13, 15, 17}}; b b[0] b[1] b[2] *b *b[1] *b[2] + 1 Winter Quarter is a pointer to b[0][0] is also a pointer to b[0][0] is a pointer to b[1][0] is a pointer to b[2][0] is a pointer to b[0] (special case) is the value of b[1][0] (which is 7) is the value of b[2][0] + 1 (which is 14) The Ohio State University Gateway Engineering Education Coalition 11

 Engineering H 192 - Computer Programming /* Double subscript arrays and user-written functions

Engineering H 192 - Computer Programming /* Double subscript arrays and user-written functions */ #include <stdio. h> void printarray (int [ ][7], int); int main( ) { int calendar[5][7]={{1, 2, 3, 4, 5, 6, 7}, {8, 9, 10, 11, 12, 13, 14}, {15, 16, 17, 18, 19, 20, 21}, {22, 23, 24, 25, 26, 27, 28}, {29, 30, 31, 32, 33, 34, 35}} ; printarray (calendar , 5); } Winter Quarter The Ohio State University Gateway Engineering Education Coalition 12

 Engineering H 192 - Computer Programming void printarray (int cal[][7], int j) {

Engineering H 192 - Computer Programming void printarray (int cal[][7], int j) { int k, n ; for (k = 0 ; k < j ; k++) { for (n = 0 ; n < 7 ; n++) printf ("%3 d ", cal[k][n]); printf ("n"); } } Winter Quarter The Ohio State University Gateway Engineering Education Coalition 13

 Engineering H 192 - Computer Programming /* Double subscript arrays, user-written functions and

Engineering H 192 - Computer Programming /* Double subscript arrays, user-written functions and pointer arithmetic */ #include <stdio. h> void printarray (int * , int); int main ( ) { int calendar[5][7]= {{1, 2, 3, 4, 5, 6, 7}, {8, 9, 10, 11, 12, 13, 14}, {15, 16, 17, 18, 19, 20, 21}, {22, 23, 24, 25, 26, 27, 28}, {29, 30, 31, 32, 33, 34, 35}}; printarray (calendar[0] , 5 , 7); } Winter Quarter The Ohio State University Gateway Engineering Education Coalition 14

 Engineering H 192 - Computer Programming void printarray (int *cal, { for (k

Engineering H 192 - Computer Programming void printarray (int *cal, { for (k = 0 ; k < j*m ; { for (n = 0 ; n < m ; printf ("%3 d ", printf ("n"); } } Winter Quarter int j, int m) k += m) n++) *(cal+k+n)); The Ohio State University Gateway Engineering Education Coalition 15

 Engineering H 192 - Computer Programming Assignment G 13 • A data file

Engineering H 192 - Computer Programming Assignment G 13 • A data file named g 13. dat exists in the class "common area" on the UNIX system. • The file contains actual data from one test of an instrumented bicycle from an engineering handson lab experiment. • You should look at the file on the screen (with a "more" command), but DO NOT print it out, as it contains several thousand (but less than 12, 000) lines of data. Winter Quarter The Ohio State University Gateway Engineering Education Coalition 16

 Engineering H 192 - Computer Programming Assignment G 13 • You will note

Engineering H 192 - Computer Programming Assignment G 13 • You will note that at the beginning of the file there are several lines of "header" information followed by many lines of data in four columns. • Count by hand the number of lines from the beginning of the file until you get to a line that has the actual data in the four columns. (You will need this number in Step 2 later. ) • The fourth column is the raw data (voltage) values from the lab experiment. Winter Quarter The Ohio State University Gateway Engineering Education Coalition 17

 Engineering H 192 - Computer Programming Steps for G 13 • Write a

Engineering H 192 - Computer Programming Steps for G 13 • Write a complete C program, (say, g 13. cpp), which does the following: – Opens the data file for input. – Input the correct number of header lines one by one, display each one on the screen and print each one to a result file (say, g 13 res. dat), and then discard the information. Winter Quarter The Ohio State University Gateway Engineering Education Coalition 18

 Engineering H 192 - Computer Programming Steps for G 13 • For opening

Engineering H 192 - Computer Programming Steps for G 13 • For opening the data file and the output file, just use the usual fopen routine. • For reading the header lines, it might be rather convenient to read the complete line into a character array or string. – How long should this string be? – What routine might be used? Winter Quarter The Ohio State University Gateway Engineering Education Coalition 19

 Engineering H 192 - Computer Programming Steps for G 13 – Input each

Engineering H 192 - Computer Programming Steps for G 13 – Input each of the lines of data arranged in the four columns, discarding the data values from each of the first three columns and storing only the data from the fourth column in a onedimensional array. For skipping over the columns with unwanted data, you will need to use the assignment suppression operator, *, in the scanf format. Your program will need to detect the end-of-file (EOF) to know when to stop inputting data. Close the input file when you reach the EOF. Winter Quarter The Ohio State University Gateway Engineering Education Coalition 20

 Engineering H 192 - Computer Programming Data from g 13. dat " "Source

Engineering H 192 - Computer Programming Data from g 13. dat " "Source File: C: PROGRA~1PSLOG195 RIDE. PL 1 "ID: IE Group 4 Bike Stress friday XR 440 12 bi "Ch 1 lbl/scl: Start/Stop /A "Ch 2 lbl/scl: input voltage signal /-5. 000 05. 000 "Ch 3 lbl/scl: Off /C "Ch 4 lbl/scl: Off /C "Rate (mins): 0. 0000833333324 Bat: 8. 8 "First: Fri 20 -Nov-1998 10: 38: 47 am "Last: Fri 20 -Nov-1998 10: 39: 40 am "Transferred: Fri 20 -Nov-1998 10: 42: 37 am "Eq. PC time: Fri 20 -Nov-1998 10: 42: 38 AM Winter Quarter The Ohio State University Gateway Engineering Education Coalition 21

 Engineering H 192 - Computer Programming Data from g 13. dat "Rate (mins):

Engineering H 192 - Computer Programming Data from g 13. dat "Rate (mins): 0. 0000833333324 Bat: 8. 8 "First: Fri 20 -Nov-1998 10: 38: 47 am "Last: Fri 20 -Nov-1998 10: 39: 40 am "Transferred: Fri 20 -Nov-1998 10: 42: 37 am "Eq. PC time: Fri 20 -Nov-1998 10: 42: 38 AM " Date, Time, Ch 1: Deg F, Ch 2: 11/20/1998 10: 38: 47. 000 -4. 989 0. 238 11/20/1998 10: 38: 47. 005 -4. 989 0. 231 11/20/1998 10: 38: 47. 010 -4. 989 0. 228 11/20/1998 10: 38: 47. 015 -4. 989 0. 231 11/20/1998 10: 38: 47. 020 -4. 989 0. 228 Winter Quarter The Ohio State University Gateway Engineering Education Coalition 22

 Engineering H 192 - Computer Programming Processing the G 13 Data File •

Engineering H 192 - Computer Programming Processing the G 13 Data File • For skipping unwanted columns, we need to use the "assignment suppression operator" in the format specification: fscanf (infile, "%*s%*s%*s%f", &data_val[ i ] ) ; • Remember, to check for EOF, you could use the feof function. Winter Quarter The Ohio State University Gateway Engineering Education Coalition 23

 Engineering H 192 - Computer Programming Assignment G 13 • Find the largest

Engineering H 192 - Computer Programming Assignment G 13 • Find the largest value in the array and the smallest value in the array. • Display the results on the screen, and also write the results to the output file, g 13 result. dat. The results to be displayed and printed are: – The total number of data points in the file – The maximum voltage and time at which it occurred – The minimum voltage and time at which it occurred – The elapsed time between the maximum and minimum values Winter Quarter The Ohio State University Gateway Engineering Education Coalition 24

 Engineering H 192 - Computer Programming Assignment G 13 • The change in

Engineering H 192 - Computer Programming Assignment G 13 • The change in the output voltage is related to the strain of the strain gage by: • where: ΔVout = V in * A * Sg*ε V in= 5. 0 Volts ε is the strain A = 500 (amplification) ΔVout is the change in voltage Sg = 2. 085 (gage factor) The equation can be solved to give the strain as a function of the output voltage Winter Quarter The Ohio State University Gateway Engineering Education Coalition 25

 Engineering H 192 - Computer Programming Assignment 13 • Stress-Strain: Hooke’s Law •

Engineering H 192 - Computer Programming Assignment 13 • Stress-Strain: Hooke’s Law • The stress(σ) in the bicycle fork can be calculated from the strain (ε), by using Hooke’s Law: σ = E * ε • Where E is the Modulus of Elasticity • For the bike fork material E = 29. 0 x 106 psi • The yield stress = 36, 000 psi Winter Quarter The Ohio State University Gateway Engineering Education Coalition 26