Top Banner
Lectures on Numerical Met hods 1 An Example Compute Squares Prints a table of squares as shown below 1 1 2 4 3 9 4 16 5 25 6 36 7 49 8 64 9 81 10 100 /* Compute Squares */ #include <stdio.h> 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; } }
43

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.

Dec 20, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 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.

Lectures on Numerical Methods 1

An ExampleAn Example

Compute SquaresPrints a table of squares as shown

below

1 12 43 94 165 256 367 498 649 8110 100

/* Compute Squares */

#include <stdio.h>

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 <stdio.h>

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;

}

}

Page 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.

Lectures on Numerical Methods 2

An ExampleAn Example

FunctionsC programs contain functions

and variablesFunction 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 <stdio.h>

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 <stdio.h>

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;

}

}

Page 3: 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.

Lectures on Numerical Methods 3

An ExampleAn Example

VariablesVariables 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 <stdio.h>

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 <stdio.h>

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;

}

}

Page 4: 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.

Lectures on Numerical Methods 4

An ExampleAn Example

DeclarationsDeclarations 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 <stdio.h>

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 <stdio.h>

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;

}

}

Page 5: 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.

Lectures on Numerical Methods 5

An ExampleAn Example

Assignments StatementThe 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 <stdio.h>

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 <stdio.h>

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;

}

}

Page 6: 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.

Lectures on Numerical Methods 6

An ExampleAn Example

LoopsThe 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 <stdio.h>

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 <stdio.h>

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;

}

}

Page 7: 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.

Lectures on Numerical Methods 7

An ExampleAn Example

Execution/* Compute Squares */

#include <stdio.h>

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 <stdio.h>

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;

}

}

Variable Before exeution

After execution

n ?? ??

n_square ?? ??

lower ?? ??

upper ?? ??

step ?? ??

Page 8: 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.

Lectures on Numerical Methods 8

An ExampleAn Example

Execution/* Compute Squares */

#include <stdio.h>

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 <stdio.h>

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;

}

}

Variable Before exeution

After execution

n ?? ??

n_square ?? ??

lower ?? ??

upper ?? ??

step ?? ??

Page 9: 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.

Lectures on Numerical Methods 9

An ExampleAn Example

Execution/* Compute Squares */

#include <stdio.h>

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 <stdio.h>

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;

}

}

Variable Before exeution

After execution

n ?? ??

n_square ?? ??

lower ?? 1

upper ?? ??

step ?? ??

Page 10: 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.

Lectures on Numerical Methods 10

An ExampleAn Example

Execution/* Compute Squares */

#include <stdio.h>

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 <stdio.h>

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;

}

}

Variable Before exeution

After execution

n ?? ??

n_square ?? ??

lower 1 1

upper ?? 10

step ?? ??

Page 11: 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.

Lectures on Numerical Methods 11

An ExampleAn Example

Execution/* Compute Squares */

#include <stdio.h>

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 <stdio.h>

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;

}

}

Variable Before exeution

After execution

n ?? ??

n_square ?? ??

lower 1 1

upper 10 10

step ?? 1

Page 12: 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.

Lectures on Numerical Methods 12

An ExampleAn Example

Execution/* Compute Squares */

#include <stdio.h>

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 <stdio.h>

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;

}

}

Variable Before exeution

After execution

n ?? 1

n_square ?? ??

lower 1 1

upper 10 10

step 1 1

Page 13: 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.

Lectures on Numerical Methods 13

An ExampleAn Example

Executionn <= upper is true.

/* Compute Squares */

#include <stdio.h>

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 <stdio.h>

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;

}

}

Variable Before exeution

After execution

n 1 1

n_square ?? ??

lower 1 1

upper 10 10

step 1 1

Page 14: 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.

Lectures on Numerical Methods 14

An ExampleAn Example

Executionn <= upper is true.

/* Compute Squares */

#include <stdio.h>

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 <stdio.h>

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;

}

}

Variable Before exeution

After execution

n 1 1

n_square ?? 1

lower 1 1

upper 10 10

step 1 1

Page 15: 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.

Lectures on Numerical Methods 15

An ExampleAn Example

Executionn <= upper is true.

/* Compute Squares */

#include <stdio.h>

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 <stdio.h>

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;

}

}

Variable Before exeution

After execution

n 1 1

n_square 1 1

lower 1 1

upper 10 10

step 1 1

Page 16: 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.

Lectures on Numerical Methods 16

An ExampleAn Example

Executionn <= upper is true.

/* Compute Squares */

#include <stdio.h>

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 <stdio.h>

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;

}

}

Variable Before exeution

After execution

n 1 2

n_square 1 1

lower 1 1

upper 10 10

step 1 1

Page 17: 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.

Lectures on Numerical Methods 17

