Fundamentals of Programming: Errors in C++ and Algorithms with Flowcharts, Cheat Sheet of Programming Languages

The different types of errors in C++ programming, including syntax, runtime, linker, logical, and semantic errors. It also provides examples and pseudocode for algorithms and flowcharts to convert length units and determine student grades and quadratic equation roots. The document concludes with decision structures using if-then-else statements.

Typology: Cheat Sheet

2020/2021

Uploaded on 10/16/2021

unknown-person-12
unknown-person-12 🇵🇰

3 documents

1 / 22

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Fundamentals of Programming
Lecture 4a:Errors in C++, Algorithms and Flowchart
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16

Partial preview of the text

Download Fundamentals of Programming: Errors in C++ and Algorithms with Flowcharts and more Cheat Sheet Programming Languages in PDF only on Docsity!

Fundamentals of Programming

Lecture 4a: Errors in C++, Algorithms and Flowchart

Errors in C++

  • Illegal operation performed
  • Detected at the time of compilation Types
  1. Syntax error
  2. Run time error
  3. Linker error
  4. Logical error
  5. Semantic error

Example Syntax Error void main() { int x = 10; int y = 15; cout<<x<<y //semicolon missed }

Run Time Error Runtime errors occur when a program with no syntax errors asks the computer to do something that the computer is unable to reliably do.

  • Trying to divide by a variable that contains a value of zero
  • Trying to open a file that doesn't exist
  • There is no way for the compiler to know about these kinds of errors when the program is compiled

Linker Errors After compilation different object files are linked with main’s object, errors generated when the executable of the program cannot be generated

  • wrong function prototyping
  • incorrect header files
  • most common linker error is writing Main() instead of main()

Example Linker Error void Main() // Here Main() should be main() { int a = 10; cout << a; }

Semantic Error Semantic error occurs when the statements written in the program are not meaningful to the compiler Example: void main() { int a, b, c; a + b = c; //semantic error }

Algorithms and Flowcharts

Pseudocode: Input the length in feet (lft) Calculate the length in centimeter (lcm) by multiplying feet with 30 Print length in centimeter (lcm) Algorithm: Flowchart: Step 1: Input lft Step 2: lcm lft x 30 Step 3: Print lcm START Input lft Print lcm STOP lcm lft x 30 13

Example 2 Write an algorithm to determine a student’s final grade and indicate whether it is passing or failing. The final grade is calculated as the average of four marks Pseudocode: Input a set of 4 marks Calculate their average by summing and dividing by 4 if average is below 50 Print “FAIL” else Print “PASS” 14

Example 3 Write an algorithm and draw a flowchart that will calculate the roots of a quadratic equation (ax 2

  • bx +c = 0) Hint: d=sqrt(𝑏 2
  • 4ac) Roots: x1 = (–b + d)/2a, x2 = (–b – d)/2a 16

Pseudocode: Input the coefficients (a,b,c) of the quadratic equation Calculate d Calculate x1 Flowchart: Calculate x Print x1 and x Algorithm: Step 1: Input a, b, c Step 2: d sqrt(bxb – 4xaxc) Step 3: x1 (- b+d) / (2xa) Step 4: x2 (- b-d) / (2xa) Step 4: Print x1, x START Input a, b, c STOP X 2 (– b d ) / (2 x a ) x 1 (– b + d ) / (2 x a ) d sqrt( b x b – 4 x a x c ) Print x 1, x 2 17

Example 4 Write an algorithm that reads two values, determines the largest value and prints largest value with an identifying message 19

Algorithm: Flowchart 20