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

DA2 Solution

This document contains the code submissions for 10 programming assignments. Each assignment includes a C code snippet to solve a given problem, and the expected output. The problems involve concepts like pointers, arrays, structures, matrices and calculating dates. For each problem, the code provided implements the logic to solve it using appropriate C functions and syntax.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

DA2 Solution

This document contains the code submissions for 10 programming assignments. Each assignment includes a C code snippet to solve a given problem, and the expected output. The problems involve concepts like pointers, arrays, structures, matrices and calculating dates. For each problem, the code provided implements the logic to solve it using appropriate C functions and syntax.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Parth Sunil Chavan

20BCI0055
CSE2010
Slot L25 + L26
Lab Assessment 2

1) Write a program to find biggest among three numbers using pointer.


C Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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:

3) Write a program to swap value of two variables using pointer.


C code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int swap(int *a,int*b)


{
int temp;
temp=*a;
*a=*b;
*b=temp;
}
int main()
{
int x,y;
x=50;
y=70;
swap(&x,&y);
printf("x=%d y=%d",x,y);
return 0;
}

Output:

4) Write a program to read a sentence and count the number of characters


&words in that
sentence.

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:

9)Write a c program to read multiple lines of a text as individual strings who's


max length is unspecified maintain a pointer to each string within a one
dimensional array of pointers then determine the no. of vowels consonants,
digits, white spaces and other characters of each line finally determine the avg
no. Vowels per line and consonants per line.

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

for (int i = 0; str[i] != '\0'; ++i) {


str[i] = tolower(str[i]);
if (str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' ||
str[i] == 'u') {
++v;
}
else if ((str[i] >= 'a' && str[i] <= 'z')) {
++c;
}
else if (str[i] >= '0' && str[i] <= '9') {
++d;
}
else if (str[i] == ' ') {
++s;
}
}

printf("Vowels: %d", v);


printf("\nConsonants: %d", c);
printf("\nDigits: %d", d);
printf("\nWhite spaces: %d", s);

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

*m2 = ((*m2) + 9) % 12;


*y2 = (*y2) - (*m2) / 10;
D2= 365*(*y2) + (*y2)/4 - (*y2)/100 + (*y2)/400 + ((*m2)*306 + 5)/10 + ( (*d2)
- 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:

You might also like