An ExampleAn Example

Executionn <= upper is true.

/* Compute Squares */

#include <stdio.h>

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 <stdio.h>

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;

}

}

Variable Before exeution

After execution

n 2 2

n_square 1 1

lower 1 1

upper 10 10

step 1 1

Page 18: 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.

Lectures on Numerical Methods 18

An ExampleAn Example

Executionn <= upper is true.

/* Compute Squares */

#include <stdio.h>

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 <stdio.h>

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;

}

}

Variable Before exeution

After execution

n 2 2

n_square 1 4

lower 1 1

upper 10 10

step 1 1

Page 19: 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.

Lectures on Numerical Methods 19

An ExampleAn Example

Executionn <= upper is true.

/* Compute Squares */

#include <stdio.h>

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 <stdio.h>

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;

}

}

Variable Before exeution

After execution

n 2 2

n_square 4 4

lower 1 1

upper 10 10

step 1 1

Page 20: 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.

Lectures on Numerical Methods 20

An ExampleAn Example

Executionn <= upper is true.

/* Compute Squares */

#include <stdio.h>

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 <stdio.h>

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;

}

}

Variable Before exeution

After execution

n 2 3

n_square 4 4

lower 1 1

upper 10 10

step 1 1

Page 21: 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.

Lectures on Numerical Methods 21

An ExampleAn Example

Executionn <= upper is true.

/* Compute Squares */

#include <stdio.h>

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 <stdio.h>

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;

}

}

Variable Before exeution

After execution

n 10 11

n_square 100 100

lower 1 1

upper 10 10

step 1 1

Page 22: 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.

Lectures on Numerical Methods 22

An ExampleAn Example

Executionn <= upper is false.

/* Compute Squares */

#include <stdio.h>

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 <stdio.h>

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;

}

}

Variable Before exeution

After execution

n 11 11

n_square 100 100

lower 1 1

upper 10 10

step 1 1

Page 23: 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.

Lectures on Numerical Methods 23

An ExampleAn Example

Executionn <= upper is false.

Program ends

/* Compute Squares */

#include <stdio.h>

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 <stdio.h>

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;

}

}

Variable Before exeution

After execution

n 11 11

n_square 100 100

lower 1 1

upper 10 10

step 1 1

Page 24: 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.

Lectures on Numerical Methods 24

Printf FunctionPrintf 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 wOther characters in the format specifier are just printed.

Page 25: 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.

Lectures on Numerical Methods 25

Printf FunctionPrintf Function

