+ All Categories
Home > Documents > "Give a person a fish and you feed them for a day; teach that person to use the Internet and they...

"Give a person a fish and you feed them for a day; teach that person to use the Internet and they...

Date post: 14-Jan-2016
Category:
Upload: blaise-manning
View: 213 times
Download: 0 times
Share this document with a friend
32
"Give a person a fish and you "Give a person a fish and you feed them for a day; feed them for a day; teach that person to use the teach that person to use the Internet and they won't bother Internet and they won't bother you for weeks.“ you for weeks.“ --unknown --unknown "Treat your password like your "Treat your password like your toothbrush. toothbrush. Don't let anybody else use it, and get a Don't let anybody else use it, and get a new one every six months." new one every six months." Clifford Stoll Clifford Stoll Monday 03, 2008
Transcript
Page 1: "Give a person a fish and you feed them for a day; teach that person to use the Internet and they won't bother you for weeks.“ --unknown "Treat your password.

"Give a person a fish and you feed "Give a person a fish and you feed them for a day; them for a day;

teach that person to use the teach that person to use the Internet and they won't bother you Internet and they won't bother you for weeks.“for weeks.“

--unknown--unknown

"Treat your password like your "Treat your password like your toothbrush. toothbrush.

Don't let anybody else use it, and Don't let anybody else use it, and get a new one every six months." get a new one every six months." — — Clifford StollClifford Stoll

Monday 03, 2008

Page 2: "Give a person a fish and you feed them for a day; teach that person to use the Internet and they won't bother you for weeks.“ --unknown "Treat your password.

Introduction to C Introduction to C ProgrammingProgramming

Lecture 3Lecture 3

Page 3: "Give a person a fish and you feed them for a day; teach that person to use the Internet and they won't bother you for weeks.“ --unknown "Treat your password.

Today’s Lecture Today’s Lecture IncludeInclude Data TypesData Types Arithmetic OperationsArithmetic Operations TokensTokens Numeric ConstantsNumeric Constants PunctuationsPunctuations OperatorsOperators Keywords of CKeywords of C

Page 4: "Give a person a fish and you feed them for a day; teach that person to use the Internet and they won't bother you for weeks.“ --unknown "Treat your password.

Data TypesData Types1.1. intint

2.2. shortshort

3.3. longlong

4.4. floatfloat

5.5. doubledouble

6.6. charchar

Page 5: "Give a person a fish and you feed them for a day; teach that person to use the Internet and they won't bother you for weeks.“ --unknown "Treat your password.

Arithmetic Arithmetic operatorsoperators

PlusPlus++

MinusMinus --

MultiplyMultiply **

DivideDivide / /

ModulusModulus %%

Page 6: "Give a person a fish and you feed them for a day; teach that person to use the Internet and they won't bother you for weeks.“ --unknown "Treat your password.

Arithmetic Arithmetic operatorsoperators

i + ji + j

x * yx * y

a / ba / b

a % ba % b

Page 7: "Give a person a fish and you feed them for a day; teach that person to use the Internet and they won't bother you for weeks.“ --unknown "Treat your password.

% = Remainder% = Remainder

5 % 2 = 15 % 2 = 1

2 % 2 = 02 % 2 = 0

Page 8: "Give a person a fish and you feed them for a day; teach that person to use the Internet and they won't bother you for weeks.“ --unknown "Treat your password.

4 / 2 = 24 / 2 = 2

5 / 2 = ?5 / 2 = ?

Page 9: "Give a person a fish and you feed them for a day; teach that person to use the Internet and they won't bother you for weeks.“ --unknown "Treat your password.

PrecedencePrecedence

Highest:Highest: ( )( ) Next:Next: * , / , * , / ,

%% Lowest:Lowest: + , -+ , -

Page 10: "Give a person a fish and you feed them for a day; teach that person to use the Internet and they won't bother you for weeks.“ --unknown "Treat your password.

Another C++ ProgramAnother C++ Program/*****************************************

** File: proj1.c

** Author: Joe Student

** Date: 9/15/01

** SSN: 123-45-6789

** Section: 0304

** E-mail: [email protected]

**

** This program prompts the user for two integer values then displays

** their product.

**

***********************************************/

Page 11: "Give a person a fish and you feed them for a day; teach that person to use the Internet and they won't bother you for weeks.“ --unknown "Treat your password.

Another C Program (con’t)Another C Program (con’t)#include <stdio.h>

#include <iostream.h>

main( )

{ int value1, value2, product ;

cout<<“Enter two integer values: ;

cin>>value1

cin>>value2; //cin>>value1>>value2

product = value1 * value2 ;

cout<<“Product = ”<<product ;

return 0 ; }

Page 12: "Give a person a fish and you feed them for a day; teach that person to use the Internet and they won't bother you for weeks.“ --unknown "Treat your password.

TokensTokens The smallest element in the C The smallest element in the C

language is the token.language is the token. It may be a single character or a It may be a single character or a

