Data Structure Lab Report
Data Structure Lab Report
“Lab report”
Submitted to
Tanim Rahman
Sr. Lecturer, Department of Computer Science & Engineering
Port City International University
Submitted by
Jubair Rafique Sultan
CSE 02207175
B.Sc. In CSE
22th Batch
Department of CSE
1
Proble Problem Name Page
m No NO
1 Write a menu-driven program to create 3-6
and display an array.
2 Write a program with a non-empty 7-9
array with N numerical values is given.
Find the location and the value of the
largest element of the array.
3 Write a program with a linear array with 10-12
N elements and a specific element in the
array or sets the location to 0.
4 Write a program using the bubble sort 13-16
algorithm to sort an array of elements.
5 Write a program using the binary search 17-20
algorithm to find a specific element of a
sorted array.
INDEX
2
1)Write a menu-driven program to create and display
an array.
Code :
#include<stdio.h>
#include<stdlib.h>
#define MAX 5
int arr[MAX];
int n = 0;
void CreateArray();
void DisplayArray();
void main()
{
int a;
while(1)
{
3
scanf("%d", &a);
switch(a)
{
case 1: CreateArray();
break;
case 2: DisplayArrray();
break;
case 3: exit(1);
break;
default: printf("\nPlease enter a valid choice:");
}
}
}
void CreateArray()
{
int i;
printf("\nEnter the number of elements: ");
scanf("%d", &n);
printf("\nEnter the elements: ");
for(i=0; i<n; i++)
{
scanf("%d", &arr[i]);
4
}
}
void DisplayArrray()
{
int i;
if(n == 0)
{
printf("\nNo elements to display");
return;
}
printf("\nArray elements are: ");
for(i=0; i<n;i++)
printf("%d\t ", arr[i]);
}
5
OUTPUT:
6
2)Write a program with a non-empty array with N
numerical values is given. Find the location and the
value of the largest element of the array.
CODE:
#include <stdio.h>
int main(){
7
i = 0;
loc = 0;
max = arr[0];
while (i < n)
{
i = i + 1;
}
loc = loc + 1;
printf("largest value is %d and location is %d", max,loc);
return 0;
8
OUTPUT:
9
3)Write a program with a linear array with N
elements and a specific element in the array or sets
the location to 0.
CODE:
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100
int main(){
int arr[MAX_SIZE];
int i, loc, item, n;
printf("how many values you want to store in the array?: \n");
scanf("%d", &n);
printf("input elements in the array: \n");
for(i=0; i<n; i++){
scanf("%d", &arr[i]);
}
printf("Which element do you want to find?: \n");
scanf("%d", &item);
i = 1;
loc = 0;
while (i < n ) {
10
if(arr[i] == item){
loc = i;
break;
}
i = i + 1;
}
if(loc == 0){
printf("%d is not in the array \n", item);
}
else{
printf("%d is in the array at %d. \n", item, i);
return 0;
}
11
OUTPUT:
12
4)Write a program using the bubble sort algorithm to
sort an array of elements.
Code:
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100
int main(){
int arr[MAX_SIZE];
int i, swap, n, k;
13
}
i = 0;
while (i < n - k )
{
i = i + 1;
14
}
return 0;
}
15
OUTPUT:
16
5)Write a program using the binary search algorithm to
find a specific element of a sorted array.
Code:
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100
int main(){
int data[MAX_SIZE];
int beg, loc, item, end, n, i;
int mid;
17
}
beg = 0;
end = n - 1;
mid = (beg + end) / 2;
18
if(item == data[mid]){
loc = mid ;
printf("%d found at the location %d. \n", item, loc);
}
else{
loc = 0;
printf("%d not found in the list.\n", item);
}
return 0;
}
19
OUTPUT:
20