ExamplesPrintf( “I am Charu” ); I am CharuPrintf( “|%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 yearsPrintf( “%d and %d” , 1, 2 ); 1 and 2

Page 26: 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.

Lectures on Numerical Methods 26

An ExampleAn Example

Area of a circleThe 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 <stdio.h>

main() { float rad; 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 <stdio.h>

main() { float rad; 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”);}

Page 27: 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.

Lectures on Numerical Methods 27

An ExampleAn Example

DecisionsThe 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 <stdio.h>

main() { float rad; 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 <stdio.h>

main() { float rad; 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”);}

Page 28: 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.

Lectures on Numerical Methods 28

An ExampleAn Example

Execution /* Compute Area and Perimeter of a circle */

#include <stdio.h>

main() { float rad; 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 <stdio.h>

main() { float rad; 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”);}

Variable Before exeution

After execution

rad ?? ??

area ?? ??

peri ?? ??

Page 29: 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.

Lectures on Numerical Methods 29

An ExampleAn Example

Execution /* Compute Area and Perimeter of a circle */

#include <stdio.h>

main() { float rad; 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 <stdio.h>

main() { float rad; 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”);}

Variable Before exeution

After execution

rad ?? 5.000000

area ?? ??

peri ?? ??

Page 30: 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.

Lectures on Numerical Methods 30

An ExampleAn Example

ExecutionCondition rad > 0.0 is true

/* Compute Area and Perimeter of a circle */

#include <stdio.h>

main() { float rad; 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 <stdio.h>

main() { float rad; 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”);}

Variable Before exeution

After execution

rad 5.000000

5.000000

area ?? ??

peri ?? ??

Page 31: 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.

Lectures on Numerical Methods 31

An ExampleAn Example

Execution /* Compute Area and Perimeter of a circle */

#include <stdio.h>

main() { float rad; 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 <stdio.h>

main() { float rad; 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”);}

Variable Before exeution

After execution

rad 5.00000 5.00000

area ?? 78.53975

peri ?? ??

Page 32: 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.

Lectures on Numerical Methods 32

An ExampleAn Example

Execution /* Compute Area and Perimeter of a circle */

#include <stdio.h>

main() { float rad; 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 <stdio.h>

main() { float rad; 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”);}

Variable Before exeution

After execution

rad 5.00000 5.00000

area 78.53975

78.53975

peri ?? 31.41590

Page 33: 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.

Lectures on Numerical Methods 33

An ExampleAn Example

Execution /* Compute Area and Perimeter of a circle */

#include <stdio.h>

main() { float rad; 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 <stdio.h>

main() { float rad; 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”);}

Variable Before exeution

After execution

rad 5.00000 5.00000

area 78.53975

78.53975

peri 31.41590

31.41590

Page 34: 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.

Lectures on Numerical Methods 34

An ExampleAn Example

Execution /* Compute Area and Perimeter of a circle */

#include <stdio.h>

main() { float rad; 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 <stdio.h>

main() { float rad; 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”);}

Variable Before exeution

After execution

rad 5.00000 5.00000

area 78.53975

78.53975

peri 31.41590

31.41590

Page 35: 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.

Lectures on Numerical Methods 35

An ExampleAn Example

Execution

Program ends

/* Compute Area and Perimeter of a circle */

#include <stdio.h>

main() { float rad; 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 <stdio.h>

main() { float rad; 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”);}

Variable Before exeution

After execution

rad 5.00000 5.00000

area 78.53975

78.53975

peri 31.41590

31.41590

Page 36: 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.

Lectures on Numerical Methods 36

An ExampleAn Example

Execution /* Compute Area and Perimeter of a circle */

#include <stdio.h>

main() { float rad; 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 <stdio.h>

main() { float rad; 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”);}

Variable Before exeution

After execution

rad ?? -3.00000

area ?? ??

peri ?? ??

Page 37: 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.

Lectures on Numerical Methods 37

An ExampleAn Example

ExecutionThe condition rad > 0.0 is false

/* Compute Area and Perimeter of a circle */

#include <stdio.h>

main() { float rad; 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 <stdio.h>

main() { float rad; 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”);}

Variable Before exeution

After execution

rad -3.00000

-3.00000

area ?? ??

peri ?? ??

Page 38: 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.

Lectures on Numerical Methods 38

An ExampleAn Example

ExecutionThe condition rad > 0.0 is false

Prints “Negative radius” andProgram ends

/* Compute Area and Perimeter of a circle */

#include <stdio.h>

main() { float rad; 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 <stdio.h>

main() { float rad; 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”);}

Variable Before exeution

After execution

rad -3.00000

-3.00000

area ?? ??

peri ?? ??

Page 39: 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.

Lectures on Numerical Methods 39

Scanf FunctionScanf 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 scannedOther characters in the format specifier are expected to be input as is.

Best to avoid any other chars in the input.

Page 40: 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.

Lectures on Numerical Methods 40

Scanf FunctionScanf Function

Format “%d%f” “%3d%6f”

Input Intvar Fltvar Intvar Fltvar

3 4.5 3 4.5 3 4.5

3\t4.5 (tab) 3 4.5 3 4.5

3\n4.5(newline) 3 4.5 3 4.5

34.5 34 0.5 34 0.5

003 4.5 3 4.5 3 4.5

0034.5 34 0.5 3 4.5

0003 4.5 3 4.5 0 3.0

00034.5 34 0.5 0 34.5

3 000004.5 3 4.5 3 4.0

3 4.5678912 3 4.567891 3 4.567800

A3a4.5 ?? ?? ?? ??

3a4.5 3 ?? 3 ??

scanf ( format, &intvar, &fltvar );

Page 41: 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.

Lectures on Numerical Methods 41

AlgorithmsAlgorithms

ProblemLet S be a finite sequence of

positive nonzero integers ak, except the last number which is always 0. Find the largest number in the sequence.

Example7, 9, 4, 6, 2, 5, 8, 0The largest is 9.If Sk is a subsequence of first k

numbers of S and mk is the largest term of Sk then

mk+1 = max { mk , ak+1 }

Algorithm

1. The m1 be the largest in S1. Clearly m1 = a1.

2. Let I = 1.3. If aI+1 is zero, go to step

7.4. mI+1 = max { mI, aI+1}.5. Increment I.6. Go to step 3.7. Print mI.

Algorithm

1. The m1 be the largest in S1. Clearly m1 = a1.

2. Let I = 1.3. If aI+1 is zero, go to step

7.4. mI+1 = max { mI, aI+1}.5. Increment I.6. Go to step 3.7. Print mI.

Page 42: 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.

Lectures on Numerical Methods 42

An ExampleAn Example

Find LargestThe 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 <stdio.h>

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 <stdio.h>

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 );

}

Page 43: 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.

Lectures on Numerical Methods 43

Lab Assignment 1Lab Assignment 1

Print a table of sine values of angles starting from 0o upto 180o in steps of 10o. ( 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 ax2+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.