tuesday, december 12, 2006 “press any key to continue or any other key to quit.” - anonymous

34
Tuesday, December 12, 2006 “Press any key to continue or any other key to quit.” - Anonymous

Post on 19-Dec-2015

217 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Tuesday, December 12, 2006 “Press any key to continue or any other key to quit.” - Anonymous

Tuesday, December 12, 2006

“Press any key to continue or any other key to quit.”

- Anonymous

Page 2: Tuesday, December 12, 2006 “Press any key to continue or any other key to quit.” - Anonymous

What is wrong here?

int myvar.d;

int data-1;

int %value;

Page 3: Tuesday, December 12, 2006 “Press any key to continue or any other key to quit.” - Anonymous

What is wrong here?

int a=3;

b=a;

Page 4: Tuesday, December 12, 2006 “Press any key to continue or any other key to quit.” - Anonymous

What is wrong here?

int a,b;

a++;

b++;

Page 5: Tuesday, December 12, 2006 “Press any key to continue or any other key to quit.” - Anonymous

What is wrong here?

cin>>a+b;

cin>>10;

Page 6: Tuesday, December 12, 2006 “Press any key to continue or any other key to quit.” - Anonymous

What is wrong here?

int a=2; b=3;

Page 7: Tuesday, December 12, 2006 “Press any key to continue or any other key to quit.” - Anonymous

What is wrong here?

int a=2, b=3;

a+b=5;

Page 8: Tuesday, December 12, 2006 “Press any key to continue or any other key to quit.” - Anonymous

What is wrong here?

int a=2, b=3, c=6, d;

d = b–4ac;

Page 9: Tuesday, December 12, 2006 “Press any key to continue or any other key to quit.” - Anonymous

What is wrong here?

int a=2, b=3;

cout<<a<<" "<<b<<endl;

double a;

cout<<a<<" "<<b<<endl;

Page 10: Tuesday, December 12, 2006 “Press any key to continue or any other key to quit.” - Anonymous

What is wrong here?

char a=h;

Page 11: Tuesday, December 12, 2006 “Press any key to continue or any other key to quit.” - Anonymous

Indentation and colors

Page 12: Tuesday, December 12, 2006 “Press any key to continue or any other key to quit.” - Anonymous

Initialize with constant or expression

int a, b=3;

a = b*53+7;

Page 13: Tuesday, December 12, 2006 “Press any key to continue or any other key to quit.” - Anonymous

int a=70, b=80;

How to print 70 80?

Page 14: Tuesday, December 12, 2006 “Press any key to continue or any other key to quit.” - Anonymous

double amount;

cout<<“Enter the balance in dollars:$”;

cin>>amount;

Page 15: Tuesday, December 12, 2006 “Press any key to continue or any other key to quit.” - Anonymous

int x=5, y=5;

y=++x;

cout<<x<<" "<<y<<endl;

y=x--;

cout<<x<<" "<<y<<endl;

Page 16: Tuesday, December 12, 2006 “Press any key to continue or any other key to quit.” - Anonymous

int x=5, y=5;

y=++x;

cout<<x<<" "<<y<<endl; // prints 6 6

y=x--;

cout<<x<<" "<<y<<endl; // prints 5 6

Page 17: Tuesday, December 12, 2006 “Press any key to continue or any other key to quit.” - Anonymous

int x=5, y=5;

cout << x++ << endl;

cout << ++y << endl;

cout << x << ‘,’ << y;

Page 18: Tuesday, December 12, 2006 “Press any key to continue or any other key to quit.” - Anonymous

int x=5, y=5;

cout << x++ << endl; //prints 5

cout << ++y << endl; //prints 6

cout << x << ‘,’ << y; //prints 6,6

Page 19: Tuesday, December 12, 2006 “Press any key to continue or any other key to quit.” - Anonymous

The following code causes conversions (in this example, integral promotions):

long lnum1=2345, lnum2=3456;int inum=45;

// inum promoted to type long prior to assignment.

lnum1 = inum;

// inum promoted to type long prior to multiplication.

lnum2 = inum * lnum2;

