Specification17:
Accept two vectors of same size, store addition of them into
another vector and print the sum. (Addition of two vectors)
Logic:
We can’t directly add any two vectors.
We need to add individual elements and store into another
vector. (c[i]=a[i]+b[i])
Program:
#include<stdio.h>
void main()
{
int i,n,a[50],b[50],c[50];
printf("Enter the size of vector:");
scanf("%d",&n);
printf("Enter %d elements into 1st vector:\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter %d elements into 2st vector:\n",n);
for(i=0;i<n;i++)
scanf("%d",&b[i]);
for(i=0;i<n;i++)
c[i]=a[i]+b[i];
printf("Sum of two vectors:\n");
for(i=0;i<n;i++)
printf("%5d",c[i]);
}
Execution:
Enter the size of vector: 6
Enter 6 elements into 1st vector:
12 23 10
8 16 22
Enter 6 elements into 2nd vector:
7 8 19
24 15 20
Sum of two vectors:
19 31 29
32 31 42
Specification18:
Accept "n" elements into an array, sort the
elements in ascending order and print the resultant array.
Logic:
- Select
element by element as key element.
- From
the next element to the key element compare the elements with the key
element.
- If
element is less than the key element then interchange with the key element
and continue until the end of array.
- Continue
the same procedure until the end of array.
Example:
Program:
#include<stdio.h>
void main()
{
int n,i,k,temp,a[50];
printf("How many elements?");
scanf("%d",&n);
printf("Enter %d elements:\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(k=0;k<n-1;k++)
for(i=k+1;i<n;i++)
if(a[i]<a[k])
{
temp=a[i];
a[i]=a[k];
a[k]=temp;
}
printf("Elements of array in ascending order:\n");
for(i=0;i<n;i++)
printf("%5d",a[i]);
}
Execution:
How many elements? 6
Enter 6 elements:
34 56 12
45 18 23
Elements of array in ascending order:
34 56 12
45 18 23
Specification19:
Accept "n" elements into an array, arrange them in
descending order and print the resultant array.
Program:
#include<stdio.h>
void main()
{
int n,i,k,temp,a[50];
printf("How many elements?");
scanf("%d",&n);
printf("Enter %d elements:\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(k=0;k<n-1;k++)
for(i=k+1;i<n;i++)
if(a[i]>a[k])
{
temp=a[i];
a[i]=a[k];
a[k]=temp;
}
printf("Elements of array in descending order:\n");
for(i=0;i<n;i++)
printf("%5d",a[i]);
}
Execution:
Execution:
How many elements? 6
Enter 6 elements:
34 56 12
45 18 23
Elements of array in ascending order:
56 45 34 23 18
12
Specification20:
Accept "n" elements into two arrays and print
whether they are equal or not.
Logic:
The flag is stored with 1 by default.
We will check for inequality by checking element by element
in both the arrays.
If inequality is found then 0 is stored in flag.
At the end of loop if flag has 1 then both the arrays are
equal otherwise not equal.
Program:
#include<stdio.h>
void main()
{
int i,n,flag,a[50],b[50];
printf("How many elements?");
scanf("%d",&n);
printf("Enter %d elements into 1st array:\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter %d elements into 2nd array:\n",n);
for(i=0;i<n;i++)
scanf("%d",&b[i]);
flag=1;
for(i=0;i<n;i++)
if(a[i]!=b[i])
{
flag=0;
break;
}
if(flag)
printf("Equals");
else
printf("Not
equal");
}
Execution:
How many elements? 5
Enter 5 elements into 1st array:
23 56 89
67 56
Enter 5 elements into 2nd array
23 56 89
67 56
Equals
Execution:
How many elements? 5
Enter 5 elements into 1st array:
23 56 89
67 56
Enter 5 elements into 2nd array
23 56 85
67 56
Not
equal
Specification17:
Accept two vectors of same size, store addition of them into
another vector and print the sum. (Addition of two vectors)
Logic:
We can’t directly add any two vectors.
We need to add individual elements and store into another
vector. (c[i]=a[i]+b[i])
Program:
#include<stdio.h>
void main()
{
int i,n,a[50],b[50],c[50];
printf("Enter the size of vector:");
scanf("%d",&n);
printf("Enter %d elements into 1st vector:\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter %d elements into 2st vector:\n",n);
for(i=0;i<n;i++)
scanf("%d",&b[i]);
for(i=0;i<n;i++)
c[i]=a[i]+b[i];
printf("Sum of two vectors:\n");
for(i=0;i<n;i++)
printf("%5d",c[i]);
}
Execution:
Enter the size of vector: 6
Enter 6 elements into 1st vector:
12 23 10
8 16 22
Enter 6 elements into 2nd vector:
7 8 19
24 15 20
Sum of two vectors:
19 31 29
32 31 42
Specification18:
Accept "n" elements into an array, sort the
elements in ascending order and print the resultant array.
Logic:
- Select
element by element as key element.
- From
the next element to the key element compare the elements with the key
element.
- If
element is less than the key element then interchange with the key element
and continue until the end of array.
- Continue
the same procedure until the end of array.
Example:
Program:
#include<stdio.h>
void main()
{
int n,i,k,temp,a[50];
printf("How many elements?");
scanf("%d",&n);
printf("Enter %d elements:\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(k=0;k<n-1;k++)
for(i=k+1;i<n;i++)
if(a[i]<a[k])
{
temp=a[i];
a[i]=a[k];
a[k]=temp;
}
printf("Elements of array in ascending order:\n");
for(i=0;i<n;i++)
printf("%5d",a[i]);
}
Execution:
How many elements? 6
Enter 6 elements:
34 56 12
45 18 23
Elements of array in ascending order:
34 56 12
45 18 23
Specification19:
Accept "n" elements into an array, arrange them in
descending order and print the resultant array.
Program:
#include<stdio.h>
void main()
{
int n,i,k,temp,a[50];
printf("How many elements?");
scanf("%d",&n);
printf("Enter %d elements:\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(k=0;k<n-1;k++)
for(i=k+1;i<n;i++)
if(a[i]>a[k])
{
temp=a[i];
a[i]=a[k];
a[k]=temp;
}
printf("Elements of array in descending order:\n");
for(i=0;i<n;i++)
printf("%5d",a[i]);
}
Execution:
Execution:
How many elements? 6
Enter 6 elements:
34 56 12
45 18 23
Elements of array in ascending order:
56 45 34 23 18
12
Specification20:
Accept "n" elements into two arrays and print
whether they are equal or not.
Logic:
The flag is stored with 1 by default.
We will check for inequality by checking element by element
in both the arrays.
If inequality is found then 0 is stored in flag.
At the end of loop if flag has 1 then both the arrays are
equal otherwise not equal.
Program:
#include<stdio.h>
void main()
{
int i,n,flag,a[50],b[50];
printf("How many elements?");
scanf("%d",&n);
printf("Enter %d elements into 1st array:\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter %d elements into 2nd array:\n",n);
for(i=0;i<n;i++)
scanf("%d",&b[i]);
flag=1;
for(i=0;i<n;i++)
if(a[i]!=b[i])
{
flag=0;
break;
}
if(flag)
printf("Equals");
else
printf("Not
equal");
}
Execution:
How many elements? 5
Enter 5 elements into 1st array:
23 56 89
67 56
Enter 5 elements into 2nd array
23 56 89
67 56
Equals
Execution:
How many elements? 5
Enter 5 elements into 1st array:
23 56 89
67 56
Enter 5 elements into 2nd array
23 56 85
67 56
Not
equal
0 comments:
Post a Comment