programing and algorithm, Summaries of Advanced Computational Complexity

this is the most experience book in the market where your prof will not not want to show u all but behold your prof is here to save your all

Typology: Summaries

2023/2024

Uploaded on 03/27/2026

gen-benkukune
gen-benkukune 🇿🇦

1 document

1 / 41

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Unit 1
Algorithm and Programming Development
Steps in development of a program
Program Development Life Cycle
When we want to develop a program using any programming language, we follow a
sequence of steps. These steps are called phases in program development. The program
development life cycle is a set of steps that are used to develop a program in any
language.
Generally, the program development life cycle contains 6 phases, they are as follows….
Problem Definition
Problem Analysis
Algorithm Development
Coding & Documentation
Testing & Debugging
Maintenance
1. Problem Definition
In this phase, we define the problem statement and we decide the boundaries of the
problem. In this phase we need to understand the problem statement, what is our
requirement, what should be the output of the problem solution. These are defined in this
first phase of the program development life cycle.
2. Problem Analysis
In phase 2, we determine the requirements like variables, functions, etc. to solve the
problem. That means we gather the required resources to solve the problem defined in
the problem definition phase. We also determine the bounds of the solution.
3. Algorithm Development
During this phase, we develop a step by step procedure to solve the problem using the
specification given in the previous phase. This phase is very important for program
development. That means we write the solution in step by step statements.
4. Coding & Documentation
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29

Partial preview of the text

Download programing and algorithm and more Summaries Advanced Computational Complexity in PDF only on Docsity!

Unit 1 Algorithm and Programming Development Steps in development of a program Program Development Life Cycle When we want to develop a program using any programming language, we follow a sequence of steps. These steps are called phases in program development. The program development life cycle is a set of steps that are used to develop a program in any language. Generally, the program development life cycle contains 6 phases, they are as follows….

  • Problem Definition
  • Problem Analysis
  • Algorithm Development
  • Coding & Documentation
  • Testing & Debugging
  • Maintenance 1. Problem Definition In this phase, we define the problem statement and we decide the boundaries of the problem. In this phase we need to understand the problem statement, what is our requirement, what should be the output of the problem solution. These are defined in this first phase of the program development life cycle. 2. Problem Analysis In phase 2, we determine the requirements like variables, functions, etc. to solve the problem. That means we gather the required resources to solve the problem defined in the problem definition phase. We also determine the bounds of the solution. 3. Algorithm Development During this phase, we develop a step by step procedure to solve the problem using the specification given in the previous phase. This phase is very important for program development. That means we write the solution in step by step statements. 4. Coding & Documentation

This phase uses a programming language to write or implement the actual programming instructions for the steps defined in the previous phase. In this phase, we construct the actual program. That means we write the program to solve the given problem using programming languages like C, C++, Java, etc.,

5. Testing & Debugging During this phase, we check whether the code written in the previous step is solving the specified problem or not. That means we test the program whether it is solving the problem for various input data values or not. We also test whether it is providing the desired output or not. 6. Maintenance During this phase, the program is actively used by the users. If any enhancements found in this phase, all the phases are to be repeated to make the enhancements. That means in this phase, the solution (program) is used by the end-user. If the user encounters any problem or wants any enhancement, then we need to repeat all the phases from the starting, so that the encountered problem is solved or enhancement is added. Algorithm and Flowchart development ALGORITHM : The word “algorithm” relates to the name of the mathematician Al-khowarizmi, which means a procedure or a technique. Software Engineer commonly uses an algorithm for planning and solving the problems. An algorithm is a sequence of steps to solve a particular problem or algorithm is an ordered set of unambiguous steps that produces a result and terminates in a finite time Algorithm has the following characteristics

  • Input : An algorithm may or may not require input
  • Output: Each algorithm is expected to produce at least one result
  • Definiteness : Each instruction must be clear and unambiguous.
  • Finiteness : If the instructions of an algorithm are executed, the algorithm should terminate after finite number of steps

Step 4 Output the results of your algorithm's operations : In case of area of rectangle output will be the value stored in variable AREA. if the input variables described a rectangle with a HEIGHT of 2 and a WIDTH of 3, the algorithm would output the value of 6. FLOWCHART The first design of flowchart goes back to 1945 which was designed by John Von Neumann. Unlike an algorithm, Flowchart uses different symbols to design a solution to a problem. It is another commonly used programming tool. By looking at a Flowchart one can understand the operations and sequence of operations performed in a system. Flowchart is often considered as a blueprint of a design used for solving a specific problem. Advantages of flowchart:

  • Flowchart is an excellent way of communicating the logic of a program.
  • Easy and efficient to analyze problem using flowchart.
  • During program development cycle, the flowchart plays the role of a blueprint, which makes program development process easier.
  • After successful development of a program, it needs continuous timely maintenance during the course of its operation. The flowchart makes program or system maintenance easier.
  • It is easy to convert the flowchart into any programming language code. Flowchart is diagrammatic /Graphical representation of sequence of steps to solve a problem. To draw a flowchart following standard symbols are use Symbol Name Symbol function Oval Used to represent start and end of flowchart Parallelogra m Used for input and output operation

