Specification5:
Accept "n" elements into an array and print the
minimum and maximum elements of array.
Logic:
The first element a[0] is considered as both "min"
and "max".
We will select element by element from the second element to
n-1 (a[i])
If a[i] is less than "min" then it is stored in "min"
If a[i] is greater then "max" then it is store in "max"
Once the loop is completed then "min" has the
minimum element and "max" has the maximum element.
Program:
#include<stdio.h>
void main()
{
int n,i,min,max,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(min=max=a[0],i=1;i<n;i++)
{
if(a[i]<min)
min=a[i];
if(a[i]>max)
max=a[i];
}
printf("Minimum element %d\nMaximum element %d",min,max);
}
Execution:
How many elements? 5
Enter 5 elements:
12 67 6
35 98
Minimum element 6
Maximum element 98
Specification6:
Accept "n" elements into an array copy the element
onto another array and print the resultant array.
Note: An array cannot be directly copied on to another array
using assigning operator like normal variable.
We need to copy element by element using a loop.
Logic:
a
Program:
#include<stdio.h>
void main()
{
int n,i,a[50],b[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(i=0;i<n;i++)
b[i]=a[i];
printf("Resultant array elements are:\n");
for(i=0;i<n;i++)
printf("%5d",b[i]);
}
Execution:
How many elements? 5
Enter 5 elements:
12 67 6
35 98
Resultant array elements are:
12 67 6
35 98
Specification7:
Accept "n" elements into an array reverse the
elements and print the resultant array.
n-1
Logic:
Program:
#include<stdio.h>
void main()
{
int i,j,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(i=0,j=n-1;i<j;i++,j--)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
printf("Elements in reverse order:\n");
for(i=0;i<n;i++)
printf("%5d",a[i]);
}
Execution:
How many elements? 7
Enter 7 elements:
12 67 54
34 98 27
15
Elements in reverse order:
15 27 98
34 54 67
12
Specification8:
Accept "n" elements into an array and print whether
the array is a palindrome array or not.
Logic:
Palindrome array is an array where original array must be
equal to its reverse array
Where first and last, second and last before…….until middle
must be equal.
Program:
#include<stdio.h>
void main()
{
int i,j,a[50],n,flag;
printf("How many elements?");
scanf("%d",&n);
printf("Enter %d elements:\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
flag=1;
for(i=0,j=n-1;i<j;i++,j--)
if(a[i]!=a[j])
{
flag=0;
break;
}
if(flag)
printf("Palindrome
array");
else
printf("Not a
palindrome array");
}
Execution1:
How many elements? 7
Enter 7 elements:
12 27 54
34 54 27
12
Palindrome array
Execution2:
How many elements? 7
Enter 7 elements:
12 27 54 34
54 18 12
Not a palindrome array
Example explained:
By default the value of "flag" is 1
Elements from first using "i" and last using "j"
are compared until middle (i<j).
We check for inequality.
If there is any inequality then "flag" is stored
with 0.
At the end of loop if "flag" has 1 (no inequality)
then it is a palindrome array otherwise it is not a palindrome array. I
Specification9:
Accept "n" elements into an array append a new
element to the array and print the resultant array.
Logic:
Appending is adding a new element at the end.
Because a[n-1] is the last element, new element must be
added at a[n]
Now
the number of elements would be n+1
n
Program:
#include<stdio.h>
void main()
{
int a[50],i,n;
printf("How many elements?");
scanf("%d",&n);
printf("Enter %d elements:\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter the new element:");
scanf("%d",&a[n]);
n++; /* number of elements are incremented by 1 */
printf("The resultant array is :\n");
for(i=0;i<n;i++)
printf("%5d",a[i]);
}
Execution:
How many elements? 7
Enter 5 elements:
12 67 54 34 98
27 15
Enter the new element: 68
The resultant array is
12 67 54
34 98 27
15 68
Specification5:
Accept "n" elements into an array and print the
minimum and maximum elements of array.
Logic:
The first element a[0] is considered as both "min"
and "max".
We will select element by element from the second element to
n-1 (a[i])
If a[i] is less than "min" then it is stored in "min"
If a[i] is greater then "max" then it is store in "max"
Once the loop is completed then "min" has the
minimum element and "max" has the maximum element.
Program:
#include<stdio.h>
void main()
{
int n,i,min,max,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(min=max=a[0],i=1;i<n;i++)
{
if(a[i]<min)
min=a[i];
if(a[i]>max)
max=a[i];
}
printf("Minimum element %d\nMaximum element %d",min,max);
}
Execution:
How many elements? 5
Enter 5 elements:
12 67 6
35 98
Minimum element 6
Maximum element 98
Specification6:
Accept "n" elements into an array copy the element
onto another array and print the resultant array.
Note: An array cannot be directly copied on to another array
using assigning operator like normal variable.
We need to copy element by element using a loop.
Logic:
a
Program:
#include<stdio.h>
void main()
{
int n,i,a[50],b[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(i=0;i<n;i++)
b[i]=a[i];
printf("Resultant array elements are:\n");
for(i=0;i<n;i++)
printf("%5d",b[i]);
}
Execution:
How many elements? 5
Enter 5 elements:
12 67 6
35 98
Resultant array elements are:
12 67 6
35 98
Specification7:
Accept "n" elements into an array reverse the
elements and print the resultant array.
n-1
Logic:
Program:
#include<stdio.h>
void main()
{
int i,j,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(i=0,j=n-1;i<j;i++,j--)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
printf("Elements in reverse order:\n");
for(i=0;i<n;i++)
printf("%5d",a[i]);
}
Execution:
How many elements? 7
Enter 7 elements:
12 67 54
34 98 27
15
Elements in reverse order:
15 27 98
34 54 67
12
Specification8:
Accept "n" elements into an array and print whether
the array is a palindrome array or not.
Logic:
Palindrome array is an array where original array must be
equal to its reverse array
Where first and last, second and last before…….until middle
must be equal.
Program:
#include<stdio.h>
void main()
{
int i,j,a[50],n,flag;
printf("How many elements?");
scanf("%d",&n);
printf("Enter %d elements:\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
flag=1;
for(i=0,j=n-1;i<j;i++,j--)
if(a[i]!=a[j])
{
flag=0;
break;
}
if(flag)
printf("Palindrome
array");
else
printf("Not a
palindrome array");
}
Execution1:
How many elements? 7
Enter 7 elements:
12 27 54
34 54 27
12
Palindrome array
Execution2:
How many elements? 7
Enter 7 elements:
12 27 54 34
54 18 12
Not a palindrome array
Example explained:
By default the value of "flag" is 1
Elements from first using "i" and last using "j"
are compared until middle (i<j).
We check for inequality.
If there is any inequality then "flag" is stored
with 0.
At the end of loop if "flag" has 1 (no inequality)
then it is a palindrome array otherwise it is not a palindrome array. I
Specification9:
Accept "n" elements into an array append a new
element to the array and print the resultant array.
Logic:
Appending is adding a new element at the end.
Because a[n-1] is the last element, new element must be
added at a[n]
Now
the number of elements would be n+1
n
Program:
#include<stdio.h>
void main()
{
int a[50],i,n;
printf("How many elements?");
scanf("%d",&n);
printf("Enter %d elements:\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter the new element:");
scanf("%d",&a[n]);
n++; /* number of elements are incremented by 1 */
printf("The resultant array is :\n");
for(i=0;i<n;i++)
printf("%5d",a[i]);
}
Execution:
How many elements? 7
Enter 5 elements:
12 67 54 34 98
27 15
Enter the new element: 68
The resultant array is
12 67 54
34 98 27
15 68
0 comments:
Post a Comment