0% found this document useful (0 votes)
7 views10 pages

Santosh

Uploaded by

Aryan Yadav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views10 pages

Santosh

Uploaded by

Aryan Yadav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Data structure

Practical -1
210470107055

program 1: write a program to swap two value

 with call by value function


#include<stdio.h>
swap(int ,int){
printf("before swapping a=%d b=%d",a,b);
temp=a;
a=b;
b=temp;
printf("after swaaping a=%d b=%d",a,b);
}

int main()
{
int a,b,temp;
printf("enter two number");
scanf("%d %d",&a,&b);
swap(a,b);
return 0;
}

 output

enter two number


22
78

before swapping a=22 b=78


after swaaping a=78 b=22

 with call by referance function


Data structure
Practical -1
210470107055
#include<stdio.h>
swap(int *x,int *y){
int temp;
printf("before swapping a=%d b=%d \n",*x,*y);
temp=*x;
*x=*y;
*y=temp;
printf("after swaaping a=%d b=%d \n",*x,*y);
}
int main()
{
int a,b,*x,*y;
x=&a;
y=&b;
printf("enter two number:");
scanf("%d %d",&a,&b);
swap(&a,&b);
return 0;
}

 output
enter two number:
23
36
before swapping a=23 b=36
after swaaping a=36 b=23

Program -2 compute 5 values and add, modify for 10


Data structure
Practical -1
210470107055
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, sum = 0;
int* arr = (int*)malloc(5*sizeof(int));
printf("Enter the first 5 elements: ");
for(i=0;i<5;i++)
{
scanf("%d", arr+i);
sum += *(arr+i);
}
printf("Sum of 5 elements: %d\n", sum);
arr = (int*)realloc(arr, 10*sizeof(int));
printf("Enter the next 5 elements: ");
for(i=5;i<10;i++)
{
scanf("%d", arr+i);
sum += *(arr+i);
}
printf("Sum of 10 elements: %d\n", sum);
return 0;
}

 Output
Data structure
Practical -1
210470107055
Enter the first 5 elements:
2
7
3
9
4
Sum of 5 elements: 25
Enter the next 5 elements:
4
8
2
8
3
Sum of 10 elements: 50

Program–3 record of three players in ascending order of runs

#include<stdio.h>
#include<string.h>
struct cricketar
{
Data structure
Practical -1
210470107055
int runs;
char name[25];
char teamname[25];

}
player[50],t;

int main()

{
int i,j,n=3;
printf("enter palyer name ,team name,runs\n");
for(i=0;i<n;i++)
{
scanf("%s %s %d",&player[i].name,&player[i].teamname,&player[i].runs);
}
for(i=0;i<n;i++)
{#include<stdio.h>
#include<string.h>
struct cricketar
{
int runs;
char name[25];
char teamname[25];

}
player[50],t;

int main()

{
int i,j,n=3;
printf("enter palyer name ,team name,runs\n");
for(i=0;i<n;i++)
{
scanf("%s %s %d",&player[i].name,&player[i].teamname,&player[i].runs);
}

for(i=0;i<n;i++)
{
for(j=0;j<n-1;j++)
{
if(player[j].runs>player[j+1].runs)
{s
t=player[j];
player[j]=player[j+1];
Data structure
Practical -1
210470107055
player[j+1]=t;

}
}

printf("\n");
for(i=0;i<3;i++){

printf(" %s\t\t %s\t\t %d\n" ,player[i].name, player[i].teamname, player[i].runs);


}
}

for(j=0;j<n-1;j++)
{
if(player[j].runs>player[j+1].runs)
{
t=player[j];
player[j]=player[j+1];
player[j+1]=t;

}
}

printf("\n");
for(i=0;i<3;i++){

printf(" %s\t\t %s\t\t %d\n" ,player[i].name, player[i].teamname, player[i].runs);


}
}

 Output

enter palyer name ,team name,runs


nisha
Data structure
Practical -1
210470107055
india
45
jeni
nz
30
Mishi
india
40

jeni nz 30
mishi india 40
nisha india 45

program 4: make a calculator

#include<stdio.h>

double cal(double a,double b,int n)


{
if(1 == '+')
{
printf("addition of the number=",a+b);
Data structure
Practical -1
210470107055
}

else if (2 == '-')
{
printf("subtraction of the number=",a-b);
}

else if(3 == '*')


{
printf("multiplication of the number=",a*b);
}

else if(4 == '/')


{
printf("subtraction of the number=",a/b);
}

else ()
{
printf("invalid number");
}
}

void main()
{

double a,b;
int n;

printf("Enter the number:");


scanf("%lf",&a);

printf("Enter the number:");


scanf("%lf",&b);

printf("Enter the number 1,2,3or4:");


scanf("%d",&n);

cal( a,b,n);

}
Data structure
Practical -1
210470107055

 output

enter two numbers:3


4
enter which operation you want to do:
1.addition 2.subtraction 3.multiplication 4.division
Data structure
Practical -1
210470107055
1
addition=7
enter two numbers:2
5
enter which operation you want to do:
1.addition 2.subtraction 3.multiplication 4.division
3
multiplication=10
enter two numbers:5
3
enter which operation you want to do:
1.addition 2.subtraction 3.multiplication 4.division
2
subtraction=2
enter two numbers:40
6
enter which operation you want to do:
1.addition 2.subtraction 3.multiplication 4.division
4
division=6

You might also like