0% found this document useful (0 votes)
4 views18 pages

PDF - Algorithm and Programming

The document discusses problem-solving techniques in everyday life and through computers, outlining steps such as problem definition, analysis, algorithm development, and program execution. It also introduces C++ programming, its features, and the basic structure of a C++ program, including keywords and data types. Additionally, it covers the importance of variables, their naming conventions, and memory management in C++.

Uploaded by

nasir77a10
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
4 views18 pages

PDF - Algorithm and Programming

The document discusses problem-solving techniques in everyday life and through computers, outlining steps such as problem definition, analysis, algorithm development, and program execution. It also introduces C++ programming, its features, and the basic structure of a C++ program, including keywords and data types. Additionally, it covers the importance of variables, their naming conventions, and memory management in C++.

Uploaded by

nasir77a10
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
General Problem Solving Concepts Problem Solving in Everyday Life People make decisions every day to solve problems that affect their lives. The problems may be as unimportant as what to watch on television or as important as choosing a new job. If a bad decision is made, time and resources are lost, so it’s important that people know how to make decisions well Types of Problems that can be solved on computers 1. Computational : problems involving some kind of mathematical processing 2. Logical : Problems involving relational or logical processing 3. Repetitive : Problems involving repeating a set of mathematical and/or logical instructions. Dr. Omaima Bahaidara... .. Programming and problem Solving Problem Solving with computers: Solving a Problem using a computer passes by certain steps: Step 1: Problem definition and specification, which is given by the full and detailed description of the problem. Step 2: Problem analysis. Based on the definition of the problem and data to be used, in this step the problem is to be analyzed to its main components. Step 3: Algorithm development. Solving the problem in the form of an algorithm. The sequence of instructions must be clear and precise Step 4: Program writing (Program coding). According to the algorithm, a program could be written using a given programming language. Step 5: Program transfer to the computer. The first form of the program is, known as the source program. This step includes also correcting syntax and semantic errors in the program. Step 6: Program testing and translation. The compiler translates the error-free, source program into object program, after being tested using several data sets to detect any logic errors. Step 7: Execution of program and output result. Dr. Omaima Bahaidara...... (2) Programming and problem Solving Difficulties with Problem Solving The problem-solving process is not easy. It takes practice and time to perfect, but in the long run the process proves to be of great benefit. When solving problems on the computer, one of the most difficult tasks for the problem solver is writing the instructions. Take the task of deciding which number is the largest from a group of three numbers. Almost anyone can immediately tell which is the largest, but many cannot explain the steps they followed to arrive at it. Most people will say, “I can’t explain how 1 know, 1 just know it!” This explanation is not good enough for the computer. The computer is a tool that will perform only tasks that the user can explain. ‘The computer has a specific system of communication that programmers and users must learn, This system demands that no step in the solution to a problem be left unstated and that all steps be in the proper order. You must assume the computer knows nothing except what you tell it and think of it as an ignorant but efficient aid to problem solving. Dr. Omaima Bahaidara...... (3 ) Programming and problem Solving Algorithm & Flowchart Algorithm: An algorithm is a given set of well defined, unambiguous processes (actions) for solving a problem in a finite number of steps. Flowchart: A flowchart represents a good, visible graph-wise way of solving algorithms. It is a block diagram that uses predefined symbols and interconnecting lines to represent the logic and sequence of specific program operation. Basic Program Flowcharting Symbols: Cc) Terminal (start / stop ) Input / Output Operation Processing ( calculation and storing operation) Dr. Omaima Bahaidara..... (4 ) Programming and problem Solving Decision (conditional branching) [| — Flow lines (direction) Connection Types of program flowcharts: We can distinguish between 3 main different controls in the algorithms presented by flowcharts. These controls are known as the types of program flowcharts: 1. Sequential flowchart 2. Branching (jump) flowchart 3. Loop (repetition) flowchart Dr. Omaima Bahaidara...... (5 ) Programming and problem Solving Depending on the given problem, sequential, branching and looping parts are combined in different ways to represent the solution of the given problem. Each type of flowchart could be described by its features which are represented in the following paragraphs. Generally, the problem should be analyzed before deciding about the solution. The logical order of steps of the solution indicates the type of flowcharts to be used, i.e. the final form of the solution. In addition to these three main types, nested loops in flowcharts represent another flowchart type which is used when the solution includes the dependence of one loop parameters on the parameters of another loop, 1. Sequential flowchart It is simplest type of flowcharts, involving no decision (i.e. no branches or loops). Fig. 1 gives an example for this type of flowcharts. When such a program is executed, each statement will be carried out respecting the order given by the flowchart. Dr. Omaima Bahaidara...... ( 6 ) Programming and problem Solving — Statement 1 v Statement Fig. 1: A sequential flowchart 2. Branching (jump) flowchart In this type of flowcharts, one or more decision boxes are introduced, and the action taken depends upon whether the result of the decision test is true (yes) or false (no). This operation is illustrated by Fig. 2. Fig. 2: Branching flowchart. Dr. Omaima Bahaidara...... (7 ) Programming and problem Solving 3. Loop (repetition) flowchart ‘A loop represents a segment which may be repeated a definite or indefinite number of times. In Fig. 3, the instructions are executed one or more cycles when the logical expression is true. When the logical expression become no longer tue, the control is transferred to the instruction outside the loop. | Instru Logical Inside Loop Expression Logical Expresion Tastruction Inside Loop T ¥ Tas Instruction Outside Loop Outside Loop Y v Fig. 3: Looping flowcharts Dr. Omaima Bahaidara (8) Programming and problem Solving e Nested loops in flowchart Sometimes, there is need to include a logical decision which is dependent on the response to another logical expression, which is also performed inside the loop. This is known as nested looping, where their use is dependent in mutual way. Dr. Omaima Bahaidara..... ( 9 ) Programming and problem Solving Introduction to C+ programming What Is C++ Programming? C++ is a general-purpose programming language that was developed by Bjame Stroustrup as an enhancement of the C language to add object-oriented paradigm. Y C++ is a cross-platform language that can be used to create high-performance applications. Y C++ was developed by Bjarne Stroustrup, as an extension to the C language. Y C++ gives programmers a high level of control over system resources and memory. ‘The main features C++ programming language are as follows: + Simple: It is a simple language in the sense that programs can be broken down into logical units and parts, and has a rich library support and a variety of datatypes. + Machine Independent: C++ code can be run on any machine as ong as a suitable compiler is provided. + Low-level Access: C++ provides low-level access to system resources, which makes it a suitable choice for system programming and writing efficient code. + Fast Execution Speed: C++ is one of the fastest high-level languages. There is no additional processing overhead in C+, it is blazing fast. + Object-Oriented: One of the strongest points of the language which sets it apart from C. Object-Oriented support helps C++ to make maintainable and extensible programs. i.e. large-scale applications can be built. © General Purpose and Portable © object oriented Language Main features of 7 Crt Language. > Efficient and Fast © supports abstraction and Reusability © Rich Library Support TOS. Abdulmalik Alsarori Principle of Programming Introduction to C+ programming Why Use C++ Programming? Y C++ is one of the world’s most popular programming languages. Y C++ can be found in today’s operating systems, Graphical User Interfaces, and embedded systems. Y C++ is an object-oriented programming language which gives a clear structure to programs and allows code to be reused, lowering development costs. Y C++ is portable and can be used to develop applications that can be adapted to multiple platforms. Y C++ is fun and easy to learn! Y AsC++is close to C, C# and Java, it makes it easy for programmers to switch to C++ or vice versa. Difference between C and C++ Y C#++ was developed as an extension of C, and both languages have almost the same syntax. Y The main difference between C and C++ is that C++ supports classes and objects, while C does not. First C++ Program The below C++ code shows the basic structure of a program : TOS. Abdulmalik Alsarori Principle of Programming Introduction to C++ programming Structure of the C++ program The basic structure of a C+ program defines the standard way every program must be written; otherwise, it will cause a compilation error. The structure includes: 1 | #include Header File 2 | using namespace std; Namespace declaration 3 | intmain() Execution begins at Main 4\f 5 | //This prints Hello World! Comment 6 | cout << "Hello, World!"; Statement 7 | return 0; Return 8 |} 1. Header File: #include adds input/output objects (cin, cout, etc.) via the preprocessor. Common headers: fstream (files), string (strings), vector (STL), bits/stde++.h (all-in-one). 2. Namespace Declaration: using namespace std; allows direct use of standard names like "cout" without std: 3. Main Function: int main() is the program's entry point; execution starts here and returns an integer with 0 return mean successful execution. [Link]: //for single line, /* */ for multi-line are ignored by the compiler and used only for code documentation. 5, Statement: Contains executable code. Here, cout << "Hello World!", prints the text on the screen using the insertion operator (<<) 6. Return: The return 0; statement terminates the main() function and indicates that the program executed successfully. TOS. Abdulmalik Alsarori Principle of Programming Introduction to C++ programming History of C++ C++ is an object-oriented, middle-level programming language developed by Bjarne Stroustrup at Bell Labs in 1979, originally called “C with Classes” and renamed to C++ in 1983. It extended C by adding features like classes, inheritance, and type checking to support object-oriented programming. Over time, it evolved through standards like C++98, C+#11, C+417, C+420, and the latest C++23, adding modern features for performance and safety. Today, C++ remains widely used in system software, game engines, competitive programming, and high-performance applications. Timeline of C++ Language Versions ad TOS. Abdulmalik Alsarori Principle of Programming Introduction to C++ programming C++ keywords C++ keywords are reserved words that have predefined meanings and cannot be used as identifiers (such as variable names, function names, or class names). They are fundamental to the structure and functionality of the C++ language. Here is a categorized list of standard C++ keywords: ype bool, char, char8_t,charl6_, char32_t, double, float, in, long, shor, signed, unsigned, void, wehar_t Define the type and size of data that a variable can hold, Control Flow break, case, continue, default, do, else, for, goto if, switch, while Control the low of ‘execution within a program, ‘Classes & OOP class, struct, union, enum, explicit, friend, mutable, operator, pri virtual te, protected, public, this, Pundamental for 0 bject-orented programming, defining custom types, access levels, and behavior Memory Management delete, new, sizeof, alignas, alignof Used for dynamic memory allocation/dealloca tion and querying data lignmentsize. Casting cconst_cast, dynamic_cast, reinterpret_cast, statie_cast Perform type (casting) beteen different data types Other Specifiers auto, const, constexpr, consteval, constinit, extern, inline, register, static, static_assert, threa «local, typedef, typename, volatile Provide additional information tothe compiler about the Tbchavor, storage, ‘ortype ofan Exceptions catch, throw, try, noexcept Used for handling runtime erors nd exceptions Namespaces & ‘Templates ‘namespace, template, using Orsanize code into logical scopes and ‘enable generic programming Boolean & Nul false, nullpt, true Represent boolean values and the null pointer constant. TOS. Abdulmalik Alsarori Principle of Programming Introduction to C++ programming Operator Alternatives and, eq, bitand, bitor, compl, not 0l_eg, OF, OF_e4, XOF, XOT_eq Alternative tokens for certain ‘operators, useful for keyboards without standard sumbols Concurrency & Modes co_await, co_return, co_yield, concept, requires, export, import, module Keywords iroduced in newer standards (CHI, C4417, C430) for danced features Tike

You might also like