LabManual 1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 25

1

University of Management and Technology

Lahore Campus

========================================================

Session: Fall 2024

Instructor: Somia Waseem

Email: [email protected]

Course: Programming Fundamentals Lab

Course Code: CC111L

Department of Computer Science

School of Systems and Technology

Department of Computer Science (SST), UMT.


2

Table of Contents

1.Objectives .................................................................................................................... 3

2.Scope ............................................................................................................................. 3

3.Concepts ....................................................................................................................... 3

4.Lab Tasks ................................................................................................................... 21

5.Home Tasks .............................................................................................................. 24

Department of Computer Science (SST), UMT.


3

“Lab Manual 1”

Objectives:
1. What is an Algorithm?
2. What is a Flowchart?
3. Difference between Algorithm and Flowchart.
4. What is Pseudo Code?
5. Learn Basic Components of C++ Program.
6. Learn about Compilation Process.
7. Learn how to use Code::Blocks C++.
8. Learn how to Write, Edit and Run a C++ Program.

Scope:
The student should know the following at the end of this Lab:

1. Problem Solving.
2. How to Install, Run and Compile.
3. Basic Structure of a Program.
4. Writing Complete Programs.

Concepts:
1. Algorithm:

• An algorithm is a set of steps for accomplishing a task or solving a problem.


• Before solving a problem, one should know what to do, how to do it, and
what types of steps should be taken. So, an algorithm is a step-by-step
method for solving a problem.

Department of Computer Science (SST), UMT.


4

• Typically, algorithms are executed by computers, but we also rely on


algorithms in our daily lives. Each time we follow a particular step-by-step
process, like making coffee in the morning or tying our shoelaces, we are in
fact following an algorithm.
• An algorithm refers to a set of instructions that define the execution of work
to get the expected results.

To make the program work properly, we must have to properly design the
algorithm. Designing the algorithm helps to utilize computing resources
effectively. Generally, the algorithms are written in natural language or in plain
English language. We can represent algorithms via flowcharts, pseudo code,
and others.

Sometimes algorithms are difficult to understand, and it is also difficult to show


looping and branching using an algorithm. Apart from programming languages
or programs, the algorithm can be designed for any problem, as it is a step-by-
step solution of a program.

How do Algorithms Work?

Algorithms use a set of initial data or input, process it through a series of logical
steps or rules, and produce the output (i.e., the outcome, decision, or result).

Department of Computer Science (SST), UMT.


5

Example 1: Write an algorithm for making a Cup of Coffee.

• Step 1: Start.
• Step 2: Put the teabag in a cup.
• Step 3: Fill the kettle with water.
• Step 4: Boil the water in the kettle.
• Step 5: Pour some of the boiled water into the cup.
• Step 6: Add milk to the cup.
• Step 7: Stir the tea.
• Step 8: Drink the tea.
• Step 9: Stop.

Example 2: Write an algorithm to Add two Numbers.

• Step 1: Start.
• Step 2: Input Number1, Number2.
• Step 3: Sum = Number1 + Number2.
• Step 4: Display Sum.
• Step 5: Stop.

Department of Computer Science (SST), UMT.


6

Advantages Disadvantages

Algorithms are easy to write. Algorithms are difficult to debug.


There is a use of human-readable It is hard to show branches and loops
techniques to understand logic. using an algorithm.

For big problems, algorithms can be Jumping (or GOTO statements)


written with moderate steps. makes the algorithm hard to trace
the problems.

2. Flowchart:
• The Flowchart is the most widely used graphical representation of
algorithms and procedural design workflows.
• It uses various symbols to show the operations and decisions to be followed
in a program.
• It flows in sequential order. As an instance, a parallelogram in the flowchart
may be used to indicate input and output, a rectangular box indicates a
mathematical operation, a diamond symbol indicates the decision-making
statements, and several other symbols are used in flowcharts.
• A flowchart is an important document for a system and individual program
because it summarizes a program's function in the form of symbols that are
easy to understand and clearly explained in English.

