Lab Experiments - Spring 2022
Lab Experiments - Spring 2022
Undo Ctrl + /
Pressed a prefix key (e.g C-x) or Hold Ctrl and then press g
invoked a command (e.g. Find repeatedly
File)
1. Eliminate all the syntax errors in the code to get the output as shown below. Note: The name
of the code file is e1t1.c
Output
Page 1 of 37
Prepared By: Rehan Naeem and Dr. Suleman Sami Qazi
2. Complete the following code to get the output as shown below. Don’t add extra printf
statements.
Output
Output
Page 2 of 37
Prepared By: Rehan Naeem and Dr. Suleman Sami Qazi
4. Use multiple printf statements to get the output as shown below. Pay special attention to
the alignment and position of each text and each hourglass at the output. The integers
“1234….0123” will help you to align and position the texts and the hourglasses properly.
Also pay attention to the number of rows and the number of asterisks (*) in each row of
each hourglass.
Output
Page 3 of 37
Prepared By: Rehan Naeem and Dr. Suleman Sami Qazi
Experiment # 2:
Implementation of Variables and Arithmetic
Operators in C
Things to be covered in this experiment
● Variables
● scanf() statement
● Arithmetic operations
● math.h to use pow() and trigonometric functions
● comments
Output
2. Write a program that takes as inputs three different integers a, b and c and then displays their
sum, the average, and the product.
e.g., 13, 27 and 14. Now this input could be hard coded or user-generated via the keyboard (choice
is yours). Show the output as below:
Output
Page 4 of 37
Prepared By: Rehan Naeem and Dr. Suleman Sami Qazi
3. Write a program to evaluate the following expression when a=10, b=5, c=15, d=20 and 𝜭 = 45°. The
program should work correctly for any values of the variables. Don't forget to include the header
<math.h> to use trigonometric and pow() functions. Use 22/7 as value of π.The output should be
as shown below.
Expression
Output
4. Write a program to calculate and display the value of the following expression.
𝑣 = 𝐴 * 𝑠𝑖𝑛( 2π𝑓𝑡 + ϕ)
Output
Page 5 of 37
Prepared By: Rehan Naeem and Dr. Suleman Sami Qazi
5. Write a program that requests the download speed in megabits per second (Mbs) and the size of a
file in megabytes (MB). The program should calculate the download time for the file. Note that in
this context one byte is eight bits. Use type float, and use / for division. The program should
report all three values (download speed, file size, and download time) as in the following:
Output
Page 6 of 37
Prepared By: Rehan Naeem and Dr. Suleman Sami Qazi
Experiment # 3:
Implementation of Conditionals in C
1. Part a. Complete the following code and remove all the errors to get the output as shown below.
The program displays a message based on the age entered by the user.
Output 1
Output 2
Part b. Now add a few lines of code to the code you have written which first asks the user whether he
wants to proceed or not. If yes, then the program should do what it did in part a. If no, then the program
should display a message “End of the program”. If neither yes nor no then the program should display
“Invalid Input”. Write and submit only one code for this question.
Output 1
Output 2
Page 7 of 37
Prepared By: Rehan Naeem and Dr. Suleman Sami Qazi
2. Write a program which displays the message “Let’s visit Murree.” if the temperature of
Lahore is greater than or equal to 40℃ and the temperature of Murree is less than or equal
to 20℃. If the temperatures are not within the ranges then the message “Murree tour is not
essential.” Use only the IF - ELSE IF statements in this task.
Output 1
Output 2
3. Write a program which takes three integers from the user and displays the maximum
integer of the three as shown below.
Output
4. Write a program which takes 3 integers -- x, bound1, and bound2, where bound1 is not
necessarily less than bound2. If x is between the two bounds, just display it unmodified.
Otherwise, if x is less than the lower bound, display the lower bound, or if x is greater
than the upper bound, display the upper bound.
Output
Page 8 of 37
Prepared By: Rehan Naeem and Dr. Suleman Sami Qazi
5. In the program shown below, what should be written in place of ? so that the truth table for
AND operation is fully implemented. You are only allowed to write some code in place of ? in
the code below and nothing else. The output is as shown below
Output
6. Write a program which takes a floating number from the user. The program should display
the message “The value of x is 0.1” if the user enters 0.1 otherwise the program should do
nothing.
Output
Page 9 of 37
Prepared By: Rehan Naeem and Dr. Suleman Sami Qazi
Experiment # 4:
Implementation of Loops in C
1. Complete the following program to get the output as shown below. Use the concept of field
width to introduce spaces in the output.
Output
Page 10 of 37
Prepared By: Rehan Naeem and Dr. Suleman Sami Qazi
2. Convert the following flow chart into C code to get the output as shown below.
Page 11 of 37
Prepared By: Rehan Naeem and Dr. Suleman Sami Qazi
3. Write a program to calculate the GPA of a student in a semester. The formula, grade points
table and the output is shown below. Use the concept of precision to skip digits after the
decimal point of a floating number.
Formula
Output
Page 12 of 37
Prepared By: Rehan Naeem and Dr. Suleman Sami Qazi
4. Print the following SQUARE using the flowchart given below .
Output
Page 13 of 37
Prepared By: Rehan Naeem and Dr. Suleman Sami Qazi
5. Print the following using nested for-loops.
Output
6. Write a program to print a hourglass of 5 rows as shown below. Divide this problem into two
smaller problems i.e. the first small problem prints line 1 to 3 of the hourglass whereas the
second small problem prints line 4 to 5. Follow the think-code-test-debug procedure to get
this task done. Draw a flowchart before writing the code.
Output
Page 14 of 37
Prepared By: Rehan Naeem and Dr. Suleman Sami Qazi
Experiment # 4: Loops (continued)
1. Determine the output of the following codes without using a computer.
a)
b)
Page 15 of 37
Prepared By: Rehan Naeem and Dr. Suleman Sami Qazi
2. Write a C program for any login application. Convert the given flowchart of login algorithm
into C language.
Page 16 of 37
Prepared By: Rehan Naeem and Dr. Suleman Sami Qazi
3(a) Complete the following code to get the output as shown below. The code computes the power
of an integer.
OUTPUT
(b) Rewrite the above code to get the output as shown above using a FOR-loop.
Page 17 of 37
Prepared By: Rehan Naeem and Dr. Suleman Sami Qazi
4 (a). Write a program to print an “inverted” pyramid of 3 rows as shown below. Use nested
FOR-loops to complete this task.
(b) Write a program to print a pyramid of 3 rows as shown below. Use nested FOR-loops to
complete this task.
(c) Combine and modify the codes written in part a and b to print the “hour glass” of 5 rows as
shown below.
(d) Draw a flowchart for the codes in part a, b and c of this question.
Page 18 of 37
Prepared By: Rehan Naeem and Dr. Suleman Sami Qazi
5. Write a program to get the output exactly as shown below by following the flowchart given below.
Admission to EE Department happens when the percentage of FSC and entry test marks is greater
than 80 and the Fsc marks is greater than 900. Note: Both the outputs belong to the same code. The
percentage is calculated by considering 70% of the FSc and 30% of the entry test marks.
FLOWCHART
OUTPUT 1 OUTPUT 2
Page 19 of 37
Prepared By: Rehan Naeem and Dr. Suleman Sami Qazi
Experiment # 5:
Implementation of Functions in C
1. Complete the following program to get the output as shown below. Do not change the code
written in the main body. Hint: Four functions have to be defined properly to get the output.
2. Complete the following program to calculate and display the modulo (remainder) of two integers
i.e. the dividend and the divisor. The flow chart to find the mod is given below. The program should
work correctly for any values of the dividend and the divisor.
Page 20 of 37
Prepared By: Rehan Naeem and Dr. Suleman Sami Qazi
Page 21 of 37
Prepared By: Rehan Naeem and Dr. Suleman Sami Qazi
3. Write a program to print the name of the day at which a particular date occurs. Use the function
Print_Day() which takes day number, month number, and year as arguments. The function should
then print “The day is <day>” corresponding to the given date. For example, Wednesday is the day
on March 1, 2000. Use Zeller’s Congruence to find the day. The algorithm is also given below. Use
the function calculate_modulo(dividend, divisor) defined in task 2 to find the modulo (remainder).
Page 22 of 37
Prepared By: Rehan Naeem and Dr. Suleman Sami Qazi
Page 23 of 37
Prepared By: Rehan Naeem and Dr. Suleman Sami Qazi
4. Write a program to find the date (Month and day number) at which Easter event occurs. The
pseudo code is given below. Define and use the function calculate_Easter_date( year ) to implement
the pseudocode. Your program should take “year” as input from years.in file. Your program should
store output to the easter_dates.out file as shown below. The command ./E5t4 < years.in >
easter_dates.out will do these two tasks for you. Hint: In step E8, return a positive day number if N >
31 else return a negative day number .
Pseudocode
Input File
Page 24 of 37
Prepared By: Rehan Naeem and Dr. Suleman Sami Qazi
Output File
Page 25 of 37
Prepared By: Rehan Naeem and Dr. Suleman Sami Qazi
Experiment # 6:
Implementation of Arrays in C
1. Complete the following program which swaps any two elements of an array a and then
prints the resultant array. Define and use the function swap() to swap two elements. This
function should take, as arguments, the array a, and the indices of the two elements to be
swapped. Define and use the function print_array() to print the whole array a. This
function should take the array a, and the number of elements of the a as arguments. The
flowchart to swap two elements is given below.
Page 26 of 37
Prepared By: Rehan Naeem and Dr. Suleman Sami Qazi
2. Complete the following program to implement Minimum Element Sort algorithm to sort
integers in ascending order. The pseudo code of the algorithm is given below.
Pseudocode
Page 27 of 37
Prepared By: Rehan Naeem and Dr. Suleman Sami Qazi
3. Write a program to read the command-line arguments and store them in an array named
array. You should define and use a function transfer() to transfer all the relevant items from
array argv[] to array array[] as shown below. This function should take 3 arguments i.e. the
array array[], array argv[] and number of elements of array argv[].
Page 28 of 37
Prepared By: Rehan Naeem and Dr. Suleman Sami Qazi
4. Write a program to implement Bubble Sort algorithm to sort integers in ascending order.
The code to implement Bubble Sort is given below. Define and use a function transfer() to
transfer relevant elements from array argv to array array. Define and use a function
BubbleSort() to implement the Bubble Sort algorithm. This function should take the array
to be sorted and number of elements of that array as arguments. Also define and use a
function swap() to swap two elements of an array. The output is shown below.
Page 29 of 37
Prepared By: Rehan Naeem and Dr. Suleman Sami Qazi
5. Complete the following program which uses the user-defined function
TransferSortAndPrint() to transfer 5 integers entered by the user in the command line to
an array named array. The array array is then
a. sorted using minimum element sort algorithm if the command line argument does
not contain any optional argument.
b. sorted using bubble sort algorithm if the command line argument contains the
optional argument -b which can occur anywhere in the command line after ./E6t5.
At the end, the sorted array is to be printed using the user-defined print_array() function.
Hint: Using the built-in function atoi(), one can detect an integer or non-integer element
present in array argv[].
Page 30 of 37
Prepared By: Rehan Naeem and Dr. Suleman Sami Qazi
Page 31 of 37
Prepared By: Rehan Naeem and Dr. Suleman Sami Qazi
Experiment # 7:
Implementation of Pointers in C - Part 1
1. Complete the following program which stores the addresses of x and y in two pointers i and
j. Use these pointers to add the values present in x and y.
2. Complete the program in question 1 which uses user-defined function add() to add the
values of x and y and store the sum in x. The function add() should take two arguments i.e.
the addresses of x and y and should return nothing back. The sum should be printed out
by the main function.
3. Modify the program in question 2 so that the function add() prints the sum at the output
without storing the sum into another variable.
4. Complete the following program so that the output is obtained as shown below.
Page 32 of 37
Prepared By: Rehan Naeem and Dr. Suleman Sami Qazi
Experiment # 8:
Implementation of Pointers in C - Part 2
1. Write a program to print the address of each element of the array shown below. Use the
concept of field width to introduce spaces at the output. What is the difference between
two consecutive addresses? Why is it so? Note: The addresses given below might be
different from the ones displayed at your output terminal.
2. Complete the following program which sums up all the elements of the array. Use pointer x
and the pointer-offset notation, i.e. *( x + offset ) where offset is an integer value, to access
each element of the array x.
Page 33 of 37
Prepared By: Rehan Naeem and Dr. Suleman Sami Qazi
3. Complete the following program which adds each element of the two arrays x and y and
then stores the sum in a third array z. Use pointer-offset notation to access and store the
array elements.
Page 34 of 37
Prepared By: Rehan Naeem and Dr. Suleman Sami Qazi
Experiment # 9:
Implementation of Dynamic Memory
Allocation in C
1. Create an array having max number of elements equal to 5 using malloc(). Store
and print the integers of the array as shown below. Don’t forget to free the
memory.
2. Re-do the above task by writing and using function foo() which uses a pointer j to
return the address given by malloc(). Also use pointer-offset notation to store and
print the integers stored in the array. Don’t forget to free the memory.
Incomplete code
Page 35 of 37
Prepared By: Rehan Naeem and Dr. Suleman Sami Qazi
3. Write a program the takes the max number of elements and the elements of an
array from the command line. All the elements in the command line should be
stored into an array x and then be printed out as shown below. In the pic below, 5
represents max no of elements of an array. Use calloc() to allocate memory. Use a
function foo() for memory allocation as done in question 2. Don’t forget to free
the memory.
4. Write a program which displays a pattern of characters ‘*’ and ‘.’ randomly. Store
the pattern in an array x created using dynamic memory allocation. Then print out
the elements of the array. The program should read the number of characters of the
pattern from the command line.
Page 36 of 37
Prepared By: Rehan Naeem and Dr. Suleman Sami Qazi
Experiment # 10:
Implementation of Structures in C
1. Write a program which uses the structure struct point having two members of
type int. Create and initialize the two points (variables) p1 and p2 of type struct
point and find and print the distance between the two points.
2. Re-do the above problem using a pointer j to p1 and another pointer k to p2. Use
pointers j and k to initialize p1 and p2, to calculate distance and to print the
distance.
3. Re-do question 1 using function foo() which takes two arguments p1 and p2 and
calculates the distance. The distance should then be returned back and printed at
the output.
4. Re-do question 2 using function foo() which takes three arguments j, k and a
pointer to variable dist. The function foo() should calculate and store the distance
in dist using pointers j, k and a.
Page 37 of 37
Prepared By: Rehan Naeem and Dr. Suleman Sami Qazi