You are on page 1of 43

An Example

Compute Squares /* Compute Squares */


Prints a table of squares as shown #include <stdio.h>
below
main() {
1 1 int n, n_square;
2 4 int lower, upper, step;
3 9
lower = 1; /* Lower imit */
4 16
upper = 10; /* Upper limit */
5 25
step = 1;
6 36
7 49
n = lower;
8 64 while ( n <= upper ) {
9 81 n_square = n * n;
10 100 printf(“%3d\t%6d\n”,n,n_square);
n = n + 1;
}
}

Lectures on Numerical Methods 1


An Example
Functions /* Compute Squares */
C programs contain functions #include <stdio.h>
and variables
main() {
Function names are followed
int n, n_square;
by a list of arguments enclosed int lower, upper, step;
in parenthesis.
Function definition consists of lower = 1; /* Lower imit */
a group of statements enclosed upper = 10; /* Upper limit */
in curly brackets. step = 1;

printf is a library function.


n = lower;
The information about this while ( n <= upper ) {
function is contained in a file n_square = n * n;
named stdio.h. printf(“%3d\t%6d\n”,n,n_square);
n = n + 1;
}
}

Lectures on Numerical Methods 2


An Example
Variables /* Compute Squares */
Variables refer the objects that can #include <stdio.h>
be stored in computer’s memory
locations. Variables are named main() {
(identifiers) int n, n_square;
Variables can store data of various int lower, upper, step;
types. Some basic datatypes are
char characters (1/2 bytes) lower = 1; /* Lower imit */
upper = 10; /* Upper limit */
int integers (2/4 bytes)
step = 1;
float rationals (4 bytes)
double rationals(8 bytes) n = lower;
Variables must be declared and while ( n <= upper ) {
initialized. n_square = n * n;
printf(“%3d\t%6d\n”,n,n_square);
n = n + 1;
}
}

Lectures on Numerical Methods 3


An Example
Declarations /* Compute Squares */
Declarations have form #include <stdio.h>
type var1, var2, var3;
Merely announces the data type to main() {
be associated with a variable. int n, n_square;
int lower, upper, step;
Initial value of variable is not
known.
lower = 1; /* Lower imit */
Examples upper = 10; /* Upper limit */
char name; step = 1;
int numStudents;
float applePrice; n = lower;
double veryAccurateVar; while ( n <= upper ) {
n_square = n * n;
printf(“%3d\t%6d\n”,n,n_square);
n = n + 1;
}
}

Lectures on Numerical Methods 4


An Example
Assignments /* Compute Squares */

Statement #include <stdio.h>

The assignment statements have main() {


the following form int n, n_square;
someVar = expression ; int lower, upper, step;
Expressions consist of variables,
functions and operators. lower = 1; /* Lower imit */
Examples upper = 10; /* Upper limit */
c = 5 * ( f – 32 ) / 9 step = 1;

s = sin(x) / x
n = lower;
y = log(x) + v0
while ( n <= upper ) {
I = P * R * T / 100 n_square = n * n;
Tax = 0.3 * ( I – 60000 ) printf(“%3d\t%6d\n”,n,n_square);
n = n + 1;
}
}

Lectures on Numerical Methods 5


An Example
Loops /* Compute Squares */
The form of the while statement is #include <stdio.h>
While ( condition )
statement ; main() {
While ( condition ) { int n, n_square;
statements } int lower, upper, step;

If the condition is true the


lower = 1; /* Lower imit */
statements in the body of the while
upper = 10; /* Upper limit */
loop are executed. At the end of the
step = 1;
loop the the condition is tested
again and executes the loop if the
condition is true. This procedure n = lower;
continues till the condition fails to be while ( n <= upper ) {
true. n_square = n * n;
Conditions are usually logical printf(“%3d\t%6d\n”,n,n_square);
expressions using operators like ==, n = n + 1;
<, <=, >, >= etc. These have }
boolean value }

Lectures on Numerical Methods 6


An Example
Execution /* Compute Squares */
#include <stdio.h>

