Tuesday, March 22, 2011

Structures and Unions

Create two structures named metric and British which store the values of distances. The matric structure stores the values in metres and centimetres and the British structure stores the values in feet and inches. Write a program that reads value for the structure variables and adds values contained in one variable of metric to the contents of another variables of British. The program should display the result in the format of feet and inches or metres and centimeters as required.

#include <stdio.h>
struct metric
{
int metre;
int centimetre;
};
struct british
{
int feet;
int inches;
};
void main()
{
int inch,CM,centimetre;
struct metric met;
struct british bri;
clrscr();
printf("Enter distance in metre and centimetre m cm:");
scanf("%d %d",&met.metre,&met.centimetre);
printf("Enter the distance in feet and inches f in:");
scanf("%d %d",&bri.feet,&bri.inches);
inch=bri.inches+bri.feet*12;
centimetre=met.centimetre+met.metre*100;
CM=inch*2.54;
met.metre=0;
met.metre=(CM+centimetre)/100;
met.centimetre=0;
met.centimetre=(CM+centimetre)%100;
printf("Addition of these distance: %d metre and %d centimetre",met.metre,met.centimetre);
getch();
}

OUTPUT
Enter distance in metre and centimetre m cm: 25 80
Enter the distance in feet and inches f in: 10 10
Addition of these distance: 29 metre and 10 centimetre

Structures and Unions

Add a function to the previous program that accepts two vectors as input parameters and return the addition of two vectors.

#include <stdio.h>
struct vector
{
int value[30];
}vec1,vec2,vec3;
struct vector Add(struct vector vec1,struct vector vec2,int n);
int i,n;
void main()
{
struct vector vec4;
int x;
clrscr();
printf("Enter starting value of first vector:");
scanf("%d",&x);
printf("Enter no. of Values:");
scanf("%d",&n);
for(i=1;i<=n;i++)
vec1.value[i]=x*i;
printf("Enter starting element of second vector:");
scanf("%d",&x);
for(i=1;i<=n;i++)
vec2.value[i]=x*i;
vec4=Add(vec1,vec2,n);
printf("First vector\n");
for(i=1;i<=n;i++)
printf("%d,",vec1.value[i]);
printf("\nSecond vector\n");
for(i=1;i<=n;i++)
printf("%d,",vec2.value[i]);
printf("\nAddition of these vector\n");
for(i=1;i<=n;i++)
printf("%d,",vec4.value[i]);
getch();
}
struct vector Add(struct vector vec1,struct vector vec2,int n)
{
for(i=1;i<=n;i++)
vec3.value[i]=vec1.value[i]+vec2.value[i];
return(vec3);
}

OUTPUT
Enter starting value of first vector: 10
Enter no. of Values: 10
Enter starting element of second vector: 10
First vector
10,20,30,40,50,60,70,80,90,100
Second vector
10,20,30,40,50,60,70,80,90,100
Addition of these vector
20,40,60,80,100,120,140,160,180,200

Structures and Unions

Define a structure to represent a vector ( a series of integer values) and write a modular program to perform the following tasks:
  • To create a vector
  • To modify the value fo a given element
  • To multiply by a scalar value
  • To display the vector in the form
             (10, 20, 30, ........... . .. . ..... ....)

#include <stdio.h>
struct vector
{
int value[20];
}vec;
void main()
{
int i,n,x;
clrscr();
printf("Enter the starting value:");
scanf("%d",&x);
printf("Enter no. of value:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
vec.value[i]=x*i;
printf("%d,",vec.value[i]);
}
getch();
}

OUTPUT
Enter the starting value: 10
Enter no. of value: 10
10,20,30,40,50,60,70,80,90,100