Department of Computer Science (SST), UMT.


7

Example 1: Draw a Flowchart for making a Cup of Coffee.

Department of Computer Science (SST), UMT.


8

Example 2: Draw a flowchart to Add two Numbers.

Using Condition:

Department of Computer Science (SST), UMT.


9

Advantages Disadvantages

Proper debugging. Time-consuming.

Effective analysis. Complex.

Efficient coding. Difficult to modify.

Proper documentation. It has no standard.

Efficient program maintenance.

3. Difference between Algorithm and Flowchart:

Department of Computer Science (SST), UMT.


10

Question: Why do we use Algorithm and Flowchart?

Flowcharts and Algorithms are both required for every programming language.
It helps to solve complex problems as it breaks down into simpler steps. A
flowchart is the graphical representation of the steps of a program, while an
algorithm is a step-by-step method written in simple English.

Note:

It is important to keep in mind that an algorithm is not the same as


a program or code. It is the logic or plan for solving a problem represented as a
simple step-by-step description. Code is the implementation of the algorithm
in a specific programming language (like C++ or Python), while a Program is
an implementation of code that instructs a computer on how to execute an
algorithm and perform a task.

4. Pseudo Code:

• Pseudo code is a method of representing logic in programming that is


independent of any programming language. It uses natural language rather
than the rigid syntax of programming languages since it is intended to be
easily understood by humans. Common programming features
like loops, conditionals, and function calls are frequently used in pseudo-
code but in a condensed form, making the program logic more
understandable.
• A computer program, algorithm, or process that is intended to be simple to
comprehend by humans is described in pseudo code, which is plain English.
It is a high-level illustration of the program's logic and flow that can serve as
a guide for developing the real code. Pseudo code can be used to explain both

Department of Computer Science (SST), UMT.


11

complicated procedures, like sorting algorithms, and straightforward


duties, like input validation.
• Software engineers frequently use pseudo-code as a design technique since
it enables them to work out the specifics of a program's logic before writing
any actual code. Because errors are considerably simpler to rectify in
the pseudo-code than they are in the actual code, this can reduce errors and
save time.
• Although there isn't a dedicated programming language for pseudo code, C
is a good choice for implementation. C++ is a general-purpose, high-level
programming language that is frequently used to create system software.
It is a structured programming language, which implies that it controls the
flow of the program using a few predefined programming constructs.

Example 1: Write a Pseudo Code for making a Cup of Coffee.

Begin

Put the teabag in a cup.

Fill the kettle with water.

Boil the water in the kettle.

Pour some of the boiled water into the cup.

Add milk to the cup.

Stir the tea.

Drink the tea.

End

Department of Computer Science (SST), UMT.


12

Example 2: Write a Pseudo Code to Add Two Numbers.

Begin:

Input Number 1, Number 2.

Compute Sum = Number 1 + Number 2.

Print Sum.

End