Variable Before After main() {


exeution execution int n, n_square;
int lower, upper, step;
n ?? ??
n_square ?? ?? lower = 1; /* Lower imit */
lower ?? ?? upper = 10; /* Upper limit */
step = 1;
upper ?? ??
step ?? ?? n = lower;
while ( n <= upper ) {
n_square = n * n;
printf(“%3d\t%6d\n”,n,n_square);
n = n + 1;
}
}

Lectures on Numerical Methods 7


An Example
Execution /* Compute Squares */
#include <stdio.h>

Variable Before After main() {


exeution execution int n, n_square;
int lower, upper, step;
n ?? ??
n_square ?? ?? lower = 1; /* Lower imit */
lower ?? ?? upper = 10; /* Upper limit */
step = 1;
upper ?? ??
step ?? ?? n = lower;
while ( n <= upper ) {
n_square = n * n;
printf(“%3d\t%6d\n”,n,n_square);
n = n + 1;
}
}

Lectures on Numerical Methods 8


An Example
Execution /* Compute Squares */
#include <stdio.h>

Variable Before After main() {


exeution execution int n, n_square;
int lower, upper, step;
n ?? ??
n_square ?? ?? lower = 1; /* Lower imit */
lower ?? 1 upper = 10; /* Upper limit */
step = 1;
upper ?? ??
step ?? ?? n = lower;
while ( n <= upper ) {
n_square = n * n;
printf(“%3d\t%6d\n”,n,n_square);
n = n + 1;
}
}

Lectures on Numerical Methods 9


An Example
Execution /* Compute Squares */
#include <stdio.h>

Variable Before After main() {


exeution execution int n, n_square;
int lower, upper, step;
n ?? ??
n_square ?? ?? lower = 1; /* Lower imit */
lower 1 1 upper = 10; /* Upper limit */
step = 1;
upper ?? 10
step ?? ?? n = lower;
while ( n <= upper ) {
n_square = n * n;
printf(“%3d\t%6d\n”,n,n_square);
n = n + 1;
}
}

Lectures on Numerical Methods 10


An Example
Execution /* Compute Squares */
#include <stdio.h>

Variable Before After main() {


exeution execution int n, n_square;
int lower, upper, step;
n ?? ??
n_square ?? ?? lower = 1; /* Lower imit */
lower 1 1 upper = 10; /* Upper limit */
step = 1;
upper 10 10
step ?? 1 n = lower;
while ( n <= upper ) {
n_square = n * n;
printf(“%3d\t%6d\n”,n,n_square);
n = n + 1;
}
}

Lectures on Numerical Methods 11


An Example
Execution /* Compute Squares */
#include <stdio.h>

Variable Before After main() {


exeution execution int n, n_square;
int lower, upper, step;
n ?? 1
n_square ?? ?? lower = 1; /* Lower imit */
lower 1 1 upper = 10; /* Upper limit */
step = 1;
upper 10 10
step 1 1 n = lower;
while ( n <= upper ) {
n_square = n * n;
printf(“%3d\t%6d\n”,n,n_square);
n = n + 1;
}
}

Lectures on Numerical Methods 12


An Example
Execution /* Compute Squares */
n <= upper is true. #include <stdio.h>

Variable Before After main() {


exeution execution int n, n_square;
int lower, upper, step;
n 1 1
n_square ?? ?? lower = 1; /* Lower imit */
lower 1 1 upper = 10; /* Upper limit */
step = 1;
upper 10 10
step 1 1 n = lower;
while ( n <= upper ) {
n_square = n * n;
printf(“%3d\t%6d\n”,n,n_square);
n = n + 1;
}
}

Lectures on Numerical Methods 13


An Example
Execution /* Compute Squares */
n <= upper is true. #include <stdio.h>

Variable Before After main() {


exeution execution int n, n_square;
int lower, upper, step;
n 1 1
n_square ?? 1 lower = 1; /* Lower imit */
lower 1 1 upper = 10; /* Upper limit */
step = 1;
upper 10 10
step 1 1 n = lower;
while ( n <= upper ) {
n_square = n * n;
printf(“%3d\t%6d\n”,n,n_square);
n = n + 1;
}
}

Lectures on Numerical Methods 14


