etter/ingber engineering problem solving with c fundamental concepts chapter 2 simple c programs

28
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 2 Simple C Programs

Upload: jacob-johnson

Post on 27-Mar-2015

221 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 2 Simple C Programs

Etter/Ingber

Engineering Problem Solving with C

Fundamental Concepts

Chapter 2

Simple C Programs

Page 2: Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 2 Simple C Programs

Etter/Ingber

Program Structure

Page 3: Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 2 Simple C Programs

Etter/Ingber

Program Structure - General Formpreprocessing directives

int main(void)

{

declarations

statements

}

Page 4: Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 2 Simple C Programs

Etter/Ingber

Program Structure

•Comments begin with the characters /* and end with the characters */

•Preprocessor directives give instructions to the compiler

•Every C program contains one function named main

•The body of the main function is enclosed by braces, { }

Page 5: Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 2 Simple C Programs

Etter/Ingber

Program Structure - continued

•The main function contains two types of commands: declarations and statements

•Declarations and statements are required to end with a semicolon (;)

•Preprocessor directives do not end with a semicolon

•To exit the program, use a return 0; statement

Page 6: Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 2 Simple C Programs

Etter/Ingber

Program Structure - First Program/******************************************************************//* Program chapter1 *//* *//* This program computes the sum two numbers */

#include <stdio.h>int main(void){ /* Declare and initialize variables. */ double number1 = 473.91, number2 = 45.7, sum;

/* Calculate sum. */ sum = number1 + number2;

/* Print the sum. */ printf(“The sum is %5.2f \n”, sum);

/* Exit program. */ return 0;}/***************************************************************************/

Page 7: Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 2 Simple C Programs

Etter/Ingber

Constants and Variables

Page 8: Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 2 Simple C Programs

Etter/Ingber

Constants and Variables

•A constant is a specific value

•A variable is a memory location that is assigned a name or an identifier

•An identifier is used to reference a memory location.

•Rules for selecting a valid identifier•must begin with an alphabetic character or underscore

•may contain only letters, digits and underscore (no special characters)

•case sensitive

•can not use keywords as identifiers

Page 9: Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 2 Simple C Programs

Etter/Ingber

C Data Types•Integers

•short•int•long

•Floating-Point Values•float•double•long double

•Characters•char

Page 10: Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 2 Simple C Programs

Etter/Ingber

Symbolic Constants

Defined with a preprocessor directive Compiler replaces each occurrence of the

directive identifier with the constant value in all statements that follow the directive

Example– #define PI 3.141593

Page 11: Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 2 Simple C Programs

Etter/Ingber

Assignment Statements

Page 12: Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 2 Simple C Programs

Etter/Ingber

Assignment Statements Used to assign a value to a variable General Form:

identifier = expression;

Example 1

double sum = 0; sum

Example 2int x;x=5; x

Example 3char ch;ch = ‘a’; a

0

5

a

Page 13: Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 2 Simple C Programs

Etter/Ingber

Assignment Statements - continued

Example 3int x, y, z;x=y=0;z=2; x

y

z

Example 4y=z; y

0

0

2

2

Page 14: Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 2 Simple C Programs

Etter/Ingber

Arithmetic Operators Addition + Subtraction - Multiplication * Division / Modulus %

– Modulus returns remainder of division between two integers

– Example5%2 returns a value of 1

Page 15: Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 2 Simple C Programs

Etter/Ingber

Integer Division

Division between two integers results in an integer.

The result is truncated, not rounded Example:

5/3 is equal to 1

3/6 is equal to 0

Page 16: Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 2 Simple C Programs

Etter/Ingber

Priority of Operators

1 Parentheses Inner most first2 Unary operators Right to left

(+ -)

3 Binary operatorsLeft to right(* / %)

4 Binary operatorsLeft to right(+ -)

Page 17: Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 2 Simple C Programs

Etter/Ingber

Increment and Decrement Operators Increment Operator ++

• post incrementx++;

• pre increment ++x;

Decrement Operator - -• post decrement x- -;

• pre decrement - -x;

Page 18: Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 2 Simple C Programs

Etter/Ingber

Abbreviated Assignment Operatoroperator example equivalent statement

+= x+=2; x=x+2;

-= x-=2; x=x-2;

*= x*=y; x=x*y;

/= x/=y; x=x/y;

%= x%=y; x=x%y;

Page 19: Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 2 Simple C Programs

Etter/Ingber

Precedence of Arithmetic andAssignment Operators

