SlideShare uma empresa Scribd logo
1 de 13
Introduction to Programming
in C++
Lesson 3
Common error
Omitting a semicolon (or two)
Common Error – Omitting Semicolons
1 #include <iostream>
2
3 using namespace std;
4
5 int main()
6 {
7 cout << "Hello, World!" << endl
8 return 0;
9 }
Oh No!
Suppose you (accidentally of course) wrote:
cot << "Hello World!" << endl;
• This will cause a compile-time error and the compiler will
complain that it has no clue what you mean by cot.
The exact wording of the error message is dependent on
the compiler, but it might be something like
“Undefined symbol cot”.
Consider this:
cout << "Hollo, World!" << endl;
• Logic errors or run-time errors are errors in a program that
compiles (the syntax is correct), but executes without
performing the intended action.
not really an error?
Common Error – Using Undefined Variables
You must define a variable before you use it for the first time.
For example, the following sequence of statements would not
be legal:
double can_volume = 12 * liter_factor;
double liter_factor = 0.0296;
? ?
Statements are compiled in top to bottom order.
When the compiler reaches the first statement, it does not
know that liter_factor will be defined in the next line, and
it reports an error.
Common Error – Using Uninitialized Variables
Initializing a variable is not required, but there is always a
value in every variable, even uninitialized ones.
Some value will be there, the flotsam left over from some
previous calculation or simply the random value there
when the transistors in RAM were first turned on.
int bottles; // Forgot to initialize
int bottle_volume = bottles * 2;
What value would be output from the following statement?
cout << bottle_volume << endl; // Unpredictable
// Result is unpredictable
Common Error – Unintended Integer Division
It is unfortunate that C++ uses the same symbol: /
for both integer and floating-point division.
These are really quite different operations.
It is a common error to use integer division by accident.
Consider this segment that computes the average of three integers:
cout << "Please enter your last three test scores: ";
int s1;
int s2;
int s3;
cin >> s1 >> s2 >> s3;
double average = (s1 + s2 + s3) / 3;
cout << "Your average score is " << average << endl;
What could be wrong with that?
Of course, the average of s1, s2, and s3 is
(s1+ s2+ s3) / 3
Here, however, the / does not mean division in the
mathematical sense.
It denotes integer division because
both (s1 + s2 + s3)and 3 are integers.
For example, if the scores add up to 14,
the average is computed to be 4.
WHAT?
Yes, the result of the integer division of 14 by 3 is 4
How many times does 3 evenly divide into 14?
Right!
That integer 4 is then moved into the floating-point
variable average.
So 4.0 is stored.
That’s not what we want!
The remedy is to make the numerator or denominator into
a floating-point number:
double total = s1 + s2 + s3;
double average = total / 3;
or
double average = (s1 + s2 + s3) / 3.0;
Common Error – Unbalanced Parentheses
Consider the expression
(-(b * b - 4 * a * c) / (2 * a)
What is wrong with it?
The parentheses are unbalanced.
This is very common with complicated expressions.
?
Common Error – Roundoff Errors
This program produces the wrong output:
#include <iostream>
using namespace std;
int main()
{
double price = 4.35;
int cents = 100 * price;
// Should be 100 * 4.35 = 435
cout << cents << endl;
// Prints 434!
return 0;
}
Why?
Common Error – Roundoff Errors
• In the computer, numbers are represented in the binary
number system, not in decimal.
• In the binary system, there is no exact representation for 4.35,
just as there is no exact representation for ⅓ in the decimal
system.
• The representation used by the computer is just a little less
than 4.35, so 100 times that value is just a little less than 435.
• The remedy is to add 0.5 in order to round to the nearest
integer:
int cents = 100 * price + 0.5;

Mais conteúdo relacionado

Semelhante a Prog1-L3.pptx

Classes and Errors.pdf
Classes and Errors.pdfClasses and Errors.pdf
Classes and Errors.pdfrajaratna4
 
My programming final proj. (1)
My programming final proj. (1)My programming final proj. (1)
My programming final proj. (1)aeden_brines
 
Switch case and looping new
Switch case and looping newSwitch case and looping new
Switch case and looping newaprilyyy
 
DeVry GSP 115 Week 3 Assignment latest
DeVry GSP 115 Week 3 Assignment latestDeVry GSP 115 Week 3 Assignment latest
DeVry GSP 115 Week 3 Assignment latestAtifkhilji
 
030 cpp streams
030 cpp streams030 cpp streams
030 cpp streamsHồ Lợi
 
Error correction-and-type-of-error-in-c
Error correction-and-type-of-error-in-cError correction-and-type-of-error-in-c
Error correction-and-type-of-error-in-cMd Nazmul Hossain Mir
 
INDIAN INSTITUTE OF TECHNOLOGY OF KANPURESC 111M Lec03.pptx
INDIAN INSTITUTE OF TECHNOLOGY OF KANPURESC 111M Lec03.pptxINDIAN INSTITUTE OF TECHNOLOGY OF KANPURESC 111M Lec03.pptx
INDIAN INSTITUTE OF TECHNOLOGY OF KANPURESC 111M Lec03.pptxAbhimanyuChaure
 
Logical Expressions in C/C++. Mistakes Made by Professionals
Logical Expressions in C/C++. Mistakes Made by ProfessionalsLogical Expressions in C/C++. Mistakes Made by Professionals
Logical Expressions in C/C++. Mistakes Made by ProfessionalsPVS-Studio
 
Yeahhhh the final requirement!!!
Yeahhhh the final requirement!!!Yeahhhh the final requirement!!!
Yeahhhh the final requirement!!!olracoatalub
 
Complete C++ programming Language Course
Complete C++ programming Language CourseComplete C++ programming Language Course
Complete C++ programming Language CourseVivek chan
 
Programming in C by SONU KUMAR.pptx
Programming in C by SONU KUMAR.pptxProgramming in C by SONU KUMAR.pptx
Programming in C by SONU KUMAR.pptxSONU KUMAR
 
Lesson 2 starting output
Lesson 2 starting outputLesson 2 starting output
Lesson 2 starting outputWayneJones104
 
Lec16.pptx problem specifications computer
Lec16.pptx problem specifications computerLec16.pptx problem specifications computer
Lec16.pptx problem specifications computersamiullahamjad06
 

Semelhante a Prog1-L3.pptx (20)

Classes and Errors.pdf
Classes and Errors.pdfClasses and Errors.pdf
Classes and Errors.pdf
 
My programming final proj. (1)
My programming final proj. (1)My programming final proj. (1)
My programming final proj. (1)
 
Switch case and looping new
Switch case and looping newSwitch case and looping new
Switch case and looping new
 
DeVry GSP 115 Week 3 Assignment latest
DeVry GSP 115 Week 3 Assignment latestDeVry GSP 115 Week 3 Assignment latest
DeVry GSP 115 Week 3 Assignment latest
 
030 cpp streams
030 cpp streams030 cpp streams
030 cpp streams
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 
Error correction-and-type-of-error-in-c
Error correction-and-type-of-error-in-cError correction-and-type-of-error-in-c
Error correction-and-type-of-error-in-c
 
My final requirement
My final requirementMy final requirement
My final requirement
 
INDIAN INSTITUTE OF TECHNOLOGY OF KANPURESC 111M Lec03.pptx
INDIAN INSTITUTE OF TECHNOLOGY OF KANPURESC 111M Lec03.pptxINDIAN INSTITUTE OF TECHNOLOGY OF KANPURESC 111M Lec03.pptx
INDIAN INSTITUTE OF TECHNOLOGY OF KANPURESC 111M Lec03.pptx
 
C++ in 10 Hours.pdf.pdf
C++ in 10 Hours.pdf.pdfC++ in 10 Hours.pdf.pdf
C++ in 10 Hours.pdf.pdf
 
Logical Expressions in C/C++. Mistakes Made by Professionals
Logical Expressions in C/C++. Mistakes Made by ProfessionalsLogical Expressions in C/C++. Mistakes Made by Professionals
Logical Expressions in C/C++. Mistakes Made by Professionals
 
Yeahhhh the final requirement!!!
Yeahhhh the final requirement!!!Yeahhhh the final requirement!!!
Yeahhhh the final requirement!!!
 
Complete C++ programming Language Course
Complete C++ programming Language CourseComplete C++ programming Language Course
Complete C++ programming Language Course
 
C++ programming
C++ programmingC++ programming
C++ programming
 
Code Tuning
Code TuningCode Tuning
Code Tuning
 
Programming in C by SONU KUMAR.pptx
Programming in C by SONU KUMAR.pptxProgramming in C by SONU KUMAR.pptx
Programming in C by SONU KUMAR.pptx
 
C++ programming
C++ programmingC++ programming
C++ programming
 
Lesson 2 starting output
Lesson 2 starting outputLesson 2 starting output
Lesson 2 starting output
 
Lec16.pptx problem specifications computer
Lec16.pptx problem specifications computerLec16.pptx problem specifications computer
Lec16.pptx problem specifications computer
 
Lecture 3
Lecture 3Lecture 3
Lecture 3
 

Mais de valerie5142000

Mais de valerie5142000 (8)

HOW TO MAKE YOUR OWN CAT 5 UTP Cable.ppt
HOW TO MAKE YOUR OWN CAT 5 UTP Cable.pptHOW TO MAKE YOUR OWN CAT 5 UTP Cable.ppt
HOW TO MAKE YOUR OWN CAT 5 UTP Cable.ppt
 
dotNET_Overview.pdf
dotNET_Overview.pdfdotNET_Overview.pdf
dotNET_Overview.pdf
 
Chap-08-4.ppt
Chap-08-4.pptChap-08-4.ppt
Chap-08-4.ppt
 
ch9.ppt
ch9.pptch9.ppt
ch9.ppt
 
ch06.ppt
ch06.pptch06.ppt
ch06.ppt
 
cpphtp4_PPT_02.ppt
cpphtp4_PPT_02.pptcpphtp4_PPT_02.ppt
cpphtp4_PPT_02.ppt
 
Prog1-L2.pptx
Prog1-L2.pptxProg1-L2.pptx
Prog1-L2.pptx
 
Prog1-L1.pdf
Prog1-L1.pdfProg1-L1.pdf
Prog1-L1.pdf
 

Último

How to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationHow to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationBradBedford3
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprisepreethippts
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based projectAnoyGreter
 
Cyber security and its impact on E commerce
Cyber security and its impact on E commerceCyber security and its impact on E commerce
Cyber security and its impact on E commercemanigoyal112
 
Salesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZSalesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZABSYZ Inc
 
20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...
20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...
20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...Akihiro Suda
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...Technogeeks
 
Large Language Models for Test Case Evolution and Repair
Large Language Models for Test Case Evolution and RepairLarge Language Models for Test Case Evolution and Repair
Large Language Models for Test Case Evolution and RepairLionel Briand
 
Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...Rob Geurden
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odishasmiwainfosol
 
Comparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfComparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfDrew Moseley
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesPhilip Schwarz
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfFerryKemperman
 
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...OnePlan Solutions
 
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Natan Silnitsky
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureDinusha Kumarasiri
 
Xen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfXen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfStefano Stabellini
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)jennyeacort
 

