Escolar Documentos
Profissional Documentos
Cultura Documentos
Lab Instructor Muhammad Ali Ramay B.E Mechatronics (DE-29) Email : aliramay16@gmail.com
Course Details
50%
15% 10%
20% 30%
Lab Reports
Your Code (with proper comments) Flow chart of your code Screen shots of your results
Assignment Tests
Please Note
All correspondence will be done via your official yahoo group DE 33 MTS. Please join it and check it regularly! Plagiarism is strictly not allowed and will be dealt with seriously!
Consequences
Your assignment will be cancelled. Both parties will get ZERO marks. Further disciplinary action might be taken if need be.
Programming Languages
Machine Language
Assembly Language
C++ Language
A very Strong Language Can do anything C++ is used to make from very simple programs to any type of complex programs including Games and even operating systems
Introduction
Writing a Computer Program is a multi step process consisting of four major steps
A Simple Program
Writing a Comment
Comments can be written by using // The compiler ignores everything written after // and no machine language object code is generated Commenting is a very good habbit it enables you to understand your program later Lines that begin with # are processed by the preprocessor before the program is compiled <iostream> input/output stream must be included in any program to output data on screen or to take input from keyboard
Preprocessor Directive
A Simple Program
Main Function
Every Program will start from main even if the program has alot of functions The keyword on the left of main (e.g int, void) shows what your main function will return we shall discuss this in detail when we'll get to functions Cout
A Simple Program
std::cout
It specifies we are using name in this case cout that belongs to namespace std The names cin (standard input stream) and cerr (standard error stream) also belong to namespace std The << specifies in which direction the data is going To stream output we use << and to take input we use >>
Std::cout<<
A Simple Program
Escape Characters
The backslash (\) is called an escape character. It indicates a special character is to be output
Escape Characters
Character
New line Horizontal Tab Vertical Tab Return Back slash Single Quote Double Quote Null Character
Escape Sequence
\n \t \v \r \\ \ \ \0
// Introduction // A first program in C++. #include <iostream> // function main begins program execution void main() { std::cout << "Welcome to C++!\n"; } // end function main
Welcome to C++!
// Printing a line with multiple statements. #include <iostream> // function main begins program execution void main() { std::cout << "Welcome "; std::cout << "to C++!\n"; } // end function main
// Printing multiple lines with a single statement #include <iostream> // function main begins program execution void main() { std::cout << "Welcome\n to\n \n C++! \n"; } // end function main
Variables
A variable is a location in the computer's memory where values can be stored for the use of the program Variables can be declared by writing int number; where number is the name of the variable Variable can be of many types e.g int, float, double, char etc Several variables of same type can be declared in one line seperated by commas e.g int number1, number2, sum;
Variables
int integer numbers char characters float used for decimal numbers double used fordecimal numbers
Int,double and char are often called as fundamental, primitive or builtin variable types Must declare before their use Variable names are case sensitive can contain all characters, digits and underscores but cannot begin with a digit
Variables
Always comment your variables for what you are using them
Std::cin>>variable_name;
Now you know what std:: means Cin is used to input an object using keyboard >> means data flow is from user to computer Cin waits for user to input data and waits until ENTER is pressed Value is converted to the variable type which is used on the right side and stored in it
Assignment operator =
= sign is used to assign a value to a variable e.g my_variable = 43; or my_variable = account_number;
Arithmetic operator +
+ sign is used to add to numbers or variables e.g sum = 28 + 32; or sum = number1 + number2;
// Addition program. #include <iostream> // function main begins program execution void main() { int integer1; // first number to be input by user int integer2; // second number to be input by user int sum; // variable in which sum will be stored std::cout << "Enter first integer\n"; std::cin >> integer1; // prompt // read an integer
std::cout << "Enter second integer\n"; // prompt std::cin >> integer2; // read an integer sum = integer1 + integer2; // assign result to sum
std::cout << "Sum is " << sum; // print sum } // end function main
Addition +
Add to numbers or variables e.g sum = number1 + number2; Subtract one number from another e.g sub = number1 number2; Multiply to numbers or variables e.g mul = number1 * number2;
Subtraction -
Multiplication *
Division /
e.g div = number1 / number2; Integer division truncates the remainder e.g -7 / 3 gives result 2
Modulus %
Operators in expressions contained within pairs of parentheses are evaluated first. Parentheses are said to be highest level of precedence
Incase of nested parentheses the expression in the inner most parentheses are applied first e.g in ((a + b) + c) the a + b will be done first
Multiplication, Division and modulus operations are applied next. If an expression contains several multiplications, divisions and modulus then they are applied from left to right and they are all of same precedence level
Addition and Subtraction are applied last. If an expression contains several additions and subtractions then they are applied from left to right and they are also of same precedence level.
Any Questions??
Lab Task 1
Write a Program that inputs three integers from the keyboard and prints sum, product, average, square and cube like this
Sum is __ Product is __ Average is __ square is __ cube is __
Lab Task 2
Write a program that outputs a box and a diamond made of * like this
Lab Task 3
Take a five digit nuber from user and seperate the five digits and print them in a vertical order (hint : use the division and modulus operators)
if a user gives this number 46274 then output should be 4 6 2 7 4
Lab task 4
takes x, y and z as an input from the user and outputs the value of equation