Precedence Operator Associativity1 Parentheses: () Innermost first

2 Unary operators+ - ++ -- (type)

Right to left

3 Binary operators* / %

Left ot right

4 Binary operators+ -

Left ot right

5 Assignment operators= += -= *= /= %=

Right to left

Page 20: Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 2 Simple C Programs

Etter/Ingber

Standard Input and Output

Page 21: Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 2 Simple C Programs

Etter/Ingber

Standard Output

printf Function– prints information to the screen

– requires two arguments• control string

• conversion specifier Exampledouble angle = 45.5;printf(“Angle = %.2f degrees \n”, angle);

Output:Angle = 45.50 degrees

Page 22: Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 2 Simple C Programs

Etter/Ingber

Standard Input

scanf Function– inputs values from the keyboard

– required arguments• control string• memory locations that correspond to the specifiers in the control

string Example:

double distance;char unit_length;scanf("%1f %c", &distance, &unit_length); It is very important to use a specifier that is appropriate for the data type of

the variable

Page 23: Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 2 Simple C Programs

Etter/Ingber

TABLE 2.7 Conversion Specifiersfor Input Statements

Variable Type SpecifierInteger Values

int %i, %dshort %hi, %hdlong int %li, %ldunsigned int %uunsigned short %huunsigned long %lu

Floating-Point Valuesfloat %f, %e, %E, %g, %Gdouble %lf, %le, %lE, %lg, %lGlong double %Lf, %Le, %LE, %Lg, %LG

Character Valueschar %c

Page 24: Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 2 Simple C Programs

Etter/Ingber

Practice!

•printf("Sum = %5i; Average = %7.1f \n", sum, average);

•printf("Sum = %4i \n Average = %8.4f \n", sum, average);

•printf("Sum and Average \n\n %d %.1f \n", sum, average);

•printf("Character is %c; Sum is %c \n", ch, sum);

•printf("Character is %i; Sum is %i \n", ch, sum);

Assume that the integer variable sum contains the value 65, the double variable average contains the value 12.368 and that the char variable ch contains the value 'b'. Show the output line (or lines) generated by the following statements.

Page 25: Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 2 Simple C Programs

Etter/Ingber

Library Functions

Page 26: Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 2 Simple C Programs

Etter/Ingber

Math Functions

fabs(x) Absolute value of x.

sqrt(x) Square root of x, where x>=0.

pow(x,y) Exponentiation, xy. Errors occur if x=0 and y<=0, or if x<0 and y is not an

integer.ceil(x) Rounds x to the nearest integer toward (infinity).

Example, ceil(2.01) is equal to 3.floor(x) Rounds x to the nearest integer toward - (negative infinity). Example, floor(2.01) is equal to 2.exp(x) Computes the value of ex.

log(x) Returns ln x, the natural logarithm of x to the base e. Errors occur if x<=0.log10(x) Returns log10x, logarithm of x to the base 10.

Errors occur if x<=0.

Page 27: Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 2 Simple C Programs

Etter/Ingber

Trigonometric Functions

sin(x) Computes the sine of x, where x is in radians.

cos(x) Computes the cosine of x, where x is in radianstan(x) Computes the tangent of x, where x is in

radians.asin(x)Computes the arcsine or inverse sine of x,

where x must be in the range [-1, 1]. Returns an angle in radians in the range [-

/2,/2].acos(x) Computes the arccosine or inverse

cosine of x, where x must be in the range [-1, 1].Returns an angle in radians in the range [0,

].atan(x) Computes the arctangent or inverse

tangent of x. The Returns an angle in radians in the range [-/2,/2].

atan2(y,x) Computes the arctangent or inverse tangent of the value y/x. Returns an angle in radians in the range [-, ].

Page 28: Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 2 Simple C Programs

Etter/Ingber

Character Functions

toupper(ch) If ch is a lowercase letter, this function returns the corresponding uppercase letter; otherwise, it returns ch

isdigit(ch) Returns a nonzero value if ch is a decimal digit; otherwise, it returns a zero.

islower(ch) Returns a nonzero value if ch is a lowercase letter; otherwise, it returns a zero.

isupper(ch) Returns a nonzero value if ch is an uppercase letter; otherwise, it returns a zero.

isalpha(ch) Returns a nonzero value if ch is an uppercase letter or a lowercase letter; otherwise, it returns a zero.

isalnum(ch) Returns a nonzero value if ch is an alphabetic character or a numeric digit; otherwise, it returns a zero.