Week 2 -Wednesday What did we talk about last time? Binary - - PowerPoint PPT Presentation

week 2 wednesday what did we talk about last time binary
SMART_READER_LITE
LIVE PREVIEW

Week 2 -Wednesday What did we talk about last time? Binary - - PowerPoint PPT Presentation

Week 2 -Wednesday What did we talk about last time? Binary representation C literals Unity can only be manifested by the Binary. Unity itself and the idea of Unity are already two. Buddha Function Result Function Result


slide-1
SLIDE 1

Week 2 -Wednesday

slide-2
SLIDE 2

 What did we talk about last time?  Binary representation  C literals

slide-3
SLIDE 3
slide-4
SLIDE 4
slide-5
SLIDE 5

Unity can only be manifested by the Binary. Unity itself and the idea of Unity are already two.

Buddha

slide-6
SLIDE 6
slide-7
SLIDE 7

Function Result Function Result

cos(double theta)

Cosine of theta exp(double x) ex

sin(double theta)

Sine of theta log(double x) Natural logarithm of x

tan(double theta)

Tangent of theta log10(double x) Common logarithm of x

acos(double x)

Arc cosine of x pow(double base, double exponent) Raise base to power exponent

asin(double x)

Arc sine of x sqrt(double x) Square root of x

atan(double x)

Arc tangent of x ceil(double x) Round up value of x

atan2(double y, double x)

Arc tangent of y/x floor(double x) Round down value of x

fabs(double x)

Absolute value of x fmod(double value, double divisor) Remainder of dividing value by divisor

slide-8
SLIDE 8

 You must #include <math.h> to use math functions

#include <math.h> #include <stdio.h> int main() { double a = 3.0; double b = 4.0; double c = sqrt(a*a + b*b); printf("Hypotenuse: %f\n", c); return 0; }

slide-9
SLIDE 9

 Just using #include gives the headers for math functions,

not the actual code

 You must link the math library with flag –lm  Now, how are you supposed to know that?

> gcc hypotenuse.c -o hypotenuse -lm > man 3 sqrt

slide-10
SLIDE 10

Man (manual) pages give you more information about commands and functions, in 8 areas:

1.

General commands

2.

System calls

3.

Library functions (C library, especially)

4.

Special files and devices

5.

File formats

6.

Miscellaneous stuff

7.

System administration

Try by typing man topic for something you're interested in

If it lists topics in different sections, specify the section

For more information:

> man 3 sqrt > man man

slide-11
SLIDE 11

 You are sitting at the origin  There's a hyperspace ghost demon at location (x,y)  Write a program to determine the angle to fire your C-

controlled proton accelerator in order to remove the deadly menace

slide-12
SLIDE 12
slide-13
SLIDE 13

 We haven't talked about any input in C yet  To read the next character from input, you can use the getchar()

function

 It will return the value of the next character (as an int) or -1 if the end of

the file is reached

  • Store the value as an int first to check to see if the end of the file has been

reached

  • If not, you can then store it as a char

int value = getchar(); if( value == -1 ) printf("End of file!");

slide-14
SLIDE 14

 putchar() is the output equivalent of getchar()  It outputs a single character at a time  You could use printf() with the %c formatter instead, but

putchar() can be more convenient for single characters char letter = 's'; putchar('q'); putchar(letter);

slide-15
SLIDE 15

 Let's write a function that reads input, character by character,

and returns the equivalent int value

  • For example, the characters '4', '5', '1', and ' ' would be

interpreted as the int 451

 We'll read char values until we get a space, newline, or EOF  Each time, we multiply our sum by 10 and then add the

numerical value of the input

  • We have to subtract '0' from the input, otherwise we'll get the

character values of the digits 0 through 9 (which are not 0 through 9)

 Note: a function like this will be provided for you for some labs

slide-16
SLIDE 16
slide-17
SLIDE 17

 There are preprocessor directives which are technically not

part of the C language

 These are processed before the real C compiler becomes

involved

 The most important of these are

  • #include
  • #define
  • Conditional compilation directives
slide-18
SLIDE 18

 You have already used #include before

  • #include <stdio.h>

 It can be used to include any other file

  • Use angle brackets (< >) for standard libraries
  • Use quotes (" ") for anything else

 It literally pastes the file into the document where the #include

directive is

 Never #include .c files (executable code), only .h files

(definitions and prototypes)

 It is possible to have a circular include problem

slide-19
SLIDE 19

 The primary way to specify constants in C is with a #define  When you #define something, the preprocessor does a find-and-replace

  • Don't use a semicolon!

 #define directives are usually put close to the top of a file, for easy visibility

#define SIZE 100 int main() { int array[SIZE]; int i = 0; for( i = 0; i < SIZE; i++ ) array[i] = i*i; return 0; }

slide-20
SLIDE 20

 You can also make macros with #define that take arguments  You need to be careful with parentheses  Constants and macros are usually written in ALL CAPS to avoid

confusion

#include <math.h> #define TO_DEGREES(x) ((x) * 57.29578) #define ADD(a,b) ((a) + (b)) int main() { double theta = TO_DEGREES(2*M_PI); int value = ADD(5 * 2, 7); return 0; }

slide-21
SLIDE 21

 You can use directives #if, #ifdef, #ifndef, #else,

#elif and #endif

 These are mostly used to avoid infinite include problems  Sometimes they will change what gets compiled based on

compiler version, system libraries, or other stuff

#ifndef SOMETHING_H #define SOMETHING_H int something(int a, int b); #endif

slide-22
SLIDE 22
slide-23
SLIDE 23

 Lab 2 is tomorrow  sizeof and const  System limits  Bitwise operations

slide-24
SLIDE 24

 Keep reading K&R chapter 2  Read LPI chapter 11