An Example
Execution /* Compute Squares */
n <= upper is true. #include <stdio.h>

Variable Before After main() {


exeution execution int n, n_square;
int lower, upper, step;
n 1 1
n_square 1 1 lower = 1; /* Lower imit */
lower 1 1 upper = 10; /* Upper limit */
step = 1;
upper 10 10
step 1 1 n = lower;
while ( n <= upper ) {
n_square = n * n;
printf(“%3d\t%6d\n”,n,n_square);
n = n + 1;
}
}

Lectures on Numerical Methods 15


An Example
Execution /* Compute Squares */
n <= upper is true. #include <stdio.h>

Variable Before After main() {


exeution execution int n, n_square;
int lower, upper, step;
n 1 2
n_square 1 1 lower = 1; /* Lower imit */
lower 1 1 upper = 10; /* Upper limit */
step = 1;
upper 10 10
step 1 1 n = lower;
while ( n <= upper ) {
n_square = n * n;
printf(“%3d\t%6d\n”,n,n_square);
n = n + 1;
}
}

Lectures on Numerical Methods 16


An Example
Execution /* Compute Squares */
n <= upper is true. #include <stdio.h>

Variable Before After main() {


exeution execution int n, n_square;
int lower, upper, step;
n 2 2
n_square 1 1 lower = 1; /* Lower imit */
lower 1 1 upper = 10; /* Upper limit */
step = 1;
upper 10 10
step 1 1 n = lower;
while ( n <= upper ) {
n_square = n * n;
printf(“%3d\t%6d\n”,n,n_square);
n = n + 1;
}
}

Lectures on Numerical Methods 17


An Example
Execution /* Compute Squares */
n <= upper is true. #include <stdio.h>

Variable Before After main() {


exeution execution int n, n_square;
int lower, upper, step;
n 2 2
n_square 1 4 lower = 1; /* Lower imit */
lower 1 1 upper = 10; /* Upper limit */
step = 1;
upper 10 10
step 1 1 n = lower;
while ( n <= upper ) {
n_square = n * n;
printf(“%3d\t%6d\n”,n,n_square);
n = n + 1;
}
}

Lectures on Numerical Methods 18


An Example
Execution /* Compute Squares */
n <= upper is true. #include <stdio.h>

Variable Before After main() {


exeution execution int n, n_square;
int lower, upper, step;
n 2 2
n_square 4 4 lower = 1; /* Lower imit */
lower 1 1 upper = 10; /* Upper limit */
step = 1;
upper 10 10
step 1 1 n = lower;
while ( n <= upper ) {
n_square = n * n;
printf(“%3d\t%6d\n”,n,n_square);
n = n + 1;
}
}

Lectures on Numerical Methods 19


An Example
Execution /* Compute Squares */
n <= upper is true. #include <stdio.h>

Variable Before After main() {


exeution execution int n, n_square;
int lower, upper, step;
n 2 3
n_square 4 4 lower = 1; /* Lower imit */
lower 1 1 upper = 10; /* Upper limit */
step = 1;
upper 10 10
step 1 1 n = lower;
while ( n <= upper ) {
n_square = n * n;
printf(“%3d\t%6d\n”,n,n_square);
n = n + 1;
}
}

Lectures on Numerical Methods 20


An Example
Execution /* Compute Squares */
n <= upper is true. #include <stdio.h>

Variable Before After main() {


exeution execution int n, n_square;
int lower, upper, step;
n 10 11
n_square 100 100 lower = 1; /* Lower imit */
lower 1 1 upper = 10; /* Upper limit */
step = 1;
upper 10 10
step 1 1 n = lower;
while ( n <= upper ) {
n_square = n * n;
printf(“%3d\t%6d\n”,n,n_square);
n = n + 1;
}
}

Lectures on Numerical Methods 21


An Example
Execution /* Compute Squares */
n <= upper is false. #include <stdio.h>

Variable Before After main() {


exeution execution int n, n_square;
int lower, upper, step;
n 11 11
n_square 100 100 lower = 1; /* Lower imit */
lower 1 1 upper = 10; /* Upper limit */
step = 1;
upper 10 10
step 1 1 n = lower;
while ( n <= upper ) {
n_square = n * n;
printf(“%3d\t%6d\n”,n,n_square);
n = n + 1;
}
}

