## Homework 7, due November 3, 2004.

Due Wednesday, November 3, 2004. Grader: Abigail Mitchell (amitche3@nd.edu).
goodwine
Posts: 1596
Joined: Tue Aug 24, 2004 4:54 pm
Location: 376 Fitzpatrick
Contact:

### Homework 7, due November 3, 2004.

Unless otherwise indicated, all problems are from the course text, Boyce and DiPrima, Elementary Differential Equations and Boundary Value Problems, 8th edition.
1. Section 10.1, numbers 1 and 14.
2. Section 10.2, numbers 19 and 22.
3. Section 10.3, number 1.
4. 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 */

}

``````
1. (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).
2. (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.
3. (5 points) Keeping LENGTH equal to 25, add the following two lines to the end of the above program (not your modified, sorting program):

Code: Select all

``````for(i=0;i<LENGTH+5;i++)
printf("x[%d] = %f\n",i,x[i]);
``````
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.
4. Keeping LENGTH equal to 25, add the following line at the end of your program:

Code: Select all

``````x[26] = 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!
Be very careful to make sure your program always stays within the bounds of the valid range of components of a vector. The compiler won't warn you when you exceed it. Sometimes when you write past the valid range, your program may crash with a segmentation fault. In such a case you are lucky. If you are less fortunate, you program may seem to behave very strangely, but in the end it's probably your fault. This may seem like a shortcoming in C, and from the simplistic perspective of mathematical computations using vectors, perhaps it is, but it is exactly this sort of flexibility that makes C very powerful in other applications.
5. (40 points) Optional: do this problem if you want to understand, rather than simply memorize, how vectors work in C.
1. Read section 1.6 and chapter 5 in Kernighan and Ritchie.
2. 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);
}
``````
asayers

### Section 10.2 Problem 19

When finding an and bn, we integrate from -L to L. But doing so would make the integral zero. From the notes in class when you did a similar problem on Oct. 27, the length was 1 and you integrated from 0 to 1. Can we do the same in this case ie. 0 to 2 for both an and bn?
goodwine
Posts: 1596
Joined: Tue Aug 24, 2004 4:54 pm
Location: 376 Fitzpatrick
Contact:

### Re: Section 10.2 Problem 19

asayers wrote:When finding an and bn, we integrate from -L to L. But doing so would make the integral zero. From the notes in class when you did a similar problem on Oct. 27, the length was 1 and you integrated from 0 to 1. Can we do the same in this case ie. 0 to 2 for both an and bn?
If the integral is zero, that's o.k. Maybe a_n or b_n is supposed to be zero (it will be for the combination of sine or cosine and f(x) is odd, like class yesterday). You can go from 0 to L if the combination of f(x) and sin/cos is even. If not, then you have to go from -L to L.
Bill Goodwine, 376 Fitzpatrick
mightyduck
Perhaps some direction with the "putting the random numbers in order" problem? I'm having a lot of difficulty understanding what all the code means and would appreciate a little help!
goodwine
Posts: 1596
Joined: Tue Aug 24, 2004 4:54 pm
Location: 376 Fitzpatrick
Contact:
mightyduck wrote:Perhaps some direction with the "putting the random numbers in order" problem? I'm having a lot of difficulty understanding what all the code means and would appreciate a little help!
When the program gets to the end (where the comment says to put your code here) x[0] through x[24] will be an unsorted list of numbers between 0 and 1.

So, what will it take to find the smallest one? First loop through them all to find the smallest one and switch x[0] and the smallest one x[whatever]. So, now you know that x[0] is the smallest. Then maybe loop through x[1] to the end to find the smallest of the remaining ones, and that should be the second smallest. Swap that one with x[1]. Repeat 23 more times. You should be able to do it with just two for() loops.
Bill Goodwine, 376 Fitzpatrick
mightyduck
I'm sorry, but I'm still having troubles with the sorting thing. The last tip makes sense, but I apparently don't know enough to actually implement it into a code. Is there anything else that can help me?
goodwine
Posts: 1596
Joined: Tue Aug 24, 2004 4:54 pm
Location: 376 Fitzpatrick
Contact:
mightyduck wrote:I'm sorry, but I'm still having troubles with the sorting thing. The last tip makes sense, but I apparently don't know enough to actually implement it into a code. Is there anything else that can help me?
Perhaps what you are missing is the syntax for the if() command. The following checks if the variable a is greater than b:

Code: Select all

``````#include<stdio.h>

main() {
double x=2.0,y=3.0;
if(x > y) {
printf("x is greater than y.\n");
}
else if(x == y) {
printf("x is equal to y.\n");
}
else {
printf("y is greater than x.\n");
}

}
``````
The "else" parts are optional. Note: if you want to check if something is equal to something else, you need to have two equal signs. If you want to do "greater than or equal to" replace the ">" signs with ">=".
Bill Goodwine, 376 Fitzpatrick
NDChevy07

### Problem 5

What do you want us to turn in for problem 5? Do you want the code for each part, or the listing of outputs for each part? It says they're each worth points, but it seems like a tutorial rather than a homework, so I just wanted to know what to turn in as proof that we did it.
lisaturtle

### Minimum function?

I know there is a minimum function for C called min, but I can't use it in my program. Is there a way to use it, or is there another way to find the smallest value in a vector?
goodwine
Posts: 1596
Joined: Tue Aug 24, 2004 4:54 pm
Location: 376 Fitzpatrick
Contact:

### Re: Problem 5

NDChevy07 wrote:What do you want us to turn in for problem 5? Do you want the code for each part, or the listing of outputs for each part? It says they're each worth points, but it seems like a tutorial rather than a homework, so I just wanted to know what to turn in as proof that we did it.
Just submit things like "The first print statement will print xxx because yyy. The second print statment will print zzz because aaa." etc.
Bill Goodwine, 376 Fitzpatrick
goodwine
Posts: 1596
Joined: Tue Aug 24, 2004 4:54 pm
Location: 376 Fitzpatrick
Contact:

### Re: Minimum function?

lisaturtle wrote:I know there is a minimum function for C called min, but I can't use it in my program. Is there a way to use it, or is there another way to find the smallest value in a vector?
I'm not aware of a min() function in C (there is one in matlab). I think that you'll have to loop through all the elements of x to find the smallest one, then repeat the procedure enough times to get them sorted.
Bill Goodwine, 376 Fitzpatrick
monkeybrains

### Min function in C

The min function in c is floor(). It will find the min value of an array if you put the name inside the parenthesis.
goodwine
Posts: 1596
Joined: Tue Aug 24, 2004 4:54 pm
Location: 376 Fitzpatrick
Contact:

### Re: Min function in C

monkeybrains wrote:The min function in c is floor(). It will find the min value of an array if you put the name inside the parenthesis.
You can try it, but I'm pretty sure that what floor() does is truncate a float, not find the minimum value in an array.

Here's the manual page for floor() (type "man floor" in a terminal):
FLOOR(3) Linux Programmer's Manual FLOOR(3)

NAME
floor, floorf, floorl - largest integral value not greater than argument

SYNOPSIS
#include <math.h>

double floor(double x);
float floorf(float x);
long double floorl(long double x);

DESCRIPTION
These functions round x down to the nearest integer.

RETURN VALUE
The rounded integer value. If x is integral or infinite, x itself is returned.

ERRORS
No errors other than EDOM and ERANGE can occur. If x is NaN, then NaN is returned and errno may be set to EDOM.

NOTES
SUSv2 and POSIX 1003.1-2001 contain text about overflow (which might set errno to ERANGE, or raise an exception). In
practice, the result cannot overflow on any current machine, so this error-handling stuff is just nonsense. (More pre-
cisely, overflow can happen only when the maximum value of the exponent is smaller than the number of mantissa bits.
For the IEEE-754 standard 32-bit and 64-bit floating point numbers the maximum value of the exponent is 128 (resp.
1024), and the number of mantissa bits is 24 (resp. 53).)

CONFORMING TO
The floor() function conforms to SVID 3, POSIX, BSD 4.3, ISO 9899. The other functions are from C99.

ceil(3), lrint(3), nearbyint(3), rint(3), round(3), trunc(3)

2001-05-31 FLOOR(3)
Bill Goodwine, 376 Fitzpatrick
Abigail

### AME 301 Homework 7: Solutions are posted!

Here are the solutions to Homework 7, the C code for Problem 4 and the C code for Problem 5.

Enjoy... and best of luck on tomorrow's exam!
goodwine