+ All Categories
Home > Education > CS201- Introduction to Programming- Lecture 04

CS201- Introduction to Programming- Lecture 04

Date post: 07-Jul-2015
Category:
Upload: bilal-ahmed
View: 38 times
Download: 1 times
Share this document with a friend
Description:
Virtual University Course CS201- Introduction to Programming Lecture No 04 Instructor's Name: Dr. Naveed A. Malik Course Email: [email protected]
Popular Tags:
14
Introduction to Programming Introduction to Programming Lecture 4 Lecture 4
Transcript
Page 1: CS201- Introduction to Programming- Lecture 04

Introduction to ProgrammingIntroduction to Programming

Lecture 4Lecture 4

Page 2: CS201- Introduction to Programming- Lecture 04

Key Words of CKey Words of C mainmain i fi f elseelse whilewhile do do forfor

Page 3: CS201- Introduction to Programming- Lecture 04

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

MemoryMemoryxx

66

Page 4: CS201- Introduction to Programming- Lecture 04

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

a b

x

Page 5: CS201- Introduction to Programming- Lecture 04

#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 6: CS201- Introduction to Programming- Lecture 04

Quadratic EquationQuadratic Equation In algebraIn algebra

y = axy = ax22 + bx + c + bx + c

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

Page 7: CS201- Introduction to Programming- Lecture 04

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

Page 8: CS201- Introduction to Programming- Lecture 04

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

?

Page 9: CS201- Introduction to Programming- Lecture 04

DiscriminantDiscriminantb2 - 2a

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

Solution

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

4c

Page 10: CS201- Introduction to Programming- Lecture 04

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

Integer division truncates Integer division truncates fractional partfractional part

Liberal use of Liberal use of brackets/parenthesisbrackets/parenthesis

Page 11: CS201- Introduction to Programming- Lecture 04

Interesting ProblemInteresting Problem

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

Page 12: CS201- Introduction to Programming- Lecture 04

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 13: CS201- Introduction to Programming- Lecture 04

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’; // first digit; and then << ‘\n’cout <<“The digit is: “ << digit << ‘\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;

}}

Page 14: CS201- Introduction to Programming- Lecture 04

Special Character Special Character NewlineNewline

\n\n


Recommended