Lectures on Numerical Methods 22


An Example
Execution /* Compute Squares */
n <= upper is false. #include <stdio.h>

Variable Before After main() {


exeution execution int n, n_square;
int lower, upper, step;
n 11 11
n_square 100 100 lower = 1; /* Lower imit */
lower 1 1 upper = 10; /* Upper limit */
step = 1;
upper 10 10
step 1 1 n = lower;
while ( n <= upper ) {
Program ends n_square = n * n;
printf(“%3d\t%6d\n”,n,n_square);
n = n + 1;
}
}

Lectures on Numerical Methods 23


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.

Lectures on Numerical Methods 24


Printf Function

Examples
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

Lectures on Numerical Methods 25


An Example
Area of a circle
/* Compute Area and Perimeter of a
circle */
#include <stdio.h>
The program produces the
following output when 5.0 is input main() {
float rad;
Enter the radius 5.0 float area, peri;
Area = 78.539749 printf( “Enter the radius “ );
Peri = 31.415899 scanf(“%f” , &rad);

if ( rad > 0.0 ) {


And produces the following when – area = 3.14159 * rad * rad;
3.0 is input. peri = 6.28318 * rad;

printf( “Area = %f\n” , area );


Enter the radius -3.0 printf( “Peri = %f\n” , peri );
Negative radius }
else
printf( “Negative radius\n”);
}

Lectures on Numerical Methods 26


An Example
Decisions /* Compute Area and Perimeter of
circle */
a

The form of if-else statement is #include <stdio.h>


as follows: main() {
if ( condition ) statements float rad;
float area, peri;
if ( condtion ) statements printf( “Enter the radius “ );
else statements scanf(“%f” , &rad);

if ( rad > 0.0 ) {


If condition is true then if-block of area = 3.14159 * rad * rad;
statements is executed otherwise peri = 6.28318 * rad;
else-block of statements is
executed. printf( “Area = %f\n” , area );
printf( “Peri = %f\n” , peri );
}
else
printf( “Negative radius\n”);
}

Lectures on Numerical Methods 27


An Example
Execution /* Compute Area and Perimeter of
circle */
a

#include <stdio.h>

main() {
Variable Before After float rad;
exeution execution float area, peri;
rad ?? ?? printf( “Enter the radius “ );
area ?? ?? scanf(“%f” , &rad);
peri ?? ?? 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”);
}

Lectures on Numerical Methods 28


An Example
Execution /* Compute Area and Perimeter of
circle */
a

#include <stdio.h>

main() {
Variable Before After float rad;
exeution execution float area, peri;
rad ?? 5.000000 printf( “Enter the radius “ );
area ?? ?? scanf(“%f” , &rad);
peri ?? ?? 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”);
}

Lectures on Numerical Methods 29


An Example
Execution /* Compute Area and Perimeter of
circle */
a

Condition rad > 0.0 is true #include <stdio.h>

main() {
Variable Before After float rad;
exeution execution float area, peri;
rad 5.000000 5.000000 printf( “Enter the radius “ );
area ?? ?? scanf(“%f” , &rad);
peri ?? ?? 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”);
}

Lectures on Numerical Methods 30


An Example
Execution /* Compute Area and Perimeter of
circle */
a

#include <stdio.h>

main() {
Variable Before After float rad;
exeution execution float area, peri;
rad 5.00000 5.00000 printf( “Enter the radius “ );
area ?? 78.53975 scanf(“%f” , &rad);
peri ?? ?? 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”);
}

Lectures on Numerical Methods 31


An Example
Execution /* Compute Area and Perimeter of
circle */
a

#include <stdio.h>

main() {
Variable Before After float rad;
exeution execution float area, peri;
rad 5.00000 5.00000 printf( “Enter the radius “ );
area 78.53975 78.53975 scanf(“%f” , &rad);
peri ?? 31.41590 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”);
}

Lectures on Numerical Methods 32


An Example
Execution /* Compute Area and Perimeter of
circle */
a

