Uploaded by cenamark60

USP Minor Assignment - 2 [Saswat Mohanty 1941012407 CSE-D].docx

advertisement
Department of Computer Science & Engineering
Faculty of Engineering & Technology (ITER)
Contents
Sl. No.
Date
Page No.
Remarks
Name of the Minor Assignment/ Major
Assignment/ End-Term
Project
1
29-12-2021
Minor Assignment-002
1-35
1. For the given structure below, declare the variable type, and print their values;
Name: - Saswat Mohanty
2
Regd. No.: - 1941012407
Department of Computer Science & Engineering
Faculty of Engineering & Technology (ITER)
Code: #include <stdio.h>
int main()
{
int Ia=345;
float Fb=4.5f;
char Chvar='Z';
printf("\nIa: %d",Ia);
printf("\nFb: %f",Fb);
printf("\nChvar: %c",Chvar);
return 0;
}
Output: -
2. For the given structure below, declare the variable type, print their values and
addresses;
Code: -
Name: - Saswat Mohanty
3
Regd. No.: - 1941012407
Department of Computer Science & Engineering
Faculty of Engineering & Technology (ITER)
#include<stdio.h>
int main()
{
int Ia=345;
float Fb=4.5;
char Chvar='Z';
printf("Ia= %d and address:%u\n", Ia,&Ia);
printf("FB= %f and address:%u\n", Fb,&Fb);
printf("Chvar= %c and address:%u\n", Chvar,&Chvar);
return 0;
}
Output: -
3. Declare two integer variable and assign values to them, and print their
addresses. Additionally, Swap the contents of the variables and print their
addresses after swap. State whether the addresses before and after are equal or
not.
Code: #include<stdio.h>
int main()
{
int a=5,b=7;
printf("\nBEFORE SWAPPING->\n");
printf("The value of a: %d and address of a: %u\n", a,&a);
printf("The value of b: %d and address of b: %u\n", b,&b);
a=a+b;
b=a-b;
a=a-b;
printf("AFTER SWAPPING->\n");
Name: - Saswat Mohanty
4
Regd. No.: - 1941012407
Department of Computer Science & Engineering
Faculty of Engineering & Technology (ITER)
printf("The value of a: %d and address of a: %u\n", a,&a);
printf("The value of b: %d and address of b: %u\n", b,&b);
return 0;
}
Output: -
4. Write the C statement to declare and initialize the pointer variable for the given
structure and display the values of x, y and z with the help of p.
Code: -
Name: - Saswat Mohanty
5
Regd. No.: - 1941012407
Department of Computer Science & Engineering
Faculty of Engineering & Technology (ITER)
#include<stdio.h>
int main()
{
int x=45,y=12,z=23;
int *p;
p=&x;
printf("The value of x : %d\n", *p);
p=&y;
printf("The value of y : %d\n", *p);
p=&z;
printf("The value of z : %d\n", *p);
return 0;
}
Output: -
5. Write the C statement to declare and initialize the pointer variable for the given
structure.
Code: -
Name: - Saswat Mohanty
6
Regd. No.: - 1941012407
Department of Computer Science & Engineering
Faculty of Engineering & Technology (ITER)
#include<stdio.h>
int main()
{
float x=6.7,y=1.2,z=2.3;
float *p;
p=&x;
printf("x : %.1f\n",*p);
p=&y;
printf("y : %.1f\n",*p);
p=&z;
printf("z : %.1f\n\n",*p);
return 0;
}
Output: -
6. Write the C statement to declare and initialize the pointer variable for the given
structure.
Code: -
Name: - Saswat Mohanty
7
Regd. No.: - 1941012407
Department of Computer Science & Engineering
Faculty of Engineering & Technology (ITER)
#include<stdio.h>
void main()
{
int x=89;
int *p1,*p2,*p3;
p1=&x;
printf("\nx through p1 : %d\n",*p1);
p2=&x;
printf("x through p2 : %d\n",*p2);
p3=&x;
printf("x through p3 : %d\n",*p3);
}
Output: -
7. Write the C statement to declare and initialize the pointer variable for the given
structure.
Code: -
Name: - Saswat Mohanty
8
Regd. No.: - 1941012407
Department of Computer Science & Engineering
Faculty of Engineering & Technology (ITER)
#include<stdio.h>
void main()
{
float x=8.6;
float *p1,*p2,*p3;
p1=&x;
printf("\nx through p1 : %.1f\n",*p1);
p2=&x;
printf("x through p2 : %.1f\n",*p2);
p3=&x;
printf("x through p3 : %.1f\n\n",*p3);
}
Output: -
8. Write the C statement to declare and initialize the pointer variable for the given
structure and update the values of a, b and c to be incremented by 10.
Code: -
Name: - Saswat Mohanty
9
Regd. No.: - 1941012407
Department of Computer Science & Engineering
Faculty of Engineering & Technology (ITER)
#include<stdio.h>
int main()
{
int a=12,b=25,c=18;
int *ptr;
printf("a = %d\n",a);
printf("b = %d\n",b);
printf("c = %d\n",c);
printf("AFTER INCREMENTING->\n");
ptr=&a; printf("a = %d\n",*ptr+10);
ptr=&b; printf("b = %d\n",*ptr+10);
ptr=&c; printf("c = %d\n",*ptr+10);
}
Output: -
9. Write the C statement to declare and initialize the pointer variable for the given
structure and update the values of a, b and c to be incremented by 10.
Code: #include<stdio.h>
Name: - Saswat Mohanty
10
Regd. No.: - 1941012407
Department of Computer Science & Engineering
Faculty of Engineering & Technology (ITER)
int main()
{
int a=12,b=52,c=8;
int *ptr1,*ptr2,*ptr3;
ptr1=&a; ptr2=&b; ptr3=&c;
printf("\n\ta = %d\n",*ptr1);
printf("\tb = %d\n",*ptr2);
printf("\tc = %d\n",*ptr3);
printf("\tAFTER INCREMENTING->\n");
printf("\ta = %d\n",*ptr1+10);
printf("\tb = %d\n",*ptr2+10);
printf("\tc = %d\n\n",*ptr3+10);
}
Output: -
10. Declare and initialize the pointer variables.
Code: #include <stdio.h>
Name: - Saswat Mohanty
11
Regd. No.: - 1941012407
Department of Computer Science & Engineering
Faculty of Engineering & Technology (ITER)
int main()
{
int Ivar=454;
int *ptr=&Ivar;
int **Tptr=&ptr;
printf("\nValue of Ivar= %d",*ptr);
printf("\nAddress of Ivar= %p",ptr);
printf("\nValue at ptr= %p",ptr);
printf("\nAddress oh ptr= %p",Tptr);
printf("\nValue at Tptr= %p",Tptr);
printf("\nAddress of Tptr= %p",&Tptr);
return 0;
}
Output: -
11. Two pointers are pointing to different variable. Write the C++ statement to find
the greater between a, and b through pointer manipulation.
Code: #include <stdio.h>
int main()
Name: - Saswat Mohanty
12
Regd. No.: - 1941012407
Department of Computer Science & Engineering
Faculty of Engineering & Technology (ITER)
{
int a=52;
int b=18;
int *ptr1=&a;
int *ptr2=&b;
if(*ptr1>*ptr2)
{
printf("a=%d is grater than b=%d",*ptr1,*ptr2);
}
else
{
printf("b=%d is greater than a=%d",*ptr2,*ptr1);
}
return 0;
}
Output: -
12. Write the C++ statement to manipulate the value of the variable Ia
through the pointers ptr1, ptr2, and ptr3.
We can use different pointers to
point the same data variable.
For example;
1. int Ia;
2. int ∗ptr1 = &Ia;
3. · · · // manipulate the variable Ia
4. int ∗ptr2 = &Ia;
5. · · · // manipulate the variable Ia
6. · · ·
Code: #include <stdio.h>
int main(){
int a=52;
Name: - Saswat Mohanty
13
Regd. No.: - 1941012407
Department of Computer Science & Engineering
Faculty of Engineering & Technology (ITER)
int b=18;
int *ptr1=&a;
int *ptr2=&b;
if(*ptr1>*ptr2){
printf("a=%d is grater than b=%d",*ptr1,*ptr2);
}
else{
printf("b=%d is greater than a=%d",*ptr2,*ptr1);
}
return 0;
}
Output: -
13. Trace the execution of the following fragment at line -1.
Code: #include<stdio.h>
int main(){
int m = 10, n = 5;
Name: - Saswat Mohanty
14
Regd. No.: - 1941012407
Department of Computer Science & Engineering
Faculty of Engineering & Technology (ITER)
int *mp, *np;
mp = &m;
np = &n;
*mp = *mp + *np;
*np = *mp - *np;
printf("%d %d\n%d %d\n", m, *mp, n, *np);
}
Output: -
14. Given the declarations
describe the errors in each of the following statements.
Ans: m = &n; //m not a pointer
itemp = m; //hey m not an address
*itemp = c; //itemp is not given an adress
*itemp = &c; //itemp not a charecter pointer
Name: - Saswat Mohanty
15
Regd. No.: - 1941012407
Department of Computer Science & Engineering
Faculty of Engineering & Technology (ITER)
15. Write a prototype for a function sum n avg that has three type double
input parameters and two output parameters. The function computes the
sum and the average of its three input arguments and relays its results
through two output parameters.
Ans: // void sum_n_avg(double inp1,double inp2,double inp3,double*sum,double
*avg);
16. Given the memory setup shown, fill in the chart by indicating the data
type and value of each reference as well as the name of the function in
which the reference would be legal. Describe pointer values by referring to
cell attributes.
Ans: Reference
Where Legal Data Type
Name: - Saswat Mohanty
16
Value
Regd. No.: - 1941012407
Department of Computer Science & Engineering
Faculty of Engineering & Technology (ITER)
Valp
&many
code
Sub
Main
Main
double*
int*
char*
&code
Sub
char*
countp
*countp
*valp
Letp
main
sub
main
sub
int
int
double
char*
&x
main
int
Pointer to the color-shaded cell
Pointer to the grey-shaded cell
Pointer to the color-shaded
shell
Pointer to the white-shaded
shell
pointer to the many
14
17.1
Pointer to the white-shaded
shell
address of x
17. The following code fragment is from a function preparing to call sum n
avg.
Complete the function call statement.
Define the function sum n avg whose prototype you wrote in question-15.
The function should compute both the sum and the average of its three
input parameters and relay these results through its output parameters.
Design the complete C code to get the desire result.
Code: #include <stdio.h>
void sum_n_avg(double,double,double,double*,double*);
int main(){
double one,two,three,sum, avg;
double *ptr1=&sum,*ptr2=&avg;
printf("Enter the three numbers: ");
scanf("%lf%lf%lf",&one, &two, &three);
sum_n_avg(one,two,three,ptr1,ptr2);
Name: - Saswat Mohanty
17
Regd. No.: - 1941012407
Department of Computer Science & Engineering
Faculty of Engineering & Technology (ITER)
printf("Sum = %lf, avg= %lf",*ptr1,*ptr2);
return 0;
}
void sum_n_avg(double a, double b, double c,double *ptr1,double *ptr2){
*ptr1=a+b+c;
*ptr2=*ptr1/3;
}
Output: -
19. Show the table of values for x , y , and z that is the output displayed by
the following program.
Code: #include <stdio.h>
void sum(int a, int b, int *cp);
int main(void){
int x, y, z;
x = 7; y = 2;
printf("x y z\n\n");
sum(x, y, &z);
printf("%4d%4d%4d\n", x, y, z);
sum(y, x, &z);
printf("%4d%4d%4d\n", x, y, z);
sum(z, y, &x);
printf("%4d%4d%4d\n", x, y, z);
sum(z, z, &x);
printf("%4d%4d%4d\n", x, y, z);
sum(y, y, &y);
printf("%4d%4d%4d\n", x, y, z);
return (0);
}
void sum(int a, int b, int *cp)
Name: - Saswat Mohanty
18
Regd. No.: - 1941012407
Department of Computer Science & Engineering
Faculty of Engineering & Technology (ITER)
{
*cp = a + b;
}
Output: xyz
729
729
11 2 9
18 2 9
18 4 9
20.
a) Classify each formal parameter of double trouble and trouble as input,
output, or input/output.
b) What values of x and y are displayed by this program?
void double_trouble(int *p, int y);
void trouble(int *x, int *y);
int main(void){
int x, y;
trouble(&x, &y);
printf("x = %d, y = %d\n", x, y);
return (0);
}
void double_trouble(int *p, int y){
int x;
x = 10;
*p = 2 * x - y;
}
void trouble(int *x, int *y){
double_trouble(x, 7);
double_trouble(y, *x);
}
Ans: void trouble(int *x, int *y)
Name: - Saswat Mohanty
19
Regd. No.: - 1941012407
Department of Computer Science & Engineering
Faculty of Engineering & Technology (ITER)
{
double_trouble(x, 7);
double_trouble(y, *x);
int *p→x
int y→*y
int *p→y
int y→*x }
int main(void)
{
int x, y;
trouble(&x, &y);
printf("x = %d, y = %d\n", x, y);
return (0);
}
int *x→&x
int *y→&y
22. Write a program in C to add numbers using function via call by
reference.
Code: #include <stdio.h>
void add(int *n1, int *n2);
int main(void) {
int n1, n2;
printf("Input the first number: ");
scanf("%d", &n1);
printf("Input the second number: ");
scanf("%d", &n2);
printf("The sum of %d and %d is ", n1, n2);
add(&n1, &n2);
printf("%d\n", n1);
return 0;
}
void add(int *n1, int *n2) {
*n1 = *n1 + *n2;
return;
}
Output: -
Name: - Saswat Mohanty
20
Regd. No.: - 1941012407
Department of Computer Science & Engineering
Faculty of Engineering & Technology (ITER)
23. Write a program in C to find the maximum and minimum number in an array
having 5 elements.
Code: #include <stdio.h>
void maxMin(int arr[5], int n);
int main(void) {
int arr[5];
printf("\n\tInput 5 elements in the array: ");
for(int i = 0; i < 5; i++) {
scanf("%d", &arr[i]);}
for(int i = 0; i < 5; i++) {
printf("\tarr[%d]: %d\n", i, arr[i]);}
maxMin(arr, 5);
return 0;
}
void maxMin(int arr[5], int n) {
int max = 0, min = arr[0];
for(int i = 0; i < n; i++) {
if(arr[i] > max) {
max = arr[i];}
if(arr[i] < min) {
min = arr[i];
}}
printf("\tMaximum element is: %d\n", max);
printf("\tMinimum element is: %d\n\n", min);
}
Output: -
Name: - Saswat Mohanty
21
Regd. No.: - 1941012407
Department of Computer Science & Engineering
Faculty of Engineering & Technology (ITER)
24. Write a program in C for a menu driven calculator having 4
functionalities Addition, Subtraction, Multiplication and Division. All the
functionalities should be implemented in functions which should be called
via function pointers.
Code: #include <stdio.h>
void addition(int *n1, int *n2);
void subtraction(int *n1, int *n2);
void multiplication(int *n1, int *n2);
void division(int *n1, int *n2);
int main(void) {
int n1, n2, n;
printf("1 - ADDITION\n");
printf("2 - SUBTRACTION\n");
printf("3 - MULTIPLICATION\n");
printf("4 - DIVISION\n");
printf("Please Enter The Number As Mentioned To Do Calculation: ");
scanf("%d", &n);
if(n >= 1 && n <= 4) {
printf("Enter The First Number: ");
scanf("%d", &n1);
printf("Enter The Second Number: ");
scanf("%d", &n2);
if(n == 1) {
printf("%d + %d = ", n1, n2);
addition(&n1, &n2);
printf("%d\n", n1);
}
else if(n == 2) {
printf("%d - %d = ", n1, n2);
subtraction(&n1, &n2);
Name: - Saswat Mohanty
22
Regd. No.: - 1941012407
Department of Computer Science & Engineering
Faculty of Engineering & Technology (ITER)
printf("%d\n", n1);
}
else if(n == 3) {
printf("%d * %d = ", n1, n2);
multiplication(&n1, &n2);
printf("%d\n", n1);
}
else {
printf("%d / %d = ", n1, n2);
division(&n1, &n2);
printf("%d\n", n1);
}
}
else {
printf("Oh! You Entered Something Unexpected.");
}
return 0;
}
void addition(int *n1, int *n2) {
*n1 = *n1 + *n2;
}
void subtraction(int *n1, int *n2) {
*n1 = *n1 - *n2;
}
void multiplication(int *n1, int *n2) {
*n1 = *n1 * *n2;
}
void division(int *n1, int *n2) {
*n1 = *n1 / *n2;
}
Output: -
Name: - Saswat Mohanty
23
Regd. No.: - 1941012407
Department of Computer Science & Engineering
Faculty of Engineering & Technology (ITER)
25. Write a program in C to read a sentence and replace lowercase
characters by uppercase and vice-versa.
Code: #include<stdio.h>
#include<ctype.h>
void main()
{
char str[100];
int count,ch,i;
printf("Enter the String: \n");
for(i=0;(str[i]=getchar())!='\n';i++)
{
;
}
str[i]='\0';
count=i;
printf("The given string is : %s",str);
printf("\nNew bstring after changing the case is : ");
for(i=0;i<count;i++)
{
ch=islower(str[i])? toupper(str[i]):tolower(str[i]);
putchar(ch);
}
}
Name: - Saswat Mohanty
24
Regd. No.: - 1941012407
Department of Computer Science & Engineering
Faculty of Engineering & Technology (ITER)
Output: -
26. Create a program to compute the mean and standard deviation of an
array of data given below and displays the difference between each value
and the mean.
15.3 4
90
34
2.5 104 7
25
82
For MAX ITEM data items, if we assume that x is an array whose lowest subscript
is 0, the standard deviation is given by the formula.
Code: #include <stdio.h>
#include <math.h>
#define MAX_ITEM 10
void main()
{
float x[MAX_ITEM];
int i, n;
float mean, std_deviation, sum = 0, sum1 = 0,diffrence;
printf("\nEnter the value of N \n");
scanf("%d", &n);
printf("Enter %d real numbers \n", n);
for (i = 0; i < n; i++){
scanf("%f", &x[i]);
}
for (i = 0; i < n; i++){
sum = sum + x[i];
}
mean = sum / (float)n;
for(i=0;i<n;i++){
Name: - Saswat Mohanty
25
Regd. No.: - 1941012407
Department of Computer Science & Engineering
Faculty of Engineering & Technology (ITER)
diffrence=mean-x[i];
printf("\nThe diffrence is : %.2f ",diffrence);
}
for (i = 0; i < n; i++)
{
sum1 = sum1 + pow((x[i] - mean), 2);
}
std_deviation = sqrt(sum1 / (float)n);
printf("\nMean of all elements = %.2f", mean);
//printf("variance of all elements = %.2f\n", variance);
printf("\nStandard deviation = %.2f\n\n", std_deviation);
}
Output: -
27. You have two independent sorted arrays of size m, and n respectively,
where m, n > 0. You are required to merge the two arrays such that the
merged array will be in sorted form and will contain exactly m + n number
Name: - Saswat Mohanty
26
Regd. No.: - 1941012407
Department of Computer Science & Engineering
Faculty of Engineering & Technology (ITER)
of elements. You are not allowed to use any kind of sorting algorithm.
Design your program to meet the above given requirement. Assume the
elements of the array are non-negative integers. The elements can be read
from the keyboard or can be generated randomly.
Code: #include <stdio.h>
#include <limits.h>
int main(void){
int m, n, i;
printf("Enter Size Of First Array: ");
scanf("%d", &m);
int a1[m];
printf("Enter Values Of First Array: ");
for(i = 0; i < m; i++) {
scanf("%d", &a1[i]);
}
printf("Enter Size Of Second Array: ");
scanf("%d", &n);
int a2[n];
printf("Enter Values Of Second Array: ");
for(i = 0; i < n; i++) {
scanf("%d", &a2[i]);
}
int a[m + n];
int j = 0, k = 0;
for(i = 0; i < m + n; i++) {
int temp1 = INT_MAX, temp2 = INT_MAX;
if(j < m) {
temp1 = a1[j];
}
if(k < n) {
temp2 = a2[k];
}
if(temp1 < temp2) {
a[i] = temp1;
j++;
}
else {
a[i] = temp2;
Name: - Saswat Mohanty
27
Regd. No.: - 1941012407
Department of Computer Science & Engineering
Faculty of Engineering & Technology (ITER)
k++;
}
}
printf("First Array: \n");
for(i = 0; i < m; i++) {
printf("%d ", a1[i]);
}
printf("\nSecond Array: \n");
for(i = 0; i < n; i++) {
printf("%d ", a2[i]);
}
printf("\nThe Merged Sorted Array: \n");
for(i = 0; i < m + n; i++) {
printf("%d ", a[i]);
}
printf("\n");
return 0; }
Output: -
28. Write a program to declare one array for storing the square roots of the
integers from 0 through 10 and a second array for storing the cubes of the
same integers.
Code: #include <stdio.h>
#include<math.h>
double square_root(int n){
Name: - Saswat Mohanty
28
Regd. No.: - 1941012407
Department of Computer Science & Engineering
Faculty of Engineering & Technology (ITER)
double sq;
sq=sqrt(n);
return sq;}
int cube_func(int n){
int cb;
cb=n*n*n;
return cb;}
int main(){
double root[11];
int cubes[11];
printf("\n");
for(int i=0;i<11;i++){
root[i]=square_root(i);}
for(int i=0;i<11;i++){
cubes[i]=cube_func(i);}
printf("Square root array:");
for(int i=0;i<11;i++){
printf("%.2lf ",root[i]);}
printf("\nCube array:");
for(int i=0;i<11;i++){
printf("%d ",cubes[i]);}
printf("\n\n");
return 0;
}
Output: -
29. Write a program to use the user-defined function multiply that takes two
type int array input arguments and their effective size and produces a
result array containing the sums of corresponding elements. For example,
for the three-element input arrays 5 1 7 and 2 4 2 , the result would be an
array containing 7 3 5 .
Name: - Saswat Mohanty
29
Regd. No.: - 1941012407
Department of Computer Science & Engineering
Faculty of Engineering & Technology (ITER)
Code: #include <stdio.h>
void multiply(int a1[], int a2[], int n, int *a);
int main(void) {
int n, i;
printf("\n");
printf("Enter The Size Of The Array: ");
scanf("%d", &n);
int a1[n], a2[n], a[n];
printf("Enter The Elements Of First Array: \n");
for(i = 0; i < n; i++) {
scanf("%d", &a1[i]);
}
printf("Enter The Elements Of Second Array: \n");
for(i = 0; i < n; i++) {
scanf("%d", &a2[i]);
}
printf("Result Array: \n");
multiply(a1, a2, n, a);
for(i = 0; i < n; i++){
printf("%d ", a[i]);
}
printf("\n\n");
return 0;
}
void multiply(int a1[], int a2[], int n, int *a) {
int i;
for(i = 0; i < n; i++) {
a[i] = a1[i] + a2[i];
}}
Output: -
Name: - Saswat Mohanty
30
Regd. No.: - 1941012407
Department of Computer Science & Engineering
Faculty of Engineering & Technology (ITER)
31. Write a program which uses a user defined function, find largest() to
find the largest element of an integer array.
Code: #include <stdio.h>
int find_largest(int a[], int n);
int main(void)
{
int n;
printf("Enter Size Of The Array: \n");
scanf("%d", &n);
int a[n], i;
printf("Enter Value To The Array: \n");
for(i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
int max = find_largest(a, n);
printf("Largest Element In The Array Is: %d\n", max);
return 0;
}
int find_largest(int a[], int n) {
int max = 0, i;
for(i = 0; i < n; i++) {
if(max < a[i]) {
max = a[i];
}
}
return max;
Name: - Saswat Mohanty
31
Regd. No.: - 1941012407
Department of Computer Science & Engineering
Faculty of Engineering & Technology (ITER)
}
Output: -
32. The bubble sort is another technique for sorting an array. A bubble sort
compares adjacent array elements and exchanges their values if they are
out of order. In this way, the smaller values “bubble” to the top of the array
(toward element 0), while the larger values sink to the bottom of the array.
After the first pass of a bubble sort, the last array element is in the correct
position; after the second pass the last two elements are correct, and so
on. Thus, after each pass, the unsorted portion of the array contains one
less element. Write and test a function that implements this sorting
method.
Code: #include <stdio.h>
void bubblesort(int a[], int n);
int main(void) {
int n;
printf("Enter Size Of The Array: ");
scanf("%d", &n);
int a[n], i;
printf("Enter Value To The Array: \n");
for(i = 0; i < n; i++) {
scanf("%d", &a[i]);}
printf("Given Array Is: \n");
for(i = 0; i < n; i++) {
Name: - Saswat Mohanty
32
Regd. No.: - 1941012407
Department of Computer Science & Engineering
Faculty of Engineering & Technology (ITER)
printf("%d ", a[i]);}
bubblesort(a, n);
printf("\nSorted Array Using Bubble Sort Is: \n");
for(i = 0; i < n; i++) {
printf("%d ", a[i]);}
printf("\n");
return 0;
}
void bubblesort(int a[], int n) {
int i, j;
for(i = 0; i < n; i++) {
for(j = 0; j < n - 1; j++) {
if(a[j] > a[j + 1]) {
int temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
}
Output: -
33. The binary search algorithm that follows may be used to search an
array when the elements are in order. The algorithm for binary search given
as;
Name: - Saswat Mohanty
33
Regd. No.: - 1941012407
Department of Computer Science & Engineering
Faculty of Engineering & Technology (ITER)
1. Let bottom be the subscript of the initial array element.
2. Let top be the subscript of the last array element.
3. Let found be false.
4. Repeat as long as bottom isn’t greater than top and the target has not been found
5. Let middle be the subscript of the element halfway between bottom and top.
6. if the element at middle is the target
7. Set found to true and index to middle.
else if the element at middle is larger than the target
8. Let top be middle - 1.
else
9. Let bottom be middle + 1.
Write and test a function binary srch that implements this algorithm for an array of
integers. When there is a large number of array elements, which function do you think is
faster: binary_srch or the linear search algorithm.
Code: #include <stdio.h>
typedef enum {false, true} boolean;
boolean binary_srch(int a[], int *bottom, int top, int target);
int main(void) {
int n;
printf("Enter Size Of The Array: \n");
scanf("%d", &n);
int a[n], i;
printf("Enter Value To The Array: \n");
for(i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
int target;
printf("Enter The Target Value: \n");
scanf("%d", &target);
int bottom = 0, top = n - 1;
boolean b = binary_srch(a, &bottom, top, target);
if(b) {
printf("Target Element Is Present At Index %d In The Array.\n", bottom);
}
else {
printf("Target Element Is Present Not Present In The Array.\n");
}
return 0;
Name: - Saswat Mohanty
34
Regd. No.: - 1941012407
Department of Computer Science & Engineering
Faculty of Engineering & Technology (ITER)
}
boolean binary_srch(int a[], int *bottom, int top, int target) {
boolean found = false;
while(*bottom <= top) {
int middle = *bottom + (top - *bottom) / 2;
if(a[middle] == target) {
found = true;
*bottom = middle;
break;
}
else if(a[middle] > target) {
top = middle - 1;
}
else{
*bottom = middle + 1;
}
}
return found;
}
Output: -
34. Implement the following algorithm for linear search that sets a flag (for
loop control) when the element being tested matches the target.
1. Assume the target has not been found.
2. Start with the initial array element.
Name: - Saswat Mohanty
35
Regd. No.: - 1941012407
Department of Computer Science & Engineering
Faculty of Engineering & Technology (ITER)
3. repeat while the target is not found and there are more array elements
4. if the current element matches the target
5. Set a flag to indicate that the target has been found.
else
6. Advance to the next array element.
7. if the target was found
8. Return the target index as the search result.
else
9. Return -1 as the search result.
Create a user-defined function with prototype int linear search(const int arr[
],
int target, int n); in your program to search the target element.
Code: #include <stdio.h>
int linear_search(const int arr[], int target, int n);
int main(void) {
int n;
printf("Enter Size Of The Array: \n");
scanf("%d", &n);
int a[n], i;
printf("Enter Value To The Array: \n");
for(i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
int target;
printf("Enter The Target Element: \n");
scanf("%d", &target);
int flag = linear_search(a, target, n);
if(flag != -1) {
printf("Target Element Found At Index %d In The Array.\n",flag);
}
else {
printf("Target Element Is Not Present In The Array.\n");
}
return 0;
}
int linear_search(const int arr[], int target, int n) {
int i, flag = -1;
Name: - Saswat Mohanty
36
Regd. No.: - 1941012407
Department of Computer Science & Engineering
Faculty of Engineering & Technology (ITER)
for(i = 0; i < n; i++) {
if(arr[i] == target) {
flag = i;
break;
}
}
return flag;
}
Output: -
Name: - Saswat Mohanty
37
Regd. No.: - 1941012407
Download