Title: CSC 215
1CSC 215 Introduction to Structured Programming
with C Instructor Eric Sedgwick
2Administrative Issues
- Roll Call
- Syllabus
- WebPage
- HyperNews
- Lab
3Tonight
- Class administration
- Introduction to C programming
4Are you in the right class?
- This course is a prerequisite for CSC 310. You
should not be taking both at the same time! - CSC 225 - for those with programming experience
- Java
5CSC 215 covers
- Intro to programming
- variables and types
- looping
- branching
- functions
- call by reference
- call by value
- file I/O (input/output)
- arrays
- strings
- and, hopefully
- classes
- recursion
6Q What is the purpose of CS 215?
- A1 To learn the syntax (grammatical rules) of
C. - A2 To learn how to program. This is harder, and
more important.
7C
- Created by Bjarne Stroustrup, ATT, 80s
- Extends C, most C programs are C programs
- A high-level language
- A compiled language
- Supports object-oriented programming (covered in
CS310) - Widely Used
8Programming
Problem Statement
Algorithm(Recipe)
?
Program
A problem well stated is a problem half solved
-Charles Kettering, inventor of the electric
cash register
Experiment !
9Algorithm
- An algorithm is a sequence of precise
instructions that leads to a solution. - Recipe
10C Compiler
Object Code(executable on computer)
Source Code(entered by programmer)
- Translates from high-level code (C) to
low-level code (object code) that can be
understood by the machine. - Microsoft Visual C is available in the labs and
is the recommended compiler. - Linking - combine with previously compiled code
(for example input/output code)
11hello.cpp
- Type into editor
- Compile\Link\Execute
- To save output - cut and paste
- Save source code ( .cpp) to diskette
- Turn in
- Hard Copy of source code and output
12Using Visual C
- Create folder (directory) for your files on hard
drive - Create source file
- File gt New gt files gt C source file gt type name
- Build compile and link
- respond yes to create default workspace?
- Important close workspace before creating a new
file - In lab copy source files to your floppy disk
13peas.cpp
- Copy from disk to hard drive
- Errors
- Syntax Error, program compiles with erroror
warning - Logic Error, program does the wrong thing
- Run-Time Error, e.g., division by zero
14Layout of a simple program
- include ltiostreamgt
- using namespace std
-
- int main() Variable Declarations Statement_
1 - Statement_2 Statement_n return 0
-
Necessary for input and output
Every program has exactly one main function
Statements are the instructions thatget executed
Signal that program terminated sucessfully
15Syntax
- include ltiostreamgtusing namespace std
- include directive and namespace
- necessary for input and output
- cin gtgt
- reads from the keyboard
- cout ltlt
- outputs to the screen
- \n
- newline character
16Programming Goals
- Eliminate errors
- Syntax - error or warning when compiling
- Logic - program does the wrong thing
- Run-Time - e.g., division by zero,
- Style
- Program should be easy to read and understand.
17Execute code by hand
- Important skill
- read code and determine the output
- usually need pencil and paper
- The two most important types of questions on the
exams - write code to perform task
- execute code by hand
18bars.cpp
- What does this program do?
- What happens if the user inputs the numbers
- 3
- 2.3
- 7
- 1.1
- Variables
- Input and Output
19Comments
- Comments are ignored by the compiler, and are
used to provide information to people (the
grader) reading your source code. There are two
forms. - Everything on the line following // is ignored.
- // bars.cpp
- // author Eric Sedgwick
-
- double height // in centimeters
- Everything between / and / is ignored
- /
- This program was last modified 9/13/99
- /
20Variables
memory
- A variable is a box in which to store values.
- Actually an allocated block of memory.
- The value of a variable can be changed repeatedly
- Every variable has a type ...integer number,
floating point number, character, word, ... - The name of a variable is called an identifier.
21Identifiers
- Start with _ or letter
- Other characters are either _ , letter, or
digit - Cannot use reserved words (keywords)
- return, int, double, ...
- Do not use names that already used.
- main, cout, cin, ...
- Legal x, a123, f_1, Rate, count, _number_
- Illegal 123a, g.h, this_at_here
22More on Identifiers
- C is case sensitiveCount, COUNT, count, CoUnT
are all different identifiers. - Choose meaningful names
- interest_rate is better than x
- Caution! some compilers do not use all characters
of the identifier. (Can assume 6 are used.) - interest_rate1 vs. interest_rate2
- what about your compiler?
23Variable Declarations
memory
- Variables must be declared before they can be
used. This allocates space in memory. The amount
of space depends on the variable type. - Bit - a binary digit - 0 or 1
- byte - 8 bits
- Form of a Declarationtype_name
variable_1,variable_2, - Examplesint soc_sec_numdouble
degrees,wind_chill
24Types
- int - integer 5 423,564 -97,234
0 - double - floating point number 5.1 0.0
-4.2 0.0023 1.5e03 - More types soon!
- bool, long double, char, float
25Assignment Statements
memory
- To change the value of a variable use an
assignment statement.variable expression - Examplesdistance speed timeinterest_rate
prime_rateanswer -13count count 1
26What is the output?
memory
- ... double interest_rate, prime_rate
- interest_rate prime_rate 5.9
- cout ltlt interest_rate
- ...
27Variable Initialization
- Initializing Declaration
- type_name variable_1 expression_1, variable_2
expression_2, variable_n
expression_n - Exampledouble interest_rate, prime_rate 5.9
- A very good idea!
- Be careful with initialization and loops!
28I/O - Input and Output
- Need to get data to the program.
- We will work with I/O streams
- cin - input stream associated with keyboard
- cout - output stream associated with terminal
- Later we will learn how to work with files.
29Output using cout
- bars.cpp
- To insert spaces between numbers
- cout ltlt number_1 ltlt ltlt number_2
- For a new line
- \n is used inside quotes
- cout ltlt number_1 ltlt \n ltlt number_2
- endl is used without quotes
- cout ltlt number_1 ltlt endl ltlt number_2
- For mathematical expressions use parentheses
- cout ltlt The sum is ltlt (number_1 number_2)
30Decimal Points
- What is the output?
- double price 75.5
- cout ltlt The price is ltlt price ltlt endl
- Depending on the compiler
- The price is 75.5000 -or-
- The price is 75.5 -or-
- The price is 7.55000001
-
31- Use magic formula
- double price 75.5
- cout.setf(iosfixed)
- cout.setf(iosshowpoint)
- cout.precision(2)
- cout ltlt The price is ltlt price ltlt endl
- Output
- The price is 75.50
- For other precisions
- cout.precision(?)
32cin
- More than one variable can be read by a single
statement. - cout ltlt Please enter month day year. \n
- cin gtgt month gtgt day gtgt year
- Numbers can be separated by some number of
whitespaces (spaces, tabs or newlines). Either
of these are ok - Please enter month day year. Please enter month
day year. - 10 13 1999 10 13
- 1999
33cout and Escape sequences
- Escape sequences
- \n - newline character
- \t - tab character - useful for lining up tables
- \a - alert
- \ - double quote
- \\ - backslash
- Each is a single character!
34weight.cpp
- Design an algorithm, then write a program (on
paper) that allows the user to input a weight in
kilograms, converts it to pounds, and then
outputs the answer. For our purposes, each
kilogram weighs 2.204 pounds.
35I/O Goals
- Prompt user on what to enter.
- Echo input back to the terminal, so that the user
knows that the information was inputted
correctly. - Provide text so that the user knows what the
output means. Dont just output numbers. - End your programs output with a new line.
36HW
- First Assignment is posted and due in a week!
- Submit via email
- attach source files (.cpp files) to email
- CSC215HW