#include <stdio.h>

main() {
Variable Before After float rad;
exeution execution float area, peri;
rad 5.00000 5.00000 printf( “Enter the radius “ );
area 78.53975 78.53975 scanf(“%f” , &rad);
peri 31.41590 31.41590 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”);
}

Lectures on Numerical Methods 33


An Example
Execution /* Compute Area and Perimeter of
circle */
a

#include <stdio.h>

main() {
Variable Before After float rad;
exeution execution float area, peri;
rad 5.00000 5.00000 printf( “Enter the radius “ );
area 78.53975 78.53975 scanf(“%f” , &rad);
peri 31.41590 31.41590 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”);
}

Lectures on Numerical Methods 34


An Example
Execution /* Compute Area and Perimeter of
circle */
a

#include <stdio.h>

main() {
Variable Before After float rad;
exeution execution float area, peri;
rad 5.00000 5.00000 printf( “Enter the radius “ );
area 78.53975 78.53975 scanf(“%f” , &rad);
peri 31.41590 31.41590 if ( rad > 0.0 ) {
area = 3.14159 * rad * rad;
Program ends peri = 6.28318 * rad;

printf( “Area = %f\n” , area );


printf( “Peri = %f\n” , peri );
}
else
printf( “Negative radius\n”);
}

Lectures on Numerical Methods 35


An Example
Execution /* Compute Area and Perimeter of
circle */
a

#include <stdio.h>

main() {
Variable Before After float rad;
exeution execution float area, peri;
rad ?? -3.00000 printf( “Enter the radius “ );
area ?? ?? scanf(“%f” , &rad);
peri ?? ?? 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”);
}

Lectures on Numerical Methods 36


An Example
Execution /* Compute Area and Perimeter of
circle */
a

The condition rad > 0.0 is false #include <stdio.h>

main() {
Variable Before After float rad;
exeution execution float area, peri;
rad -3.00000 -3.00000 printf( “Enter the radius “ );
area ?? ?? scanf(“%f” , &rad);
peri ?? ?? 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”);
}

Lectures on Numerical Methods 37


An Example
Execution /* Compute Area and Perimeter of
circle */
a

The condition rad > 0.0 is false #include <stdio.h>

main() {
Variable Before After float rad;
exeution execution float area, peri;
rad -3.00000 -3.00000 printf( “Enter the radius “ );
area ?? ?? scanf(“%f” , &rad);
peri ?? ?? if ( rad > 0.0 ) {
area = 3.14159 * rad * rad;
peri = 6.28318 * rad;

printf( “Area = %f\n” , area );


Prints “Negative radius” and printf( “Peri = %f\n” , peri );
}
Program ends
else
printf( “Negative radius\n”);
}

Lectures on Numerical Methods 38


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.

Lectures on Numerical Methods 39


Scanf Function
scanf ( format, &intvar, &fltvar );
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 ??

Lectures on Numerical Methods 40


Algorithms
Algorithm
Problem
Let S be a finite sequence of 1. The m1 be the largest in
positive nonzero integers ak, except S1. Clearly m1 = a1.
the last number which is always 0. 2. Let I = 1.
Find the largest number in the 3. If aI+1 is zero, go to step
sequence. 7.
Example 4. mI+1 = max { mI, aI+1}.
5. Increment I.
7, 9, 4, 6, 2, 5, 8, 0
6. Go to step 3.
The largest is 9. 7. Print mI.
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 }

Lectures on Numerical Methods 41


An Example
Find Largest
/* Find the largest number */
#include <stdio.h>
The program produces the
main() {
following output int number, largest;

Enter a number 7 printf( "Enter a number " );


scanf( "%d" , &largest );
Enter a number 9
Enter a number 4 printf( "Enter a number " );
Enter a number 6 scanf( "%d" , &number );

Enter a number 2 while ( number > 0 ) {


Enter a number 0 if ( number > largest )
largest = number;
Largest is 9
printf( "Enter a number " );
scanf( "%d" , &number );
}

printf( "Largest is %d\n" ,


largest );
}

Lectures on Numerical Methods 42


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

Lectures on Numerical Methods 43

You might also like