Lab Manual_PPS
Lab Manual_PPS
B.E. Semester 1
Computer Engineering
Certificate
Place: __________
Date: __________
By using this lab manual students can go through the relevant theory and procedure in advance
before the actual performance which creates an interest and students can have basic idea prior
to performance. This in turn enhances pre-determined outcomes amongst students. Each
experiment in this manual begins with competency, industry relevant skills, course outcomes
as well as practical outcomes (objectives). The students will also achieve safety and necessary
precautions to be taken while performing practical.
This manual also provides guidelines to faculty members to facilitate student centric lab
activities through each experiment by arranging and managing necessary resources in order
that the students follow the procedures with required safety and necessary precautions to
achieve the outcomes. It also gives an idea that how students will be assessed by providing
rubrics.
Utmost care has been taken while preparing this lab manual however always there is chances
of improvement. Therefore, we welcome constructive suggestions for improvement and
removal of errors if any.
i) * ii) * iii) * * *
** * * **
*** * * * *
Write a C Program using concept of function.
(a) Write a program that defines a function to
7 add first n numbers.
(b) Write a program using function to find
maximum number from two numbers.
Write a C Program using concept of function.
(a) Write a program to exchange two numbers
8 by using function
(b) Write a program to reverse the number
using function.
Write a C Program using the concept of an array.
(a) Write a C program to find out the Maximum
and Minimum number from given 10
9
numbers in an array.
(b) Write a program to sort the elements of an
array in ascending order.
Write a C Program using the concept of pointer,
structure & File.
(a) Write a program to access elements using
pointer.
(b) Design a structure student_record to contain
name, branch and total marks obtained.
Develop a program to read data for 10
10
students in a class and print them.
(c) A file named data contains series of integer
numbers. Write a c program to read all
numbers from file and then write all odd
numbers into file named “odd” and write all
even numbers into file named “even”.
Display all the contents of these file on screen
Student Name [Enrollment Number] Page 8 of 70
DTEs’ Vision:
Vision:
✔ To provide globally competitive technical education
✔ Develop student friendly resources with a special focus on girls’ education and support
to weaker sections
Vision:
✔ To contribute for sustainable development of nation through achieving excellence in
technical education and research while facilitating transformation of students into
responsible citizens and competent professionals.
Mission:
✔ To impart affordable and quality education in order to meet the needs of industries and
achieve excellence in teaching-learning process.
✔ To create a conducive research ambience that drives innovation and nurtures research-
oriented scholars and outstanding professionals.
✔ To collaborate with other academic & research institutes as well as industries in order to
strengthen education and multidisciplinary research.
Vision:
✔ To achieve academic excellence in Computer Engineering by providing value based
education.
Mission:
✔ To produce graduates according to the needs of industry, government, society and
scientific community.
5. Modern tool usage: Create, select, and apply appropriate techniques,resources, and modern
engineering and IT tools including prediction and modeling to complex engineering activities with
an understanding of the limitations.
6. The engineer and society: Apply reasoning informed by the contextualknowledge to assess
societal, health, safety, legal and cultural issues and the consequent responsibilities relevant to the
professional engineering practice.
8. Ethics: Apply ethical principles and commit to professional ethics andresponsibilities and
norms of the engineering practice.
11. Project management and finance: Demonstrate knowledge and understandingof the
engineering and management principles and apply these to one’s own work, as a member and
leader in a team, to manage projects and in multidisciplinary environments.
12. Life-long learning: Recognize the need for, and have the preparation and abilityto engage
in independent and life-long learning in the broadest context of technological change.
• Graduates will be able to explore and propose effective solutions to the problems in the
area of Computer Engineering as per the needs of society and industry.
• Graduates will be able to apply standard practice and strategies to develop quality software
products using modern techniques, programming skills, tools & an open ended
programming environment and work in a team.
• Graduates will manifest the skills of continuous learning in the fast changing field of
Computer Engineering.
Provide computing solutions of complex problems as per business and societal needs.
Procure requisite skills to pursue entrepreneurship, research and development, and imbibe
high degree of professionalism in the fields of computing.
Embrace life-long learning and remain continuously employable.
Work and excel in a highly competence supportive, multicultural and professional
environment which abiding to the legal and ethical responsibilities.
Example of Algorithm:
Student Name [Enrollment Number] Page 12 of 70
Algorithm for finding the average of three numbers is as follows −
Step 1: Start
Step 2: Read 3 numbers a, b, c
Terminal Symbol: In the flowchart, it is represented with the help of a circle for denoting the start
and stop symbol. The symbol given below is used to represent the terminal symbol.
Input/output Symbol: The input symbol is used to represent the input data, and the output symbol
is used to display the output operation.
Processing Symbol: It is represented in a flowchart with the help of a rectangle box used to
represent the arithmetic and data movement instructions. The symbol given below is used to
represent the processing symbol.
Connector Symbol: The connector symbol is used if flows discontinued at some point and
continued again at another place. The following symbol is the representation of the connector
symbol.
Flow lines: It represents the exact sequence in which instructions are executed. Arrows are used
to represent the flow lines in a flowchart. The symbol given below is used for representing the
flow lines:
Hexagon symbol (Flat): It is used to create a preparation box containing the loop setting statement.
The symbol given below is used for representing the Hexagon symbol.
Advantages of Flowchart in C:
Following are the various advantages of flowchart:
Communication: A flowchart is a better way of communicating the logic of a program.
Synthesis: Flowchart is used as working models in designing new programs and software systems.
Efficient Coding: Flowcharts act as a guide for a programmer in writing the actual code in a high-
level language.
Proper Debugging: Flowcharts help in the debugging process.
Effective Analysis: Effective analysis of logical programs can be easily done with the help of a
related flowchart.
Proper Documentation: Flowchart provides better and proper documentation. It consists of various
activities such as collecting, organizing, storing, and maintaining all related program records.
Testing: A flowchart helps in the testing process
Examples of flowchart
Draw a flowchart for finding average of 3 Numbers.
Design a flowchart to find out greater number from the two numbers.
the desktop icon: by double clicking it. The below window will be visible soon.
Output:
IF-ELSE Statement:
The if-else is statement is an extended version of If. The general form of if-else is as follows:
if (test-expression)
{
True block of statements
}
Else
{
False block of statements
}
Statements;
Student Name [Enrollment Number] Page 34 of 70
In this type of a construct, if the value of test-expression is true, then the true block of statements
will be executed. If the value of test-expression if false, then the false block of statements will be
executed. In any case, after the execution, the control will be automatically transferred to the
statements appearing outside the block of If.
Following programs illustrate the use of the if-else construct:
We will initialize a variable with some value and write a program to determine if the value is less
than ten or greater than ten.
#include<stdio.h>
int main()
{
int num=19;
if(num<10)
{
printf("The value is less than 10");
}
else
{
printf("The value is greater than 10");
}
return 0;
}
Output:
The value is greater than 10
Description of above program:
1. We have initialized a variable with value 19. We have to find out whether the number is bigger
or smaller than 10 using a ‘C’ program. To do this, we have used the if-else construct.
2. Here we have provided a condition num<10 because we have to compare our value with 10.
3. As you can see the first block is always a true block which means, if the value of test-expression
is true then the first block which is If, will be executed.
The general syntax of how else-if ladders are constructed in ‘C’ programming is as follows:
if (test - expression 1)
{
statement1;
}
else if (test - expression 2)
{
Statement2;
}
else if (test - expression 3)
{
Statement3;
}
else if (test - expression n)
{
Statement n;
}
else
{
default;
}
Statement x;
This type of structure is known as the else-if ladder. This chain generally looks like a ladder hence
it is also called as an else-if ladder. The test-expressions are evaluated from top to bottom.
Whenever a true test-expression if found, statement associated with it is executed. When all the n
test-expressions becomes false, then the default else statement is executed.
The switch statement allows us to execute one code block among many alternatives.
You can do the same thing with the if...else..if ladder. However, the syntax of the switch statement
is much easier to read and write.
switch (expression)
case constant1:
// statements
break;
case constant2:
// statements
break;
default:
// default statements
The expression is evaluated once and compared with the values of each case label.
If there is a match, the corresponding statements after the matching label are executed. For
example, if the value of the expression is equal to constant2, statements after case constant2: are
executed until break is encountered.
If there is no match, the default statements are executed.
Loops in programming are used to repeat a block of code until the specified condition is met. A
loop statement allows programmers to execute a statement or group of statements multiple times
without repetition of code.
Exit Controlled loops: In Exit controlled loops the test condition is evaluated at the end of the loop
body. The loop body will execute at least once, irrespective of whether the condition is true or
false. do-while Loop is Exit Controlled loop.
do-while loop in C
The do-while loop continues until a given condition satisfies. It is also called post tested loop. It is
used when it is necessary to execute the loop at least once (mostly menu driven programs).
do {
//code to be executed
} while (condition);
while loop in C
The while loop in c is to be used in the scenario where we don't know the number of iterations in
advance. The block of statements is executed in the while loop until the condition specified in the
while loop is satisfied. It is also called a pre-tested loop.
while(condition){
//code to be executed
The for loop in C language is used to iterate the statements or a part of the program several times.
It is frequently used to traverse the data structures like the array and linked list.
Syntax:
//
//
In C, we can divide a large program into the basic building blocks known as function. The function
contains the set of programming statements enclosed by {}. A function can be called multiple
times to provide reusability and modularity to the C program. In other words, we can say that the
collection of functions creates a program. The function is also known as procedureor subroutinein
other programming languages.
Advantage of functions in C
● By using functions, we can avoid rewriting same logic/code again and again in a program.
● We can call C functions any number of times in a program and from any place in a program.
● We can track a large C program easily when it is divided into multiple functions.
● Reusability is the main achievement of C functions.
● However, Function calling is always a overhead in a C program.
Function Aspects
Function declaration A function must be declared globally in a c program to tell the compiler
about the function name, function parameters, and return type.
Function call Function can be called from anywhere in the program. The parameter list must not
differ in function calling and function declaration. We must pass the same number of functions as
it is declared in the function declaration.
Function definition It contains the actual statements which are to be executed. It is the most
important aspect to which the control comes when the function is called. Here, we must notice that
only one value can be returned from the function.
Types of Functions
Library Functions: are the functions which are declared in the C header files such as scanf(),
printf(), gets(), puts(), ceil(), floor() etc.
Return Value
A C function may or may not return a value from the function. If you don't have to return any value
from the function, use void for the return type.
Let's see a simple example of C function that doesn't return any value from the function.
void hello(){
printf("hello c");
If you want to return any value from the function, you need to use any data type such as int, long,
char, etc. The return type depends on the value to be returned from the function.
Let's see a simple example of C function that returns int value from the function.
int get(){
return 10;
In the above example, we have to return 10 as a value, so the return type is int. If you want to
return floating-point value (e.g., 10.2, 3.1, 54.5, etc), you need to use float as the return type of the
method.
A function may or may not accept any argument. It may or may not return any value. Based on
these facts, There are four different aspects of function calls.
There are two methods to pass the data into the function in C language, i.e., call by value and call
by reference.
Call by value in C
● In call by value method, the value of the actual parameters is copied into the formal
parameters. In other words, we can say that the value of the variable is used in the function
call in the call by value method.
● In call by value method, we can not modify the value of the actual parameter by the formal
parameter.
● In call by value, different memory is allocated for actual and formal parameters since the
value of the actual parameter is copied into the formal parameter.
● The actual parameter is the argument which is used in the function call whereas formal
parameter is the argument which is used in the function definition.
Call by reference in C
Arrays are used to store multiple values in a single variable, instead of declaring separate variables
for each value. To create an array, define the data type (like int) and specify the name of the array
followed by square brackets []. To insert values to it, use a comma-separated list, inside curly
braces:
To access an array element, refer to its index number. Array indexes start with 0: [0] is the first
element. [1] is the second element, etc.
This statement accesses the value of the first element [0] in myNumbers:
Example
printf("%d", myNumbers[0]);
// Outputs 25
Advantage of C Array
2) Ease of traversing: By using the for loop, we can retrieve the elements of an array easily.
3) Ease of sorting: To sort the elements of the array, we need a few lines of code only.
4) Random Access: We can access any element randomly using the array.
1) Fixed Size: Whatever size, we define at the time of declaration of the array, we can't exceed
the limit. So, it doesn't grow the size dynamically like LinkedList which we will learn later.
The two-dimensional array can be defined as an array of arrays. The 2D array is organized as
matrices which can be represented as the collection of rows and columns. However, 2D arrays are
created to implement a relational database lookalike data structure. It provides ease of holding the
bulk of data at once which can be passed to any number of functions wherever required.
data_type array_name[rows][columns];
C Pointers
The pointer in C language is a variable which stores the address of another variable. This variable
can be of type int, char, array, function, or any other pointer. The size of the pointer depends on
the architecture. However, in 32-bit architecture the size of a pointer is 2 byte. Consider the
following example to define a pointer which stores the address of an integer.
int n = 10;
int* p = &n;
Declaring a pointer
The pointer in c language can be declared using * (asterisk symbol). It is also known as indirection
pointer used to dereference a pointer.
Pointer to array
int arr[10];
int *p[10]=&arr;
Pointer to a function
Pointer to structure
struct st {
int i;
float f;
}ref;
struct st *p = &ref;
Advantage of pointer
1) Pointer reduces the code and improves the performance, it is used to retrieving strings, trees,
etc. and used with arrays, structures, and functions.
3) It makes you able to access any memory location in the computer's memory.
Usage of pointer
In c language, we can dynamically allocate memory using malloc() and calloc() functions where
the pointer is used.
Pointers in c language are widely used in arrays, functions, and structures. It reduces the code and
improves the performance.
The address of operator '&' returns the address of a variable. But, we need to use %u to display the
address of a variable.
In C, there are cases where we need to store multiple attributes of an entity. It is not necessary that
an entity has all the information of one type only. It can have different attributes of different data
Construct individual arrays for storing names, roll numbers, and marks.
Use a special data structure to store the collection of different data types.
What is Structure
Structure in c is a user-defined data type that enables us to store the collection of different data
types. Each element of a structure is called a member. Structures ca; simulate the use of classes
and templates as it can store various information The ,struct keyword is used to define the structure.
Let's see the syntax to define the structure in c.
struct structure_name
data_type member1;
data_type member2;
data_type memeberN;
};
struct employee
{ int id;
char name[20];
float salary;
};
We can declare a variable for the structure so that we can access the member of the structure easily.
There are two ways to declare structure variable:
1st way:
Let's see the example to declare the structure variable by struct keyword. It should be declared
within the main function.
struct employee
{ int id;
char name[50];
float salary;
};
The variables e1 and e2 can be used to access the values stored in the structure. Here, e1 and e2
can be treated in the same way as the objects in C++ and Java.
2nd way:
Let's see another way to declare variable at the time of defining the structure.
struct employee
int id;
char name[50];
float salary;
}e1,e2;
Let's see the code to access the id member of p1 variable by. (member) operator.
Student Name [Enrollment Number] Page 61 of 70
p1.id
C Structure example
#include<stdio.h>
#include <string.h>
struct employee
{ int id;
char name[50];
int main( )
e1.id=101;
return 0;
File Handling in C
In programming, we may require some specific input data to be generated several numbers of
times. Sometimes, it is not enough to only display the data on the console. The data to be displayed
may be very large, and only a limited amount of data can be displayed on the console, and since
the memory is volatile, it is impossible to recover the programmatically generated data again and
again. However, if we need to do so, we may store it onto the local file system which is volatile
and can be accessed every time. Here, comes the need of file handling in C.
There are many functions in the C library to open, read, write, search and close the file. A list of
file functions are given below:
We must open a file before it can be read, write, or update. The fopen() function is used to open a
file. The syntax of the fopen() is given below.
The file name (string). If the file is stored at some specific location, then we must mention the path
at which the file is stored. For example, a file name can be like "c://some_folder/some_file.ext".
The mode in which the file is to be opened. It is a string. We can use one of the following modes
in the fopen() function.
Mode Description
AIM: Write a C Program using the concept of pointer, structure & File.
(a) Write a program to access elements using pointer.
(b) Design a structure student_record to contain name, branch and total
marks obtained. Develop a program to read data for 10 students in a class and
print them.
(c) A file named data contains series of integer numbers. Write a c program
to read all numbers from file and then write all odd numbers into file named
“odd” and write all even numbers into file named “even”. Display all the
contents of these file on screen