Último (20)

How to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationHow to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion Application
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprise
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based project
 
Cyber security and its impact on E commerce
Cyber security and its impact on E commerceCyber security and its impact on E commerce
Cyber security and its impact on E commerce
 
Salesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZSalesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZ
 
20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...
20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...
20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...
 
Large Language Models for Test Case Evolution and Repair
Large Language Models for Test Case Evolution and RepairLarge Language Models for Test Case Evolution and Repair
Large Language Models for Test Case Evolution and Repair
 
Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
 
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort ServiceHot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
 
Comparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfComparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdf
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a series
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdf
 
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
 
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with Azure
 
Xen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfXen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdf
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
 

Prog1-L3.pptx

  • 2. Common error Omitting a semicolon (or two) Common Error – Omitting Semicolons 1 #include <iostream> 2 3 using namespace std; 4 5 int main() 6 { 7 cout << "Hello, World!" << endl 8 return 0; 9 } Oh No!
  • 3. Suppose you (accidentally of course) wrote: cot << "Hello World!" << endl; • This will cause a compile-time error and the compiler will complain that it has no clue what you mean by cot. The exact wording of the error message is dependent on the compiler, but it might be something like “Undefined symbol cot”.
  • 4. Consider this: cout << "Hollo, World!" << endl; • Logic errors or run-time errors are errors in a program that compiles (the syntax is correct), but executes without performing the intended action. not really an error?
  • 5. Common Error – Using Undefined Variables You must define a variable before you use it for the first time. For example, the following sequence of statements would not be legal: double can_volume = 12 * liter_factor; double liter_factor = 0.0296; ? ? Statements are compiled in top to bottom order. When the compiler reaches the first statement, it does not know that liter_factor will be defined in the next line, and it reports an error.
  • 6. Common Error – Using Uninitialized Variables Initializing a variable is not required, but there is always a value in every variable, even uninitialized ones. Some value will be there, the flotsam left over from some previous calculation or simply the random value there when the transistors in RAM were first turned on. int bottles; // Forgot to initialize int bottle_volume = bottles * 2; What value would be output from the following statement? cout << bottle_volume << endl; // Unpredictable // Result is unpredictable
  • 7. Common Error – Unintended Integer Division It is unfortunate that C++ uses the same symbol: / for both integer and floating-point division. These are really quite different operations. It is a common error to use integer division by accident. Consider this segment that computes the average of three integers: cout << "Please enter your last three test scores: "; int s1; int s2; int s3; cin >> s1 >> s2 >> s3; double average = (s1 + s2 + s3) / 3; cout << "Your average score is " << average << endl;
  • 8. What could be wrong with that? Of course, the average of s1, s2, and s3 is (s1+ s2+ s3) / 3 Here, however, the / does not mean division in the mathematical sense. It denotes integer division because both (s1 + s2 + s3)and 3 are integers.
  • 9. For example, if the scores add up to 14, the average is computed to be 4. WHAT? Yes, the result of the integer division of 14 by 3 is 4 How many times does 3 evenly divide into 14? Right! That integer 4 is then moved into the floating-point variable average. So 4.0 is stored. That’s not what we want!
  • 10. The remedy is to make the numerator or denominator into a floating-point number: double total = s1 + s2 + s3; double average = total / 3; or double average = (s1 + s2 + s3) / 3.0;
  • 11. Common Error – Unbalanced Parentheses Consider the expression (-(b * b - 4 * a * c) / (2 * a) What is wrong with it? The parentheses are unbalanced. This is very common with complicated expressions. ?
  • 12. Common Error – Roundoff Errors This program produces the wrong output: #include <iostream> using namespace std; int main() { double price = 4.35; int cents = 100 * price; // Should be 100 * 4.35 = 435 cout << cents << endl; // Prints 434! return 0; } Why?
  • 13. Common Error – Roundoff Errors • In the computer, numbers are represented in the binary number system, not in decimal. • In the binary system, there is no exact representation for 4.35, just as there is no exact representation for ⅓ in the decimal system. • The representation used by the computer is just a little less than 4.35, so 100 times that value is just a little less than 435. • The remedy is to add 0.5 in order to round to the nearest integer: int cents = 100 * price + 0.5;