DA2 Solution
DA2 Solution
20BCI0055
CSE2010
Slot L25 + L26
Lab Assessment 2
int main()
{
int a=5,b=7,c=1;
int *p,*q,*r;
p=&a;
q=&b;
r=&c;
int s;
s=(*p>*q)?(*p>*r?*p:*r):(*q>*r?*q:*r);
printf("%d",s);
Output:
2) Write a program to find the sum of all the elements of an array using
pointers.
C code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int arr[]={1,2,3,7,5,21};
int *ptr;
ptr=arr;
int s,sum=0;
s=sizeof(arr)/sizeof(arr[0]);
for(int i=0;i<s;i++)
{
sum=sum+*(ptr+i);
}
printf("%d",sum);
return 0;
}
Output:
Output:
C code:
#include<stdio.h>
#include<stdlib.h>
int main()
{
char s[100];
int i=0,w=0,c=0;
printf("Enter String: ");
gets(s);
while (s[i] !='\0')
{
if (s[i] == ' ')
{
w++;
c++;
}
else
c++;
i++;
}
printf("Number of characters: %d\n", c);
printf("Number of words: %d",w+1);
return 0;
}
Output:
5) Write a program to read a sentence & delete all the white spaces. Replace
all “.” by “:”.
C Code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
char s[100];
gets(s);
int l=sizeof(s)/sizeof(s[0]);
for(int i=0;i< l;i++)
{
if(s[i] == ' ')
{
for(int j=i;j<l;j++)
{
s[j]=s[j+1];
}
l--;
}
}
l=sizeof(s)/sizeof(s[0]);
for(int i=0;i<l;i++)
{
if(s[i]=='.')
{
s[i]=':';
}
}
printf("%s",s);
}
Output:
6) Write a program to read RollNo, Name, Address, Age & marks in physics, C,
math in 1st semester of three students in B.Tech and display the student details
with average marks achieved.(use structures)
C code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct students
{
int rollno;
int age;
char name[30];
char addr[100];
float p_marks,c_marks,m_marks;
};
int main()
{
struct students s[3];
for(int i=0;i<3;i++)
{
scanf("%d",&s[i].rollno);
scanf("%s %s",s[i].name,s[i].addr);
scanf("%d %f %f
%f",&s[i].age,&s[i].p_marks,&s[i].c_marks,&s[i].m_marks);
}
for(int i=0;i<3;i++)
{
printf("\nRoll no %d\n",s[i].rollno);
printf("Name %s\nAddress %s\n",s[i].name,s[i].addr);
printf("Age %d\nAvg. Marks
%.2f\n",s[i].age,(s[i].p_marks+s[i].c_marks+s[i].m_marks)/3);
}
return 0;
}
Output:
7) Write a C program to compute addition of two matrix (3x3) using array of
pointers.
C code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int *arr1[3],*arr2[3];
for(int i=0;i<3;i++)
{
*(arr1+i)=(int*)malloc(3*sizeof(int));
*(arr2+i)=(int*)malloc(3*sizeof(int));
}
printf("Enter elements of matrix 1:\n");
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
scanf("%d",(*(arr1+i)+j));
}
}
printf("Enter elements of matrix 2:\n");
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
scanf("%d",(*(arr2+i)+j));
}
}
printf("Addition of two matrices:\n");
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
int r=*(*(arr1+i)+j)+*(*(arr2+i)+j);
printf("%d ",r);
}
printf("\n");
}
return 0;
}
Output:
8)Write a C program to demonstrate multiplication of two (mxn) matrices
using array of pointers.
C code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int r1,c1,r2,c2;
printf("Enter the no. of rows and columns for matrix1:");
scanf("%d %d",&r1,&c1);
int *arr1[r1];
for(int i=0;i<r1;i++)
{
*(arr1+i)=(int*)malloc(c1*sizeof(int));
}
printf("Enter elements of matrix 1:\n");
for(int i=0;i<r1;i++)
{
for(int j=0;j<c1;j++)
{
scanf("%d",(*(arr1+i)+j));
}
}
printf("Enter the no. of rows and columns for matrix2:");
scanf("%d %d",&r2,&c2);
int *arr2[r2];
for(int i=0;i<r2;i++)
{
*(arr2+i)=(int*)malloc(c2*sizeof(int));
}
printf("Enter elements of matrix 2:\n");
for(int i=0;i<r2;i++)
{
for(int j=0;j<c2;j++)
{
scanf("%d",(*(arr2+i)+j));
}
}
printf("Multiplication of two matrices:\n");
for(int i=0;i<r1;i++)
{
for(int j=0;j<c2;j++)
{
int r=0;
for(int k=0;k<c1;k++)
{
r=r+(*(*(arr1+i)+k))*(*(*(arr2+k)+j));
}
printf("%d ",r);
}
printf("\n");
}
return 0;
}
Output:
C code:
#include <stdio.h>
# include<ctype.h>
int main() {
char str[100];
int v=0,c=0,d=0,s=0;
printf("Enter text: ");
int *pstr[10];
fgets(str, sizeof(str), stdin);
return 0;
}
Output:
10) Write a c program to calculate no of days between given 2 dates using
pointers.
C code:
#include <stdlib.h>
# include<stdio.h>
int difference_btwn_dates(int *d1,int *m1, int *y1, int *d2, int *m2,int *y2)
{
int D1,D2;
*m1 = (*m1 + 9) % 12;
*y1 = *y1 - *m1 / 10;
D1= 365*(*y1) + (*y1)/4 - (*y1)/100 + (*y1)/400 + ((*m1)*306 + 5)/10 + ( (*d1)
- 1 );
return D2 - D1;
}
int main()
{
int diff;
int d1,m1,y1,d2,m2,y2;
printf("Enter the start date:\n");
scanf("%d %d %d",&d1,&m1,&y1);
printf("Enter the end date:\n");
scanf("%d %d %d",&d2,&m2,&y2);
diff=difference_btwn_dates(&d1,&m1,&y1,&d2,&m2,&y2);
printf("Number of days between the two dates: %d days",diff);
return 0;
}
Output: