0% found this document useful (0 votes)
29 views

Chap01 - Intro To Programming

The document introduces programming and the C++ language, explaining how programs are developed through understanding problems, designing algorithms and data structures, writing code, and testing solutions, and how early computer languages evolved from machine language to today's high-level languages like C++ that are compiled into executable programs.

Uploaded by

lewjw-wm22
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Chap01 - Intro To Programming

The document introduces programming and the C++ language, explaining how programs are developed through understanding problems, designing algorithms and data structures, writing code, and testing solutions, and how early computer languages evolved from machine language to today's high-level languages like C++ that are compiled into executable programs.

Uploaded by

lewjw-wm22
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

C++ Programming:

Problem Solving and Programming

Chapter 1
Introduction To Programming
Objectives
In this chapter you will:
• Program and software
• Learn about the language of a computer
• Learn about the evolution of programming languages
• Examine high-level programming languages
• Discover what a compiler is and what it does
• Examine how a high-level language program is processed
• Compilation, executing and debugging
• Good programming habits

2
Computer Components

Input

Hardware

Output Process
Computer Operations

• Data
Processing • Information
• Keyboard data • Validation • Reports
• Mouse click • Calculation • Audio
• Others • Storing
• Others
Input Output
Program and Software

Computer – programmable machine designed to


follow instructions
Program – instructions in computer memory which
computer follows to perform a task
Programmer – person who writes instructions
(programs) to make computer perform a task

So, without programmers, no programs; without


programs, a computer cannot do anything

5
Program and Software

• Software is a collection of programs and data that


instruct the computer what to do
• Categories of software:
− System software: Operating System that manage
the computer hardware and the programs that run
on them. Examples: Windows, UNIX, Linux
− Application software: programs that provide
services to the user. Examples : word processing,
games, programs to solve specific problems

6
Program and Software

● The programmer uses Computer / Programming


Language to write programs
● Syntax refers to the spelling and grammars of a
programming language
● Algorithm is a sequence of instructions written for
computer to solve a problem.

7
Algorithms (Example)
Steps for a customer to buy a movie ticket
1. Queue up
2. Go to the designated counter
3. Order ticket
➔ Specify movie
➔ Specify time
➔ Specify number of tickets
1. Make payment
2. Get tickets
3. Leave the counter
Computer Languages

● Categories of computer languages:


○ Machine language
○ Symbolic/Assembly language
○ High-level language

9
Evolution of Computer Languages

10
Machine Language

• The computer can only understand and execute


machine language instructions
• Machine language instructions consist of binary
numbers (0s and 1s) such as
10110100000101
• It is platform-dependent
• Rather than writing programs in machine
language which is very hard, the programmers
use other programming languages

11
Symbolic/Assembly Language
• Use symbolic codes, mnemonic and meaning
abbreviations. It is also platform-dependent
• Assembler: used to translate a program written in
assembly language into machine language for
execution by the computer
• Easier to program

12
High-Level Language

• Use English-like codes and mathematical


operators
• It is platform-independent
• High-level languages include Python, C++, C, C#,
Java, Swift, etc
• Compiler: used to translate a program written in a
high-level language to machine language
• Very easy to program

13
Creating a Simple Program
• Program coding
− Text editor (such as Notepad)
− Integrated Development Environment (IDE)
• Type of application
− Console (command line)
− Windows (desktop)
− Web
Example of IDE - Visual Studio
Build: To compile code
Build

Run: To execute program

Text editor: write code

Output: Check error/ status


Console Application
Compilation process of C++ program

1) Source code is written in Editor


Library
C++

2) Convert the C++ code into Compiler


object code

3) Linker links the object code disk


Linker
with libraries

4) Loader puts program in


memory Loader

Memory
5) Ready to run
CPU
(platform-dependent)
17
Program Development

• To develop a program, programmer must


complete the following steps:
− Understand the problem
− Develop a solution
− Write the program
− Test the program

