Sei sulla pagina 1di 13

ASSIGNMENT NUMBER-6

 Program name: Write a menu-driven program to perform following


function on array:
1) Print even-valued elements
2) Print odd-valued elements
3) Print sum and averages of elements
4) Print maximum and minimum element of array
5) Remove duplicate element of array
6) Print the array in reverse order
 Algorithm:
Input: Array size, array element, choice to perform an action.
Output: Even-valued elements of array, odd-valued elements of array, sum
and averages of elements of array, and minimum element of array, array
element after removing duplicate element of array, the array element in
reverse order.
Process:
Step 1: Declare three integer variable ch, n, i.
Step 2: Take a input of n.
Step 3: Declare two integer array a[] and b[] with the size of n.
Step 4: Declare a loop:
for(i=0;i<n;i++)
Step 4.1:Take element input at the different memory location a[] array.
Step 5: Repeat the step 4.1 until the condition satisfies.
Step 6: End of for() loop.
Step 7: Declare a loop:
for(i=0;i<n;i++)
Step 7.1: The elment at the ith memory location of a[] get copied to ith
memory location of b[].
Step 8: Repeat the step 7.1 until the condition dissatisfies.
Step 9: End of for() loop.
Step 10: Declare a do-while() loop
do
{
}while(1);
Step 10.1: A menu is displayed and a choice, ch is taken from user.
Step 10.2: switch case() condition is declared with user given choice,ch.
Step 10.2.1:In case 1,it calls function printeven() by passing two variable n,
a.
printeven(n,a);
Step 10.2.2: Declaration of printeven() function with return type int.
Step 10.2.2.1: An integer variable i is declared.
Step 10.2.2.2: Declare a for loop:
for(i=0;i<n;i++)
Step 10.2.2.2.1: If condition declared with the condition that the element at
ith memory location of a[] array is moduled by 2 leaves no remainder.
if(a[i]%2==0)
Step 10.2.2.2.2:It declare that element at ith location of a[] array.
Step 10.2.2.2.3:Repeat the steps 10.2.1.3.1 to 10.2.1.3.2 until the condition
dissatisfies.
Step 10.2.2.3:End of loop.
Step 10.2.3:Termination of printeven() function.
Step 10.2.4: Then break keyword is used to escape from the switch case()
condition.
Step 10.2.5:In case 2,it calls function printodd() by passing two variable n, a.
printodd(n,a);
Step 10.2.6:Declaration of printodd() function with return type int.
Step 10.2.6.1: An integer variable i is declared.
Step 10.2.6.2: Declare a for loop:
For(i=0;i<n;i++)
Step 10.2.6.2.1: If condition declared with the condition that element at ith
memory location of a[] array is moduled by 2 leaves remainder.
if(a[i]%2!=0)
Step 10.2.6.2.2:It declare that element at ith location of a[] array.
Step 10.2.6.2.3:Repeat the steps 10.2.1.3.1 to 10.2.1.3.2 until the condition
dissatisfies.
Step 10.2.6.3:End of loop.
Step 10.2.7:Termination of printodd() function.
Step 10.2.8: Then break keyword is used to escape from the switch case()
condition.
Step 10.2.9:In case 3,it calls printsumaverages() function by passing two
variable n,a:
printsumaverages(n,a)
Step 10.2.10:Declaration of printsumaverages() function with float return
type.
Step 10.2.10.1:One integer type variable i and two float variables s,avg are
declared and s is initialized with 0.
Step 10.2.10.2:A for loop is declared:
for(i=0;i<n;i++)
Step 10.2.10.2.1:s=s+a[i].
Step 10.2.10.2.2:Repeat the step 10.2.10.2.1 until the condition dissatisfies.
Step 10.2.10.3:End of loop.
Step 10.2.10.4:Decare the value of s.
Step 10.2.10.5:Then s is divided by n and stored in avg:
avg=s/n.
Step 10.2.10.6:Then it declare the value of avg.
Step 10.2.11:Termination of printsumaverages() function.
Step 10.2.12: Then break keyword is used to escape from the switch case()
condition.
Step 10.2.13:In case 4,then it calls printmaxmin() function by passing two
variable n,a.
Step 10.2.14:Declare of printmaxmin() function with int return type.
Step 10.2.14.1:Declartion of three int type variables max,min and i.
Step 10.2.14.1:max variable is initialized by 0 and min is initialised by the
element at the 0th memory location of a[] array.
Step 10.2.14.2:Declaration of a for loop:
for(i=0;i<n;i++)
Step 10.2.14.2.1:A if condition is declared that if the element at the ith
memory location of a[] array is less than min or not.
if(a[i]<min)
Step 10.2.14.2.1.1:If condition satisfies then min is initialised by the
element at the ith memory location a[] array.
Step 10.2.14.2.2: :Another if condition is declared that if the element at the
ith memory location of a[] array is greater than max or not.
if(a[i]<min)
Step 10.2.14.2.2.1:If condition satisfies then max is initialised by the
element at the ith memory location a[] array.
Step 10.2.14.2.3:Repeat the steps from 10.2.14.2.1 to 10.2.14.2.2.1 until
the condition dissatisfies.
Step 10.2.14.3:End of loop.
Step 10.2.14.4:Declare two values of max and min.
Step 10.2.15:Termination of printmaxmin() function.
Step 10.2.16: Then break keyword is used to escape from the switch case()
condition.
Step 10.2.17:In case 5,it calls the function duplicate() by passing two
variable n,a.
duplicate(n,a)
Step 10.2.18:Declaration of duplicate() function with return type int.
Step 10.2.18.1:Declaration a three int type variable i,j,k.
Step 10.2.18.2:A for loop is decared:
for(i=0;i<n;i++)
Step 10.2.18.2.1:A nested loop is declared:
for(j=i+1;j<n;j++)
Step 10.2.18.2.1.1:A if condition is declared that if the element at the ith
memory location of a[] array is equal to the element at the jth memory
location of a[] array.
Step 10.2.18.2.1.1.1:If yes,then another nested loop is declared.
for(k=j;k<n;k++)
Step 10.2.18.2.1.1.1.1:Then the element at the kth memory location of a[]
array is initialized with the element of (k+1)th memory location of a[] array.
Step 10.2.18.2.1.1.1.2:Repeat the step 10.2.18.2.1.1.2.1 until the condition
dissatisfies.
Step 10.2.18.2.1.1.2:End of loop.
Step 10.2.18.2.1.1.3:Value of n is reduced by 1.
Step 10.2.18.2.1.1.4:Repeat the step from 10.2.18.2.1.1.1 to 10.2.18.2.1.1.3
until the condition dissatisfies.
Step 10.2.18.2.1.2:End of loop.
Step 10.2.18.2.2:Repeat the step from 10.2.18.2.1 to 10.2.18.2.2.
Step 10.2.18.3:End of loop.
Step 10.2.18.4:A loop is declared:
for(i=0;i<n;i++)
Step 10.2.18.4.1:Declare the element at ith memory location of a[] array .
Step 10.2.18.4.2:Repeat the step 10.2.18.4.1 untilo the condition
dissatisfies.
Step 10.2.18.5:End of loop.
Step 10.2.19:Termination of duplicate() method.
Step 10.2.20: Then break keyword is used to escape from the switch case()
condition.
Step 10.2.21:In case 6,it calls the function reverse() by passing two variable
n,b:
reverse(n,b)
Step 10.2.22:Declaration ofreverse() function with return type int.
Step 10.2.22.1:A integer variable i is decalred
Step 10.2.22.2: A loop is declared:
for(i=n-1;i>=0;i--)
Step 10.2.23.2.1: Declare the element at ith memory location of b[] array .
Step 10.2.23.2.2: Repeat the step 10.2.23.2.1 untilo the condition
dissatisfies.
Step 10.2.23.3:End of loop.
Step 10.2.24:Termination of reverse() method.
Step 10.2.25:Then break keyword is used to escape from the switch case()
condition.
Step 10.2.26:In case 7, it exited from the main by using exit() library
function.
Step 10.2.27:In case default, it print a default message and asks user to
input choice again.
Step 10.2.28: Then break keyword is used to escape from the switch case()
condition.
Step 10.3:Termination of swith case() condition.
Step 10.4:Repeat the steps from 10.1 to 10.4until the condition
diossatisfies.
Step 11:Termination of do-while() loop with the condition 1.
Step 12:Then it returns 0 to terminate the main().
 Program code:
