Copyright © 2018 Pearson Addison-Wesley. All rights reserved.
Course Objectives
 The aim is not to make you the expert of
computer programming but only the basics of
computer programming.
 To familiarize students with the use of C++.
 To equip students with tools and techniques to
implement a given problem programmatically
Copyright © 2018 Pearson Addison-Wesley. All rights reserved.
IDEs used for this course
 You can use any of the IDEs given below; but we
will use Dev C++ in the classes and Labs
 DEV C++
 Visual C++
 Turbo C++
Copyright © 2018 Pearson Addison-Wesley. All rights reserved.
Grading Criteria (Theory)
Total Marks = 100
Mid Term = 30%
Final Exam = 50%
Quizzes = 10%
Project = 10%
Copyright © 2018 Pearson Addison-Wesley. All rights reserved.
Grading Criteria (Lab)
Total Marks = 50
Mid Term = 30%
Final Exam = 50%
Assignment = 10%
Mega Task = 10%
Copyright © 2018 Pearson Addison-Wesley. All rights reserved.
Recommended Readings
Chapter No. 1 of C++ Programming from Problem
Analysis to Program Design written by DS Malik
Copyright © 2018 Pearson Addison-Wesley. All rights reserved.
Chapter 1
Introduction to Computers and
C++ Programming
Copyright © 2018 Pearson Addison-Wesley. All rights reserved.
Overview
1.1 Computer Systems
1.2 Programming and Problem Solving
1.3 Introduction to C++
1.4 Testing and Debugging
Copyright © 2018 Pearson Addison-Wesley. All rights reserved.
1.1
Computer Systems
Copyright © 2018 Pearson Addison-Wesley. All rights reserved.
Computer Systems
 A computer program is…
 A set of instructions for a computer to follow
 Computer software is …
 The collection of programs used by a computer

Includes:

Editors

Translators

System Managers
Copyright © 2018 Pearson Addison-Wesley. All rights reserved.
Hardware
 Three main classes of computers
 PCs (Personal Computer)

Relatively small used by one person at a time
 Workstation

Larger and more powerful than a PC
 Mainframe

Still larger

Requires support staff

Shared by multiple users
Copyright © 2018 Pearson Addison-Wesley. All rights reserved.
Data or Code?
 ‘A’ may look like 01000001
 65 may look like 01000001
 An instruction may look like 01000001
 How does the computer know the meaning
of 01000001?
 Interpretation depends on the current instruction
 Programmers rarely need to be concerned with
this problem.
 Reason as if memory locations contain letters and
numbers rather than zeroes and ones
Copyright © 2018 Pearson Addison-Wesley. All rights reserved.
Secondary and Main Memory
 Main memory stores instructions and
data while a program is running.
 Secondary memory
 Stores instructions and data between sessions
 A file stores data or instructions in
secondary memory
Copyright © 2018 Pearson Addison-Wesley. All rights reserved.
Memory Access
 Random Access
 Usually called RAM

Computer can directly access any memory location
 Sequential Access
 Data is generally found by searching through
other items first

More common in secondary memory
Copyright © 2018 Pearson Addison-Wesley. All rights reserved.
Computer Input
 Computer input consists of
 A program
 Some data
Display 1.3
Copyright © 2018 Pearson Addison-Wesley. All rights reserved.
High-level Languages
 Common programming languages include …
C C++ Java Pascal Visual Basic FORTRAN Perl
PHP Lisp Scheme Ada C# Python
 These high – level languages
 Resemble human languages
 Are designed to be easy to read and write
 Use more complicated instructions than
the CPU can follow
 Must be translated to zeros and ones for the CPU
to execute a program
Copyright © 2018 Pearson Addison-Wesley. All rights reserved.
Low-level Languages
 An assembly language command such as
ADD X Y Z
might mean add the values found at x and y
in memory, and store the result in location z.
 Assembly language must be translated to
machine language (zeros and ones)
0110 1001 1010 1011
 The CPU can follow machine language
Copyright © 2018 Pearson Addison-Wesley. All rights reserved.
Processing of C++ Program
 To execute a C++ program:
 Use an editor to create a source program in
C++
 Preprocessor directives begin with # and are
