Вы находитесь на странице: 1из 6

ABHISHEK KANSAL

1410991703
EXP 1 :- Write a Program to calculate the average of value stored in three
integer variable
and display it correctly upto two decimal places .
CODE :

//Abhishek Kansal ECE 140991703


#include<stdio.h>
int main(){
int a,b,c;
float avg;
printf("Enter 3 Numbers : ");
scanf("%d %d %d",&a,&b,&c);
avg=(float)(a+b+c)/3;
printf("Average = %0.2f",avg);
return 0;
}
OUTPUT :

EPC LAB FILE Page 1


ABHISHEK KANSAL

1410991703
EXP 2 :- Write a swap() function that uses pass-by-Reference to swap
values of two variables
declared in the main function
CODE :

//Abhishek Kansal ECE 140991703


#include<stdio.h>
void swap(int *,int *);
int main(){
int a,b;
printf("Enter a : ");
scanf(" %d",&a);
printf("Enter b : ");
scanf(" %d",&b);
swap(&a,&b);
printf("After swap\na : %d\nb = %d ",a,b);
return 0;
}

void swap(int *a,int *b){


int temp=*a;
*a=*b;
*b=temp;
}
OUTPUT :

EPC LAB FILE Page 2


ABHISHEK KANSAL

1410991703
EXP 3 :- Write a program that reads two strings 's1' and 's2' from keyboard
and counts the
occurrence of string 's2' from 's1'
CODE :
//Abhishek Kansal ECE 140991703
#include <iostream>
#include<cstring>
using namespace std;
int main()
{
char s1[100], s2[100];
int count = 0, count1 = 0;
int i, j, l1, l2;
cout<<"Enter the S1:"<<endl;
cin.get(s1,100);
l1 = strlen(s1);
s1[l1]='m';s1[l1+1]=NULL;
cout<<"Enter the S2:"<<endl;
cin>>s2;
l2 = strlen(s2);
for (i = 0; i < l1;)
{
j = 0;
count = 0;
while ((s1[i] == s2[j]))
{
count++;
i++;
j++;
}
if (count == l2)
{

EPC LAB FILE Page 3


ABHISHEK KANSAL

1410991703
count1++;
count = 0;
}
else
i++;
}
cout<<"substring occurs in main string "<<count1<<" times";
return 0;
}
OUTPUT :

EPC LAB FILE Page 4


ABHISHEK KANSAL

1410991703
EXP 4 :- Write a program that allocates memory dynamically to a 2D
array , reads values in it
from keyboard and then applies the transpose function on it.
verify the result by
displaying 2D array on screen
CODE :

//Abhishek Kansal ECE 140991703


#include <iostream>
#include<cstdlib>
using namespace std;
int main()
{
int **array;
int i,j;
int nrows,ncolumns;
cin>>nrows>>ncolumns;
int trans[ncolumns][nrows];
array = (int**)malloc(nrows * sizeof(int *));
for(i = 0; i < nrows; i++)
{
array[i] =(int*) malloc(ncolumns * sizeof(int));
if(array[i] == NULL)
{
exit;
}
}
for (i = 0; i < nrows; i++)
{for (j = 0; j < ncolumns; j++)
cin>>array[i][j];
}
cout<<"original 2-D array:"<<endl;
for (i = 0; i < nrows; i++)

EPC LAB FILE Page 5


ABHISHEK KANSAL

1410991703
{
for (j = 0; j < ncolumns; j++)
{ cout<<array[i][j]<<" ";}
cout<<endl;
}
for (i = 0; i < nrows; i++)
{for (j = 0; j < ncolumns; j++)
{trans[j][i]=array[i][j];
}
}
cout<<"transposed 2-D array:"<<endl;
for (i = 0; i < nrows; i++)
{
for (j = 0; j < ncolumns; j++)
{ cout<<trans[i][j]<<" ";}
cout<<endl;
}
return 0;
}
OUTPUT :

EPC LAB FILE Page 6

Вам также может понравиться