You are on page 1of 29

CMPSC 201C

Programming for Engineers


Lecture Set #2 (v3)

R.Avanzato © 2017 Video part 1 of 4

Topics:
• Review HW and C++ Compiler Installation  Video part 1 of 4
• Software Development Life Cycle (SDLC)  Video part 2 of 4
• Problem-solving methodology
• Program Structure
• Constants and Variables
• Assignment Statements
• Input and Output  Video part 3 of 4
• Computer Literacy Questions and Answers  Video part 4 of 4
• HW #1

© R. Avanzato 1
Review HW
• Any problems with Canvas discussion board? (if so,
contact instructor by email).

• Did you successfully download Microsoft Visual


Studio C++ Express?
• Did you download and try Dev C++?
• What is your plan if you have a Mac laptop?

• Any questions?
NOTE: This course has been transitioned to the Canvas
learning management system – ignore any references to
the Angel course management system

© R. Avanzato 2
Review Questions
1) Software instructions and data are stored in
(a) Clock
(b) CPU
(c) Memory
(d) Keyboard

2) Source code is
a) Output of compiler
b) Set of machine instructions
c) Set of programming language instructions
d) Data stored in memory

© R. Avanzato 3
Review Questions

3) What operation(s) are accomplished by the compiler?


(a) Identifies syntax errors
(b) Converts source code to machine code Pause video and
(c) Executes program answer before
solution is
(d) Links library functions to code
revealed.

4) What is generally the fastest memory on a PC?


a) hardrive
b) RAM
c) ROM
d) Cache

© R. Avanzato 4
Review Questions
1. What is the difference between HTML and C/C++
programming language?

2. What is a compiler? Give an example?

3. What is the difference between a syntax error and


semantic/logic error? Provide an example of
each.

4. What is an IDE? Give example

5. What is difference between terms “compile” and


“build”?
© R. Avanzato 5
Compilation/Execution Steps
C/C++ source Object file Executable
Compiler .exe file
code (binary file)
Ex: VC++, Linker
(text file) Ex: average. (binary file)
Dev-C
Ex: average.cpp obj Ex: average.exe

Libraries

Programmer User
• When you (or the user) run (execute) your C program, you are really
executing the .exe. file.
• A source program needs to be re-compiled every time you make a
change to the source code. That is, you must create a new executable
file (.exe) every time you make a change.
• The executable file can be executed (run) many times.
• When you sell a program (ex. game), you distribute the executable file,
not the source code. © R. Avanzato 6
Is this C program useful? - Discuss

#include <iostream>
using namespace std;

int main ()
{
double x = 10, y = 25, sum;
sum = x + y;
cout << "Sum = " << sum;
return 0;
}

// How can a user change the inputs x and y?

© R. Avanzato 7
What is wrong with this C program?

#include <iostream>
using namespace std;

int main ()
{
double x , y, sum;
sum = x + y;
cout << "Sum = " << sum;
return 0;
}

// What is the output? Try it.

© R. Avanzato 8
CMPSC 201C
Programming for Engineers

Lecture Set #2
Video part 2 of 4
R.Avanzato © 2017

Topics:
• Review HW and C++ Compiler Installation
• Software Development Life Cycle (SDLC)  Video part 2 of 4
• Problem-solving methodology
• Program Structure
• Constants and Variables
• Assignment Statements
• Input and Output
• Computer Literacy Questions and Answers
• HW #1

