Introduction To Computing Lab (CPEN-1214) : Department of Mechanical Engineering

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 32

Department of Mechanical

Engineering

Introduction to computing Lab

(CPEN-1214)

Lab Manual

Submitted To Engr. Muhammad Usman Mushtaq


Submitted By
Regd. No.
Semester
Session
List of Experiments

Lab Session # 01. Introduction to computer & Programming Languages.


Lab Session # 02. Introduction to C++: Basic I.
Lab Session # 03. Basic Arthimetic Statements and Expressions
Lab Session # 04. Conditional Structures Program

Introduction to computing Lab


Lab Session # 01

Introduction to computer & Programming Languages.


Objective:
1. To learn about computer Hardware and Software.
2. To learn about types of software.
3. To understand programs and programming languages

1. Introduction to Computer:
 Software

 Known as Programs or app.


 Tells hardware how to perform a task.
 Operating system + CPU
 Hardware

 Physical element of computer/machinery.


 Example: the monitor, mouse, CPU.
 Some internal one surrounded by tower

2. Types of Software
 System software:

 Helps run computer hardware and computer system itself.


 Includes Operating system, device driver, diagnostic tools etc.
 Always pre-installed on computer
 Application software:

 Allows to accomplish one or more tasks.


 e.g. Word processing, web browsing

3. Introduction to program and programming languages


1.1. Computer Program
 Collection of task for performing a specific task that is designed to solve sp.
problem.
 Instructions of program are designed to be executed by a computer.

Introduction to computing Lab


 It is required that computer is able to execute programs in order for it to
function.

4. Computer Programming
 Process of writing or editing source code.
 Person called computer programmer/software developer/coder.
 Lengthy process of computer programming is referred to as software
development.

5. Programming languages
 Languages use to write, and code computer programs.
 It is vocabulary & set of grammatical rules for instructing a computer.
 Examples: BASIC,C,C++, COBOL,java,Fortran, ada& Pascal
 We are going to learn C++ programming language in this lab

6. Dev C++
 C++ is high level programming language, while computer only understands
machine language.
 To successfully run and execute computer program we need a compiler to convert
source code to object code so that computer can understand it.
 We are using Dev C++ compiler for this purpose in lab

7. Practice exercise
 Install Dev C++ in your PC’s.
 Create and save a new file in Dev C++. As introduction to computing Lab.

Introduction to computing Lab


Figure 1

Introduction to computing Lab


Laboratory Rubrics
Sr. Performance Exemplary Satisfactory Developing Unsatisfactory
# Indicator (5 Marks) (4-3 Marks) (2-1 Marks) (0 Marks)
Respectful to one
Unsupportive
Individual work another while
Delayed feedback to Need training and attitude with no
providing timely
achieve a task in a support to work interest towards
1 (PLO-09, and helpful
less efficient way. effectively individually. individual or
Affective Domain) individual feedback.
common goal.

Scale
Unable to use
Quite able to utilize
modern tools on
Skill to perform test modern tools to Able to use modern
Able to use modern his own and lab
programming perform program tools with some help
tools with a lot of help instructor
2 (PLO-05, with negligible help from the lab
from the lab instructor. provides help in
Psychomotor Domain) from the lab instructor.
almost every step
instructor.
of the experiment.
Scale
Table 1: Viva Voce
Sr.# Performance Exemplary Satisfactory Developing Unsatisfactory
Indicator (5 Marks) (4-3 Marks) (2-1 Marks) (0 Marks)

Depth of relevant Demonstration of full Clearly no knowledge


At ease with content
Information knowledge of the Only basic concepts of subject matter, no
and able to elaborate
1 (PLO-04, subject with are demonstrated questions are answered
and explain to some
Cognitive explanations and and interpreted. and no interpretation
degree.
Domain) elaboration. made.

Scale

Table 2: Lab Performance

Lab Session # 02
Introduction to C++: Basic I
Objective:

Introduction to computing Lab


1. To understand the basic structure of C++ program.
8. To be able to code C++ program.

1. C++ Basic Structure of a program:


 It uses notation that may appear strange to non-programmers.
 Simple Program to illustrate several important features of C++ language.
 Task: Print out a line of text.
 E.g. Welcome to Introduction and computing Lab // TO give any comment

9. Program Structure:
#include<iostream> // allows program to output data to screen
using namespace std;
int main () // Function main begins program execution
{
cout<< "Welcome to Introduction and computing Lab”; // display message
return 0; // indicate that program ended successfully
} // ended function main

10. Output Window

Welcome to Introduction and computing Lab

---------------------------------------------------------------------------------------------------
Process exited after 18.97 seconds with return value 0
Press any key to continue .….
1.

11. # include preprocessor directive System software:


#include<iostream>
 Lines that begins with # are processed by preprocessor before the program is
complied