5. C++ Program:
A typical structure of a C++ program consists of:
i. Pre-processor Directives that always begin with #.
(e.g. #include <iostream>)
• The two most frequent commands are #include and #define.
• #include command will include header files that have the definition
of a function used in the program like cout that is in iostream file.
• #define is used to replace a text with a value, e.g. #define PI
3.141593. It allows the programmer to give a name to a constant
value before the program is compiled.
• Note: #include <file> tells the compiler to look for file where system
include files are held.
ii. Functions
A function is a block of statements that make a specific kind of
processing. Every C++ program has a main() function. The execution
starts from main().

Department of Computer Science (SST), UMT.


13

iii. Variables / Identifiers


These are used to store data in a program. You must declare a variable
before you use it.

Here is a general structure of a C++ program

pre-processor directives #include <iostream>

main function heading using namespace std;


{ int main()
{
declarations executable cout << “hello World”;
statements return 0;
} }

6. Installing Code::Blocks on Windows:

Code::Blocks is free, cross-platform IDE.

Step 1: Go to https://www.codeblocks.org/

Department of Computer Science (SST), UMT.


14

Step 2: Go to Downloads.

Step 3: Look at the left sidebar. Click on Binaries.

Step 4: Click on Windows 2000/XP/Vista/7/8.

Department of Computer Science (SST), UMT.


15

Step 5: We want to install codeblocks-12.11mingw-setup.exe. Click on the


Sourceforge.net link to the right of it. This is the version with the GCC
compiler.

Step 6: On the Sourceforge page, the download file should* pop up after
severalseconds. Click save file.

*If the download file does not pop up after a few seconds, use the direct link
provided at the top of the page by the Code::Blocks logo.

Department of Computer Science (SST), UMT.


16

Step 7: Go to your downloads folder and double click on codeblocks-


12.11mingw-setup.exe. A warning will pop up, click yes.

Department of Computer Science (SST), UMT.


17

Step 8: The set-up wizard will appear. Click next.

Step 9: Click I agree.

Department of Computer Science (SST), UMT.


18

Step 10: Click next.

Step 11: Click Install. Code::Blocks will immediately start installing and
shouldtake less than 5 minutes.

Department of Computer Science (SST), UMT.


19

Step 12: If you want, start Code::Blocks now. If not, click no, then next.

Step 13: Click finish.

You’re done!

Department of Computer Science (SST), UMT.


20

1. Here is the video guide link (How to install CodeBlocks IDE on windows):
https://www.youtube.com/watch?v=zmT79Ljo0lI

2. Here is the video guide link (How to install Visual Studio Code IDE on
windows):
https://www.youtube.com/watch?v=CPmQwlycfGI

3. Here is the link of online GDB compiler for C++:


https://www.onlinegdb.com/online_c++_compiler

4. Here is the link of online Programiz compiler for C++:


https://www.programiz.com/cpp-programming/online-compiler/

Department of Computer Science (SST), UMT.


21

Lab Tasks:
Task 1: Compile, Run, and Test the program.

/* Sum of Two Numbers*/


#include <iostream>
using namespace std;
int main()
{
int first_number = 2,
second_number = 8, sum;
// Display Numbers.
cout << "First Number = ";
cout << first_number <<
endl;
cout << "Second Number =
";
cout << second_number <<
endl;
// Sum of two numbers.
sum = first_number +
second_number;
// Print Sum
cout << "Sum = " <<
first_number << " + " <<
second_number << " = " <<
sum << endl;
return 0;
}

Task 2: Type and save the program with the Name LabQ1.cpp.

Task 3: Write a program in C++ to print Welcome Text (“Welcome to PF


Lab.”) on a separate line.

Department of Computer Science (SST), UMT.


22

Task 4: Write a C++ program to display the following output:

Task 5: Write a C++ program to display the following output:

Task 6: Write a C++ program to display the following output:

Task 7: Write a C++ program to display the following output:

Department of Computer Science (SST), UMT.


23

Task 8: Write a C++ program to display the following output:

Task 9: Write a C++ program to display the following output:

Department of Computer Science (SST), UMT.


24

Task 10: You have to write a C++ program that will print your complete bio
data on the screen.

Output:

Name:

University ID:

Father Name:

Phone Number:

Address:

Semester:

Course:

Home Tasks:
Task 1: Launch Code::Blocks C++ in your computer and run all of the
above programs.

Task 2: Write a C++ program to Print Multiplication Table of a Number 2.

Task 3: Write a C++ program to display the following output:

Department of Computer Science (SST), UMT.


25

Task 4: Write a C++ program to display the following output:

Task 5: Write a C++ program to display the following output:

Task 6: Write a pseudo code and draw a flowchart to check whether the
number is even or odd.

Department of Computer Science (SST), UMT.

You might also like