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.

Slides:



Advertisements
Similar presentations
Introduction to C Systems Programming Concepts. Introduction to C A simple C Program A simple C Program –Variable Declarations –printf ( ) Compiling and.
Advertisements

Lectures on Numerical Methods1 Statements çExpressions, when terminated by a semicolon, become statements. çExamples X = 5; I++; IsPrime(c); c = 5 * (
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Lecture 2 Introduction to C Programming
True or false A variable of type char can hold the value 301. ( F )
Introduction to Systems Programming - Recitation Omer Kotlicki Two instances: 1.Tuesdays 15:00-16:00; Kitot Wednesdays.
1 ICS103 Programming in C Lecture 3: Introduction to C (2)
Differences between Java and C CS-2303, C-Term Differences between Java and C CS-2303, System Programming Concepts (Slides include materials from.
1 CSE1301 Computer Programming Lecture 10: Iteration (Part 1)
Basic C Programming Data Types and Arithmetic Operations 01/30/15.
CS1061 C Programming Lecture 5: Building Blocks of Simple Programs A. O’Riordan, 2004.
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
C. About the Crash Course Cover sufficient C for simple programs: variables and statements control functions arrays and strings pointers Slides and captured.
1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1)
Lectures on Numerical Methods1 Tokens in C zKeywords  These are reserved words of the C language. For example int, float, if, else, for, while etc. zIdentifiers.
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
Computer Science 210 Computer Organization Introduction to C.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
1. Function prototype Function prototype is a declaration; indicates the function exists Should have function name, return type and parameter Placed before.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
C Programming n General Information on C n Data Types n Arithmetic Operators n Relational Operators n if, if-else, for, while by Kulapan Waranyuwat.
1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1) Linda M c Iver.
/* C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
Structure of a C program Preprocessor directive (header file) Program statement } Preprocessor directive Global variable declaration Comments Local variable.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
CECS 121 Test 1. Functions allow you to group program statements under one name C and C++ are case-sensitive so main(), MAIN(), and Main() are all different.
BASICS CONCEPTS OF ‘C’.  C Character Set C Character Set  Tokens in C Tokens in C  Constants Constants  Variables Variables  Global Variables Global.
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
Constants, Variables and Data types in C The C character Set A character denotes any alphabet, digit or special symbol used to represent information.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
1 Programming a Computer Lecture Ten. 2 Outline  A quick introduction to the programming language C  Introduction to software packages: Matlab for numerical.
Control statements Mostafa Abdallah
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
NOTE: C programs consist of functions one of which must be main. C programs consist of functions one of which must be main. Every C program begins executing.
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Introduction to ‘c’ language
Computer Science 210 Computer Organization
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
Chapter 4 C Program Control Part I
ECE Application Programming
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
- Standard C Statements
Tokens in C Keywords Identifiers Constants
CS1010 Discussion Group 11 Week 4 – Overview of C programming.
INC 161 , CPE 100 Computer Programming
ICS103 Programming in C Lecture 3: Introduction to C (2)
CSI 121 Structure Programming Language Lecture 10: Iteration (Part 1)
Computer Science 210 Computer Organization
Arrays, Part 1 of 2 Topics Definition of a Data Structure
IDENTIFIERS CSC 111.
Lectures on Numerical Methods
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Lectures on Numerical Methods1 Tokens in C zKeywords  These are reserved words of the C language. For example int, float, if, else, for, while etc. zIdentifiers.
Program Breakdown, Variables, Types, Control Flow, and Input/Output
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Dale Roberts, Lecturer IUPUI
DATA TYPES There are four basic data types associated with variables:
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Introduction to C Programming
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Presentation transcript:

Lectures on Numerical Methods1 An Example zCompute Squares çPrints a table of squares as shown below /* 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; }

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

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

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

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 – ) /* 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; }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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.

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|”, ); | | çPrintf( “|%f|”, ); | | çPrintf( “|%8.3d|”, ); | | çPrintf( “ Age = %d years”, 22 ); Age = 22 years çPrintf( “%d and %d”, 1, 2 ); 1 and 2

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 = Peri = ç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 = * rad * rad; peri = * 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 = * rad * rad; peri = * rad; printf( “Area = %f\n”, area ); printf( “Peri = %f\n”, peri ); } else printf( “Negative radius\n”); }

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 = * rad * rad; peri = * 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 = * rad * rad; peri = * rad; printf( “Area = %f\n”, area ); printf( “Peri = %f\n”, peri ); } else printf( “Negative radius\n”); }

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 = * rad * rad; peri = * 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 = * rad * rad; peri = * rad; printf( “Area = %f\n”, area ); printf( “Peri = %f\n”, peri ); } else printf( “Negative radius\n”); } VariableBefore exeution After execution rad ?? area ?? peri ??

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 = * rad * rad; peri = * 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 = * rad * rad; peri = * rad; printf( “Area = %f\n”, area ); printf( “Peri = %f\n”, peri ); } else printf( “Negative radius\n”); } VariableBefore exeution After execution rad ?? area ?? peri ??

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 = * rad * rad; peri = * 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 = * rad * rad; peri = * rad; printf( “Area = %f\n”, area ); printf( “Peri = %f\n”, peri ); } else printf( “Negative radius\n”); } VariableBefore exeution After execution rad area ?? peri ??

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 = * rad * rad; peri = * 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 = * rad * rad; peri = * rad; printf( “Area = %f\n”, area ); printf( “Peri = %f\n”, peri ); } else printf( “Negative radius\n”); } VariableBefore exeution After execution rad area ?? peri ??

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 = * rad * rad; peri = * 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 = * rad * rad; peri = * rad; printf( “Area = %f\n”, area ); printf( “Peri = %f\n”, peri ); } else printf( “Negative radius\n”); } VariableBefore exeution After execution rad area peri ??

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 = * rad * rad; peri = * 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 = * rad * rad; peri = * rad; printf( “Area = %f\n”, area ); printf( “Peri = %f\n”, peri ); } else printf( “Negative radius\n”); } VariableBefore exeution After execution rad area peri

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 = * rad * rad; peri = * 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 = * rad * rad; peri = * rad; printf( “Area = %f\n”, area ); printf( “Peri = %f\n”, peri ); } else printf( “Negative radius\n”); } VariableBefore exeution After execution rad area peri

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 = * rad * rad; peri = * 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 = * rad * rad; peri = * rad; printf( “Area = %f\n”, area ); printf( “Peri = %f\n”, peri ); } else printf( “Negative radius\n”); } VariableBefore exeution After execution rad area peri

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 = * rad * rad; peri = * 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 = * rad * rad; peri = * rad; printf( “Area = %f\n”, area ); printf( “Peri = %f\n”, peri ); } else printf( “Negative radius\n”); } VariableBefore exeution After execution rad ?? area ?? peri ??

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 = * rad * rad; peri = * 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 = * rad * rad; peri = * rad; printf( “Area = %f\n”, area ); printf( “Peri = %f\n”, peri ); } else printf( “Negative radius\n”); } VariableBefore exeution After execution rad area ?? peri ??

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 = * rad * rad; peri = * 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 = * rad * rad; peri = * rad; printf( “Area = %f\n”, area ); printf( “Peri = %f\n”, peri ); } else printf( “Negative radius\n”); } VariableBefore exeution After execution rad area ?? peri ??

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.

Lectures on Numerical Methods40 Scanf Function Format“%d%f”“%3d%6f” InputIntvarFltvarIntvarFltvar \t4.5 (tab) \n4.5(newline) A3a4.5?? 3a4.53??3 çscanf ( format, &intvar, &fltvar );

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.

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

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.