Page 20: Tuesday, December 12, 2006 “Press any key to continue or any other key to quit.” - Anonymous

float fVal=3.0, fVal2=4.5;double dVal=9.768;int iVal=6;

fVal2 = iVal * fVal; // iVal converted to float; // result of multiplication is float.

dVal = iVal + dVal; // iVal converted to double;// result of addition is double.

Page 21: Tuesday, December 12, 2006 “Press any key to continue or any other key to quit.” - Anonymous

What is wrong here?

double total_price ;

int feet;

cin>>feet;

//There are 5280 feet in a mile

total_price = 5000*(feet/5280);

//suppose user enters 15000 feet

Page 22: Tuesday, December 12, 2006 “Press any key to continue or any other key to quit.” - Anonymous

int y;

char c='3';

y = 5 + c;

cout<<y<<endl;

output?

Page 23: Tuesday, December 12, 2006 “Press any key to continue or any other key to quit.” - Anonymous

Character Data Each character corresponds to a binary code Most commonly use binary codes are ASCII (American

Standard Code for Information Interchange)Character ASCII Code Integer Equivalent

% 0100101 37

3 0110011 51

A 1000001 65

a 1100001 97

b 1100010 98

c 1100011 99

Page 24: Tuesday, December 12, 2006 “Press any key to continue or any other key to quit.” - Anonymous

int y;

char c='3';

y = 5 + c;

cout<<y<<endl;

output 56

Character ASCII Code Integer Equivalent

3 0110011 51

Page 25: Tuesday, December 12, 2006 “Press any key to continue or any other key to quit.” - Anonymous

char ck='3';

int i=ck;

cout <<i<<endl;

Page 26: Tuesday, December 12, 2006 “Press any key to continue or any other key to quit.” - Anonymous

Casts can be usedcout<<(float)5/3<<endl;

cout<<(float)3/6<<endl;

Casts are considered operators – same precedence as unary operators

Page 27: Tuesday, December 12, 2006 “Press any key to continue or any other key to quit.” - Anonymous

char ck='3';

cout <<(int) ck<<endl;

________________________________

char ck='3';

int i=ck;

cout <<i<<endl;

Page 28: Tuesday, December 12, 2006 “Press any key to continue or any other key to quit.” - Anonymous

Be careful with casts!

e.g. Don’t cast a double into float

Page 29: Tuesday, December 12, 2006 “Press any key to continue or any other key to quit.” - Anonymous

//Compiler will give warnings. Try this out!#include <iostream.h>int main(){

int i, j;

i=23.867;

j=9876543219876;

cout<<i<<" "<<j<<endl;

return 0;}

Page 30: Tuesday, December 12, 2006 “Press any key to continue or any other key to quit.” - Anonymous

//Compiler will give warnings Try this out!#include <iostream.h>int main(){

int i, j, k;i=23.867;j=9876543219876;cout<<i<<" "<<j<<endl;

return 0;}

23 -1881560924

Page 31: Tuesday, December 12, 2006 “Press any key to continue or any other key to quit.” - Anonymous

cout<<"This is called \"fund raising\"";

cout<<"\"\\";

cout<<"one"<<"\t"<<"two";

Page 32: Tuesday, December 12, 2006 “Press any key to continue or any other key to quit.” - Anonymous

Concept of true and falsetrue is any value other than zerofalse is zero

Page 33: Tuesday, December 12, 2006 “Press any key to continue or any other key to quit.” - Anonymous

bool b=true;cout<<b<<endl;b=false;cout<<b<<endl;//zero is false and non-zero is trueb=35;cout<<b<<endl;b=-45;cout<<b<<endl;b=0;cout<<b<<endl;

Page 34: Tuesday, December 12, 2006 “Press any key to continue or any other key to quit.” - Anonymous

bool b=true;cout<<b<<endl; //prints 1b=false;cout<<b<<endl; //prints 0//zero is false and non-zero is trueb=35;cout<<b<<endl; //prints 1b=-45;cout<<b<<endl; //prints 1b=0;cout<<b<<endl; //prints 0