18
Program Development
Understand
the problem Understand the problem
• Carefully study the user
Develop a
requirements.
solution • Understand what he wants the
program to do and what kind of
Write the output he wants to have.
program

Test the
program
19
Program Development
Understand Develop a solution
the problem
• Design the logic of the program by
using tools such as:
Develop a
solution
− Structure chart
− Pseudocode
− Flowchart
Write the
program

Test the
program
20
Program Development
Understand Write the program
the problem
• Code the actual program by using
the preferred programming
Develop a language.
solution

Write the
program

Test the
program
21
Program Development
Understand Test the program
the problem
• Run and test the program to
ensure it is free of logical errors.
Develop a
solution

Write the
program

Test the
program
22
Example 1-1 : Rectangle

• Write a C++ program to find the area and


perimeter of a rectangle
• The area and perimeter of the rectangle are given
by the following formulas:

perimeter = 2 * (length + width)


area = length * width

23
Tool: Structure Chart

• A hierarchy chart that shows the functional flow


through the program.
• Shows how the program is broken into logical
steps. Each step will be a separate module.

24
Example 1.1: Structure chart
Area &
perimeter of
rectangular

Get length & Calculate area Display area


width & perimeter & perimeter
Tool: Pseudocode (Algorithm)

• Use English words to convey program logic.


• Contains the logical steps / algorithms to
accomplish the task.

26
Example 1-1 : Pseudocode
Start
1. Get length of the rectangle
2. Get width of the rectangle
3. perimeter = 2 * (length + width)
4. area = length * width
5. Display area and perimeter
End

27
Tool: Flowchart

• Use standard graphical symbols to represent the


logical flow of data through a program.

28
Flowchart Symbols

Input/Output Processing Module

Terminal Decision Flowlines

On-page Off-page
Connector Connector
Example 1.1: Flowchart
START

Read
length &
width

area =
Length * width

perimeter =
2 * (Length + width)

Display area
& perimeter

END
Example 1-1 : C++ program
1. #include <iostream>
2. using namespace std;
3. main() {
4. int length, width, area, perimeter;
5. cout<<"Enter length and width of
rectangle\n";
6. cin>>length;
7. cin>>width;
8. area = length * width;
9. perimeter = 2*(length + width);
10. cout<< "Area = "<<area;
11. cout<< "Perimeter = "<<perimeter;
12. return 0;
13. }

31
Exercise

By using structure chart, pseudocode and


flowchart, find the total and product of 2
numbers. Display the result.

32
Debugging

• To find any error in the program


• Bugs – error, also known as exception, occurs
during compilation or execution.
• Common programming errors:
− Syntax errors (Compilation errors)
− Run-time errors
− Logic errors
Common programming errors
• Syntax Errors
− Code violation during compilation, e.g.:
• Missing semicolon (;)
• Undefined variable
• Did not close ’/*’ with ‘/’
• Run-time Errors
− Illegal operation occur during execution , e.g.:
• Divide a number by zero
• Open a non-existing file
• Write to a non-existing printer
Common programming errors
• Logic Errors
− Passed Syntax and Run-time Errors but produce
false result
− It is usually caused by the algorithm of the
program
// to calculate the average
int a =20;
int b = 34;
cout << “The average is ” << (a + b); ← Used wrong
formula
C++ Basic Syntax
1. #include <iostream> //allow input/output
preprocessor directive
2. int main() //starting point
3. { variable
4. int mark; // variable declaration Escape sequence
Data type
5. std::cout << “Sample: \n Enter mark:”; //display
6. std::cin >> mark; //get input comment
7. std::cout << “Mark is ” << Mark << std::endl;
8. return 0; //end point
9. } Use semicolon (;) to end a statement
Good Programming Habits

• Use program comments


• Use meaningful identifiers
• Design clear statements
• Write clear message & clear instruction
• Backup your code

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy