Computer Programming Lab1 - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

Computer Programming Lab1

Description:

1. Computer Programming. Lab1. Shyh-Kang Jeng. Department of Electrical Engineering ... Precedence of Arithmetic Operators. 17. Equality and Relational Operators. 18 ... – PowerPoint PPT presentation

Number of Views:44
Avg rating:3.0/5.0
Slides: 23
Provided by: shyhka
Category:

less

Transcript and Presenter's Notes

Title: Computer Programming Lab1


1
Computer ProgrammingLab1
  • Shyh-Kang Jeng
  • Department of Electrical Engineering/
  • Graduate Institute of Communication Engineering
  • National Taiwan University

2
Textbook 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

3
Microsoft Visual C
4
Projects, Workspaces, and Files
5
First Program
6
First 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

7
Preparing 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

8
Good Programming Practices
  • Keep it simple
  • Read the manuals/on line help (MSDN)
  • Write small programs to learn how a C feature
    works

9
Escape Sequences
  • \n newline
  • \t horizontal tab
  • \r carriage return
  • \a alert
  • \\ backslash
  • \ double quote

10
Adding Two Integers
11
Adding 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

12
Good Programming Practices
  • Choose meaningful variable name
  • Avoid identifiers begin with underscores and
    double underscores
  • Place a blank line between declaration and
    adjacent executable statements

13
Memory Concepts
  • stdcin gtgt integer1
  • Assume user entered 45
  • stdcin gtgt integer2
  • Assume user entered 72
  • sum integer1 integer2

14
Using the Debugger
15
Arithmetic Operators
  • Multiplication
  • /
  • Division
  • Integer division truncates remainder
  • 7 / 5 evaluates to 1
  • Modulus operator returns remainder
  • 7 5 evaluates to 2

16
Precedence of Arithmetic Operators
17
Equality and Relational Operators
18
Relational Operators Test
19
Relational 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

20
Relational 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

21
Relational 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

22
Good 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
Write a Comment
User Comments (0)
About PowerShow.com