reinforce

Upload: shipra-sharma

Post on 14-Apr-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/29/2019 Reinforce

    1/5

    1

    C PROGRAMMING: SOME ADDITIONAL PRACTICE

    Programming can be a frustrating experience. In many other endeavors, a small mistake is

    okay: You can afford a few mis-spellings on an essay and still get a 98%. But not so with

    programming. The tiniest error (an extra semicolon, a %d instead of %lf

    ) can completelydestroy an otherwise perfect program.

    On the other hand, this perfectionism is also an advantage. The computer will always

    perfectly follow your instruction. So one trick in learning to program is to try to turn

    the computers precise obedience into a virtue. Think carefully about what you want the

    program to do, and then carefully craft the instuctions to accomplish your goal. With care,

    your code is sure to work.

    At the end of this note I have appended Dr. Grotjahns Laws of Computing, written by

    a UCD Professor of Atmospheric Science. Law number 1 (The computer does not give

    partial credit) embodies the comment above. You will find the other laws useful as well.

    This afternoon we will talk some about programming, and answer any questions you have.

    If we finish with questions, we can write some additional practice codes. These additional

    codes are on the pages which follow. I begin each page with a statement of what your

    code should do. If you like, read no further and just try to write the necessary program.

    Alternately, look at the lines which follow and type them in. If you follow this second path,

    make sure you think carefully about what each line is doing.

  • 7/29/2019 Reinforce

    2/5

    2

    Write a program to print the first 20 integers, their square roots, squares, and cubes.

    #include

    #include

    int main(void)

    {

    int n=20,i;

    double sqrtofi;

    for (i=1;i

  • 7/29/2019 Reinforce

    3/5

    3

    -6 -4 -2 0 2 4 6x

    -40

    0

    40

    80

    120

    y

    y=3x2-5x-34

    The parabola y = 3x2 5x 34 is shown in

    the figure. y is positive at the far left of the

    horizontal axis (for example, x = 6). But y

    becomes positive as you move to the right.

    Write a program to determine the integer for

    which the parabola first becomes positive.

    #include

    #include

    int main(void)

    {

    int x=-30;

    double y;

    do{

    x=x+1;

    y=3.*x*x-5.*x-34.;

    }while(y>0);

    printf(" The first integer where y=3x^2-5x-34 becomes negative is x= %d\n",x);

    return 0;

    }

    Comments/Questions:

    [1] Can you change the code so that x decreases (comes from the right) again to find the

    first integer where y becomes negative?

    [2] Suppose you wanted to find the x value where y becomes negative more accurately. What

    would you do?

  • 7/29/2019 Reinforce

    4/5

    4

    Using a C program and xmgrace, make the plot which we used on page 3.

    #include

    #include

    int main(void)

    {

    FILE * fileout;

    fileout=fopen("parabola.dat","w");

    int n=300,i;

    double x,y,dx=0.02;

    for (i=-n;i

  • 7/29/2019 Reinforce

    5/5

    5

    Dr. Grotjahns Laws of Computing

    [1] The computer does not give partial credit.

    [2] Efficiency and ease of interpretation have an inverse relationship: the more you have of

    one the less you have of the other. (A clever, compact, piece of code, with multiple uses ofstorage and cryptic modules, may be very efficient, but a devil for anyone else to figure out.)

    [3] Programming languages provide many paths to the same correct answer.

    [4] The more options you have, the more ways you have for your program to go wrong.

    (Fewer options allows fewer types of errors.) Corollary: develop a good, standard way to do

    a common task; then use the same way each time to avoid making new types of errors.

    [5] The error displayed is not necessarily the error you made. (Dont stop looking for errors

    until you find one that can create the erroneous result.)

    [6] The worst errors always masquerade as correct code in a listing: subtle inconsistencies

    are harder to spot. (mistyped characters l vs 1, O vs 0; undefined variables due to subtle

    spelling errors, variables not passed to subprograms, etc.)

    [7] Never assume you know something is correct until you can prove it is correct. (While a

    listing may look like a variable is set in one part of the code, that value may be undone

    by an error someplace else.) Corrollary: print out everything relevant at the point where

    the error seems to occur.

    [8] The best hedge against frustration is .... concentration. (Try to get the code perfect

    in the first pass.)

    [9] The second best hedge against frustration is .... preparation. Have a good understanding

    of the problem being studied. (Know what answer to expect. Design simple tests with known

    answers. Choose tests that target small portions of the code at a time.)

    [10] Most programming time is spent duplicating known results: i.e. testing the code. Expect

    to spend comparatively little time actually calculating a new result.