0% found this document useful (0 votes)
67 views9 pages

Week 11 Solutions

The document discusses programs using pointers and structures in C. It includes 5 programs on pointers that deal with arrays, memory allocation and reallocation. It also includes 5 programs on structures to store and manipulate student, employee, time, distance and complex number data. The programs demonstrate use of structures, functions, pointers and performing operations like addition, subtraction and multiplication on the structured data.
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)
67 views9 pages

Week 11 Solutions

The document discusses programs using pointers and structures in C. It includes 5 programs on pointers that deal with arrays, memory allocation and reallocation. It also includes 5 programs on structures to store and manipulate student, employee, time, distance and complex number data. The programs demonstrate use of structures, functions, pointers and performing operations like addition, subtraction and multiplication on the structured data.
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/ 9

WEEK-11

Programs on Pointers and Structures

1. Write a C program to read the elements of 1-d array using pointers and print them in
reverse order using pointers.
#include<stdio.h>
#include<stdlib.h>
void main()
{
int *a,n,i,n1;
printf("enter size of an array");
scanf("%d",&n);
a=(int *)calloc(n,sizeof(int));
printf("Enter Elements");
for(i=0;i<n;i++)
{
scanf("%d",(a+i));
}
printf("elements are");
for(i=n;i>0;i--)
{
printf("%d\n",*(a+i));
}
}

2. Write a C Program to read two elements dynamically using malloc() function and
interchange the two numbers using call by reference.
#include<stdio.h>
#include<stdlib.h>
int swap(int *a,int *b);
int main()
{
int *a,*b;
a=(int *)malloc(sizeof(int));
b=(int *)malloc(sizeof(int));
printf("Enter values forswapping");
scanf("%d%d",a,b);
swap(a,b);
printf("after swaping a=%d and b=%d and address are %d%d",*a,*b,a,b);
}
int swap(int *x,int *y)
{
int temp;
temp=*x;
*x=*y;
*y=temp;
}

3. Write a C Program to read and print the elements of 1-D array using calloc() memory
allocation function and reallocate memory for the array by increasing the size of the
array, read and print the elements of reallocated array.

#include<stdio.h>
#include<stdlib.h>
void main()
{
int *a,n,i,n1;
printf("enter size of an array");
scanf("%d",&n);
a=(int *)calloc(n,sizeof(int));
printf("Enter Elements");
for(i=0;i<n;i++)
{
scanf("%d",(a+i));
}
printf("elements are");
for(i=0;i<n;i++)
{
printf("%d\n",*(a+i));
}
printf("Enter new size tore allocate");
scanf("%d",&n1);
a=(int *)realloc(a,n1*sizeof(int));
printf("Enter Elements for new size");
for(i=0;i<n1;i++)
{
scanf("%d",(a+i));
}

printf("elements are");
for(i=0;i<n1;i++)
{
printf("%d\t",*(a+i));
}
}
4. Write a C Program to read and print elements of 1-D array using calloc() memory
allocation function.
#include<stdio.h>
#include<stdlib.h>
void main()
{
int *a,n,i;
printf("enter size of an array");
scanf("%d",&n);
a=(int *)calloc(n,sizeof(int));
printf("Enter Elements");
for(i=0;i<n;i++)
{
scanf("%d",(a+i));
}
printf("elements are");
for(i=0;i<n;i++)
{
printf("%d\n",*(a+i));
}
}

5. Write a C Program to print 2-D array using pointers.


#include<stdio.h>
#include<stdlib.h>
void main()
{
int a[3][3]={{1,2,3},{4,5,6},{7,8,9}},i,n,j;
printf("elements are");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\n",*(*(a+i)+j));
}
}
}

Structure programs