Rectangle Processing: Used for arithmetic operations and data- manipulations Diamond Decision making. Used to represent the operation in which there are two/three alternatives, true and false etc Arrows Flow line Used to indicate the flow of logic by connecting symbols Circle Page Connector Off Page Connector Predefined Process /Function Used to represent a group of statements performing one processing task. Preprocessor

Comments Algorithm & Flowchart to find the sum of two numbers

step- 6 Stop Programme Debugging Definition: Debugging is the process of detecting and removing of existing and potential errors (also called as ‘bugs’) in a software code that can cause it to behave unexpectedly or crash. To prevent incorrect operation of a software or system, debugging is used to find and resolve bugs or defects. When various subsystems or modules are tightly coupled, debugging becomes harder as any change in one module may cause more bugs to appear in another. Sometimes it takes more time to debug a program than to code it. To debug a program, user has to start with a problem, isolate the source code of the problem, and then fix it. A user of a program must know how to fix the problem as knowledge about problem analysis is expected. When the bug is fixed, then the software is ready to use. Debugging tools (called debuggers) are used to identify coding errors at various development stages. They are used to reproduce the conditions in which error has occurred, then examine the program state at that time and locate the cause. Programmers can trace the program execution step-by-step by evaluating the value of variables and stop the execution wherever required to get the value of variables or reset the program variables. Some programming language packages provide a debugger for checking the code for errors while it is being written at run time.

Unit 2 Program Structure I/O statements, assign statements Unformatted and Formatted IOS In C Language input and output function are available as C compiler function or C library provided with each C compiler implementation. These all functions are collectively known as Standard I/O Library function. Here I/O stands for Input and Output used for different inputting and outputting statements. These I/O functions are categorized into three prcessing functions. Console input/output function (deals with keyborad and monitor), disk input/output function (deals with floppy or hard disk) and port input/output function (deals with serial or parallet port). As all the input/output statements deals with the console, so these are also Console Input/Output functions. Console Input/Output function access the three major files before the execution of a C Program. These are as: stdin: This file is used to receive the input (usually is keyborad file, but can also take input from the disk file). stdout: This file is used to send or direct the output (usually is a monitor file, but can also send the output to a disk file or any other device). stderr: This file is used to display or store error messages. Input Ouput Statement Input and Output statement are used to read and write the data in C programming. These are embedded in stdio.h (standard Input/Output header file). There are mainly two of Input/Output functions are used for this purpose. These are discussed as: A)Unformatted I/O functions There are mainly six unformatted I/O functions discussed as follows: a) getchar() b) putchar() c) gets() d) puts() e) getch() f) getche() a) getchar() This function is an Input function. It is used for reading a single character from the keyborad. It is buffered function. Buffered functions get the input from the keyboard and store it in the memory buffer temporally until you press the Enter key. The general syntax is as: v = getchar();

a non-buffered function. The character data read by this function is directly assign to a variable rather it goes to the memory buffer, the character data directly assign to a variable without the need to press the Enter key. Another use of this function is to maintain the output on the screen till you have not press the Enter Key. The general syntax is as: v = getch(); where v is the variable of character type. A simple C program to illustrate the use of getch() function: f) getche() All are same as getch() function execpt it is an echoed function. It means when you type the character data from the keyboard it will visible on the screen. The general syntax is as: v = getche(); where v is the variable of character type. A simple C program to illustrate the use of getch() function: B)Formatted I/O functions Formatted I/O functions which refers to an Input or Ouput data that has been arranged in a particular format. There are mainly two formatted I/O functions discussed as follows: a) scanf() b) printf() a) scanf() The scanf() function is an input function. It used to read the mixed type of data from keyboard. You can read integer, float and character data by using its control codes or format codes. Format Code Meaning %c To read a single character %d To read a signed decimal integer (short) %ld To read a signed long decimal integer /Program to illustrate the use of formatted code by using the formatted scanf() function / #include main() { int abc; printf("Enter the integer value"); scanf("%d”, &abc); getch(); } b) printf() This is an output function. /Below the program which show the use of printf() function/ main() { int a; printf("Enter the mixed type of data"); scanf("%d”,&a);

getch(); } Variables,Constant and data types Variable A variable is an identifier which is used to store some value. Constants can never change at the time of execution. Variables can change during the execution of a program and update the value stored inside it. A single variable can be used at multiple locations in a program. A variable name must be meaningful. It should represent the purpose of the variable. Example: Height, age, are the meaningful variables that represent the purpose it is being used for. Height variable can be used to store a height value. Age variable can be used to store the age of a person A variable must be declared first before it is used somewhere inside the program. A variable name is formed using characters, digits and an underscore. Following are the rules that must be followed while creating a variable: A variable name should consist of only characters, digits and an underscore. A variable name should not begin with a number. A variable name should not consist of whitespace. A variable name should not consist of a keyword. 'C' is a case sensitive language that means a variable named 'age' and 'AGE' are different. For example, we declare an integer variable my_variable and assign it the value 48: int my_variable; my_variable = 48; By the way, we can both declare and initialize (assign an initial value) a variable in a single statement: int my_variable = 48; Data types 'C' provides various data types to make it easy for a programmer to select a suitable data type as per the requirements of an application. Following are the three data types: Primitive data types Derived data types User-defined data types There are five primary fundamental data types, int for integer data char for character data float for floating point numbers double for double precision floating point numbers void Array, functions, pointers, structures are derived data types. 'C' language provides more extended versions of the above mentioned primary data types. Each data type differs from one another in size and range. Following table displays the size and range of each data type. Integer data type Integer is nothing but a whole number. The range for an integer data type varies from machine to machine. The standard range for an integer data type is - 32768 to 32767. Whenever we want to use an integer data type, we have place int before the identifier such as, int age; Here, age is a variable of an integer data type which can be used to store integer values. Constants Constants are the fixed values that never change during the execution of a program.

