Title: Computer Programming Lab1
1Computer ProgrammingLab1
- Shyh-Kang Jeng
- Department of Electrical Engineering/
- Graduate Institute of Communication Engineering
- National Taiwan University
2Textbook and Course Website
- H. M. Deitel and P. J. Deitel, C How to
Program, 4th ed., Prentice-Hall, 2003. ?? - http//cc.ee.ntu.edu.tw/skjeng/ComputerProgrammin
g2004Fall.htm
3Microsoft Visual C
4Projects, Workspaces, and Files
5First Program
6First Program
- // MyFirstProgram\Main.cpp
- // Fig. 1.2 of C How to Program
- // A first program in C
- include ltiostreamgt
- int main()
- stdcout ltlt "Welcome to C!\n"
- return 0
7Preparing and Running a Program
- New Projects Console Application
- Location, Project name
- New Files C Source File, File
- Editing the source program .h or .cpp
- Build Compile .cpp
- Build Build .exe
- Execute .exe
8Good Programming Practices
- Keep it simple
- Read the manuals/on line help (MSDN)
- Write small programs to learn how a C feature
works
9Escape Sequences
- \n newline
- \t horizontal tab
- \r carriage return
- \a alert
- \\ backslash
- \ double quote
10Adding Two Integers
11Adding Two Integers
- // AddingTwoIntegers\Main.cpp
- // Fig. 1.6 of C How to Program
- // Addition program.
- include ltiostreamgt
- int main()
- int integer1// first number to be input by user
- int integer2// second number to be input by
user - int sum // variable in which sum will be stored
- stdcout ltlt "Enter first integer\n" // prompt
- stdcin gtgt integer1
- stdcout ltlt "Enter second integer\n"
- stdcin gtgt integer2
- sum integer1 integer2
- stdcout ltlt "Sum is " ltlt sum ltlt stdendl
- return 0
12Good Programming Practices
- Choose meaningful variable name
- Avoid identifiers begin with underscores and
double underscores - Place a blank line between declaration and
adjacent executable statements
13Memory Concepts
- stdcin gtgt integer1
- Assume user entered 45
- stdcin gtgt integer2
- Assume user entered 72
- sum integer1 integer2
14Using the Debugger
15Arithmetic Operators
-
- Multiplication
- /
- Division
- Integer division truncates remainder
- 7 / 5 evaluates to 1
-
- Modulus operator returns remainder
- 7 5 evaluates to 2
16Precedence of Arithmetic Operators
17Equality and Relational Operators
18Relational Operators Test
19Relational Operators Test (1)
- // RelationalOperatorsTest\Main.cpp
- // Fig. 1.14 of C How to Program
- // Using if statements, relational
- // operators, and equality operators
- include ltiostreamgt
- using stdcout
- using stdcin
- using stdendl
- int main()
- int num1 // first number to be read from user
- int num2 // second number to be read from user
20Relational Operators Test (2)
- cout ltlt
- "Enter two integers, and I will tell you\n"
- ltlt "the relationships they satisfy "
- cin gtgt num1 gtgt num2
- if( num1 num2 )
- cout ltlt num1 ltlt " is equal to " ltlt num2 ltlt
endl - if( num1 ! num2 )
- cout ltlt num1 ltlt " is not equal to " ltlt num2 ltlt
endl - if( num1 lt num2 )
- cout ltlt num1 ltlt " is less than " ltlt num2 ltlt
endl
21Relational Operators Test (3)
- if( num1 gt num2 )
- cout ltlt num1 ltlt " is greater than " ltlt num2 ltlt
endl - if( num1 lt num2 )
- cout ltlt num1 ltlt " is less than or equal to "
- ltlt num2 ltlt endl
- if( num1 gt num2 )
- cout ltlt num1 ltlt " is greater than or equal to "
- ltlt num2 ltlt endl
- return 0
-
22Good Programming Practices
- There should be no more than one statement per
line in a program - Split a lengthy statement across lines
consistently and carefully to make it easy to read