Você está na página 1de 35

Algorithms & Computing Lab

Lab Instructor Muhammad Ali Ramay B.E Mechatronics (DE-29) Email : aliramay16@gmail.com

Course Details

Graded Lab Tasks


50%
15% 10%

Lab Reports Lab tasks

Assignment Tests 25%

Mid Term Final

20% 30%

Lab Reports

Lab Reports have to be submitted of each lab it should contain


Your Code (with proper comments) Flow chart of your code Screen shots of your results

Assignment Tests

Assignment test will be conducted in every second lab


It will be of 20 mins duration Results will be shown within a week

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!

What constitutes PLAGIARISM??


Plagiarism is when you
Copy code (from a fellow or the internet) Share code (working together) Borrow code (even just for an idea) Take or give help in coding (from your fellows, the internet or anyone else)

Consequences

Your assignment will be cancelled. Both parties will get ZERO marks. Further disciplinary action might be taken if need be.

What does not constitute PLAGIARISM??


Discussions
You may discuss the strategies of solving a problem without sharing any piece of code.

Programming Languages
Machine Language

Assembly Language

High Level Languages

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

Program Concept Algorithm Development Program Coding Debugging

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

How to take output on screen

Standart Output stream

A Simple Program

Standard Output Stream Object

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

Program to add to numbers

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;

Program to add numbers

Variables

Common variable types are


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

Program to add numbers

Variables

ALWAYS give meaningful names to your variables


Int x,y,z; (not a good practice) Int account_number; (correct)

Always comment your variables for what you are using them

Program to add numbers

Standard Input Stream Object

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

Program to add numbers

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

Enter first integer 45 Enter second integer 72 Sum is 117

Other Arithmetic Operators

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 *

Other Arithmetic Operators

Division /

e.g div = number1 / number2; Integer division truncates the remainder e.g -7 / 3 gives result 2

Modulus %

Modulus operator returns the remainder e.g 7 % 3 returns 1

Rules of operator precedence

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

Rules of operator precedence

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.

e.g in expression a + b + c a + b is done first then c is added to their result

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

Write a program to solve the following equation


4x 32y 2 z9 2 2x 8

takes x, y and z as an input from the user and outputs the value of equation

Você também pode gostar