L3-L5 Algorithm, Flowchart PDF
Document Details
Uploaded by SelfSufficientFunction8183
Manipal Institute of Technology
2024
CSE 1171
Tags
Related
Summary
This document is a set of lecture notes and tutorials on algorithms and flowcharts for use in C programming. It includes examples and practice questions.
Full Transcript
Introduction to Computing L3-L5 Review Problem solving Logic and its importance in problem solving Computational problems and its classifications Computer organization and operating system Different computer languages 25-09-2024 CSE 1171 Programming for Problem S...
Introduction to Computing L3-L5 Review Problem solving Logic and its importance in problem solving Computational problems and its classifications Computer organization and operating system Different computer languages 25-09-2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 2 Application software, System software & Hardware 25-09-2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 3 Objectives To learn and appreciate the following concepts Introduction to algorithms and flowcharts Algorithms and flowcharts for simple problems Typical C program development environment Introduction to C Language: Program Structure Simple C Programs Simple C programs 25-09-2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 4 Session outcome At the end of session the student will be able to write Algorithms and flowcharts for solving arithmetical and logical problems Simple C programs 25-09-2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 5 Algorithm A step by step procedure to solve a particular problem Named after Arabic Mathematician Abu Jafar Mohammed Ibn Musa Al Khowarizmi 25-09-2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 6 Algorithmic Notations Name of the algorithm [mandatory] [gives a meaningful name to the algorithm based on the problem] Start [Beginning of the algorithm] Step Number [mandatory] [indicate each individual simple task] Explanatory comment [optional] [gives an explanation for each step, if needed] Termination [mandatory] [tells the end of algorithm] 25-09-2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 7 Properties of an algorithm 25-09-2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 8 Steps to develop an algorithm 25-09-2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 9 Compute the area of circle Name of the algorithm : Compute the area of a circle Step1: Start Step 2: Input radius Step 3: [Compute the area] Area 3.1416 * radius * radius Step 4: [Print the Area] Print ‘Area of a circle =‘, Area Step 5: [End of algorithm] Stop 25-09-2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 10 Interchange values of two variables Name of the algorithm: Interchange values of 2 variables Step 1: Start Step 2: Input A,B Step 3: temp A Step 4: AB Step 5: Btemp Step 6: Print ‘A=’ , A Print ‘B=’ , B Step 7: [End of Algorithm] Stop 25-09-2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 11 Largest of 3 numbers Name of the algorithm: Find largest of 3 numbers Step 1: Start Step 5: [Compare A and C for largest] Step 2: [Read the values of A, B and C] if A>C then Read A, B, C Print ‘A’ is largest’ Step 3: [Compare A and B] else if A>B Go to step 5 Print ‘C’ is largest’ Step 4: [Otherwise compare B with C] Step 6: [End of the algorithm] if B>C then Stop Print ‘B’ is largest’ else Print ‘C’ is largest’ Go to Step 6 25-09-2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 12 Factorial of a number Name of the algorithm: Compute the factorial of a number Step1: Start Step 2: Input N Step 3: fact 1 Step 4: for count=1 to N in step of 1 do begin factfact*count end Step 5: Print ‘fact of N=‘, fact Step 6: [End of algorithm] Stop 25-09-2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 13 Tutorial on Algorithms Write the algorithm to Find the area of triangle when three sides are given. Add two integers Find the sum of digits of a number Find the Sum and Mean of first N natural numbers 25-09-2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 14 Flowcharts In Computer Science, Flow chart is used to represent algorithm which basically provides a solution to any computational problem. Flowchart: A graphical/pictorial representation of computation 25-09-2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 15 Basic Flowchart Symbols 25-09-2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 16 RAPTOR TOOL Raptor is a simple-to-use problem solving tool that enables the user to generate executable flowcharts. To introduce the computing discipline in order to develop problem solving skills and improve algorithmic thinking. The primary window consists of four main areas: 1. Symbols 2. Watch Window 3. Workspace 4. Menu and Toolbar 25-09-2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 17 Raptor symbols The six symbols used in Raptor are displayed in the Symbol Window in the upper left corner of the main window The assignment symbol is used to give a variable a numeric or string value. The call symbol is used to make calls to outside procedures, such as graphics routines. The input symbol is used for getting input from the user. The output symbol is used to display text to the Master Console. The selection structure is used for decision making. The loop structure is used for iteration and repetition. 25-09-2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 18 Raptor: Watch Window The area immediately below the Symbols area is the Watch Window. This area allows the user to view the current contents of any variables and arrays as the flowchart is executing. 25-09-2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 19 Raptor: Workspace The large, white area to the right is the primary Workspace. Users can build their flowcharts in this area and watch them update as they execute. The workspace is tabbed. Most flowcharts have a single tab named "main", but programmer-defined subcharts appear as tabs in the workspace. 25-09-2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 20 Raptor: Menu and Toolbars The final area is the menu and toolbar. This area allows the user to change settings and control the view and execution of individual flowcharts. 25-09-2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 21 Area of the circle Name of the algorithm: Compute the area of a circle Flowchart Step1: Input radius Step 2: [Compute the area] Area 3.1416 * radius*radius Step 3: [Print the Area] Print ‘Area of a circle =‘, Area Step 4: [End of algorithm] Stop 25-09-2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 22 Comparing two numbers Flowchart Name of the algorithm: Comparing 2 numbers Step 1: Start Step 2: Input num1, num2 Step 3: if num1 > num2 then Print num1 is bigger else Print num2 is bigger Step 4: end 25-09-2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 23 Swapping two numbers Name of the algorithm: Swapping 2 numbers Flowchart Step1: Input two numbers Step 2: [swapping] temp=a a=b b=temp Step 3: [Print] Print ‘after swapping=‘, a, b Step 4: [End of algorithm] Stop 25-09-2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 24 Find Factorial of given no Name of the algorithm: Compute the factorial of a number Step1: Start Step 2: Input data Step 3: factorial 1 Step 4: for i=1 to N in step of 1 do begin factorialfactorial*i end Step 5: Print ‘fact of N=‘, factorial Step 6: [End of algorithm] Stop 25-09-2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 25 Key features of flow chart Diagrammatic / visual / graphical representation of computation of an algorithm/pseudo code Easier to understand and analyze the problem and it’s solution before programming Machine independent Well suited for any type of logic 25-09-2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 26 Tutorial Write the flowchart to find the area of triangle when three sides are given Write the flowchart to add two integers 25-09-2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 27 Summary What is Algorithm and Flowchart Writing algorithms and drawing flowcharts for simple problems 25-09-2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 28 Programming for Problem Solving Steps in Problem Solving Problem solving is a part of our day to day activity. Algorithm Flowchart Code Documenting 25-09-2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 29 Typical C program development environment 25-09-2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 30 Typical C program development environment C programs typically go through six phases to be executed. These are: edit, preprocess, compile, link, load and execute Phase 1 : creating a program Phases 2 and 3: Preprocessing and Compiling a C Program Phase 4: Linking Phase 5: Loading Phase 6: Execution 25-09-2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 31 General Structure of C program 25-09-2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 32 25-09-2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 33 General Structure of C program 1. Documentation section: The documentation section consists of a set of comment lines giving the name of the program, the author and other details, which the programmer would like to use later. 2. Link section: The link section provides instructions to the compiler to link functions from the system library such as using the #include directive. 3. Definition section: The definition section defines all symbolic constants such using the #define directive. 4. Global declaration section: There are some variables that are used in more than one function. Such variables are called global variables and are declared in the global declaration section that is outside of all the functions. This section also declares all the user-defined functions. 25-09-2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 34 General Structure of C program 5. main () function section: Every C program must have one main function section. This section contains two parts; declaration part and executable part a) Declaration part: The declaration part declares all the variables used in the executable part. b) Executable part: There is at least one statement in the executable part. These two parts must appear between the opening and closing braces. The program execution begins at the opening brace and ends at the closing brace. The closing brace of the main function is the logical end of the program. All statements in the declaration and executable part end with a semicolon. 6. Subprogram section: If the program is a multi-function program then the subprogram section contains all the user-defined functions that are called in the main () function. User-defined functions are generally placed immediately after the main () function, although they may appear in any order. All sections, except the main () function section may be absent when they are not required. 25-09-2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 35 Hello world program 25-09-2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 36 Formatting in C Statements are terminated with semicolons. Indentation to be used for increased readability. Free format: white spaces and indentation is ignored by compiler. New line is represented by \n (Escape sequence). 25-09-2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 37 Formatting in C C is case sensitive – pay attention to lower and upper case letters when typing ! All keywords and standard functions are lower case Typing MAIN, Main etc instead of main is a compiler error Strings are placed in double quotes 25-09-2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 38 C program for reading a number and display it on the screen #include int main() { int num; printf("\nEnter the number: "); scanf("%d", &num); printf("The number read is: %d", num); return(0); } 25-09-2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 39 scanf() scanf ( ) is used to obtain the value from the user through an input device (keyboard). Eg: scanf(“%d”, &number); int main(){ int num; printf("\nEnter the number: "); scanf("%d", &num); printf(“The number read is: %d", num); return(0); } 25-09-2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 40 C program essentials: Format specifiers Similarly, the format specifiers are used with printf( ) function for printing/displaying. 25-09-2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 41 Input/ Output functions C provides the printf( ) to display the data on the output device (monitor). It is included in stdio.h int main() { int num; Examples are: printf("\nEnter the number: "); printf("programming isscanf("%d", an art"); &num); printf("The number read is: %d", num); printf("%d", number); return(0); printf("%f%f", p, q); } 25-09-2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 42 Adding two integers #include int main() { int sum; int integer1; int integer2; printf( "Enter first integer\n" ); scanf( "%d", &integer1 ); printf( "Enter second integer\n" ); scanf( "%d", &integer2 ); sum = integer1 + integer2; printf( "Sum is %d\n", sum ); return 0; } 25-09-2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 43 Syntax and Logical errors Syntax errors: violation of programming language rules (grammar). Detected by the compiler Eg: printf (“hello world”) // semicolon missing Logical errors: errors in meaning: Programs are syntactically correct but don’t produce the expected output User observes output of running program 25-09-2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 44 Summary Problem solving ! Typical C program development environment Program structure Formatting Input / Output Simple C programs 25-09-2024 CSE 1171 Programming for Problem Solving (PPS) - 2024 45