Introduction to computing Lab


 This line notify preprocessor to include in the program the content of the
input/output stream header
 Forgetting to include <iostream> header results in error.

12. The Main Function


 Part of every C++ program.
 Parenthesis after main indicate that main is a program building block called a

function.
 int Main()
 It indicate that main returns an integer(whole number) value.
 { leftbrace_function begins.
 } right brace_ function ends.

13. Output and input


 cout <<“Welcome to Introduction and computing Lab \n”;
 cout ,<< ,the string “Welcome to Introduction and computing Lab \n”
and semicolon(;) _ all are called statement.
 cin>>n;

14. Escape sequences


 \ (Backslash)_ escape character _ use to format output
 \n _new line
 \t _Horizontal tab
 \r_not to next line
 \a_Alert
 \\_ Backslach
 \’_Single quote

Introduction to computing Lab


 \’’_ Double qoute

15. Practice exercise


Task-1: Print out a line of text.
Welcome to Introduction to computing Lab

Program to print out a line of text “Welcome to Introduction to computing Lab”.


#include<iostream>
using namespace std;
int main ()
{
cout<< "Welcome to Introduction to computing Lab ";
return 0;
}
Output Window of Task-1

Welcome to Introduction to computing Lab


---------------------------------------------------------------------------------------------------
Process exited after 8.8967 seconds with return value 0
Press any key to continue.….
2.

Task-2: Built a program to add two numbers.


Program to add two numbers.
#include<iostream>
using namespace std;
int main ()
{
int x1,x2,add;
cout<< "first number = \n";
cin>>x1;
cout<< "2nd number = \n";
cin>>x2;

Introduction to computing Lab


add=x1+x2;
cout<<"answer is = "<<add;
return 0;
}

Output Window of Task-2

First number = 2
3. 2nd number = 2
4. Answer is = 4

---------------------------------------------------------------------------------------------------
Process exited after 12.87 seconds with return value 0
Press any key to continue .….

Task-3:Print out like below.

Welcome
To
Programming

Program to print out a text.


#include<iostream>
using namespace std;
int main ()
{

return 0;
}

Introduction to computing Lab


Output Window of Task-3

Welcome
To
Programming
---------------------------------------------------------------------------------------------------
Process exited after 12.87 seconds with return value 0
Press any key to continue .….
6.

Task-4: Print out like below.


Welcome
To
Programming
Program to print out a text.
#include<iostream>
using namespace std;
int main ()
{

return 0;
}
Welcome
Output Window of Task-4
To
Programming
---------------------------------------------------------------------------------------------------
Process exited
Introduction to after 12.87 Lab
computing seconds with return value 0
Press any key to continue .….
7.
Task-5: Print out like below.
Hello
My Name is ‘Hafiz Ali’
I am student of Mechanical Engineering
My roll number is ME 1810060
Program to print out a text.
#include<iostream>
using namespace std;
int main ()
{

return 0;
}

Output Window of Task-5


Hello
My Name is ‘Hafiz Ali’
I am student of Mechanical Engineering
My roll number is ME 1810060
---------------------------------------------------------------------------------------------------
Introduction to computing Lab
Process exited after 12.87 seconds with return value 0
Press any key to continue .….
\

Introduction to computing Lab


Laboratory Rubrics
Sr. Performance Exemplary Satisfactory Developing Unsatisfactory
# Indicator (5 Marks) (4-3 Marks) (2-1 Marks) (0 Marks)
Respectful to one
Unsupportive
Individual work another while
Delayed feedback to Need training and attitude with no
providing timely
achieve a task in a support to work interest towards
1 (PLO-09, and helpful
less efficient way. effectively individually. individual or
Affective Domain) individual feedback.
common goal.

Scale
Unable to use
Quite able to utilize
modern tools on
Skill to perform test modern tools to Able to use modern
Able to use modern his own and lab
programming perform program tools with some help
tools with a lot of help instructor
2 (PLO-05, with negligible help from the lab
from the lab instructor. provides help in
Psychomotor Domain) from the lab instructor.
almost every step
instructor.
of the experiment.
Scale
Table 1: Viva Voce
Sr.# Performance Exemplary Satisfactory Developing Unsatisfactory
Indicator (5 Marks) (4-3 Marks) (2-1 Marks) (0 Marks)

Depth of relevant Demonstration of full Clearly no knowledge


At ease with content
Information knowledge of the Only basic concepts of subject matter, no
and able to elaborate
1 (PLO-04, subject with are demonstrated questions are answered
and explain to some
Cognitive explanations and and interpreted. and no interpretation
degree.
Domain) elaboration. made.

Scale

Table 2: Lab Performance

Lab Session # 03
Basic Arthimethic Statements and Expression
Objective

Introduction to computing Lab


To learn about arthimetic statements and expression in C++
Be able to implement and execute any mathematical formula into C++ statement

