Title: C Basics
1C Basics
2Learning Programming Language Basics
- Data Types simple
- Expressions
- Relational and Logical Operators
- Conditional Statements
- Arrays
- Iteration
- Memory Allocation
- I/O
- Functions
3Hello World
- //Simple C program
- include ltiostreamgt //input and output library
- include ltstringgt //string library
- using namespace std // predefined objects
- int main() //first function,
- //function delimiter
- cout ltlt Hello world! ltlt endl //output stream
- return 0 //ends execution and
returns
4Simple Data Types
- Int 32 bit, optional qualifiers long, short
- Float 32 bit, IEEE floating point
- Double 64 bit, optional qualifer long
- Char 8 bit
- Byte 8 bit
5Operators
- Arithmetic
- , -, /,
- - modulus or remainder, integer only, e.g. 234
- Relational
- lt, gt, lt, gt, !,
- (c a) \\ common error is omitting one of the
- Be careful using ! or with floating point
values, rounding occurs at the machine level - Logical
- And , e.g. (c lt a) (c gt b)
- Or , e.g. (c lt a) (c gt b)
6Operator shortcuts Compound Assignment
- Increment, Decrement
- , --
- a a 1 \\can be written as a
- a, a, a--, --a
- Position of operator is important, e.g.
- b f(a) \\a is incremented after f(a) call
- c f(a) \\a is incremented before f(a) call
- Compound Assignment Operators
- int a
-
- a a 5
- a 5
- Can be used with all arithmetic operators
7Exercise - 1
- Compute volume of an object
- Read in
- Object mass (grams)
- Object density (grams per cubic centimeter)
- Output
- Volume (cubic centimeters)
- Relationship
- Density Mass/Volume
-
8Code Needs
- Determine variables (with types needed)
- Issue prompts use cout, e.g.
- Cout ltlt Object Mass? ltlt endl
- Read in values use cin, e.g.
- Cin gtgt mass
- Compute Distance and Velocity
9Conditional Expressions
- Any combination of logical and relational
operators - Use () as needed
- Examples
- Simple if statement
- if( a lt b)
- s1
- else
- s2
10If/else statement
- General format single statement execution on
true condition - Long format multiple statement execution on
true condition - General rule more than one statement under if
and/or else condition requires - Short-hand format single statement
- Nested if statements
11If single statement
- Format
- if(expression)
- s1
- else
- s2
12If multiple statements
- Format
- if(expression)
-
- s1
- s2
-
-
- else
-
- s1
- s2
-
-
13If shorthand
- Used only with single statements
- Format
- Expression ? S1 s2
- Example
- c ! b ? c b
- Example
- int a,b
-
- int min a lt b ? a b
14Short-circuit evaluation
- - evaluation terminates with first false
condition - - evaluation terminates with first true
condition - Done to insure objects can be properly
manipulated, e.g. - (i ! 0) ((j/i) gt 5)
15Arrays
- Included in C for backward compatibility with C
- Arrays less popular and class representation of
lists, especially the vector class more
frequently used - Vector class is an example of container classes
is defined in the Standard Template Library (STL)
16Simple arrays - Declaration
- const int N 20
- const int M 40
- const int MaxListSize 1000
- // Note const qualifier used to denote constant
values - //Declaration examples
- int a10
- float cMN
- int ValuesMaxListSize
17Simple Arrays - Referencing
- Array elements are referenced using subscripts or
indices - First index in C is 0
- Examples
- a0 b
- ai aj 3
- aak 12
- Warning No automatic checking for array
boundaries. Illegal subscripts will result in
inaccurate memory references or storage -
18Simple Array - Initialization
- Int frequency5 0,0,0,0,0
- Int total5 0 // first element set to 0,
remainder are uninitialized and default at 0 - Int digits 0,1,2,3,4,5,6,7,8,9
- Int zero 0 //one-element array
19Iteration
20Iteration using while
- Format
- while (conditional expression)
-
- s1
-
-
- Statement in the block are executed as long as
condition is true - If condition is evaluated to false, statements
are never executed
21Interation using for
- Format
- for (ForInit ForExpression PostExpression)
-
-
- ForInit initialization step to start loop
- ForExpression logical expression
- PostExpression next iteration of loop
22For examples
- for( i 0 ilt maxsize i)
-
-
- for(jminsize j lt maxsize j2)
-
-
- for(k maxsize k gt minsize k--)
-
23Exercises
- Compute n! using a for loop
- Compute the nth Fibonacci term using a while loop
24- Example, compute roots of a quadratic equation
- //Quadratic roots
- include ltiostreamgt
- include ltstringgt
- include ltmath.hgt
- Using namespace std
- int main()
-
- double a, b, c
- cout ltlt Coefficients for quadratic equation
- cin gtgt a gtgt b gtgt c
- if ((a ! 0) ((bb 4ac) gt 0))
- double radical sqrt(bb 4 a c)
- double root1 (-b radical) / (2 a)
- double root2 (-b radical) / (2 a)
- cout ltlt roots are ltlt root1 ltlt and
ltlt root2 ltlt endl -
- else
- cout ltlt No real roots ltlt endl
25Compute area of a circle
- include ltiostreamgt
- include ltstringgt
- Using namespace std
- const float Pi 3.1415
- int main()
-
- float radius
- cout ltlt Enter radius
- cin gtgt radius
- //compute area
- float area Pi radius radius
- //output
- cout ltlt Area ltlt area
26More general using functions
include ltiostreamgt include ltstringgt Using
namespace std float CircleArea(float r)
const float Pi 3.1415 return Pi r
r int main() float radius cout ltlt
Enter radius cin gtgt radius //compute
area float area CircleArea(radius)
//output cout ltlt Area ltlt area
27Function prototyping
- Functions cannot be used until they are defined
- Can create problems with functions calling other
functions - Use prototype conventions
- Define function prototype before main
- Implement function after main
28Prototype Example
include ltiostreamgt include ltstringgt Using
namespace std float CircleArea(float r) int
main() float radius cout ltlt Enter
radius cin gtgt radius //compute area
float area CircleArea(radius) //output
cout ltlt Area ltlt area float
CircleArea(float r) const float Pi
3.1415 return Pi r r
29Homework
- Write a program for integrating a quadratic
polynomial - User input will be
- Quadratic coefficients a1, a2, a3
- Interval of interest n1, n2
- Output will be the area