processed by the preprocessor
 Use the compiler to:

Check that the program obeys the language rules

Translate into machine language (object program)
Copyright © 2018 Pearson Addison-Wesley. All rights reserved.
Processing of C++ Program
 To execute a C++ program (cont'd.):
 Linker:

Combines object program with other programs
provided by the SDK to create executable code

Library: contains prewritten code you can use
 Loader:

Loads executable program into main memory
 Execution
 The last step is to execute the program
 Some IDEs do all this with a Build or Rebuild
command
Copyright © 2018 Pearson Addison-Wesley. All rights reserved.
Processing of C++ Program
Copyright © 2018 Pearson Addison-Wesley. All rights reserved.
Compilers
 Translate high-level language to
machine language
 Source code

The original program in a high level language
 Object code

The translated version in machine language
Display 1.4
Copyright © 2018 Pearson Addison-Wesley. All rights reserved.
Linkers
 Some programs we use are already compiled

Their object code is available for us to use

For example: Input and output routines
 A Linker combines

The object code for the programs we write
and

The object code for the pre-compiled routines
into
 The machine language program the CPU can
run
Display 1.5
Copyright © 2018 Pearson Addison-Wesley. All rights reserved.
1.2
Programming and Problem-
Solving
Copyright © 2018 Pearson Addison-Wesley. All rights reserved.
Program Design
 Programming is a creative process
 No complete set of rules for creating a program
 Program Design Process
 Problem Solving Phase

Result is an algorithm that solves the problem
 Implementation Phase

Result is the algorithm translated into a
programming
language Display 1.7
Copyright © 2018 Pearson Addison-Wesley. All rights reserved.
Problem Solving Phase
 Be certain the task is completely specified
 What is the input?
 What information is in the output?
 How is the output organized?
 Develop the algorithm before implementation
 Experience shows this saves time in getting
your program to run.
 Test the algorithm for correctness
Copyright © 2018 Pearson Addison-Wesley. All rights reserved.
Algorithms
 Algorithm
 A sequence of precise instructions that
leads to a solution
 Program
 An algorithm expressed in a language the
computer can understand
Display 1.6
Copyright © 2018 Pearson Addison-Wesley. All rights reserved.
Example 1
 Design an algorithm to find the perimeter and area
of a rectangle
 The perimeter and area of the rectangle are given
by the following formulas:
perimeter = 2 * (length + width)
area = length * width
Copyright © 2018 Pearson Addison-Wesley. All rights reserved.
Example 1
 Algorithm:
 Get length of the rectangle
 Get width of the rectangle
 Find the perimeter using the following equation:
perimeter = 2 * (length + width)
 Find the area using the following equation:
area = length * width
Copyright © 2018 Pearson Addison-Wesley. All rights reserved.
Implementation Phase
 Translate the algorithm into a programming
language
 Easier as you gain experience with the language
 Compile the source code
 Locates errors in using the programming language
 Run the program on sample data
 Verify correctness of results
 Results may require modification of
the algorithm and program
Copyright © 2018 Pearson Addison-Wesley. All rights reserved.
Software Life Cycle
 Analysis and specification of the task
(problem definition)
 Design of the software
(object and algorithm design)
 Implementation (coding)
 Maintenance and evolution of the system
 Obsolescence
Copyright © 2018 Pearson Addison-Wesley. All rights reserved.
Section 1.2 Conclusion
 Can you…
 Describe the first step to take when creating
a program?
 List the two main phases of the program
design process?
 Explain the importance of the problem-solving phase?
 List the steps in the software life cycle?
Copyright © 2018 Pearson Addison-Wesley. All rights reserved.
1.3
Introduction to C++
Copyright © 2018 Pearson Addison-Wesley. All rights reserved.
Introduction to C++
 Where did C++ come from?
 Derived from the C language
 C was derived from the B language
 B was derived from the BCPL language
 Why the ‘++’?
 ++ is an operator in C++ and results in a cute pun
Copyright © 2018 Pearson Addison-Wesley. All rights reserved.
Introduction and History of C++
 C++ is a programming language
 It was developed by Dennis Riche at Bell’s Lab in
1971.
 In C++, we can make new software or programs.
 Program is a set of instructions, which performs any
particular task.
Copyright © 2018 Pearson Addison-Wesley. All rights reserved.
Introduction and History of C++
 Before C++, there were so many languages
which were being used by the programmers
like GW Basic, Pascal, and Fortran etc. But
after the birth of C++, it becomes more
famous than all other languages. So many of
the programmers divorced the other languages
and happily married with C++.
Copyright © 2018 Pearson Addison-Wesley. All rights reserved.
Features of C Language
 C++ is middle level language.
 C++ is case sensitive language.
 C++ has compiler as a language translator.
 C++ is hybrid language (Combination of
structured as well as Object orientation
paradigm)
Copyright © 2018 Pearson Addison-Wesley. All rights reserved.
A Sample C++ Program
 A simple C++ program begins this way
#include <iostream>
using namespace std;
int main()
{
 And ends this way
return 0;
}
Copyright © 2018 Pearson Addison-Wesley. All rights reserved.
First Program of C++
#include <iostream>
using namespace std ;
int main ()
{
cout <<"Hello World... n " ;
system ("PAUSE") ;
return 0 ;
}
Copyright © 2018 Pearson Addison-Wesley. All rights reserved.
Explanation of First Program
#: It is a preprocessor directive
include: It is a name of directory
iostream: Built-in file
using : Keyword
Namespace: For directory
Std: library file
int: data type
main (): Entry point for every C++ / C program
{ // Open curly bracket of the main function
cout: output stream
<<: Operator
system (“PAUSE”): Built-in Function
}// Closing curley brackets
Copyright © 2018 Pearson Addison-Wesley. All rights reserved.
Compile and Execute
• For compilation press CNTRL+F9
• For compilation as well as execution press
CNTRL+F10
• Compilation and Execution F9
• For saving the program go to file and click on save.
• By default file is saved with the extension of .CPP.
Copyright © 2018 Pearson Addison-Wesley. All rights reserved.
Comments
• Single line comments
• //
• Multiple line comments
• /* */
Copyright © 2018 Pearson Addison-Wesley. All rights reserved.
Running a C++ Program
 C++ source code is written with a text
editor
 The compiler on your system converts
source code to object code.
 The linker combines all the object code
into an executable program.
Copyright © 2018 Pearson Addison-Wesley. All rights reserved.
1.4
Testing and Debugging
Copyright © 2018 Pearson Addison-Wesley. All rights reserved.
Testing and Debugging
 Bug
 A mistake in a program
 Debugging
 Eliminating mistakes in programs
 Term used when a moth caused a failed relay
on the Harvard Mark 1 computer. Grace Hopper
and other programmers taped the moth in logbook
stating:
“First actual case of a bug being found.”
Copyright © 2018 Pearson Addison-Wesley. All rights reserved.
Program Errors
 Syntax errors
 Violation of the grammar rules of the language
 Discovered by the compiler

Error messages may not always show correct location of
errors
 Run-time errors
 Error conditions detected by the computer at run-time
 Logic errors
 Errors in the program’s algorithm
 Most difficult to diagnose
 Computer does not recognize an error
Copyright © 2018 Pearson Addison-Wesley. All rights reserved.
Section 1-4 Conclusion
 Can you…
 Describe the three kinds of program errors?
 Tell what kind of errors the compiler catches?
 What kind of error is produced if you forget a
punctuation symbol such as a semi-colon?
 Tell what type of error is produced when a program
runs but produces incorrect results?
Copyright © 2018 Pearson Addison-Wesley. All rights reserved.
Chapter 1 -- End
Copyright © 2018 Pearson Addison-Wesley. All rights reserved.
Display 1.3 Back Next
Copyright © 2018 Pearson Addison-Wesley. All rights reserved.
Display 1.4 Back Next
Copyright © 2018 Pearson Addison-Wesley. All rights reserved.
Display 1.5 Back Next
Copyright © 2018 Pearson Addison-Wesley. All rights reserved.
Display 1.6 Back Next
Copyright © 2018 Pearson Addison-Wesley. All rights reserved.
Display 1.7 Back Next

Lecture 1-2_Programming fundamentals.pptx

  • 2.
    Copyright © 2018Pearson Addison-Wesley. All rights reserved. Course Objectives  The aim is not to make you the expert of computer programming but only the basics of computer programming.  To familiarize students with the use of C++.  To equip students with tools and techniques to implement a given problem programmatically
  • 3.
    Copyright © 2018Pearson Addison-Wesley. All rights reserved. IDEs used for this course  You can use any of the IDEs given below; but we will use Dev C++ in the classes and Labs  DEV C++  Visual C++  Turbo C++
  • 4.
    Copyright © 2018Pearson Addison-Wesley. All rights reserved. Grading Criteria (Theory) Total Marks = 100 Mid Term = 30% Final Exam = 50% Quizzes = 10% Project = 10%
  • 5.
    Copyright © 2018Pearson Addison-Wesley. All rights reserved. Grading Criteria (Lab) Total Marks = 50 Mid Term = 30% Final Exam = 50% Assignment = 10% Mega Task = 10%
  • 6.
    Copyright © 2018Pearson Addison-Wesley. All rights reserved. Recommended Readings Chapter No. 1 of C++ Programming from Problem Analysis to Program Design written by DS Malik
  • 7.
    Copyright © 2018Pearson Addison-Wesley. All rights reserved. Chapter 1 Introduction to Computers and C++ Programming
  • 8.
    Copyright © 2018Pearson Addison-Wesley. All rights reserved. Overview 1.1 Computer Systems 1.2 Programming and Problem Solving 1.3 Introduction to C++ 1.4 Testing and Debugging
  • 9.
    Copyright © 2018Pearson Addison-Wesley. All rights reserved. 1.1 Computer Systems
  • 10.
    Copyright © 2018Pearson Addison-Wesley. All rights reserved. Computer Systems  A computer program is…  A set of instructions for a computer to follow  Computer software is …  The collection of programs used by a computer  Includes:  Editors  Translators  System Managers
  • 11.
    Copyright © 2018Pearson Addison-Wesley. All rights reserved. Hardware  Three main classes of computers  PCs (Personal Computer)  Relatively small used by one person at a time  Workstation  Larger and more powerful than a PC  Mainframe  Still larger  Requires support staff  Shared by multiple users
  • 12.
    Copyright © 2018Pearson Addison-Wesley. All rights reserved. Data or Code?  ‘A’ may look like 01000001  65 may look like 01000001  An instruction may look like 01000001  How does the computer know the meaning of 01000001?  Interpretation depends on the current instruction  Programmers rarely need to be concerned with this problem.  Reason as if memory locations contain letters and numbers rather than zeroes and ones
  • 13.
    Copyright © 2018Pearson Addison-Wesley. All rights reserved. Secondary and Main Memory  Main memory stores instructions and data while a program is running.  Secondary memory  Stores instructions and data between sessions  A file stores data or instructions in secondary memory
  • 14.
    Copyright © 2018Pearson Addison-Wesley. All rights reserved. Memory Access  Random Access  Usually called RAM  Computer can directly access any memory location  Sequential Access  Data is generally found by searching through other items first  More common in secondary memory
  • 15.
    Copyright © 2018Pearson Addison-Wesley. All rights reserved. Computer Input  Computer input consists of  A program  Some data Display 1.3
  • 16.
    Copyright © 2018Pearson Addison-Wesley. All rights reserved. High-level Languages  Common programming languages include … C C++ Java Pascal Visual Basic FORTRAN Perl PHP Lisp Scheme Ada C# Python  These high – level languages  Resemble human languages  Are designed to be easy to read and write  Use more complicated instructions than the CPU can follow  Must be translated to zeros and ones for the CPU to execute a program
  • 17.
    Copyright © 2018Pearson Addison-Wesley. All rights reserved. Low-level Languages  An assembly language command such as ADD X Y Z might mean add the values found at x and y in memory, and store the result in location z.  Assembly language must be translated to machine language (zeros and ones) 0110 1001 1010 1011  The CPU can follow machine language
  • 18.
    Copyright © 2018Pearson Addison-Wesley. All rights reserved. Processing of C++ Program  To execute a C++ program:  Use an editor to create a source program in C++  Preprocessor directives begin with # and are processed by the preprocessor  Use the compiler to:  Check that the program obeys the language rules  Translate into machine language (object program)
  • 19.
    Copyright © 2018Pearson Addison-Wesley. All rights reserved. Processing of C++ Program  To execute a C++ program (cont'd.):  Linker:  Combines object program with other programs provided by the SDK to create executable code  Library: contains prewritten code you can use  Loader:  Loads executable program into main memory  Execution  The last step is to execute the program  Some IDEs do all this with a Build or Rebuild command
  • 20.
    Copyright © 2018Pearson Addison-Wesley. All rights reserved. Processing of C++ Program
  • 21.
    Copyright © 2018Pearson Addison-Wesley. All rights reserved. Compilers  Translate high-level language to machine language  Source code  The original program in a high level language  Object code  The translated version in machine language Display 1.4
  • 22.
    Copyright © 2018Pearson Addison-Wesley. All rights reserved. Linkers  Some programs we use are already compiled  Their object code is available for us to use  For example: Input and output routines  A Linker combines  The object code for the programs we write and  The object code for the pre-compiled routines into  The machine language program the CPU can run Display 1.5
  • 23.
    Copyright © 2018Pearson Addison-Wesley. All rights reserved. 1.2 Programming and Problem- Solving
  • 24.
    Copyright © 2018Pearson Addison-Wesley. All rights reserved. Program Design  Programming is a creative process  No complete set of rules for creating a program  Program Design Process  Problem Solving Phase  Result is an algorithm that solves the problem  Implementation Phase  Result is the algorithm translated into a programming language Display 1.7
  • 25.
    Copyright © 2018Pearson Addison-Wesley. All rights reserved. Problem Solving Phase  Be certain the task is completely specified  What is the input?  What information is in the output?  How is the output organized?  Develop the algorithm before implementation  Experience shows this saves time in getting your program to run.  Test the algorithm for correctness
  • 26.
    Copyright © 2018Pearson Addison-Wesley. All rights reserved. Algorithms  Algorithm  A sequence of precise instructions that leads to a solution  Program  An algorithm expressed in a language the computer can understand Display 1.6
  • 27.
    Copyright © 2018Pearson Addison-Wesley. All rights reserved. Example 1  Design an algorithm to find the perimeter and area of a rectangle  The perimeter and area of the rectangle are given by the following formulas: perimeter = 2 * (length + width) area = length * width
  • 28.
    Copyright © 2018Pearson Addison-Wesley. All rights reserved. Example 1  Algorithm:  Get length of the rectangle  Get width of the rectangle  Find the perimeter using the following equation: perimeter = 2 * (length + width)  Find the area using the following equation: area = length * width
  • 29.
    Copyright © 2018Pearson Addison-Wesley. All rights reserved. Implementation Phase  Translate the algorithm into a programming language  Easier as you gain experience with the language  Compile the source code  Locates errors in using the programming language  Run the program on sample data  Verify correctness of results  Results may require modification of the algorithm and program
  • 30.
    Copyright © 2018Pearson Addison-Wesley. All rights reserved. Software Life Cycle  Analysis and specification of the task (problem definition)  Design of the software (object and algorithm design)  Implementation (coding)  Maintenance and evolution of the system  Obsolescence
  • 31.
    Copyright © 2018Pearson Addison-Wesley. All rights reserved. Section 1.2 Conclusion  Can you…  Describe the first step to take when creating a program?  List the two main phases of the program design process?  Explain the importance of the problem-solving phase?  List the steps in the software life cycle?
  • 32.
    Copyright © 2018Pearson Addison-Wesley. All rights reserved. 1.3 Introduction to C++
  • 33.
    Copyright © 2018Pearson Addison-Wesley. All rights reserved. Introduction to C++  Where did C++ come from?  Derived from the C language  C was derived from the B language  B was derived from the BCPL language  Why the ‘++’?  ++ is an operator in C++ and results in a cute pun
  • 34.
    Copyright © 2018Pearson Addison-Wesley. All rights reserved. Introduction and History of C++  C++ is a programming language  It was developed by Dennis Riche at Bell’s Lab in 1971.  In C++, we can make new software or programs.  Program is a set of instructions, which performs any particular task.
  • 35.
    Copyright © 2018Pearson Addison-Wesley. All rights reserved. Introduction and History of C++  Before C++, there were so many languages which were being used by the programmers like GW Basic, Pascal, and Fortran etc. But after the birth of C++, it becomes more famous than all other languages. So many of the programmers divorced the other languages and happily married with C++.
  • 36.
    Copyright © 2018Pearson Addison-Wesley. All rights reserved. Features of C Language  C++ is middle level language.  C++ is case sensitive language.  C++ has compiler as a language translator.  C++ is hybrid language (Combination of structured as well as Object orientation paradigm)
  • 37.
    Copyright © 2018Pearson Addison-Wesley. All rights reserved. A Sample C++ Program  A simple C++ program begins this way #include <iostream> using namespace std; int main() {  And ends this way return 0; }
  • 38.
    Copyright © 2018Pearson Addison-Wesley. All rights reserved. First Program of C++ #include <iostream> using namespace std ; int main () { cout <<"Hello World... n " ; system ("PAUSE") ; return 0 ; }
  • 39.
    Copyright © 2018Pearson Addison-Wesley. All rights reserved. Explanation of First Program #: It is a preprocessor directive include: It is a name of directory iostream: Built-in file using : Keyword Namespace: For directory Std: library file int: data type main (): Entry point for every C++ / C program { // Open curly bracket of the main function cout: output stream <<: Operator system (“PAUSE”): Built-in Function }// Closing curley brackets
  • 40.
    Copyright © 2018Pearson Addison-Wesley. All rights reserved. Compile and Execute • For compilation press CNTRL+F9 • For compilation as well as execution press CNTRL+F10 • Compilation and Execution F9 • For saving the program go to file and click on save. • By default file is saved with the extension of .CPP.
  • 41.
    Copyright © 2018Pearson Addison-Wesley. All rights reserved. Comments • Single line comments • // • Multiple line comments • /* */
  • 42.
    Copyright © 2018Pearson Addison-Wesley. All rights reserved. Running a C++ Program  C++ source code is written with a text editor  The compiler on your system converts source code to object code.  The linker combines all the object code into an executable program.
  • 43.
    Copyright © 2018Pearson Addison-Wesley. All rights reserved. 1.4 Testing and Debugging
  • 44.
    Copyright © 2018Pearson Addison-Wesley. All rights reserved. Testing and Debugging  Bug  A mistake in a program  Debugging  Eliminating mistakes in programs  Term used when a moth caused a failed relay on the Harvard Mark 1 computer. Grace Hopper and other programmers taped the moth in logbook stating: “First actual case of a bug being found.”
  • 45.
    Copyright © 2018Pearson Addison-Wesley. All rights reserved. Program Errors  Syntax errors  Violation of the grammar rules of the language  Discovered by the compiler  Error messages may not always show correct location of errors  Run-time errors  Error conditions detected by the computer at run-time  Logic errors  Errors in the program’s algorithm  Most difficult to diagnose  Computer does not recognize an error
  • 46.
    Copyright © 2018Pearson Addison-Wesley. All rights reserved. Section 1-4 Conclusion  Can you…  Describe the three kinds of program errors?  Tell what kind of errors the compiler catches?  What kind of error is produced if you forget a punctuation symbol such as a semi-colon?  Tell what type of error is produced when a program runs but produces incorrect results?
  • 47.
    Copyright © 2018Pearson Addison-Wesley. All rights reserved. Chapter 1 -- End
  • 48.
    Copyright © 2018Pearson Addison-Wesley. All rights reserved. Display 1.3 Back Next
  • 49.
    Copyright © 2018Pearson Addison-Wesley. All rights reserved. Display 1.4 Back Next
  • 50.
    Copyright © 2018Pearson Addison-Wesley. All rights reserved. Display 1.5 Back Next
  • 51.
    Copyright © 2018Pearson Addison-Wesley. All rights reserved. Display 1.6 Back Next
  • 52.
    Copyright © 2018Pearson Addison-Wesley. All rights reserved. Display 1.7 Back Next