C ++ Basic Sructure of a program


1.Arthemetic Expression in Sraight-Line Form
 Arithmetic Expression in C++ must be entered into the computer in straight-line
form E.g“a divided by b” must be written as a/b ,so that all constant ,variable and
operater appear in a straight

2.Parenthesis for grouing subexpressions


 Parenthesis are used in C++ expression in same manner as in algebraic expression
for example to multiply the quantity b+c by a we write a*(b+c)

Rules of Operator Precedence


C++ applies the operator in arithmetic expression in aprecise order determined by these rules
of operator precedence ,which are generally the same as in algebra
 Operator in expression contained within pairs of parenthesis are evaluated first
 Multiplication and division are applied next if an expression contains multiple
multiplication and division expressions than operator are applied form left to right
 Addition and subtraction operation are applied last if an expression contains
multiple addition and subtraction aperation in expressions thanoperator are applied
from left to right

Task 1 : Write C++ Program that will find the value of “a” by using following
equation where b=5 and c=6
d=b+c
e=d-b
f=e*d
g=f/c
a=(2*g)/5
Program task 1
#include <iostream>
Int main ()

Introduction to computing Lab


{ int a,b,c,d,e,f,g;
Cout <<”enter one number =”;
Cin>>b;
Cout<<”enter two number=”;
Cin>>c;
d=b+c;
e=d-b;
f=e*d;
g=f/c;
a=(2*g)/5;
cout<<”value of a=”<<a;
return 0;
}

‘Task 2: Write a C++ program that will enter width and length of a rectangle

from user and will calculate its area.

Program task 2
#include <iostream>

Introduction to computing Lab


Int main ()
{ int a,b;
Cout <<”enter the length and width of arectangle”;
Cin>>b;
Int area =a*b;
Cout<<”area of arectangle=<<area;
return 0;
}

Task 3 Write a C++ program that will enter value of “a” from user where a is
integer and find the value of b
b=a+2/a*(5+6)

program task 3

Introduction to computing Lab


#include <iostream>
Using namespace std;
int main ()
{ int a,b;
Cout<<”enter the value of a=”;
Cin >>a;
b=a+2/a*(5+6);
cout<<”value of b is=”<<b;
return 0;
}

Task 4: Write a C++ program that will asks user his year of birth and tell him
his approximate age.

Program task 4
#include<iostream>

Introduction to computing Lab


Using namespace
int main ()
{ int a;
Cout<<”write your year of birth=”;
Cin>>a;
Int age =2020-a;
Cout<<”your age is =”<<age;
return 0;
}

Task 5: Write a C++ program that will asks user about item for which he want
to calculate profit and profit percent also he ask user to about sale price and
cost price of that item.

Program task 5
#include <iostream>
Introduction to computing Lab
Using namespace std;
int main ()
{ int c,s,p;
Cout<<”cost price of item =”;
Cin>>c;
Cout<<”sales price of item =”;
Cin<<s;
P=s-c;
int mul = p*100;
int div = mul /c;
cout<<”your profit is =”<<p<<”your profit percent is =”<<div;
return 0;
}

Task 6:Write a c++ program that will calculate aggregate of student for
admission enengineering according to UET LHR criteria ,70% goes from Fsc
(Total 1100) and 30% from ECAT (Total 400)

Program task 6
#include <iostream>

Introduction to computing Lab


Using namespace sta;
int main ()
{ int a, b ;
Cout <<”your Fsc marks=”;
Cin>>a;
Cout<<”your ecat marks=”;
Cin>>b;
int mul1=70*a;
intdiv=muli/1100;
int mul2=30*b;
int div2=mul2/400;
int agg =div1+div2 100;
cout<<”your agregatete is =”<<agg;
return 0 ;
}

Task 7: Write a C++ program that will enter value of a and b from user and find
value of (a-b)3
Where as (a-b)3=a3-3a2b+3ab2-b3

Program task 7
#include<iostream>

Introduction to computing Lab


Using namespace std;
int main ()
{ int a,b ;
Cout<<”give the value of a=”;
Cin>>a;
Cout<<”give the value of b=”;
Cin>>b;
int mul1=a*a*a;
int mul2=-b*b*b;
int mul3=-3*a*a*b;
int mul4=3*a*b*b;
int add =mul1+mul2+mul3+mul4;
cout<<”cube of (a-b) is =”<<add;
return 0 ;
}

Task 8: Write a c++ program that that input two numbers from user and will
perform summation ,subtraction , multiplication on numbers.

Program task 8
#include <iostream>
Using namespace std;

Introduction to computing Lab


