Department of Electrical Engineering

You might also like

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

Department of Electrical Engineering

Faculty of Engineering & Applied Sciences


Riphah International University, Islamabad, Pakistan

Program: B.Sc. Electrical Engineering Semester: III


Subject: SEL-201 Data Structure & Algorithm Date:
…………….
Experiment 4: Implementation of Function and pointer by executing C programs
CLO3 (P2)
OBJECTIVES:

 Value-Returning Functions
 Function Call
 Pointer Data Type and Pointer Variables 794.
 Declaring Pointer Variables.
 Address of Operator (&).
 Dereferencing Operator (*).
 Operation on Pointers.

Name: Urooj Abid Roll No: 13777

Performance Lab Report

Description Total Marks Description Total Marks


Mark Obtaine Mark Obtaine
s d s d
Ability to 5 Organization/Structu 5
conduct re
Experiment &
Tool usage
Implementatio 5 5
n and Results Data Presentation
Total Marks obtained
Remarks (if any): ………………………………….

Name & Signature of faculty: …………………………………


Lab Task
Lab Task 1: An integer number is a prime number if it is divisible only by one

and itself. For example, 2, 3, 5, 7, and 11 are prime numbers. Write a program
that reads an integer and determines if it is a prime number. Suppose it is not a
prime number, print out the smallest divisor. Otherwise, print out the prime
number. Create a function to return the smallest divisor for an integer. Use this
function to determine and print all the prime numbers between 1 and 1000.
(Hint: use the modulus operator % to determine if a number is divisible by
another number. The smallest divisor of even number is 2. However, you only
need to test up to √n to verify if it is divisible)
Code
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
void temp(int*);
int main()
{
int a;
printf("Please Enter The No.: ");
scanf("%d",&a);
temp (&a);
printf("Prime Numbers From 1 to 1000 Are:\n ");
print();
return 0;
}
void temp(int *x)
{
int counter=0;
for(int i=2; i<*x ; i++)
{
if(*x%i==0)
{
printf("Number Is Not Prime\n");
printf("Smallest Divisor Is : %d \n",i);
counter=1;
break;
}
}
if (counter==0)
{
printf("Number Is Prime.\n");
}
}
void print()
{
int i, j, counter2;
for(i=2; i<=1000; i++)
{
counter2 = 1;
for(j=2; j<=i/2; j++)
{
if(i%j==0)
{
counter2 = 0;
break;
}
}
if(counter2==1)
{
printf("%d,",i);
}
}
}
Output

Lab Task 2: Write a program that reads the values for the width, length, and

height of a rectangular box using three double type variables in the main()
function. Pass values of these variables to a function, which calculates the
volume and surface area for six sides of the box. The box's volume and surface
area passed back from two other arguments are printed out in the function
main(). Check your program with the user input for the width, length, and height
of 2,3, 4 meters, respectively.
Code
#include<stdio.h>
#include<conio.h>
void fun(int w, int l, int h, int *v, int *s)
{
*v = w * l * h;
*s = 2 * (w*l + h*l + h*w);
}
int main()
{
int w,l,h;
int v,s;
printf("Enter the width: ");
scanf("%d",&w);
printf("Enter the length: ");
scanf("%d",&l);
printf("Enter the height: ");
scanf("%d",&h);
fun(w,l,h,&v,&s);
printf("Volume of the Rectangular Box = %d",v);
printf("\nSurface Area of the Rectangular Box = %d",s);
return 0;
}
Output

Lab Task 3: Implement the following equation in main without using any

operator or sign. Note: use De Morgan's laws


For example:
z= x + y/ x * x;
z= div (sum (x, y), multi (x, x));
F = y * y + x / y;
D = x / y - x * x;
G =x* y – x + y* y; Verify the answer by using sign operators
Code
#include <stdio.h>
#include <stdlib.h>
int sum(int P, int L)
{
int U;
U = P+L;
}
int diff(int P, int L)
{
int U;
U = P-L;
}
int divide(int P, int L)
{
int U;
U = P/L;
}
int pro(int P, int L)
{
int U;
U = P*L;
}
int main()
{
int F,D,G;
int a_value_in_G;
int x,y;
printf("Enter the value for x: ");
scanf("%d",&x);
printf("Enter the value for y: ");
scanf("%d",&y);
F = sum(pro(y,y),divide(x,y));
D = diff(divide(x,y),pro(x,x));
a_value_in_G=pro(y,y);
G = diff(pro(x,y),sum(x,a_value_in_G));
printf("Value of F: %d\n",F);
printf("Value of D: %d\n",D);
printf("Value of G: %d\n",G);
return 0;
}
Output

You might also like