Download presentation
Presentation is loading. Please wait.
2
Lectures on Numerical Methods1 An Example zCompute Squares çPrints a table of squares as shown below1 24 39 416 525 636 749 864 981 10100 /* Compute Squares */ #include main() { int n, n_square; int lower, upper, step; lower = 1;/* Lower imit */ upper = 10;/* Upper limit */ step = 1; n = lower; while ( n <= upper ) { n_square = n * n; printf(“%3d\t%6d\n”,n,n_square); n = n + 1; } /* Compute Squares */ #include main() { int n, n_square; int lower, upper, step; lower = 1;/* Lower imit */ upper = 10;/* Upper limit */ step = 1; n = lower; while ( n <= upper ) { n_square = n * n; printf(“%3d\t%6d\n”,n,n_square); n = n + 1; }
3
Lectures on Numerical Methods2 An Example zFunctions çC programs contain functions and variables çFunction names are followed by a list of arguments enclosed in parenthesis. çFunction definition consists of a group of statements enclosed in curly brackets. printf is a library function. The information about this function is contained in a file named stdio.h. /* Compute Squares */ #include main() { int n, n_square; int lower, upper, step; lower = 1;/* Lower imit */ upper = 10;/* Upper limit */ step = 1; n = lower; while ( n <= upper ) { n_square = n * n; printf(“%3d\t%6d\n”,n,n_square); n = n + 1; } /* Compute Squares */ #include main() { int n, n_square; int lower, upper, step; lower = 1;/* Lower imit */ upper = 10;/* Upper limit */ step = 1; n = lower; while ( n <= upper ) { n_square = n * n; printf(“%3d\t%6d\n”,n,n_square); n = n + 1; }
4
Lectures on Numerical Methods3 An Example zVariables çVariables refer the objects that can be stored in computer’s memory locations. Variables are named (identifiers) çVariables can store data of various types. Some basic datatypes are char characters (1/2 bytes) int integers (2/4 bytes) float rationals (4 bytes) double rationals(8 bytes) çVariables must be declared and initialized. /* Compute Squares */ #include main() { int n, n_square; int lower, upper, step; lower = 1;/* Lower imit */ upper = 10;/* Upper limit */ step = 1; n = lower; while ( n <= upper ) { n_square = n * n; printf(“%3d\t%6d\n”,n,n_square); n = n + 1; } /* Compute Squares */ #include main() { int n, n_square; int lower, upper, step; lower = 1;/* Lower imit */ upper = 10;/* Upper limit */ step = 1; n = lower; while ( n <= upper ) { n_square = n * n; printf(“%3d\t%6d\n”,n,n_square); n = n + 1; }
5
Lectures on Numerical Methods4 An Example zDeclarations çDeclarations have form type var1, var2, var3; çMerely announces the data type to be associated with a variable. çInitial value of variable is not known. çExamples char name; int numStudents; float applePrice; double veryAccurateVar; /* Compute Squares */ #include main() { int n, n_square; int lower, upper, step; lower = 1;/* Lower imit */ upper = 10;/* Upper limit */ step = 1; n = lower; while ( n <= upper ) { n_square = n * n; printf(“%3d\t%6d\n”,n,n_square); n = n + 1; } /* Compute Squares */ #include main() { int n, n_square; int lower, upper, step; lower = 1;/* Lower imit */ upper = 10;/* Upper limit */ step = 1; n = lower; while ( n <= upper ) { n_square = n * n; printf(“%3d\t%6d\n”,n,n_square); n = n + 1; }
6
Lectures on Numerical Methods5 An Example zAssignments Statement çThe assignment statements have the following form someVar = expression ; çExpressions consist of variables, functions and operators. çExamples c = 5 * ( f – 32 ) / 9 s = sin(x) / x y = log(x) + v0 I = P * R * T / 100 Tax = 0.3 * ( I – 60000 ) /* Compute Squares */ #include main() { int n, n_square; int lower, upper, step; lower = 1;/* Lower imit */ upper = 10;/* Upper limit */ step = 1; n = lower; while ( n <= upper ) { n_square = n * n; printf(“%3d\t%6d\n”,n,n_square); n = n + 1; } /* Compute Squares */ #include main() { int n, n_square; int lower, upper, step; lower = 1;/* Lower imit */ upper = 10;/* Upper limit */ step = 1; n = lower; while ( n <= upper ) { n_square = n * n; printf(“%3d\t%6d\n”,n,n_square); n = n + 1; }
7
Lectures on Numerical Methods6 An Example zLoops çThe form of the while statement is While ( condition ) statement ; While ( condition ) { statements } çIf the condition is true the statements in the body of the while loop are executed. At the end of the loop the the condition is tested again and executes the loop if the condition is true. This procedure continues till the condition fails to be true. çConditions are usually logical expressions using operators like ==,, >= etc. These have boolean value /* Compute Squares */ #include main() { int n, n_square; int lower, upper, step; lower = 1;/* Lower imit */ upper = 10;/* Upper limit */ step = 1; n = lower; while ( n <= upper ) { n_square = n * n; printf(“%3d\t%6d\n”,n,n_square); n = n + 1; } /* Compute Squares */ #include main() { int n, n_square; int lower, upper, step; lower = 1;/* Lower imit */ upper = 10;/* Upper limit */ step = 1; n = lower; while ( n <= upper ) { n_square = n * n; printf(“%3d\t%6d\n”,n,n_square); n = n + 1; }
8
Lectures on Numerical Methods7 An Example zExecution /* Compute Squares */ #include main() { int n, n_square; int lower, upper, step; lower = 1;/* Lower imit */ upper = 10;/* Upper limit */ step = 1; n = lower; while ( n <= upper ) { n_square = n * n; printf(“%3d\t%6d\n”,n,n_square); n = n + 1; } /* Compute Squares */ #include main() { int n, n_square; int lower, upper, step; lower = 1;/* Lower imit */ upper = 10;/* Upper limit */ step = 1; n = lower; while ( n <= upper ) { n_square = n * n; printf(“%3d\t%6d\n”,n,n_square); n = n + 1; } VariableBefore exeution After execution n ?? n_square ?? lower ?? upper ?? step ??
9
Lectures on Numerical Methods8 An Example zExecution /* Compute Squares */ #include main() { int n, n_square; int lower, upper, step; lower = 1;/* Lower imit */ upper = 10;/* Upper limit */ step = 1; n = lower; while ( n <= upper ) { n_square = n * n; printf(“%3d\t%6d\n”,n,n_square); n = n + 1; } /* Compute Squares */ #include main() { int n, n_square; int lower, upper, step; lower = 1;/* Lower imit */ upper = 10;/* Upper limit */ step = 1; n = lower; while ( n <= upper ) { n_square = n * n; printf(“%3d\t%6d\n”,n,n_square); n = n + 1; } VariableBefore exeution After execution n ?? n_square ?? lower ?? upper ?? step ??
10
Lectures on Numerical Methods9 An Example zExecution /* Compute Squares */ #include main() { int n, n_square; int lower, upper, step; lower = 1;/* Lower imit */ upper = 10;/* Upper limit */ step = 1; n = lower; while ( n <= upper ) { n_square = n * n; printf(“%3d\t%6d\n”,n,n_square); n = n + 1; } /* Compute Squares */ #include main() { int n, n_square; int lower, upper, step; lower = 1;/* Lower imit */ upper = 10;/* Upper limit */ step = 1; n = lower; while ( n <= upper ) { n_square = n * n; printf(“%3d\t%6d\n”,n,n_square); n = n + 1; } VariableBefore exeution After execution n ?? n_square ?? lower ??1 upper ?? step ??
11
Lectures on Numerical Methods10 An Example zExecution /* Compute Squares */ #include main() { int n, n_square; int lower, upper, step; lower = 1;/* Lower imit */ upper = 10;/* Upper limit */ step = 1; n = lower; while ( n <= upper ) { n_square = n * n; printf(“%3d\t%6d\n”,n,n_square); n = n + 1; } /* Compute Squares */ #include main() { int n, n_square; int lower, upper, step; lower = 1;/* Lower imit */ upper = 10;/* Upper limit */ step = 1; n = lower; while ( n <= upper ) { n_square = n * n; printf(“%3d\t%6d\n”,n,n_square); n = n + 1; } VariableBefore exeution After execution n ?? n_square ?? lower 11 upper ??10 step ??
12
Lectures on Numerical Methods11 An Example zExecution /* Compute Squares */ #include main() { int n, n_square; int lower, upper, step; lower = 1;/* Lower imit */ upper = 10;/* Upper limit */ step = 1; n = lower; while ( n <= upper ) { n_square = n * n; printf(“%3d\t%6d\n”,n,n_square); n = n + 1; } /* Compute Squares */ #include main() { int n, n_square; int lower, upper, step; lower = 1;/* Lower imit */ upper = 10;/* Upper limit */ step = 1; n = lower; while ( n <= upper ) { n_square = n * n; printf(“%3d\t%6d\n”,n,n_square); n = n + 1; } VariableBefore exeution After execution n ?? n_square ?? lower 11 upper 10 step ??1
13
Lectures on Numerical Methods12 An Example zExecution /* Compute Squares */ #include main() { int n, n_square; int lower, upper, step; lower = 1;/* Lower imit */ upper = 10;/* Upper limit */ step = 1; n = lower; while ( n <= upper ) { n_square = n * n; printf(“%3d\t%6d\n”,n,n_square); n = n + 1; } /* Compute Squares */ #include main() { int n, n_square; int lower, upper, step; lower = 1;/* Lower imit */ upper = 10;/* Upper limit */ step = 1; n = lower; while ( n <= upper ) { n_square = n * n; printf(“%3d\t%6d\n”,n,n_square); n = n + 1; } VariableBefore exeution After execution n ??1 n_square ?? lower 11 upper 10 step 11
14
Lectures on Numerical Methods13 An Example zExecution n <= upper is true. /* Compute Squares */ #include main() { int n, n_square; int lower, upper, step; lower = 1;/* Lower imit */ upper = 10;/* Upper limit */ step = 1; n = lower; while ( n <= upper ) { n_square = n * n; printf(“%3d\t%6d\n”,n,n_square); n = n + 1; } /* Compute Squares */ #include main() { int n, n_square; int lower, upper, step; lower = 1;/* Lower imit */ upper = 10;/* Upper limit */ step = 1; n = lower; while ( n <= upper ) { n_square = n * n; printf(“%3d\t%6d\n”,n,n_square); n = n + 1; } VariableBefore exeution After execution n 11 n_square ?? lower 11 upper 10 step 11
15
Lectures on Numerical Methods14 An Example zExecution n <= upper is true. /* Compute Squares */ #include main() { int n, n_square; int lower, upper, step; lower = 1;/* Lower imit */ upper = 10;/* Upper limit */ step = 1; n = lower; while ( n <= upper ) { n_square = n * n; printf(“%3d\t%6d\n”,n,n_square); n = n + 1; } /* Compute Squares */ #include main() { int n, n_square; int lower, upper, step; lower = 1;/* Lower imit */ upper = 10;/* Upper limit */ step = 1; n = lower; while ( n <= upper ) { n_square = n * n; printf(“%3d\t%6d\n”,n,n_square); n = n + 1; } VariableBefore exeution After execution n 11 n_square ??1 lower 11 upper 10 step 11
16
Lectures on Numerical Methods15 An Example zExecution n <= upper is true. /* Compute Squares */ #include main() { int n, n_square; int lower, upper, step; lower = 1;/* Lower imit */ upper = 10;/* Upper limit */ step = 1; n = lower; while ( n <= upper ) { n_square = n * n; printf(“%3d\t%6d\n”,n,n_square); n = n + 1; } /* Compute Squares */ #include main() { int n, n_square; int lower, upper, step; lower = 1;/* Lower imit */ upper = 10;/* Upper limit */ step = 1; n = lower; while ( n <= upper ) { n_square = n * n; printf(“%3d\t%6d\n”,n,n_square); n = n + 1; } VariableBefore exeution After execution n 11 n_square 11 lower 11 upper 10 step 11
17
Lectures on Numerical Methods16 An Example zExecution n <= upper is true. /* Compute Squares */ #include main() { int n, n_square; int lower, upper, step; lower = 1;/* Lower imit */ upper = 10;/* Upper limit */ step = 1; n = lower; while ( n <= upper ) { n_square = n * n; printf(“%3d\t%6d\n”,n,n_square); n = n + 1; } /* Compute Squares */ #include main() { int n, n_square; int lower, upper, step; lower = 1;/* Lower imit */ upper = 10;/* Upper limit */ step = 1; n = lower; while ( n <= upper ) { n_square = n * n; printf(“%3d\t%6d\n”,n,n_square); n = n + 1; } VariableBefore exeution After execution n 12 n_square 11 lower 11 upper 10 step 11
18
Lectures on Numerical Methods17 An Example zExecution n <= upper is true. /* Compute Squares */ #include main() { int n, n_square; int lower, upper, step; lower = 1;/* Lower imit */ upper = 10;/* Upper limit */ step = 1; n = lower; while ( n <= upper ) { n_square = n * n; printf(“%3d\t%6d\n”,n,n_square); n = n + 1; } /* Compute Squares */ #include main() { int n, n_square; int lower, upper, step; lower = 1;/* Lower imit */ upper = 10;/* Upper limit */ step = 1; n = lower; while ( n <= upper ) { n_square = n * n; printf(“%3d\t%6d\n”,n,n_square); n = n + 1; } VariableBefore exeution After execution n 22 n_square 11 lower 11 upper 10 step 11
19
Lectures on Numerical Methods18 An Example zExecution n <= upper is true. /* Compute Squares */ #include main() { int n, n_square; int lower, upper, step; lower = 1;/* Lower imit */ upper = 10;/* Upper limit */ step = 1; n = lower; while ( n <= upper ) { n_square = n * n; printf(“%3d\t%6d\n”,n,n_square); n = n + 1; } /* Compute Squares */ #include main() { int n, n_square; int lower, upper, step; lower = 1;/* Lower imit */ upper = 10;/* Upper limit */ step = 1; n = lower; while ( n <= upper ) { n_square = n * n; printf(“%3d\t%6d\n”,n,n_square); n = n + 1; } VariableBefore exeution After execution n 22 n_square 14 lower 11 upper 10 step 11
20
Lectures on Numerical Methods19 An Example zExecution n <= upper is true. /* Compute Squares */ #include main() { int n, n_square; int lower, upper, step; lower = 1;/* Lower imit */ upper = 10;/* Upper limit */ step = 1; n = lower; while ( n <= upper ) { n_square = n * n; printf(“%3d\t%6d\n”,n,n_square); n = n + 1; } /* Compute Squares */ #include main() { int n, n_square; int lower, upper, step; lower = 1;/* Lower imit */ upper = 10;/* Upper limit */ step = 1; n = lower; while ( n <= upper ) { n_square = n * n; printf(“%3d\t%6d\n”,n,n_square); n = n + 1; } VariableBefore exeution After execution n 22 n_square 44 lower 11 upper 10 step 11
21
Lectures on Numerical Methods20 An Example zExecution n <= upper is true. /* Compute Squares */ #include main() { int n, n_square; int lower, upper, step; lower = 1;/* Lower imit */ upper = 10;/* Upper limit */ step = 1; n = lower; while ( n <= upper ) { n_square = n * n; printf(“%3d\t%6d\n”,n,n_square); n = n + 1; } /* Compute Squares */ #include main() { int n, n_square; int lower, upper, step; lower = 1;/* Lower imit */ upper = 10;/* Upper limit */ step = 1; n = lower; while ( n <= upper ) { n_square = n * n; printf(“%3d\t%6d\n”,n,n_square); n = n + 1; } VariableBefore exeution After execution n 23 n_square 44 lower 11 upper 10 step 11
22
Lectures on Numerical Methods21 An Example zExecution n <= upper is true. /* Compute Squares */ #include main() { int n, n_square; int lower, upper, step; lower = 1;/* Lower imit */ upper = 10;/* Upper limit */ step = 1; n = lower; while ( n <= upper ) { n_square = n * n; printf(“%3d\t%6d\n”,n,n_square); n = n + 1; } /* Compute Squares */ #include main() { int n, n_square; int lower, upper, step; lower = 1;/* Lower imit */ upper = 10;/* Upper limit */ step = 1; n = lower; while ( n <= upper ) { n_square = n * n; printf(“%3d\t%6d\n”,n,n_square); n = n + 1; } VariableBefore exeution After execution n 1011 n_square 100 lower 11 upper 10 step 11
23
Lectures on Numerical Methods22 An Example zExecution n <= upper is false. /* Compute Squares */ #include main() { int n, n_square; int lower, upper, step; lower = 1;/* Lower imit */ upper = 10;/* Upper limit */ step = 1; n = lower; while ( n <= upper ) { n_square = n * n; printf(“%3d\t%6d\n”,n,n_square); n = n + 1; } /* Compute Squares */ #include main() { int n, n_square; int lower, upper, step; lower = 1;/* Lower imit */ upper = 10;/* Upper limit */ step = 1; n = lower; while ( n <= upper ) { n_square = n * n; printf(“%3d\t%6d\n”,n,n_square); n = n + 1; } VariableBefore exeution After execution n 11 n_square 100 lower 11 upper 10 step 11
24
Lectures on Numerical Methods23 An Example zExecution n <= upper is false. çProgram ends /* Compute Squares */ #include main() { int n, n_square; int lower, upper, step; lower = 1;/* Lower imit */ upper = 10;/* Upper limit */ step = 1; n = lower; while ( n <= upper ) { n_square = n * n; printf(“%3d\t%6d\n”,n,n_square); n = n + 1; } /* Compute Squares */ #include main() { int n, n_square; int lower, upper, step; lower = 1;/* Lower imit */ upper = 10;/* Upper limit */ step = 1; n = lower; while ( n <= upper ) { n_square = n * n; printf(“%3d\t%6d\n”,n,n_square); n = n + 1; } VariableBefore exeution After execution n 11 n_square 100 lower 11 upper 10 step 11
25
Lectures on Numerical Methods24 Printf Function çThe first argument of printf is a format specifier. çEach % sign in the format specifier is a formatting instruction. %d print a decimal integer %f print a floating number %wd prints a decimal number with min width of w chars %w.pf prints a floating number with precision p and min width w çOther characters in the format specifier are just printed.
26
Lectures on Numerical Methods25 Printf Function zExamples çPrintf( “I am Charu” ); I am Charu çPrintf( “|%3d|”, 8 ); | 8| çPrintf( “|%03d|”, 8 ); |8 | çPrintf( “|%3d|”, 8000 ); |8000| çPrintf( “|%f|”, 123.456 ); |123.456000| çPrintf( “|%f|”, 123.4567890123 ); |123.456789| çPrintf( “|%8.3d|”, 123.4567890123 ); | 123.456| çPrintf( “ Age = %d years”, 22 ); Age = 22 years çPrintf( “%d and %d”, 1, 2 ); 1 and 2
27
Lectures on Numerical Methods26 An Example zArea of a circle çThe program produces the following output when 5.0 is input Enter the radius 5.0 Area = 78.539749 Peri = 31.415899 çAnd produces the following when – 3.0 is input. Enter the radius -3.0 Negative radius /* Compute Area and Perimeter of a circle */ #include main() { floatrad; float area, peri; printf( “Enter the radius “ ); scanf(“%f”, &rad); if ( rad > 0.0 ) { area = 3.14159 * rad * rad; peri = 6.28318 * rad; printf( “Area = %f\n”, area ); printf( “Peri = %f\n”, peri ); } else printf( “Negative radius\n”); } /* Compute Area and Perimeter of a circle */ #include main() { floatrad; float area, peri; printf( “Enter the radius “ ); scanf(“%f”, &rad); if ( rad > 0.0 ) { area = 3.14159 * rad * rad; peri = 6.28318 * rad; printf( “Area = %f\n”, area ); printf( “Peri = %f\n”, peri ); } else printf( “Negative radius\n”); }
28
Lectures on Numerical Methods27 An Example zDecisions The form of if-else statement is as follows: if ( condition ) statements if ( condtion ) statements else statements çIf condition is true then if-block of statements is executed otherwise else-block of statements is executed. /* Compute Area and Perimeter of a circle */ #include main() { floatrad; float area, peri; printf( “Enter the radius “ ); scanf(“%f”, &rad); if ( rad > 0.0 ) { area = 3.14159 * rad * rad; peri = 6.28318 * rad; printf( “Area = %f\n”, area ); printf( “Peri = %f\n”, peri ); } else printf( “Negative radius\n”); } /* Compute Area and Perimeter of a circle */ #include main() { floatrad; float area, peri; printf( “Enter the radius “ ); scanf(“%f”, &rad); if ( rad > 0.0 ) { area = 3.14159 * rad * rad; peri = 6.28318 * rad; printf( “Area = %f\n”, area ); printf( “Peri = %f\n”, peri ); } else printf( “Negative radius\n”); }
29
Lectures on Numerical Methods28 An Example zExecution /* Compute Area and Perimeter of a circle */ #include main() { floatrad; float area, peri; printf( “Enter the radius “ ); scanf(“%f”, &rad); if ( rad > 0.0 ) { area = 3.14159 * rad * rad; peri = 6.28318 * rad; printf( “Area = %f\n”, area ); printf( “Peri = %f\n”, peri ); } else printf( “Negative radius\n”); } /* Compute Area and Perimeter of a circle */ #include main() { floatrad; float area, peri; printf( “Enter the radius “ ); scanf(“%f”, &rad); if ( rad > 0.0 ) { area = 3.14159 * rad * rad; peri = 6.28318 * rad; printf( “Area = %f\n”, area ); printf( “Peri = %f\n”, peri ); } else printf( “Negative radius\n”); } VariableBefore exeution After execution rad ?? area ?? peri ??
30
Lectures on Numerical Methods29 An Example zExecution /* Compute Area and Perimeter of a circle */ #include main() { floatrad; float area, peri; printf( “Enter the radius “ ); scanf(“%f”, &rad); if ( rad > 0.0 ) { area = 3.14159 * rad * rad; peri = 6.28318 * rad; printf( “Area = %f\n”, area ); printf( “Peri = %f\n”, peri ); } else printf( “Negative radius\n”); } /* Compute Area and Perimeter of a circle */ #include main() { floatrad; float area, peri; printf( “Enter the radius “ ); scanf(“%f”, &rad); if ( rad > 0.0 ) { area = 3.14159 * rad * rad; peri = 6.28318 * rad; printf( “Area = %f\n”, area ); printf( “Peri = %f\n”, peri ); } else printf( “Negative radius\n”); } VariableBefore exeution After execution rad ??5.000000 area ?? peri ??
31
Lectures on Numerical Methods30 An Example zExecution Condition rad > 0.0 is true /* Compute Area and Perimeter of a circle */ #include main() { floatrad; float area, peri; printf( “Enter the radius “ ); scanf(“%f”, &rad); if ( rad > 0.0 ) { area = 3.14159 * rad * rad; peri = 6.28318 * rad; printf( “Area = %f\n”, area ); printf( “Peri = %f\n”, peri ); } else printf( “Negative radius\n”); } /* Compute Area and Perimeter of a circle */ #include main() { floatrad; float area, peri; printf( “Enter the radius “ ); scanf(“%f”, &rad); if ( rad > 0.0 ) { area = 3.14159 * rad * rad; peri = 6.28318 * rad; printf( “Area = %f\n”, area ); printf( “Peri = %f\n”, peri ); } else printf( “Negative radius\n”); } VariableBefore exeution After execution rad 5.000000 area ?? peri ??
32
Lectures on Numerical Methods31 An Example zExecution /* Compute Area and Perimeter of a circle */ #include main() { floatrad; float area, peri; printf( “Enter the radius “ ); scanf(“%f”, &rad); if ( rad > 0.0 ) { area = 3.14159 * rad * rad; peri = 6.28318 * rad; printf( “Area = %f\n”, area ); printf( “Peri = %f\n”, peri ); } else printf( “Negative radius\n”); } /* Compute Area and Perimeter of a circle */ #include main() { floatrad; float area, peri; printf( “Enter the radius “ ); scanf(“%f”, &rad); if ( rad > 0.0 ) { area = 3.14159 * rad * rad; peri = 6.28318 * rad; printf( “Area = %f\n”, area ); printf( “Peri = %f\n”, peri ); } else printf( “Negative radius\n”); } VariableBefore exeution After execution rad 5.00000 area ??78.53975 peri ??
33
Lectures on Numerical Methods32 An Example zExecution /* Compute Area and Perimeter of a circle */ #include main() { floatrad; float area, peri; printf( “Enter the radius “ ); scanf(“%f”, &rad); if ( rad > 0.0 ) { area = 3.14159 * rad * rad; peri = 6.28318 * rad; printf( “Area = %f\n”, area ); printf( “Peri = %f\n”, peri ); } else printf( “Negative radius\n”); } /* Compute Area and Perimeter of a circle */ #include main() { floatrad; float area, peri; printf( “Enter the radius “ ); scanf(“%f”, &rad); if ( rad > 0.0 ) { area = 3.14159 * rad * rad; peri = 6.28318 * rad; printf( “Area = %f\n”, area ); printf( “Peri = %f\n”, peri ); } else printf( “Negative radius\n”); } VariableBefore exeution After execution rad 5.00000 area 78.53975 peri ??31.41590
34
Lectures on Numerical Methods33 An Example zExecution /* Compute Area and Perimeter of a circle */ #include main() { floatrad; float area, peri; printf( “Enter the radius “ ); scanf(“%f”, &rad); if ( rad > 0.0 ) { area = 3.14159 * rad * rad; peri = 6.28318 * rad; printf( “Area = %f\n”, area ); printf( “Peri = %f\n”, peri ); } else printf( “Negative radius\n”); } /* Compute Area and Perimeter of a circle */ #include main() { floatrad; float area, peri; printf( “Enter the radius “ ); scanf(“%f”, &rad); if ( rad > 0.0 ) { area = 3.14159 * rad * rad; peri = 6.28318 * rad; printf( “Area = %f\n”, area ); printf( “Peri = %f\n”, peri ); } else printf( “Negative radius\n”); } VariableBefore exeution After execution rad 5.00000 area 78.53975 peri 31.41590
35
Lectures on Numerical Methods34 An Example zExecution /* Compute Area and Perimeter of a circle */ #include main() { floatrad; float area, peri; printf( “Enter the radius “ ); scanf(“%f”, &rad); if ( rad > 0.0 ) { area = 3.14159 * rad * rad; peri = 6.28318 * rad; printf( “Area = %f\n”, area ); printf( “Peri = %f\n”, peri ); } else printf( “Negative radius\n”); } /* Compute Area and Perimeter of a circle */ #include main() { floatrad; float area, peri; printf( “Enter the radius “ ); scanf(“%f”, &rad); if ( rad > 0.0 ) { area = 3.14159 * rad * rad; peri = 6.28318 * rad; printf( “Area = %f\n”, area ); printf( “Peri = %f\n”, peri ); } else printf( “Negative radius\n”); } VariableBefore exeution After execution rad 5.00000 area 78.53975 peri 31.41590
36
Lectures on Numerical Methods35 An Example zExecution çProgram ends /* Compute Area and Perimeter of a circle */ #include main() { floatrad; float area, peri; printf( “Enter the radius “ ); scanf(“%f”, &rad); if ( rad > 0.0 ) { area = 3.14159 * rad * rad; peri = 6.28318 * rad; printf( “Area = %f\n”, area ); printf( “Peri = %f\n”, peri ); } else printf( “Negative radius\n”); } /* Compute Area and Perimeter of a circle */ #include main() { floatrad; float area, peri; printf( “Enter the radius “ ); scanf(“%f”, &rad); if ( rad > 0.0 ) { area = 3.14159 * rad * rad; peri = 6.28318 * rad; printf( “Area = %f\n”, area ); printf( “Peri = %f\n”, peri ); } else printf( “Negative radius\n”); } VariableBefore exeution After execution rad 5.00000 area 78.53975 peri 31.41590
37
Lectures on Numerical Methods36 An Example zExecution /* Compute Area and Perimeter of a circle */ #include main() { floatrad; float area, peri; printf( “Enter the radius “ ); scanf(“%f”, &rad); if ( rad > 0.0 ) { area = 3.14159 * rad * rad; peri = 6.28318 * rad; printf( “Area = %f\n”, area ); printf( “Peri = %f\n”, peri ); } else printf( “Negative radius\n”); } /* Compute Area and Perimeter of a circle */ #include main() { floatrad; float area, peri; printf( “Enter the radius “ ); scanf(“%f”, &rad); if ( rad > 0.0 ) { area = 3.14159 * rad * rad; peri = 6.28318 * rad; printf( “Area = %f\n”, area ); printf( “Peri = %f\n”, peri ); } else printf( “Negative radius\n”); } VariableBefore exeution After execution rad ??-3.00000 area ?? peri ??
38
Lectures on Numerical Methods37 An Example zExecution The condition rad > 0.0 is false /* Compute Area and Perimeter of a circle */ #include main() { floatrad; float area, peri; printf( “Enter the radius “ ); scanf(“%f”, &rad); if ( rad > 0.0 ) { area = 3.14159 * rad * rad; peri = 6.28318 * rad; printf( “Area = %f\n”, area ); printf( “Peri = %f\n”, peri ); } else printf( “Negative radius\n”); } /* Compute Area and Perimeter of a circle */ #include main() { floatrad; float area, peri; printf( “Enter the radius “ ); scanf(“%f”, &rad); if ( rad > 0.0 ) { area = 3.14159 * rad * rad; peri = 6.28318 * rad; printf( “Area = %f\n”, area ); printf( “Peri = %f\n”, peri ); } else printf( “Negative radius\n”); } VariableBefore exeution After execution rad -3.00000 area ?? peri ??
39
Lectures on Numerical Methods38 An Example zExecution The condition rad > 0.0 is false Prints “Negative radius” and Program ends /* Compute Area and Perimeter of a circle */ #include main() { floatrad; float area, peri; printf( “Enter the radius “ ); scanf(“%f”, &rad); if ( rad > 0.0 ) { area = 3.14159 * rad * rad; peri = 6.28318 * rad; printf( “Area = %f\n”, area ); printf( “Peri = %f\n”, peri ); } else printf( “Negative radius\n”); } /* Compute Area and Perimeter of a circle */ #include main() { floatrad; float area, peri; printf( “Enter the radius “ ); scanf(“%f”, &rad); if ( rad > 0.0 ) { area = 3.14159 * rad * rad; peri = 6.28318 * rad; printf( “Area = %f\n”, area ); printf( “Peri = %f\n”, peri ); } else printf( “Negative radius\n”); } VariableBefore exeution After execution rad -3.00000 area ?? peri ??
40
Lectures on Numerical Methods39 Scanf Function çThe first argument of scanf is a format specifier. çEach % sign in the format specifier is a formatting instruction. %d scans a decimal integer and stores it in int variable %f scans a floating number and stores it in float variable %wd maximum width of w chars are scanned %wf maximum width of w chars are scanned çOther characters in the format specifier are expected to be input as is. Best to avoid any other chars in the input.
41
Lectures on Numerical Methods40 Scanf Function Format“%d%f”“%3d%6f” InputIntvarFltvarIntvarFltvar 3 4.534.53 3\t4.5 (tab)34.53 3\n4.5(newline)34.53 34.5340.5340.5 003 4.534.53 0034.5340.534.5 0003 4.534.503.0 00034.5340.5034.5 3 000004.534.534.0 3 4.567891234.56789134.567800 A3a4.5?? 3a4.53??3 çscanf ( format, &intvar, &fltvar );
42
Lectures on Numerical Methods41 Algorithms zProblem çLet S be a finite sequence of positive nonzero integers a k, except the last number which is always 0. Find the largest number in the sequence. çExample ç7, 9, 4, 6, 2, 5, 8, 0 çThe largest is 9. çIf S k is a subsequence of first k numbers of S and m k is the largest term of S k then çm k+1 = max { m k, a k+1 } Algorithm 1.The m 1 be the largest in S 1. Clearly m 1 = a 1. 2.Let I = 1. 3.If a I+1 is zero, go to step 7. 4.m I+1 = max { m I, a I+1 }. 5.Increment I. 6.Go to step 3. 7.Print m I. Algorithm 1.The m 1 be the largest in S 1. Clearly m 1 = a 1. 2.Let I = 1. 3.If a I+1 is zero, go to step 7. 4.m I+1 = max { m I, a I+1 }. 5.Increment I. 6.Go to step 3. 7.Print m I.
43
Lectures on Numerical Methods42 An Example zFind Largest çThe program produces the following output Enter a number 7 Enter a number 9 Enter a number 4 Enter a number 6 Enter a number 2 Enter a number 0 Largest is 9 /* Find the largest number */ #include main() { int number, largest; printf( "Enter a number " ); scanf( "%d", &largest ); printf( "Enter a number " ); scanf( "%d", &number ); while ( number > 0 ) { if ( number > largest ) largest = number; printf( "Enter a number " ); scanf( "%d", &number ); } printf( "Largest is %d\n", largest ); } /* Find the largest number */ #include main() { int number, largest; printf( "Enter a number " ); scanf( "%d", &largest ); printf( "Enter a number " ); scanf( "%d", &number ); while ( number > 0 ) { if ( number > largest ) largest = number; printf( "Enter a number " ); scanf( "%d", &number ); } printf( "Largest is %d\n", largest ); }
44
Lectures on Numerical Methods43 Lab Assignment 1 çPrint a table of sine values of angles starting from 0 o upto 180 o in steps of 10 o. ( There is a standard library function sin(x) declared in math.h ) çRead a sequence of positive integers and count the number of input integers and calculate their sum. Calculate the average of the integers. çLet ax 2 +bx+c=0. Read a, b and c. Print the solutions to the quadratic equation. (The solutions may be complex. ) çPrint a sequence of Fibonacci numbers less than 100. çInput a positive integer and determine if it is a prime.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.