1. Write a C program to store name, roll number, year and marks of three subjects of n students
and print the student the name, roll number, average and grade based on average marks of the
student using structures.
#include<stdio.h>
struct student
{
char name[30];
char rollno[20];
int year;
int m1,m2,m3;
};
struct student s[20];
int main()
{
float avg;
char grade;
printf(“enter n value\n”);
scanf(“%d”,&n);
for(i=0i<n;i++)
{
printf("enter details student [%d]",i+1);
scanf("%s%s%d%d%d%d",s[i].name,s[i].rollno,&s[i].year,&s[i]
.m1,&s[i].m2,&s[i].m3);
}
for(i=0;i<n;i++)
{
avg=(float) s[i].m1+s[i].m2+s[i].m3/3;
if(avg>90)
grade=’O’;
else if(avg>75 &&avg<90)
grade=’A’;
else if(avg>75 &&avg<70)
grade=’B’;
else
grade=’F’;

printf("detais\n");
printf("\nstudent name\nroll no\nyear\naverage\nGrade\n");

printf("%s\n%s\n%d\n\n%c\n",s[i].name,s[i].rollno,s[i].year,avg,grade)
;
}

2.Write a C program to read employee details employee number, employee name, basic salary,
hra and da of n employees using structures and print employee number, employee name and
gross salary of n employees.
#include<stdio.h>
struct emp
{
char name[30];
char empno[20];
float bs,hra,da,gs;
};
struct emp e[20];
int main()
{
printf(“enter n value\n”);
scanf(“%d”,&n);
for(i=0i<n;i++)
{
printf("enter details employee [%d]",i+1);
scanf("%s%s%f%f%f",e[i].name,e[i].empno,&e[i].bs,&e[i].bs,&
e[i].hra,&e[i].da);
}
for(i=0;i<n;i++)
{
e[i].gs=e[i].bs+e[i].hra+e[i]da;
printf("Employee detais\n");
printf("\nEmployee name\nEmployeeno\nGross salary\n");
printf("%s\n%s\n%f\n",e[i].name,e[i].empno,e[i].gs);
}

3. Write a C Program to read time in hours, minutes, seconds using structures and perform the
following operations on time.
Addition of two time periods
Subtraction of two time periods
#include<stdio.h>
void add();
void sub();
void mul();

struct time
{
int hr,min,sec;
};
struct time t1,t2,t3;
main()
{
printf("enter t1");
scanf("%d%d%d",&(t1.hr),&(t1.min),&(t1.sec));
printf("enter t2");
scanf("%d%d%d",&(t2.hr),&(t2.min),&(t2.sec));
}

void add()
{
t3.hr=t1.hr+t2.hr;
t3.min=t1.min+t2.min;
t3.sec=t1.sec+t2.sec;
while(t3.sec>=60)
{
t3.min++;
t3.sec=t3.sec-60;
}
while(t3.min>=60)
{
t3.hr=t3.hr++;
t3.min=t3.min-60;
}
printf("add=%d:%d:%d",t3.hr,t3.min,t3.sec);
}
void sub()
{
if(t1.sec<t2.sec)
{
t1.min--;
t1.sec=t1.sec+60;
}
if(t1.min<t2.min)
{
t1.hr--;
t1.min=t1.min+60;
}
t3.hr=t1.hr-t2.hr;
t3.min=t1.min-t2.min;
t3.sec=t1.sec=t2.sec;
printf("sub=%d:%d:%d",t3.hr,t3.min,t3.sec);
}

4.Write a C Program to read distance in inches and feet using structures and perform the
following operations
Add two heights
Subtract two heights
#include<stdio.h>
void add();
void sub();
struct height
{
int ft;
int inch;
};
struct distance h1,h2,h3;
main()
{
printf("enter height1");
scanf("%d%d",&h1.ft.,&h1.inch);
printf("enter d2");
scanf("%d%d",&h2.ft,&h2.inch));
add();
sub();
}
void add()
{

h3.ft=h1.ft+h2.ft;
h3.inch=h1.inch+h2.inch;
while(h3.inch>=12)
{
h3.ft++;
h3.inch=h3.inch-12;
}
printf("add=%dfeet\t%dinches"h3.ft,h3.inch);
}
void sub()
{
if(h1.inch<h2.inch)
{
h1.ft--;
h1.inch=h1.inch+12;
}
h3.ft=h1.ft-h2.ft;
h3.inch=h1.inch-h2.inch;
printf("sub=%dfeet\t%dinches"h3.ft,h3.inch);

5. Write a C Program to read real and imaginary parts of a complex number using structures
and perform the following operations on complex numbers.
Add two complex numbers.
Multiply two complex numbers.
Subtract two complex numbers.

#include<stdio.h>
void add();
void sub();
void mul();
struct complex
{
float real;
float img;
};
struct complex c1,c2,c3;
int main()
{
printf("enter c1\n");
scanf("%f%f",&(c1.real),&(c1.img));
printf("enter c2\n");
scanf("%f%f",&(c2.real),&(c2.img));
}
void add()
{
c3.real=c1.real+c2.real;
c3.img=c1.img+c2.img;
printf("result=%f+i%f",c3.real,c3.img);
}
void sub()
{
c3.real=c1.real-c2.real;
c3.img=c1.img-c2.img;
printf("result=%f+i%f",c3.real,c3.img);
}

void mul()
{
c3.real=(c1.real*c2.real)-(c1.img*c2.img);
c3.img=(c1.real*c2.img)+(c2.real*c1.img);
printf("result=%f+i%f",c3.real,c3.img);
}

You might also like