cgs 3460 servers n rain.cise.ufl.edu n sand.cise.ufl.edu n shine.cise.ufl.edu n thunder.cise.ufl.edu...

Post on 13-Dec-2015

295 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

     

CGS 3460

ServersServers rain.cise.ufl.edu sand.cise.ufl.edu shine.cise.ufl.edu thunder.cise.ufl.edu storm.cise.ufl.edu

     

CGS 3460

Variables ContinuedVariables Continued

Getting Input&

Operators

     

CGS 3460

Making calculationsMaking calculations We know the different data types

int float double char

How do we transform data into results

     

CGS 3460

Casting - ImplicitCasting - Implicit int x = 7.9; int x = -3.9; float y = 7; float y = -65; char c = 123; int x = ‘7’

x = 7x = -3y = 7.0y = -65.0c = ‘{‘x = 55

ASCII TABLE

     

CGS 3460

Casting - ExplicitCasting - Explicit int x = (int)7.9; int x = (int)-3.9; float y = (float)7; float y = (float)-65; char c = (char)123; int x = (int)‘7’

x = 7x = -3y = 7.0y = -65.0c = ‘{‘x = 55

ASCII TABLE

     

CGS 3460

Operations for int typeOperations for int type Declaration

int x, y, z;

Assignment y = 10; z = 6;

Calculation Plus: +

• x = y + z; Minus: -

• x = y – z; Multiply: *

• x = y * z; Divide: /

• x = y / z; Modulus

• x = y % z;

result of y/z will be truncated

     

CGS 3460

Integer math - ExamplesInteger math - Examples 7 + 4 6 * 8 4 – 12 10 / 3 3 / 8 14 / 7 24 / 5 24 % 6 22 % 7 23 % 8 4 % 5 -7 % 5

1148-830240174-2

     

CGS 3460

Integer math – More ExamplesInteger math – More Examples 20 % 5 20 / 5 * 5 24 % 5 24 / 5 * 5 1204309383 / 10 1204309383 % 10

0204

20 120430938

3

     

CGS 3460

float: single-precision Variablesfloat: single-precision Variables For values containing decimal

3., 125.8, -0.1 Scientific notation

2.25e-3 = 2.25 * 10-3 = 0.00225• Use e or E for exponent

no commas

     

CGS 3460

float Variables - IIfloat Variables - II Ranges

IEEE floating-point standard• e = 8, f = 23

• ±3.4×1038

     

CGS 3460

Operations for float typeOperations for float type Declaration

float x, y, z;

Assignment y = 10.00; z = 5.8;

Calculation Plus: +

• x = y + z; Minus: -

• x = y – z; Multiply: *

• x = y * z; Divide: /

• x = y / z; result of y/z will NOT be truncated

     

CGS 3460

Floating point math - ExamplesFloating point math - Examples 7.2 + 4.3 6.5 * 8.0 4.2 – 12.3 10.0 / 3.0 4.0 / 8.0 24.0 / 5.0

11.552.0-8.1

3.3330.54.8

     

CGS 3460

Mixed arithmetic - ExamplesMixed arithmetic - Examples 7.2 + 4 6.5 * 8 4 – 12.3 10 / 3.0 4.0 / 8 24 / 5.0

11.252.0-8.3

3.3330.54.8

     

CGS 3460

Example – IExample – I#include <stdio.h>int main(){ int a, b, c; float f;

a = 10; b = 20; c = a/b; printf(“%i / %i = %i\n”, a, b, c); f = a/b; printf(“%i / %i = %f\n”, a, b, f);}

10 / 20 = 010 / 20 = 0.000000

     

CGS 3460

Example – IIExample – II#include <stdio.h>int main(){ int a; float f, g, h;

f = 10.0; g = 20.0; a = f/g; printf(“%f / %f = %i\n”, f, g, a); h = f/g; printf(“%f / %f = %f\n”, f, g, h);}

10.000000 / 20.000000 = 010.000000 / 20.000000 = 0.500000

     

CGS 3460

Assignment OperatorsAssignment Operators Join the arithmetic operators

Format: op=

Examples:

count = count + 10; count += 10;

count = count - 5; count -= 5;

a /= b + c; a = a / (b + c);

     

CGS 3460

Unary OperatorsUnary Operators Unary plus / minus

+ / - Example: -a

Unary increment/decrement ++ / --

M = M + 1; M += 1;

++M;

M++;

     

CGS 3460

Example – IIIExample – III#include <stdio.h>int main(){ int m = 0; //m is 0 at this point printf(“m post increment: %i\n”, m++); //now m is 1 printf(“m pre increment : %i\n”, ++m); //now m is 2

}

m post increment: 0m pre increment : 2

     

CGS 3460

Arithmetic OperatorsArithmetic Operators add: +, minus: -, multiply: *, divide: /, modulus: % Parentheses (grouping): ( ) Unary plus / minus

+ -

Unary increment/decrement ++ --

     

CGS 3460

c =( * b )

Operator PrecedenceOperator Precedence Precedence

Operators with higher precedence are evaluated first Operators with same precedence are evaluated from left to right In decreasing precedence

• ( )• unary increment (++), unary decrement (--)• unary plus (+), unary minus (-)• multiply (*), divide(/), modulus(%)• add(+), minus(-)• assignment (=)

Order for c = -a * b a + b * c / d

(-a)

(b * c)( / d )(a + )

     

CGS 3460

Operator Return Types (z = x ? y)Operator Return Types (z = x ? y)

x y z

int int int

float float float

int float float

float int float

     

CGS 3460

How do we get dataHow do we get data We know how to turn the data into “useful information” How do we get the data from the user?

     

CGS 3460

Getting InputGetting Input Need input from user

scanf• Same format as printf, except put “&” in front of variable names• scanf(“%i”, &count);• “&” means the "address of“

• to store whatever the user enters into the memory address where number is stored

• Leaving out the & will cause your program to work incorrectly!

• Exception: double uses %lf in scanf and %f in printf

     

CGS 3460

Example-IIIExample-III#include <stdio.h>

int main(){ int x, y;

printf("What is the value for x? \n"); scanf("%i", &x); //read the input

//calculate (x-1)^2 + 10 y = (x-1)*(x-1) + 10;

//print the output printf("The result is: %i\n", y); return 0;}

What is the value for x?7The result is: 46

     

CGS 3460

Example-IVHalf your age plus 7

Example-IVHalf your age plus 7

#include <stdio.h>

int main(){ float age;

printf(“How old are you?\n"); scanf("%f", &age);

printf(“You can date someone %f years old or older.\n", 0.5 * age + 7); return 0;}

How old are you?26You can date someone 20.000000 years old or older.

top related