AND A < B AND B < C Result is True if both AB) Result is True if A>B is false else true Selection control Statements Selection Control Example Meaning IF ( Condition ) Then … ENDIF

IF ( X > 10 ) THEN Y=Y+

ENDIF

If condition X>10 is True execute the statement between THEN and ENDIF IF ( Condition ) Then … ELSE ….. ENDIF

IF ( X > 10 ) THEN Y=Y+

ELSE Y=Y+8 Z=Z+

ENDIF

If condition X>10 is True execute the statement between THEN and ELSE otherwise execute the statements between ELSE and ENDIF Loop control Statements Selection Control Example Meaning WHILE (Condition) DO .. .. ENDDO

WHILE ( X < 10) DO

print x x=x+ ENDDO Execute the loop as long as the condition is TRUE DO …. … UNTILL (Condition)

DO

print x x=x+ UNTILL ( X >10) Execute the loop as long as the condition is false Data type casting Converting one datatype into another is known as type casting or, type-conversion. For example, if you want to store a 'long' value into a simple integer then you can type cast 'long' to 'int'. You can convert the values from one type to another explicitly using the cast operator as follows − (type_name) expression Consider the following example where the cast operator causes the division of one integer variable by another to be performed as a floating-point operation − main() { int sum = 17 , count = 5 ; double mean;

mean = (double) sum / count; printf("Value of mean : %f\n", mean ); } When the above code is compiled and executed, it produces the following result − Value of mean : 3. It should be noted here that the cast operator has precedence over division, so the value of sum is first converted to type double and finally it gets divided by count yielding a double value. Type conversions can be implicit which is performed by the compiler automatically, or it can be specified explicitly through the use of the cast operator. It is considered good programming practice to use the cast operator whenever type conversions are necessary

A switch statement allows a variable to be tested for equality against a list of values. 5 nested switch statements You can use one switch statement inside another switch statement(s). . C provides two styles of flow control:

  • Branching
  • Looping Branching is deciding what actions to take and looping is deciding how many times to take a certain action. Branching is so called because the program chooses to follow one branch or another. 1.if statement This is the most simple form of the branching statements. It takes an expression in parenthesis and an statement or block of statements. if the expression is true then the statement or block of statements gets executed otherwise these statements are skipped. NOTE: Expression will be assumed to be true if its evaulated values is non-zero. if statements take the following form: Show Example if (expression) statement; or if (expression) { Block of statements; } or if (expression) { Block of statements; } else { Block of statements; }

or if (expression) { Block of statements; } else if(expression) { Block of statements; } else { Block of statements; } 2.switch statement: The switch statement is much like a nested if .. else statement. Its mostly a matter of preference which you use, switch statement can be slightly more efficient and easier to read. Show Example switch( expression ) { case constant-expression1: statements1; [case constant-expression2: statements2;] [case constant-expression3: statements3;] [default : statements4;] } If none of the listed conditions is met then default condition executed. Looping Loops provide a way to repeat commands and control how many times they are repeated. C provides a number of looping way. 1)while loop The most basic loop in C is the while loop.A while statement is like a repeating if statement. Like an If statement, if the test condition is true: the statments get executed. The difference is that after the statements have been executed, the test condition is checked again. If it is still true the statements get executed again.This cycle repeats until the test condition evaluates to false. Basic syntax of while loop is as follows: Show Example while ( expression ) { Single statement

This will produce following output: Hello 0 Hello 1 Hello 2 Hello 3 Hello 4 Hello 6 Hello 7 Hello 8 Hello 9 Hello 10 The break; continue; and goto; statements are used to alter the normal flow of a program. Loops perform a set of repetitive task until text expression becomes false but it is sometimes desirable to skip some statement/s inside loop or terminate the loop immediately without checking the test expression. In such cases, break and continue statements are used. break statement In C programming, break statement is used with conditional if statement. The break is used in terminating the loop immediately after it is encountered. break; The break statement can be used in terminating loops like for , while and do...while Just like break, continue is also used with conditional if statement.

go to statement

  • Though, using goto statement give power to jump to any part of program, using goto statement makes the logic of the program complex and tangled.
  • In modern programming, goto statement is considered a harmful construct and a bad programming practice.
  • The goto statement can be replaced in most of C program with the use of break and continue statements.
  • In fact, any program in C programming can be perfectly written without the use of goto statement.