#include<stdio.h>
#include<stdlib.h>
int printeven(int n,int a[])
{
int i;
printf("Even-valued elements: ");
for(i=0;i<n;i++)
{
if(a[i]%2==0)
{
printf("%d ",a[i]);
}
}
printf("\n");
}
int printodd(int n,int a[])
{
int i;
printf("Odd-valued elements: ");
for(i=0;i<n;i++)
{
if(a[i]%2!=0)
{
printf("%d ",a[i]);
}
}
printf("\n");
}
float printsumaverages(int n,int a[])
{
int i;
float s;
float avg;
s=0;
for(i=0;i<n;i++)
{
s=s+a[i];
}
avg=s/n;
printf("Sum=%f\n",s);
printf("Average=%f\n",avg);
}
int printmaxmin(int n,int a[])
{
int i,max,min;
max=0,min=a[0];
for(i=0;i<n;i++)
{
if(a[i]<min)
min=a[i];
if(a[i]>max)
max=a[i];
}
printf("Maximum element=%d\n",max);
printf("Minimum element=%d\n",min);
}
int duplicate(int n,int a[])
{
int i,j,k;
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]==a[j])
{
for(k=j;k<n;k++)
{
a[k]=a[k+1];
}
n--;
}
}
}
printf("Elements after removing duplicates: ");
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
printf("\n");
}
int reverse(int n,int a[])
{
int i;
printf("Elements in reverse order: ");
for(i=n-1;i>=0;i--)
{
printf("%d ",a[i]);
}
printf("\n");
}
int main()
{
int ch;
int i;
int n;
printf("Enter size\n");
scanf("%d",&n);
int a[n];
int b[n];
printf("Enter elements\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
b[i]=a[i];
}
do
{
printf("MENU\n");
printf("Enter 1 for print even\nEnter 2 for print odd\nEnter 3
for sum and averages of elements\nEnter 4 for print maximum and
minimum elements\nEnter 5 for removing duplicate elments\nEnter 6 for
print elements in reverse order\nEnter 7 to exit\n");
printf("Enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
printeven(n,a);
break;
case 2:
printodd(n,a);
break;
case 3:
printsumaverages(n,a);
break;
case 4:
printmaxmin(n,a);
break;
case 5:
duplicate(n,a);
break;
case 6:
reverse(n,b);
break;
case 7:
exit(0);
default:
printf("Wrong choice,Enter again\n");
break;
}
}while(1);
return 0;
}
 Output:
Enter size
5
Enter elements
1
1
2
3
3
MENU
Enter 1 for print even
Enter 2 for print odd
Enter 3 for sum and averages of elements
Enter 4 for print maximum and minimum elements
Enter 5 for removing duplicate elments
Enter 6 for print elements in reverse order
Enter 7 to exit
Enter your choice
1
Even-valued elements: 2
MENU
Enter 1 for print even
Enter 2 for print odd
Enter 3 for sum and averages of elements
Enter 4 for print maximum and minimum elements
Enter 5 for removing duplicate elments
Enter 6 for print elements in reverse order
Enter 7 to exit
Enter your choice
2
Odd-valued elements: 1 1 3 3
MENU
Enter 1 for print even
Enter 2 for print odd
Enter 3 for sum and averages of elements
Enter 4 for print maximum and minimum elements
Enter 5 for removing duplicate elments
Enter 6 for print elements in reverse order
Enter 7 to exit
Enter your choice
3
Sum=10.000000
Average=2.000000
MENU
Enter 1 for print even
Enter 2 for print odd
Enter 3 for sum and averages of elements
Enter 4 for print maximum and minimum elements
Enter 5 for removing duplicate elments
Enter 6 for print elements in reverse order
Enter 7 to exit
Enter your choice
4
Maximum element=3
Minimum element=1
MENU
Enter 1 for print even
Enter 2 for print odd
Enter 3 for sum and averages of elements
Enter 4 for print maximum and minimum elements
Enter 5 for removing duplicate elments
Enter 6 for print elements in reverse order
Enter 7 to exit
Enter your choice
5
Elements after removing duplicates: 1 2 3
MENU
Enter 1 for print even
Enter 2 for print odd
Enter 3 for sum and averages of elements
Enter 4 for print maximum and minimum elements
Enter 5 for removing duplicate elments
Enter 6 for print elements in reverse order
Enter 7 to exit
Enter your choice
6
Elements in reverse order: 3 3 2 1 1
MENU
Enter 1 for print even
Enter 2 for print odd
Enter 3 for sum and averages of elements
Enter 4 for print maximum and minimum elements
Enter 5 for removing duplicate elments
Enter 6 for print elements in reverse order
Enter 7 to exit
Enter your choice
7

--------------------------------
Process exited after 59.95 seconds with return value 0
Press any key to continue . . .

Potrebbero piacerti anche