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

Post on 14-Jan-2016

213 views 0 download

transcript

"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

Introduction to C Introduction to C ProgrammingProgramming

Lecture 3Lecture 3

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

Data TypesData Types1.1. intint

2.2. shortshort

3.3. longlong

4.4. floatfloat

5.5. doubledouble

6.6. charchar

Arithmetic Arithmetic operatorsoperators

PlusPlus++

MinusMinus --

MultiplyMultiply **

DivideDivide / /

ModulusModulus %%

Arithmetic Arithmetic operatorsoperators

i + ji + j

x * yx * y

a / ba / b

a % ba % b

% = Remainder% = Remainder

5 % 2 = 15 % 2 = 1

2 % 2 = 02 % 2 = 0

4 / 2 = 24 / 2 = 2

5 / 2 = ?5 / 2 = ?

PrecedencePrecedence

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

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

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

** File: proj1.c

** Author: Joe Student

** Date: 9/15/01

** SSN: 123-45-6789

** Section: 0304

** E-mail: jstudent22@umbc.edu.pk

**

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

** their product.

**

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

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 ; }

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.

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

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

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’

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”

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

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!

PunctuationPunctuation

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

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

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

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

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

MemoryMemoryxx

66

MemoryMemory

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

x

#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 ;}}

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

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

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

?

DiscriminantDiscriminantb2 - 2a

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

Solution

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

4c

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

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

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

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;

}}