DOC PREVIEW
UIUC CS 101 - lect1617

This preview shows page 1-2-24-25 out of 25 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 25 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 25 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 25 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 25 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 25 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Slide 1Slide 2Slide 3Slide 4Slide 5Slide 6Slide 7Slide 8Slide 10Slide 11Slide 12Slide 13Slide 14Slide 15Slide 16Slide 17Slide 18Slide 19Slide 20Slide 21Slide 22Slide 23Slide 24Slide 2516&17-2•Grasp the concept of top-down programming •Identify Function Headers and Prototypes•Understand when and where prototypes used•Understand how arguments are passed to a functionusing call-by-value or call-by-reference•Related Chapters: ABC Chapters 5.1 – 5.716&17-3In lecture slides 2-17 and 10-15 we discussed a method for creating programs. In step three “Develop the Algorithm”, the nature of the programming problem may suggest that we use top down programming (as opposed to bottom-up programming , ...)An analogy for top-down design is the following: Suppose a general contractor was hired to construct a commercial building. The contractor would need to know where to build and what are the blue-prints. Then the contractor would hire various sub-contractors to dig foundation, install electric, plumbing, frame, roof. That is, the large job would be broken into smaller jobs, however these jobs must be performed in some sequence and not at random.16&17-4In top-down design for programming, the first function to be invoked in a program (the top function or “general contractor”) is called “main”. The programmer has the “main” function call other component functions (sub-contractors), which may in turn call functions, (sub-sub-contractors) and so on.16&17-51. Problem DefinitionWrite a function “sinc” that computes the value of sin(x)/x . If x is zero return 1.0. 2. Refine, Generalize, Decompose the problem definition(i.e., identify sub-problems, I/O, etc.)Input = One real number of type double.Output= sin(x)/x as a double if x is not zero else 1.0.3. Develop AlgorithmUse an if statement to check for a non-zero value.16&17-6#include <stdio.h>#include <math.h>double sinc (double z) /* function header */{if (z == 0.0)return 1.0;else return sin(z) / z; /* notice that sinc calls sin */}void main(void){double x, y;printf("Enter a number: ");scanf("%lf", &x);y = sinc(x); /* function call */printf("\n sinc(%lf) = %lf \n", x, y);} /* end of main */16&17-7Using gedit, user-defined functions can be typed into the same file as “main”. Functions may be typed into a separate file. For example, suppose “main” is in the file test.c and the user defined function “sinc” is entered into the file func.c then to compile both “main” and “sinc” you would type (at the Unix prompt)gcc test.c func.c -lm Note the use of the “-lm” optionThe -lm option is needed only if you use a math library function in “main” or “sinc”.Don’t forget that if you put the function “sinc” into a separate file, then you must still put the prototype for “sinc” in the same file as main. The prototype for “sinc” must precede the function main.16&17-8• The function definition in C is of the form:return-type function-name(parameter-list){ /* function code */return (value); /* parenthesis optional */}• The “value” can be a single variable, constant, or any C expression.• The data type of “value” should be of type “return-type”, if not then a conversion of the “value” to the “return-type” is specified by the C compiler.• If the “return-type is void then the programmer can omit the return statement or code:return ; /* return nothing */16&17-9• A function may be called from any function (including itself) in C.• A call takes one of the following forms:function-name(argument-list);var = function-name(argument-list);• To “call” or invoke a function, you must pass the same number of arguments for which the function has parameters. The order of the arguments is important!!! • If the data types in the argument list do not match the data types in the parameter list, a conversion(or coercion) takes place. (see p. 132 in ABC)16&17-10/* code fragment in main */int x = 1;float y = 2.0;float value;double z = 5.0;value = sum(x,y,z+4.0);} /* end of main */ /* sum adds an integer a float and a double and returns *//* a double */double sum(int a, float b, double c){ double result; result = a + b + c; return result;}Data-types agree!Since value has typefloat and sum returnsa double, a conversiontakes place.16&17-11• A function that does not return a value is declared to be of type void. e.g.,void printFunction (int x , int y)• If a function takes no parameters, we specify the parameter list as void. e.g.,void printPageHeading (void)It is also possible to specify an "empty" parameter list:void myFunction ( )- argument checking is "turned off", so that anything can be passed to myFunction. Best to avoid this in C.16&17-12• By default , if you omit the data type of the return value, C assumes that the function returns an int value (not void). Therefore the following function header: main(void)int main(void)is equivalent to:16&17-131. Problem DefinitionRedo the problem in Lecture 15 slides 12,13 using a modular approach. The problem is:Write a program that inputs a list of numbers into an array. The program then calculates the average value, and then prints a list of differences. The differences are computed by taking the original values in the list minus the average. 2. Refine, Generalize, Decompose the problem definition(i.e., identify sub-problems, I/O, etc.)Input = list of reals in a file “input.dat” . We will use Unix redirection to read values from this file.Output= The average and the list of differences.#include <stdio.h>int read(double alias[]); /* prototype for read func */double ave(int count,double alias[]); /* prototype for ave function */void diff(double average, int count, double alias[]);/* prototype for diff function */void main(void){ int counter,k; double datVal[500]; /* 500 element max */ double datAve; counter = read(datVal); /* read file into datVal */ datAve = ave(counter,datVal); /* compute, print average */ printf("The average is:%f \n", datAve); diff(datAve,counter,datVal); /* compute diff list */ for(k=0;k<counter;++k) /* print the diff list */ printf("%lf\n", datVal[k]);} /* end of main */int read(double alias[]) { int count = 0; while (EOF != scanf("%lf", &alias[count])) { ++count; } return count;} double ave(int count,double alias[]){ int i; double sum = 0.0; for (i=0;i<count;++i) sum += alias[i]; return sum/count;}void diff(double average, int count,


View Full Document

UIUC CS 101 - lect1617

Documents in this Course
Notes

Notes

114 pages

lect2223

lect2223

35 pages

lect2223

lect2223

35 pages

lect1920

lect1920

23 pages

lect1920

lect1920

23 pages

lect1617

lect1617

25 pages

lect1314

lect1314

34 pages

lect1314

lect1314

34 pages

lect0607

lect0607

25 pages

lect0607

lect0607

25 pages

lect25

lect25

31 pages

lect24

lect24

15 pages

lect21

lect21

25 pages

lect21

lect21

25 pages

lect18

lect18

22 pages

lect18

lect18

22 pages

lect15

lect15

37 pages

lect15

lect15

37 pages

lect12

lect12

31 pages

lect12

lect12

31 pages

lect11

lect11

28 pages

lect11

lect11

28 pages

lect10

lect10

28 pages

lect09

lect09

24 pages

lect09

lect09

6 pages

lect08

lect08

23 pages

lect08

lect08

23 pages

lect05

lect05

26 pages

lect05

lect05

26 pages

lect04

lect04

36 pages

lect04

lect04

36 pages

lect03

lect03

26 pages

lect03

lect03

26 pages

lect02

lect02

36 pages

lect02

lect02

36 pages

lect01

lect01

32 pages

lect01

lect01

32 pages

lect00

lect00

23 pages

lect00

lect00

23 pages

Load more
Download lect1617
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view lect1617 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view lect1617 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?