sequence of characters to form a sequence of characters to form a single item.single item.

Page 13: "Give a person a fish and you feed them for a day; teach that person to use the Internet and they won't bother you for weeks.“ --unknown "Treat your password.

Tokens are:Tokens are:

Tokens can be:Tokens can be:– Numeric constantsNumeric constants– Character constantsCharacter constants– String constantsString constants– KeywordsKeywords– Names (identifiers)Names (identifiers)– PunctuationPunctuation– OperatorsOperators

Page 14: "Give a person a fish and you feed them for a day; teach that person to use the Internet and they won't bother you for weeks.“ --unknown "Treat your password.

Numeric ConstantsNumeric Constants

Numeric constants are an Numeric constants are an uninterrupted sequence of digits uninterrupted sequence of digits (and may contain a period). They (and may contain a period). They never contain a comma.never contain a comma.

Examples:Examples:– 123123– 98.698.6– 10000001000000

Page 15: "Give a person a fish and you feed them for a day; teach that person to use the Internet and they won't bother you for weeks.“ --unknown "Treat your password.

Character ConstantsCharacter Constants

Singular!Singular! One character defined character set.One character defined character set. Surrounded on the single quotation Surrounded on the single quotation

mark.mark. Examples:Examples:

– ‘‘A’A’– ‘‘a’a’– ‘‘$’$’– ‘‘4’4’

Page 16: "Give a person a fish and you feed them for a day; teach that person to use the Internet and they won't bother you for weeks.“ --unknown "Treat your password.

String ConstantsString Constants

A sequence characters surrounded by A sequence characters surrounded by double quotation marks.double quotation marks.

Considered a single item.Considered a single item. Examples:Examples:

– ““UMBC”UMBC”– ““I like ice cream.”I like ice cream.”– ““123”123”– ““CAR”CAR”– ““car”car”

Page 17: "Give a person a fish and you feed them for a day; teach that person to use the Internet and they won't bother you for weeks.“ --unknown "Treat your password.

KeywordsKeywords

Sometimes called reserved words.Sometimes called reserved words. Are defined as a part of the C Are defined as a part of the C

language.language. Can not be used for anything else!Can not be used for anything else! Examples:Examples:

– intint– whilewhile– forfor

Page 18: "Give a person a fish and you feed them for a day; teach that person to use the Internet and they won't bother you for weeks.“ --unknown "Treat your password.

NamesNames

Sometimes called identifiers.Sometimes called identifiers. Can be of anything length, but on the Can be of anything length, but on the

first 31 are significant (too long is as bad first 31 are significant (too long is as bad as too short).as too short).

Are case sensitive:Are case sensitive:– abc is different from ABCabc is different from ABC

Must begin with a letter and the rest can Must begin with a letter and the rest can be letters, digits, and underscores.be letters, digits, and underscores.

Must follow the standards for this course!Must follow the standards for this course!

Page 19: "Give a person a fish and you feed them for a day; teach that person to use the Internet and they won't bother you for weeks.“ --unknown "Treat your password.

PunctuationPunctuation

Semicolons, colons, commas, Semicolons, colons, commas, apostrophes, quotation marks, apostrophes, quotation marks, braces, brackets, and braces, brackets, and parentheses.parentheses.

; : , ‘ “ [ ] { } ( ) ; : , ‘ “ [ ] { } ( )

Page 20: "Give a person a fish and you feed them for a day; teach that person to use the Internet and they won't bother you for weeks.“ --unknown "Treat your password.

OperatorsOperators There are operators for:There are operators for:

– assignmentsassignments– mathematical operationsmathematical operations– relational operationsrelational operations– Boolean operationsBoolean operations– bitwise operationsbitwise operations– shifting valuesshifting values– calling functionscalling functions– subscriptingsubscripting– obtaining the size of an objectobtaining the size of an object– obtaining the address of an objectobtaining the address of an object– referencing an object through its addressreferencing an object through its address– choosing between alternate subexpressionschoosing between alternate subexpressions

Page 21: "Give a person a fish and you feed them for a day; teach that person to use the Internet and they won't bother you for weeks.“ --unknown "Treat your password.

Key Words of CKey Words of C mainmain ifif elseelse whilewhile do do forfor

Page 22: "Give a person a fish and you feed them for a day; teach that person to use the Internet and they won't bother you for weeks.“ --unknown "Treat your password.

x = 2 + 4 ;x = 2 + 4 ; = 6 ;= 6 ;

MemoryMemoryxx

66

Page 23: "Give a person a fish and you feed them for a day; teach that person to use the Internet and they won't bother you for weeks.“ --unknown "Treat your password.

MemoryMemory

x = a + b ;x = a + b ;a b

x

Page 24: "Give a person a fish and you feed them for a day; teach that person to use the Internet and they won't bother you for weeks.“ --unknown "Treat your password.

#include <iostream.h>#include <iostream.h>main ( )main ( ){{

int age1, age2, age3, age4, age5, age6, age7, age8, age9, age10 ;int age1, age2, age3, age4, age5, age6, age7, age8, age9, age10 ;int TotalAge ;int TotalAge ;

int AverageAge ; int AverageAge ;

cout << “ Please enter the age of student 1: “ ;cout << “ Please enter the age of student 1: “ ;cin >> age1 ;cin >> age1 ;

cout << “ Please enter the age of student 2: “ ;cout << “ Please enter the age of student 2: “ ;cin >> age2 ;cin >> age2 ;::::TotalAge = age1+ age2 + age3+ age4+ age5+age6+ age7+ age8+age9 + TotalAge = age1+ age2 + age3+ age4+ age5+age6+ age7+ age8+age9 + age10 ;age10 ;AverageAge = TotalAge / 10 ; AverageAge = TotalAge / 10 ;

cout<< “The average age of the class is :” << AverageAge ;cout<< “The average age of the class is :” << AverageAge ;}}

Page 25: "Give a person a fish and you feed them for a day; teach that person to use the Internet and they won't bother you for weeks.“ --unknown "Treat your password.

Quadratic Quadratic EquationEquation

In algebraIn algebray = axy = ax22 + bx + c + bx + c

In C In C

y = ay = a**xx**x + bx + b**x + cx + c

Page 26: "Give a person a fish and you feed them for a day; teach that person to use the Internet and they won't bother you for weeks.“ --unknown "Treat your password.

a*b%c +d a*b%c +d

Page 27: "Give a person a fish and you feed them for a day; teach that person to use the Internet and they won't bother you for weeks.“ --unknown "Treat your password.

a*(b%c) = a*b%ca*(b%c) = a*b%c

?

Page 28: "Give a person a fish and you feed them for a day; teach that person to use the Internet and they won't bother you for weeks.“ --unknown "Treat your password.

DiscriminantDiscriminantb2 - 2a

= b*b - 4*a*c /2 *a Incorrect answer

Solution

= (b*b - 4*a*c) /(2 *a) Correct answer

4c

Page 29: "Give a person a fish and you feed them for a day; teach that person to use the Internet and they won't bother you for weeks.“ --unknown "Treat your password.

No expression on the left No expression on the left hand side of the assignmenthand side of the assignment

Integer division truncates Integer division truncates fractional partfractional part

Liberal use of Liberal use of brackets/parenthesisbrackets/parenthesis

Page 30: "Give a person a fish and you feed them for a day; teach that person to use the Internet and they won't bother you for weeks.“ --unknown "Treat your password.

Interesting Interesting ProblemProblem

Given a four-digit Given a four-digit integer, separate and integer, separate and print the digits on the print the digits on the screenscreen

Page 31: "Give a person a fish and you feed them for a day; teach that person to use the Internet and they won't bother you for weeks.“ --unknown "Treat your password.

AnalysisAnalysis Number = 1234Number = 1234

Take the remainder of the above number after dividing by 10Take the remainder of the above number after dividing by 10Eg 1234 / 10 gives remainder 4Eg 1234 / 10 gives remainder 41234 % 10 = 41234 % 10 = 4

Remove last digitRemove last digit– 1234/10 = 123.41234/10 = 123.4– 123123 (Truncation due to Integer Division)(Truncation due to Integer Division)

123 %10 gives 3123 %10 gives 3 Remove last digitRemove last digit

– 123/10 = 12.3123/10 = 12.3– 1212 (Truncation due to Integer Division) (Truncation due to Integer Division)

12 % 10 gives remainder 212 % 10 gives remainder 2 Remove last digitRemove last digit

– 12/10 = 1.212/10 = 1.2– 11 (Truncation due to Integer Division) (Truncation due to Integer Division)

Final digit remainsFinal digit remains

Page 32: "Give a person a fish and you feed them for a day; teach that person to use the Internet and they won't bother you for weeks.“ --unknown "Treat your password.

CodeCode#include <iostream.h>#include <iostream.h>main ( )main ( ){{

int number;int number;int digit;int digit;cout << “Please enter a 4 digit integer : ”;cout << “Please enter a 4 digit integer : ”;cin >> number;cin >> number;digit = number %10;digit = number %10;cout <<“The digit is: “ << digit << ‘\n’; cout <<“The digit is: “ << digit << ‘\n’;

// first digit; and then << ‘\n’// first digit; and then << ‘\n’number = number / 10;number = number / 10;digit = number % 10;digit = number % 10;cout <<“The digit is: “ << digit << ‘\n’;cout <<“The digit is: “ << digit << ‘\n’;number = number / 10;number = number / 10;digit = number % 10;digit = number % 10;cout <<“The digit is: “ << digit << ‘\n’;cout <<“The digit is: “ << digit << ‘\n’;number = number / 10;number = number / 10;digit = number % 10;digit = number % 10;cout <<“The digit is: “ << digit; cout <<“The digit is: “ << digit;

}}


Recommended