Homework 8, due 24 November 2005.
Posted: Thu Nov 17, 2005 12:21 pm
- You will need to know how to implement arrays (or vectors) in a program to implement the approximate numerical methods for partial differential equations, so this problem is designed to step you through the process and warn you of the pitfalls before you need to write a program to solve a PDE.
While this problem may seem unrelated to the numerical methods material, it is practically guaranteed that you will save more time on problem 4 based upon what you learn from this problem than it takes to do this problem.
One way (not the only way) to make a variable an array in C is to simply declare its size at the beginning of the program. For example consider the following program that fills up a vector called x with 25 random numbers between 0 and 1.Code: Select all
#include<stdio.h> #include<stdlib.h> /* needed for rand() */ #define LENGTH 25 main() { int i; double y; double x[LENGTH]; y = 1.23456; /* Seed for random number generator. Change the 123 to a different integer to generate a different sequence of random numbers. */ srand(123); /* Before we assign any values to x, let's see what the components are. */ for(i=0;i<LENGTH;i++) printf("x[%d] = %f\n",i,x[i]); for(i=0;i<LENGTH;i++) { /* This makes x[i] an number between 0 and 1. */ x[i] = 1.0*rand()/(RAND_MAX+1.0); printf("x[%d] = %f\n",i,x[i]); } /* add your code here for the different problems */ }
- Copy and compile this code by typing "gcc filename.c". In the loop that prints the values of the components of x before they are assigned any values, are all the values zero? Lesson: when you declare an array, it is not necessarily cleared to be all zeros (sometimes it may be, though).
- Modify the code so that the list of 25 numbers is sorted from smallest to largest. Then print the sorted list. Change the value in the srand() function to change the list of random numbers to make sure it works for different sequences of numbers as well.
- Keeping LENGTH equal to 25, add the following two lines to the end of the above program (not your modified, sorting program):
Note that this prints x[0] through x[29] even though x really only goes up to x[24]. Does your program give you any warning either when it compiles or runs that it is going way past the end of the valid range for x? Take a close look at the array values greater than 25. Does any of them look like anything else in your program? On many computer architectures one of them will be the value for y! Lessons: in C, an array is simply a sequential segment in memory. It turns out that y was stored in memory after the x array at exactly the location that one of the x[25] through x[30] would have been if x were actually that long.
Code: Select all
for(i=0;i<LENGTH+5;i++) printf("x[%d] = %f\n",i,x[i]);
- Keeping LENGTH equal to 25, add the following line at the end of your program:
Change the 25 to be whatever the index was that printed the value for y. I checked and on the Sun machines, 25 is the correct number.
Code: Select all
x[25] = 2.3456; printf("y = %f\n",y);
When you compile or run your program does it give you any warning that you are assigning a value to a component of x that is not valid? Does the value for y change even though you were seemingly only changing a component of x? Lesson: it's very easy to screw up and change values beyond the valid range for an array. At a minimum, this won't have the effect you expect. Perhaps even worse, you could be changing the values of other variables!
- Section 10.5 numbers 18 and 22.
- Section 10.6 numbers 11 and 12. Note that bounday conditions for 12 were not covered in class, but it's a simple modification of the solution that is clearly covered in the book.
- Modify the wave equation code that was distributed in class to compute an approximate numerical solution to problems 18 (a) in Section 10.5 and problem 11 in Section 10.6 (5 points each). Submit your code for each and a plot of the temperature profile at a sufficient number of times to verify your analytical answer and provide an adequate representation of the nature of the solution to the problems.
Note: I did not yet get a chance to cover this in class, but it must be the case that alpha^2*dt/dx^2 be less than or equal to 1/2 for this finite difference approch to be stable. Check this by running your code for one of the problems with dt slightly smaller and slightly bigger than this value. The slightly bigger value should become unstable. - Optional extra credit: do this problem if you want to understand, rather than simply memorize, how vectors work in C.
- Read section 1.6 and chapter 5 in Kernighan and Ritchie (the recommend C programming book).
- Explain indetail why each print statement in the following program prints what it does.
Code: Select all
#include<stdio.h> main() { int i=0,*j,k=2; double x=0.0,z=11.0; double y[10] = {1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0}; /* fun with i, j and k */ j = &k; printf("%d\n%d\n%d\n",i,*j,k); *j -= 1; printf("%d\n%d\n%d\n",i,*j,k); printf("%#x\n%#x\n%#x\n",&i,j,&k); j = &i; printf("%#x\n%#x\n%#x\n",&i,j,&k); *j -= 1; printf("%d\n%d\n%d\n",i,*j,k); /* fun with x, y and z */ printf("%#x\n%#x\n%#x\n",&x,y,&z); for(i=0;i<11;i++) printf("%f\n",*(y+i)); printf("%d\n",&z-y); y[&z-y] = 12.0; printf("%f\n",z); }