CSE 130 Fall 2009 Lab 10 DO NOT SUBMIT THIS. THIS IS FOR YOUR PRACTICE ONLY. ---------------------------------------------------------------------- 0. Word count - We studied a word count program in class. Similar program is available on UNIX. The command is: wc file-name Try wc lab9.c or some other program that you may have. Sparky will respond with 3 numbers, line-count, word-count and character-count for that file. It counts lines by counting how many \n or `return' charac- ters are there. We know how to do a word count. Character count is just count of all characters in that file. 1. Pointers a. In this lab we are going to study pointers to basic types such as int, double and char. We will also study how array is passed to a function and accessed using pointers. Next, we write a func- tion for calculating average of all values stored in an array. Finally, we return the answer using a pointer. 2. Program for lab12.c b. Copy this program from class account. The command for that is cp ~cse130/lab10.c lab10.c This will copy lab12.c to your working directory. The other option is to Cut and paste program for lab10 in your pico window. Save it as lab10.c. c. Compile lab10.c and execute. It has two parts. In the first part, we work with some pointers. The second part just prints the array A after initialization. It also prints average = 0, because it has not been calculated yet. d. TA will explain both parts. e. Program also has a global variable size, used by all functions. // lab10.c // CSE 130 Fall 2009. Lab 10, Nov 18, 2009. // Sample program for studying pointers. #include void fillIt(int *someArray); //Prototype for fillIt. //Prototype for avg. const int size = 10; // Global variable size. int main(void) { // Part - I double d1 = 2.57, d2; int x = 23; // <-- Insert declaration for y int *px; // <-- Insert declaration for *py double *pd1, *pd2; // Pointers to double px = &x; pd1 = &d1; pd2 = &d2; // Initialize py. printf("\nx = %d, d = %f\n\n", x, d); printf("Address of d1 = %u, Address of d2 = %u\n\n", pd1, pd2); printf("Address of x = %u\n\n", px); // Insert code for modifying integers x, y. // Print x, y after modification. printf("End of Part-I\n\n"); // Part-II int A[size]; int i; float avg = 0; fillIt( A ); for(i=0; i < size; i++) printf("%d ", A[i]); printf("\n"); // Insert a function call for average. printf ("Average of array values is: %f\n\n", avg); return 0; } void fillIt( int *someArray ) { int i; for( i = 0; i < size; i++) { *someArray = 100 + i; someArray++; } } // Write code for function average. 3. Pointers to double and int f. See that program prints address of double variables d1 and d2 using pointers pd1 and pd2. They differ by 8. Ask TA why? It also prints address of variable x using pointer px. It is less by 4 when counted from address of d2. g. Now declare another integer y, and its pointer py as shown in the code. Initialize py to address of y (&y). h. Modify the statement that prints addresses. Print address of y using py. It is 4 less than that of x. Ask TA why? i. x initially contains 23. Using px (pointer to x), add 1 to x. *px = *px + 1; // same as x = x+1; Using pointer to y (py), calculate a new value for y. This is equal to new value of x - 10. *py = x - 10; // same as y = *px - 10; j. Insert a print statement for printing x and y, at the place shown in your program. k. Save, compile and execute lab12.c. Does it print new values of x and y. (x = 24, and y = 14). 4. Function fillIt l. TA will will discuss function fillIt. It initializes array of the main program. See how pointer someArray has been used. m. It is passed from main using call-by-reference. Note that A without square brackets is a pointer. n. Array A is printed in main after it has been initialized by func- tion fillIt. 5. Function average o. Write a function average for calculating average of all values in array A. p. Follow style of function fillIt. Use pointer declaration in the header. Return type float. float average( int *B) q. Here B is a pointer to any array that is passed to function aver- age. r. Write a for loop and calculate sum of all values. Use pointer notation. Declare i and sum as integer variables. s. At the end return (float)sum/size. t. In main insert a call to function average. Get the answer in variable avg. avg = average (A); u. Declare a prototype for function average, above main, at the place indicated. float average(int *B); // Prototype v. Print statement for printing average is there. Save, compile and execute. Program should print 104.5 7. Getting average back using a pointer. If you have some time left, try this modification. x. Add one more pointer to the function average, called answer. So the header becomes, void average(int *B, int *answer); y. Since we are getting answer back using a pointer, we do not need a return type float. Make it void. Use answer pointer to return average. *answer = (float)sum/size; z. Change the prototype as well. In main, call to function average becomes, average (B, &avg); Here address of avg is sent as a second parameter. Using that pointer, average is sent back. - Save, Compile and execute. You should get the same answer for average.