Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

C++ Programming ch01

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 39

Chapter 1:

An Overview of Computers and


Programming Languages
Outlines
• In this chapter, you will study:
– The Language of a Computer
– The Evolution of Programming Languages
– Processing a C++ Program
– Programming with the Problem Analysis–Coding–
Execution Cycle

2
Introduction
• Without software, the computer is useless
• Software is developed with programming languages
– C++ is a programming language
• C++ suited for a wide variety of programming tasks

C++ Programming: From Problem Analysis to Program Design, Sixth Edition 3


Elements of a Computer System
• Hardware
• CPU
• Main memory
• Secondary storage
• Input/Output devices
• Software

C++ Programming: From Problem Analysis to Program Design, Sixth Edition 4


Software
• Software: programs that do specific tasks
• System programs control the computer
– Operating system monitors the overall activity of the
computer and provides services such as:
• Memory management
• Input/output activities
• Storage management

• Application programs perform a specific task


– Word processors
– Spreadsheets
– Games

5
The Language of a Computer
• Analog signals: continuous wave forms
• Digital signals: sequences of 0s and 1s
• Machine language: language of a computer; a
sequence of 0s and 1s
• Binary digit (bit): the digit 0 or 1
• Binary code (binary number): a sequence of 0s
and 1s

6
The Language of a Computer (cont’d.)
• Byte:
– A sequence of eight bits
• Kilobyte (KB): 210 bytes = 1024 bytes
• ASCII (American Standard Code for Information
Interchange)
– 128 characters
– A is encoded as 1000001 (65th character)
– 3 is encoded as 0110011

7
Processing a C++ Program
#include <iostream>
using namespace std;
int main()
{
cout << "My first C++ program." << endl;
return 0;
}

Sample Run:
My first C++ program.

8
Processing a C++ Program (cont’d.)
• 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)

9
Processing a C++ Program (cont’d.)
• 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
– The last step is to execute the program
• Some IDEs do all this with a Build or Rebuild
command

10
Processing a C++ Program (cont’d.)

11
Programming with the Problem
Analysis–Coding–Execution Cycle
• Algorithm:
– Step-by-step problem-solving
process
– Solution achieved in finite
amount of time
• Programming is a process of
problem solving

12
The Problem Analysis–Coding–
Execution Cycle (cont’d.)
• Step 1: Analyze the problem
– Outline the problem and its requirements
– Design steps (algorithm) to solve the problem
• Step 2: Implement the algorithm
– Implement the algorithm in code
– Verify that the algorithm works
• Step 3: Maintenance
– Use and modify the program if the problem domain
changes

13
The Problem Analysis–Coding–
Execution Cycle (cont’d.)
• Thoroughly understand the problem and all requirements
– Does program require user interaction?
– Does program manipulate data?
– What is the output?

• If the problem is complex, divide it into subproblems


– Analyze and design algorithms for each subproblem

• Check the correctness of algorithm


– Can test using sample data
– Some mathematical analysis might be required

14
The Problem Analysis–Coding–
Execution Cycle (cont’d.)
• Once the algorithm is designed and correctness
verified
– Write the equivalent code in high-level language
• Enter the program using text editor

15
The Problem Analysis–Coding–
Execution Cycle (cont’d.)
• Run code through compiler
• If compiler generates errors
– Look at code and remove errors
– Run code again through compiler
• If there are no syntax errors
– Compiler generates equivalent machine code
• Linker links machine code with system resources

16
The Problem Analysis–Coding–
Execution Cycle (cont’d.)
• Once compiled and linked, loader can place program
into main memory for execution
• The final step is to execute the program
• Compiler guarantees that the program follows the
rules of the language
– Does not guarantee that the program will run correctly

17
Example 1-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

18
Example 1-1 (cont’d.)
• 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

19
Example 1-3
• Design an algorithm to calculates the monthly
paycheck of a salesperson at a local department
store.
payCheck = baseSalary + bonus + additionalBonus

• Data:
– base salary
– The number of years that the salesperson has been with
the company
– The total sales made by the salesperson for that month

20
Example 1-3 (cont’d.)
• Suppose noOfServiceYears denotes the number of years that
the salesperson has been with the store
• Suppose bonus denotes the bonus.

• Algorithm to calculate the bonus


if (noOfServiceYears is less than or equal to five)
bonus = 10 * noOfServiceYears
otherwise
bonus = 20 * noOfServiceYears

21
Example 1-3 (cont’d.)
• Suppose totalSales denotes the total sales made by the
salesperson for the month
• Suppose additionalBonus denotes the additional bonus.

• Algorithm to calculate additional bonus


if (totalSales is less than 5000)
additionalBonus = 0
otherwise
if (totalSales is greater than or equal to 5000 and
totalSales is less than 10000)
additionalBonus = totalSales (0.03)
otherwise
additionalBonus = totalSales (0.06)
22
Example 1-3 (cont’d.)
• The algorithm to calculate a salesperson’s monthly
paycheck:
1. Get baseSalary.
2. Get noOfServiceYears.
3. Calculate bonus using calculate bonus algorithm
4. Get totalSales.
5. Calculate additionalBonus using the algorithm to
calculate additional bonus
6. Calculate payCheck using the equation:
payCheck = baseSalary + bonus + additionalBonus

23
Example 1-5
• Calculate each student’s grade
– 10 students in a class; each student has taken five tests;
each test is worth 100 points
• Design algorithms to:
– Calculate the grade for each student and class average
– Find the average test score
– Determine the grade
• Data: students’ names; test scores

24
Example 1-5 (cont’d.)
• Algorithm to determine the average test score:
– Get the five test scores
– Add the five test scores
• Suppose sum stands for the sum of the test scores
– Suppose average stands for the average test score:
• average = sum / 5;

25
Example 1-5 (cont’d.)
• Algorithm to determine the grade:
if average is greater than or equal to 90
grade = A
otherwise
if average is greater than or equal to 80 and less than 90
grade = B
otherwise
if average is greater than or equal to 70 and less than 80
grade = C
otherwise
if average is greater than or equal to 60 and less than 70
grade = D
otherwise
grade = F

26
Example 1-5 (cont’d.)
• Main algorithm is as follows:
– totalAverage = 0;
– Repeat the following for each student:
• Get student’s name
• Use the algorithm to find the average test score
• Use the algorithm to find the grade
• Update totalAverage by adding current student’s average test
score
– Determine the class average as follows:
• classAverage = totalAverage / 10

27
Refer to text book and read
examples 1-2, and 1-4

28
Compiling C++ Code

Self Study Slides


Some examples on C++ IDEs
• Online IDEs
– https://www.codechef.com/ide
– https://www.onlinegdb.com/
– https://www.jdoodle.com/online-compiler-c++

• Offline IDEs
– Microsoft visual studio (will be explained in this slides)
– Eclipse
– Code::Blocks
– CodeLite
Microsoft visual studio 2015 home
screen
Before you create your first C++ project in Visual Studio, you
need to install Visual C++ 2015 Tools for Windows Desktop:
To create your HelloWorld project => File ->new-
>project, you can choose the Win32 Console Application
template, Name your project and click “ok”
When this window appears click next
Choose Empty project and click
finish
From solution explorer window, right click on
source files and choose to add new item as follows
Select C++ File, give it a name and
click add
Write your code in the .cpp file and click
on the green triangle to run your program
This window will appear

You might also like