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
#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
No comments:
Post a Comment