cs201- introduction to programming- lecture 04
DESCRIPTION
Virtual University Course CS201- Introduction to Programming Lecture No 04 Instructor's Name: Dr. Naveed A. Malik Course Email: [email protected]TRANSCRIPT
![Page 1: CS201- Introduction to Programming- Lecture 04](https://reader034.vdocuments.site/reader034/viewer/2022052602/559b73821a28ab6a4f8b457f/html5/thumbnails/1.jpg)
Introduction to ProgrammingIntroduction to Programming
Lecture 4Lecture 4
![Page 2: CS201- Introduction to Programming- Lecture 04](https://reader034.vdocuments.site/reader034/viewer/2022052602/559b73821a28ab6a4f8b457f/html5/thumbnails/2.jpg)
Key Words of CKey Words of C mainmain i fi f elseelse whilewhile do do forfor
![Page 3: CS201- Introduction to Programming- Lecture 04](https://reader034.vdocuments.site/reader034/viewer/2022052602/559b73821a28ab6a4f8b457f/html5/thumbnails/3.jpg)
x = 2 + 4 ;x = 2 + 4 ; = 6 ;= 6 ;
MemoryMemoryxx
66
![Page 4: CS201- Introduction to Programming- Lecture 04](https://reader034.vdocuments.site/reader034/viewer/2022052602/559b73821a28ab6a4f8b457f/html5/thumbnails/4.jpg)
MemoryMemoryx = a + b ;x = a + b ;
a b
x
![Page 5: CS201- Introduction to Programming- Lecture 04](https://reader034.vdocuments.site/reader034/viewer/2022052602/559b73821a28ab6a4f8b457f/html5/thumbnails/5.jpg)
#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](https://reader034.vdocuments.site/reader034/viewer/2022052602/559b73821a28ab6a4f8b457f/html5/thumbnails/6.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022052602/559b73821a28ab6a4f8b457f/html5/thumbnails/7.jpg)
a*b%c +d a*b%c +d
![Page 8: CS201- Introduction to Programming- Lecture 04](https://reader034.vdocuments.site/reader034/viewer/2022052602/559b73821a28ab6a4f8b457f/html5/thumbnails/8.jpg)
a*(b%c) = a*b%ca*(b%c) = a*b%c
?
![Page 9: CS201- Introduction to Programming- Lecture 04](https://reader034.vdocuments.site/reader034/viewer/2022052602/559b73821a28ab6a4f8b457f/html5/thumbnails/9.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022052602/559b73821a28ab6a4f8b457f/html5/thumbnails/10.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022052602/559b73821a28ab6a4f8b457f/html5/thumbnails/11.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022052602/559b73821a28ab6a4f8b457f/html5/thumbnails/12.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022052602/559b73821a28ab6a4f8b457f/html5/thumbnails/13.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022052602/559b73821a28ab6a4f8b457f/html5/thumbnails/14.jpg)
Special Character Special Character NewlineNewline
\n\n