© R. Avanzato 9
Software Development Life Cycle
(SDLC)
• Discuss the software crisis in industry today.
• Software development is extremely expensive.
• Many software projects are late, more expensive than planned, do
not work well, etc.
• Small s/w projects (~100 to 5000 lines of code)
• Medium s/w projects (~5000 to 50,000 lines of code
• Large s/w projects (~50,000 to 10 million+ lines of code)

• Software life cycle phases (sequential):


1. Requirements Definition (what should the software do;
English)
2. Specification
3. Coding and Modular testing
4. Integrated testing
5. Maintenance ( 60% of effort )
© R. Avanzato 10
Software Engineering Issues
• Apply engineering methodologies to make software design
more reliable and predictable

• Role of rapid prototyping

• Object-oriented programming

• Extreme Programming (latest craze; teams of 2 programmers at


a PC; may not be suitable for big projects)

• Compare software project to building a bridge or a building

• Consider “Star Wars” project – 1 billion lines of code – must


work!!

• GE x-ray machine software error – killed patient


© R. Avanzato 11
• Importance of thorough testing.
Engineering Problem-solving Methodology
1. State the problem clearly.

2. Describe the input and output carefully.

3. Work the problem by hand (or calculator) with a


simple set of data.

4. Develop a solution and convert to a computer


program.

5. Test the computer program solution with test data.

6. Compare/verify computer output with predicted out


using calculator
(Ref: Etter)
© R. Avanzato 12
Structure of a C Program
1. Preprocessor Commands (e.g. #include < iostream > )

2. Main function (int main() )

3. Declarations ( declare type of variables: int, float,


double, etc.)

4. Initial values ( e.g. double cost = 50.0 )

5. Statements (executed in order from top to bottom)

6. Return statement (e.g. return 0; )

© R. Avanzato 13
2 versions of preprocessor directives

#include <iostream> // version #1


using namespace std;

<< or >>

#include <iostream.h> // version #2

Note: We will use version #1 (more modern)


Avoid version #2 but might be encountered in
older textbooks
© R. Avanzato 14
Constants and Variables
• Constants  specific values; e.g. 2, 4.5, 3.14159

• Variables  correspond to memory locations labeled with an identifier


(name)

• Variable names must begin with alphabetic character or underscore.


Examples: grade, cost, velocity, distance_km, priceOfCar

• Variables can be uppercase or lowercase or mix (see conventions).


Remember, C language is case-sensitive.

• Variables can be of any length, but only first 31 characters are used.

• By convention, the first character of a variable name should be


lowercase. Variable names should be meaningful.

• No embedded spaces allow in a variable name.


© R. Avanzato 15
Numeric Data Types
int float
short double
integer floating pt.
(no decimal point) long (with decimal point) long double

• Most common type specifiers  int & double

• Computers internally store integers differently than floating point


numbers.

• Hint: never directly compare 2 floating point numbers for equality.

• Each type (int, float, double, etc) has a unique maximum value
(total number of bits)

• What happens if value out of range? Experiment.

int x = 2147483647 + 1;  -2147483648 !!! Overflow. Try it.


© R. Avanzato 16
Boolean, Character, String Data types
• Boolean  value can be either “true” (1) or “false” (0) -- only 2 choices
– Example: bool found = true; bool underage = false; bool cost = 10;

• Character  value can be any single ASCII character


– Example: char input = ‘Y’; // notice single quotes are used with characters

• String  sequence of characters enclosed in double quotes


– Example: string name = “Edward”; // notice double quotes for strings
– Example: string name = “CD Player”; // strings may contain spaces

• Constants  values never change within program – 2 approaches


– const double PI = 3.14159; // always use all uppercase for constants
– # define PI 3.14159 // preprocessor (before main) ; no equal sign used

• NOTE: As a programmer, you must choose the appropriate data types to


solve your problem!! Each data type is stored differently in computer memory.
When you write a program you should make a list of all input variables and
output variables and assign types.
© R. Avanzato 17
Review of Comment Statements
Single-line comments:

// xxxxxxxx (this version is preferred for single line comments)

/* xxxxxxxxx */

Multi-line (block comments):


/* xxxxxx
xxxxxx
xxxxxx
*/

Note: You cannot "nest" comments. That is, you cannot place comments
within comments (the C++ compiler gets confused and complains)

© R. Avanzato << Lecture Break >> 18


CMPSC 201C
Programming for Engineers

Lecture Set #2
Video part 3 of 4
R.Avanzato © 2017

Topics:
• Review HW and C++ Compiler Installation
• Software Development Life Cycle (SDLC)
• Problem-solving methodology
• Program Structure
• Constants and Variables
• Assignment Statements
• Input and Output  Video part 3 of 4
• Computer Literacy Questions and Answers
• HW #1

© R. Avanzato 19
User Input Example (lab)
#include <iostream>
using namespace std;
int main ()
{
double x, y, sum;
cout << “Enter a number”;
cin >> x;
cout << “Enter another number”;
cin >> y;
sum = x + y;
cout << "Sum = " << sum;
return 0;
}
// modify above pgm to also calculate the average

© R. Avanzato 20
Conversion Program (lab)
// Program converts inches into feet
#include <iostream>
using namespace std;
int main ()
{
double inches, feet;
cout << “Enter number of inches”;
cin >> inches;
feet = inches / 12.0;
cout << inches << " inches = " << feet << “ feet”;
return 0;
}

© R. Avanzato 21
Conversion Program (lab) - version 2
// Program converts inches into feet
#include <iostream>
using namespace std;
int main ()
{
double inches, feet;
Defining
constants using const double INPERFT = 12.0;
‘const’ is cout << “Enter number of inches”;
considered cin >> inches;
good feet = inches / INPERFT;
programming cout << inches << " inches = " << feet << " feet";
style
return 0;
}

© R. Avanzato 22
Conversion Program (lab) - version 3

// Program converts inches into feet


#include <iostream>
using namespace std;
In our #define INPERFT 12.0 // no equal sign; no semicolon
course, we
// AVOID this style
will not use
#define int main ()
statements {
(but you double inches, feet;
might see
cout << “Enter number of inches”;
them used in
cin >> inches;
other feet = inches / INPERFT;
programs or
books.) cout << inches << “ inches = “ << feet << “ feet”;
return 0;
} © R. Avanzato 23
Example: Calculate Area of Circle
// Program calculates area of a circle
#include <iostream>
using namespace std;
int main ()
{
double radius, area;
const double PI = 3.14159;
cout << “Enter radius in inches”;
cin >> radius;
area = PI * radius * radius;
cout << “Area = “ << area;
return 0;
}

© R. Avanzato 24
File sizes
• What is the size (#of bytes) of a typical .cpp file? (Check
properties)
• Is the .cpp file a text file?
• Is the .cpp file an ASCII file?
• What is the difference between a text file and a MS word doc
file?
• What is the size of a typical .exe file?
• How do you execute a .exe file? (not using the Dev-C or other
editor)

• Lab exercise: Open Notepad and enter your first name. Save
file and examine properties to find file size in bytes. What is
relationship between a character and a byte?

• How many bits are in a byte?


• What is an ASCII character? Find ASCII table on the Internet.
What is the binary and decimal code for the character ‘A’ ??
© R. Avanzato 25
CMPSC 201C
Programming for Engineers

Lecture Set #2
Video part 4 of 4
R.Avanzato © 2017

Topics:
• Review HW and C++ Compiler Installation
• Software Development Life Cycle (SDLC)
• Problem-solving methodology
• Program Structure
• Constants and Variables
• Assignment Statements
• Input and Output
• HW #1  Video part 4 of 4
• Computer Literacy Questions and Answers

© R. Avanzato 26
Homework #1 (see due date)
Write and test 2 complete C++ programs:
#1(convert miles to km), #2 (convert miles per hour to feet per second)
In each program , the user will be prompted for an input value; display all units.
Total of 2 problems. Programs should be clear and user-friendly.

Homework Guidelines…must include documentation


// CMPSC 201C HW #1
// Name: Joe Murphy
// Date created: 5-20-15
// Title: Convert miles to km
// Description: User enters miles, program displays equivalent km.

Paste sample output at end of file after program – include in comments.


Pick appropriate test cases and include output. Validate with calculator.
- Always include units. Clearly prompt user.
- Upload to appropriate Canvas dropbox. Do not upload .exe file.
- Suggested filenames:
1) miles2km.cpp, 2) mph2fps.cpp
- “Attach” .cpp files in each Drop Box – do not copy and paste into message box
- Must use proper indentation
27
- Always include a minimum of 3 test cases for each program.
© R. Avanzato
Homework #1 (Terminology)
Suggested Reading:

C++ for the Absolute Beginner 2nd edition by Mark Lee (PSU Safari)
Chapter 1 and 2
(some minor notation differences)

Prata textbook also useful but uses older style of display (printf instead of cout).
However, concepts are the same. See Amazon for purchase of ebook.

Deitel and Deitel textbook also useful. (PSU Safari)

Etter textbook (see syllabus for reference); available on Amazon $80 (ebook)

Ask questions.

Review Computer Terminology slides:

- Take notes during lecture


- Use HowStuffWorks.com website
- Use wikipedia.com
© R. Avanzato 28
Compile list of terms and definitions (that make sense to you)
Computer Literacy Questions
1. What is a computer? microcontroller? microprocessor? CPU? mainframe? PC?
2. What is the difference between software and hardware?
3. What is the typical storage of a hard drive? CD-ROM? DVD?
4. What is a bit? byte? What does each term mean?
5. How much text can be stored on a hard drive? CD-ROM? DVD?
6. What is a dual-core chip?
7. What is an operating system? examples? functions?
8. What is Linux, UNIX, MS Windows?
9. What is the operating system (OS) for the Macintosh?
10. Can PC programs run on the Mac? Explain.
11. Why is cache (pronounced "cash") memory? RAM? ROM?
12. What is a computer program?
13. Why are you studying C/C++? Why not Fortran, Pascal, ADA, Cobol, LISP, Java?
14. What is C++? What is OO programming? What is difference between C and C++?
15. What is a database program? a spreadsheet? mathematics package? Name popular products.
16. When should you use C (or other high level language) versus a spreadsheet or database program
or a math package?
17. What is a file? Is a C program a file?
18. What is a text file? ASCII file? executable file?
19. What is meant be the term "software engineering"?
20. What is Moore's Law? Singularity?

Note: We will cover several of these questions each class. Ultimately,


you should be able to answer and discuss all of these. Keep records
and notes. These terms will show © R.
upAvanzato
on exams and quizzes. 29

You might also like