DOC PREVIEW
UIUC CS 101 - lect12

This preview shows page 1-2-14-15-30-31 out of 31 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 31 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 31 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 31 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 31 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 31 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 31 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Slide 1Slide 2Slide 3Slide 4Slide 5Slide 6Slide 7Slide 8Slide 9Slide 10Slide 11Slide 12Slide 13Slide 14Slide 15Slide 16Slide 17Slide 18Slide 19Slide 20Slide 21Slide 22Slide 23Slide 24Slide 25Slide 26Slide 27Slide 28Slide 29Slide 30Slide 3112-2•Know how if and switch C statements control the sequence of execution of statements.•Be able to use relational and logical operators in the conditional part of an if or a switch statement.•Related Chapters: ABC Chapter 4.1-4.7 & 4.1612-31. Problem DefinitionWrite a program that reads a number and computes the square root if the number is non-negative. 2. Refine, Generalize, Decompose the problem definition(i.e., identify subproblems, I/O, etc.)Input = real numberOutput=real number3. Develop Algorithm(processing steps to solve problem)12-4FlowchartPrint “enter value”Read value value >= 0.0Print sqrt(value)TrueFalse12-5/* C Program to compute the square root of a positive number */#include <stdio.h>#include <math.h>void main(void){ double value; /* Declare variables. */ /* request user input */ printf("Please enter a non-negative number :"); /* read value */ scanf("%lf", &value); /* Output the square root. */ if (value >= 0.0)printf("square_root(%lf) = %lf \n", value , sqrt(value));}12-6if(expression)statement;• if expression evaluates to true, the statement is executed; otherwise execution passes to the next statement in the program.• if Selection Structureif(value >= 0.0);printf("square_root(%lf) = %lf \n", value,sqrt(value));/* Error! Don’t put semicolon here *//* This is an example of a logical error */12-71. Problem DefinitionModify the previous program to notify the user when the input is invalid.12-8FlowchartPrint “enter value”Read value value >= 0.0Print sqrt(value);TrueFalsePrint “invalid input”12-9/* C Program to compute the square root of a positive number */#include <stdio.h>#include <math.h>void main(void){ double value; /* Declare variables. */ /*request user input*/ printf(”Please enter a non-negative number :”); scanf("%lf", &value); /* read value */ /* Output the square root. */ if (value >= 0.0) printf("square_root(%lf) = %lf \n", value,sqrt(value)); else printf("invalid user input, please enter non-negative value\n");}12-10- in header file math.hArguments (parameters) for each of the following functions are assumed to be of type double. If not, a type double copy is made for use in the function. To compile a program that contains math functions you need to use the -lm (Lm not 1m )option for gcc. > gcc file.c -lmSee next pagefabs (x) - |x| (not the same as the abs(x) function) sqrt (x) - square root of x pow (x, a) - xa exp (x) - ex (e = 2.718281828 …) log (x) - ln x = loge x log10 (x) - log10 x sin (x) - sine function (x in radians) cos (x) - cosine function (x in radians) tan (x) - tangent function (x in radians) ceil (x) - smallest integer >= x floor (x) - largest integer <= x12-12if (expression)statement1;elsestatement2;-if expression evaluates to true, statement1 is executed and execution skips statement2 -If expression evaluates to false, execution skips statement1 , statement2 is executed12-13We can also execute multiple statements when a given expression is true:if (expression) {statement1;statement2;statementn;}Example -if(b < a){temp = a;a = b;b = temp;}or...if (expression) {statement1;statementn;}else { statement1; statementm;}(what does this code do?)......12-141. Problem DefinitionModify the previous program to compute the following:You must check that the value is legal, i.e. value >= 1.0 or value <= -1.0 0.12value12-15FlowchartPrint “enter value”Read value value >= 1.0 or value <= -1.0Print sqrt(value*value -1.0);TrueFalsePrint “invalid input”12-16/* Compute the square root of value*value-1.0 */#include <stdio.h>#include <math.h>void main(void){ double value; /* Declare variables. */ /* request user input*/ printf("Please enter value >= 1.0 or <= -1.0 :"); scanf("%lf", &value); /* read value */ /* Output the square root. */ if ((value >= 1.0) || (value <= -1.0)) printf("square_root(%f) = %f \n", value,sqrt(value*value - 1.0)); else { printf("invalid user input\n"); printf("input should be a value >= 1.0 or <= -1.0 \n"); }}12-17In logical expressions (which evaluate to true or false), we can use the following Relational operators:RelationalOperatorType of Test == equal to (don’t use =) != not equal to > greater than >= greater than or equal to < less than <= less than or equal to12-18A B A && BA || BTRUE TRUE TRUE TRUETRUE FALSE FALSE TRUEFALSE TRUE FALSE TRUEFALSE FALSE FALSE FALSEA !ATRUE FALSEFALSE TRUE12-19if ( 5 ) printf("True"); /* prints True */In C the value for False is represented by the value zero and True is represented by any nonzero value. The value False can be any zero value such as the number 0 or 0.0 or null character ‘ \0 ’ or the NULL pointer.Example 2:int x = 0; /* x declared as an integer variable */ /* and initialized to the value 0 */ if (x = 0) /* note the error, == should be used */ printf(" x is zero\n"); /*message not printed, why?*/Example 1:12-20Avoid using == to test real numbers for equality!Exampledouble x = .3333333333; /* ten digits of 3s */if (x == 1.0/3.0) printf("equal!\n");else printf(" not equal!\n"); /* prints not equal! */if ( fabs(x - 1.0/3.0) < 1.0e-10 ) printf("equal!\n"); /* prints equal! */else printf(" not equal!\n");12-211. Problem DefinitionWrite a program that returns a letter grade based on a quiz score. The input will be the integer score from a 10 point quiz. The letter grades are assigned by:9 - 10 “A”7 - 8 “B”5 - 6 “C”3 - 4 “D”< 3 “F” 2. Refine, Generalize, Decompose the problem definition(i.e., identify subproblems, I/O, etc.)Input = integer scoreOutput=character “grade”3. Develop Algorithm(processing steps to solve problem)12-22FlowchartPrint “enter score”Read score score == 10 || score == 9Print “A”TrueFalse(continued on next slide)(skip else part of statement)12-23False score == 8 || score == 7Print “B”;True(skip else part of statement)(continued on next slide)False12-24False score == 6 || score == 5Print “C”;True(skip else part of statement)(continued on next slide)False12-25False score == 4 || score == 3Print “D”TrueFalsePrint “F”12-26/* C Program to compute


View Full Document

UIUC CS 101 - lect12

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

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

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 lect12
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 lect12 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 lect12 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?