# CPS 393 Lecture Notes - Lecture 6: Increment And Decrement Operators, Scanf Format String, Floor And Ceiling Functions

88 views8 pages

4 Aug 2016

School

Department

Course

Professor

Lecture 6: C examples, conditionals, loops, functions

// Numeric Placeholder

#include <stdio.h>

int main (void){

/* declarations */

int a;

double x;

/* executable statements */

a = 1000; //assignment statement

x = 100.583665;

printf ("%d\n", a); //placeholder and then expression

printf ("%3d\n", a);

printf ("%4d\n", a);

printf ("%5d\n", a);

printf ("\n");

printf ("%lf\n", x);

printf ("%15lf\n", x);

printf ("%15.4lf\n", x);

printf ("%18.2lf\n", x);

printf ("%12.0lf\n", x);

return (0);

}

// Ex 1.5 Circle program (from keyboard)

#include <stdio.h>

// #define PI 3.1416 always need to do #define macro value. This is called MACRO. Pi is not

a variable

int main (void){

/* declarations */

double diam, area, circ, r;

/* get diameter from user */

printf ("Enter a value for the diameter: ");

scanf ("%lf", &diam);

/* do the computations */

r = diam / 2;

area = PI * r * r;

circ = 2 * PI * r;

/* display the report on the screen */

printf ("\nA circle with a diameter of %3.1lf cm, ", diam);

printf ("has an area of %5.3lf cm2\n", area);

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

printf ("and a circumference of %4.2lf cm.\n", circ);

return (0);

}

// Integer Division

#include <stdio.h>

int main (void){

int a, b, c;

double x, y, z, w;

a = 10; b = 20;

/* dividing two integers */

z = a / b;

c = a / b;

printf ("The value of z is %5.3lf ", z);

printf ("and the value of c is %d\n", c);

/* converting (casting) one operand before the division*/

x = (double)a / b;

printf ("The value of x is %5.3lf\n", x);

/* casting the quotient after the division*/

y = (double) (a / b);

printf ("The value of y is %5.3lf\n", y);

/* casting both operands before the division*/

w = (double)a / (double)b;

printf ("The value of w is %5.3lf\n", w);

return (0);

}

// Math Functions

#include <stdio.h>

#include <math.h> //need this for math operations

#define PI 3.1416

int main (void){

double before, after, x, y, z;

/* the ceil function */

before = -217.5;

after = ceil (before);

printf ("The ceiling of %3.1lf is %3.1lf\n", before, after);

/* the floor function */

before = -217.5;

after = floor (before);

printf ("The floor of %3.1lf is %3.1lf\n", before, after);

/* the log function */

before = 200.0;

after = log (before);

printf ("The ln of %3.1lf is %3.1lf\n", before, after);

/* the log10 function*/

before = 200.0;

after = log10 (before);

printf ("The log of %3.1lf is %3.1lf\n", before, after);

/* the sqrt function*/

before = 200.0;

after = sqrt (before);

printf ("The square root of %3.1lf is %3.1lf\n", before, after);

/* the fabs function (for doubles)*/

before = -413.56;

after = fabs (before);

printf ("The absolute value of %3.1lf is %3.1lf\n", before, after);

18

19

20

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

/* the sin function */

before = 45.0;

after = sin (before * PI / 180);

printf ("The sine of %3.1lf is %5.3lf\n", before, after);

/* the cos function */

before = 45.0;

after = cos (before * PI / 180);

printf ("The cosine of %3.1lf is %5.3lf\n", before, after);

/* the tan function */

before = 45.0;

after = tan (before * PI / 180);

printf ("The tangent of %3.1lf is %5.3lf\n", before, after);

/* the exp function */

before = 10.0;

after = exp (before);

printf ("e to the power of %3.1lf is %5.3lf\n", before, after);

/* the pow function */

x = 9.0; y = 3.0;

z = pow (x, y);

printf ("%3.1lf to the power of %3.1lf is %3.1lf\n", x, y, z);

return (0);

}

// Increment + Decrement

#include <stdio.h> //always need to come before the main method

int main (void){

int a, b;

a = 5;

/* increment (++) */

/* a is incremented by 1 */

++a;

printf ("After ++a, a is now %d\n", a);

/* a is once more incremented by 1 */

a++;

printf ("After a++, a is now %d\n", a);

/* a is incremented but b gets the current a */

b = a++;

printf ("After b=a++, a is now %d and b is %d\n", a, b);

/* a is incremented and b gets the incremented a */

b = ++a;

printf ("After b=++a, a is now %d and b is %d\n", a, b);

/* decrement (--) */

/* a is decremented by 1 */

--a;

printf ("After --a, a is now %d\n", a);

/* a is once more decremented by 1 */

a--;

printf ("After a--, a is now %d\n", a);

/* a is decremented but b gets the current a */

b = a--;

printf ("After b=a--, a is now %d and b is %d\n", a, b);

/* a is decremented and b gets the decremented a */

b = --a;

printf ("After b=++a, a is now %d and b is %d\n", a, b);

return (0);

}

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33