0% found this document useful (0 votes)
215 views

Data Structure Lab Report

1) The document is a lab report submitted by Jubair Rafique Sultan for their Data Structures course. It contains 5 problems solved using different data structure algorithms like array, bubble sort, and binary search. 2) Problem 1 creates and displays a menu-driven array. Problem 2 finds the largest element in a non-empty array. Problem 3 searches a linear array for a specific element and sets its location to 0 if not found. 3) Problem 4 sorts an array using the bubble sort algorithm. Problem 5 uses binary search to find a specific element in a sorted array. Code snippets and outputs are provided for each problem.

Uploaded by

Jubair sultan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
215 views

Data Structure Lab Report

1) The document is a lab report submitted by Jubair Rafique Sultan for their Data Structures course. It contains 5 problems solved using different data structure algorithms like array, bubble sort, and binary search. 2) Problem 1 creates and displays a menu-driven array. Problem 2 finds the largest element in a non-empty array. Problem 3 searches a linear array for a specific element and sets its location to 0 if not found. 3) Problem 4 sorts an array using the bubble sort algorithm. Problem 5 uses binary search to find a specific element in a sorted array. Code snippets and outputs are provided for each problem.

Uploaded by

Jubair sultan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Port City International University

Course Code: CSE218


Course Title: Data Structure Sessional

LAB REPORT NO: 01

“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)
{

printf("\n=>1. Create an array of N integers");


printf("\n=>2. Display of array elements");
printf("\n=>3.Exit");
printf("\nEnter your choice: ");

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(){

int i, loc, max, n;


int arr[i];

printf("\nhow 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]);

7
i = 0;
loc = 0;
max = arr[0];

while (i < n)
{

if(max < arr[i]){


loc = i;
max = arr[i];
}

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;

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]);

13
}

for(k = 1 ; k <= n -1; k++)


{

i = 0;
while (i < n - k )
{

if(arr[i] > arr[i+1])


{
swap = arr[i];
arr[i] = arr[i+1];
arr[i+1] = swap;

i = i + 1;

14
}

printf("sorted list is: \n");


for(i = 0; i < n; i++){
printf("%d\n", arr[i]);
}

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;

printf("how many values you want to store in the array?: \n");


scanf("%d", &n);

printf("input elements in the array: \t");


for(i=0; i<n; i++){
scanf("%d", &data[i]);

17
}

printf("Which element do you want to find?: \n");


scanf("%d", &item);

beg = 0;
end = n - 1;
mid = (beg + end) / 2;

while(beg <= end && data[mid] != item){


if(item < data[mid]){
end = mid - 1;
}
else{
beg = mid + 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

You might also like