Int main ()
{ int a, b ;
Cout <<”enter two number=”;
Cin>>a>>b;
Int sum =a+b;
Int sub =a-b;
Int mul =a*b;
Int div =a/b;
Cout<<end<<”sum of \n\t”<<a<<”and”<<b<<”is=”<<sum;
Cout<<endl<<”subtract of \n\t”<<a<<”and”<<b<<”is=”<<sub;
Cout<<endl<<”multiple of \n\t”<<a<<”and”<<b<<”is=”<<mul;
Cout<<endl<<”division of \n\t”<<a<<”and”<<b<<”is=”<<div;
return 0 ;
}

Introduction to computing Lab


Lab Session # 04
Conditional Structures Pogram
Objective
 To learn the if-else conditional structures
 To Solve the program using if-else structure in C++

Introduction to computing Lab


 To understand the implementing and executing of if-else structure

If-else statement
If statement can be followed by an optional,which excutes when the Boolean expression is
true

Syntax
If (Boolean-expression )
{} //statement will execute if Boolean expression is true
else
{} //statement will excute if Boolean expression is true
If (Boolean-expression)
{} //statement will excute if Boolean expression is true
else
{} // statement will execute if Boolean expression is true
If the Boolean expression evaluates to true ,then the if block of ode will be executed
,otherwise else block of code will be executed

Program
#include <iostream>
Using namespace std;
int main ()
{ int a;
Cout <<”enter number “;
Cin>>a;
If (a<20)
Cout<<”you are good”;
else
Cout<<”you are Bad”;
return 0;
}

If-else-If….else Statement
If statement can be followed by an optional else if…else statement ,which is very useful to
test various conditions using single if ..else if statement

Introduction to computing Lab


When using if,else if ,else statement there are few point to keep in mind.
1)An if can have zero or one else and itany else must come after if’s
2)An if can have a zero to many else if’s and they must come before the else
3)Once an else if succeeds ,none of the remaining else if’s or else’s will be tested.

Syntax
If (Boolean –expression 1 )
{} //executes when the Boolean expression 1 is true
else if (Boolean-expression)
{} //executes when the Boolean expression 2 is true
else if (Boolean-expression)
{} //executes when the Boolean expression 3 is true
else
{} //executes when the none of above condition is true

Program
#include <iostream>
Using namespace std;
Int main ()
{ int a;
Cout<<”enter number=”;
Cin>>a;
If (a==20)
Cout<<”you are good’;
else if (a==40)
Cout<<”you are very good”
else if (a==60)
Cout<<”you are Bad”;
else
Cout<<”you are very Bad”;
return 0;
}

Introduction to computing Lab


Task-1:Write a C++ program that will print “pass” if marks are greater than
40.

Program task 1
#include <iostream>
using namespace std;
int main ()
{ int a;
Cout<<”enter number”;
Cin>>a;
If (a>40)
Cout<<”pass”<<endl;
else
Cout<<”not pass”<<endl;
return 0;
}

Task-2: Write a C++ program that will “pass” if marks are greater than 40
otherwise print “fail”.

Program task 2
Introduction to computing Lab
#include<iostream>
using namespace std;
int main ()
{ int a;
Cout<<”enter number”;
Cin>>a;
If (a>40)
Cout<<”pass”<<endl;
else
Cout<<”fail”<<endl;
return 0;
}

Task-3:Write a C++ program that will display”Excellent”if marks are greater


than 80, it will display “good”if marks are greater than 60, otherwise it will
display “Bad”
Program task -3
#include<iostream>

Introduction to computing Lab


using namespace std;
int main ()
{ int a;
Cout<<”enter number=”;
Cin>>a;
if (a>80)
Cout<<:excellent”;
else if (a<60)
Cout<<”Bad”;
else
Cout<<”good”;
return 0;
}

Task-4 :Write a C++ program that will declare a number as positive ,negative
or zero.

Program task 4
#include <iostream>

Introduction to computing Lab


using namespace std;
int main ()
{ int a;
Cout <<”enter number=”;
Cin>>a;
if (a>o)
Cout<<”positive”;
else if (a<0)
Cout<<”negative”;
else
Cout<<”zero”
return 0:
}

Task-5: Write a C++ program that will check whethera numberis even or not.

Program task 5
#include <iostream>
using namespace std;

Introduction to computing Lab


Int main ()
{ int n,r;
Cout<<”enter the number:”;
Cin>>n;
r=n%2
if (r==0)
cout<<”is an even integer”<<n<<endl;
else
cout<<”is an odd integer”<<n<<endl;
return 0;
}

Task-6 : Write a c++program that will enter a number and check whether it is
divisible by 8 or not.

Introduction to computing Lab


Program task 6
#include <iostream>
using namespace std;
int main ()
{ int number, remainder;
Cout<<”enter the number:”;
Cin>>number;
remainder=number%8
if(remainder==0)
Cout<<number<<”yes divisible”<<endl;
else
Cout<<number<<”not divisible”<<endl;
return 0;
}

Introduction to computing Lab

You might also like