Homework 7, due November 3, 2004.
Posted: Tue Oct 26, 2004 10:02 am
Unless otherwise indicated, all problems are from the course text, Boyce and DiPrima, Elementary Differential Equations and Boundary Value Problems, 8th edition.
- Section 10.1, numbers 1 and 14.
- Section 10.2, numbers 19 and 22.
- Section 10.3, number 1.
- 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.
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("%f\n",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 */ }
- (5 points) 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).
- (10 points) 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.
- (5 points) 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 x[26]. Does it look like anything else in your program? If you are doing this on a RedHat Linux machine and copied the above code exactly, it should 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 x[26] 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:
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!
Code: Select all
x[26] = 2.3456; printf("y = %f\n",y);
- (40 points) Optional: 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.
- 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); }