Problem:  WAP to perform following actions on an array entered by the user :

i) Print the even-valued elements

ii) Print the odd-valued elements

iii) Calculate and print the sum and average of the elements of array

iv) Print the maximum and minimum element of array

v) Remove the duplicates from the array

vi) Print the array in reverse order

The program should present a menu to the user and ask for one of the options. The

 menu should also include options to re-enter array and to quit the program. 

Source Code:

  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<stdlib.h>

  4. void even();  //User defined function to check the even elements of the array
  5. void odd();   //User defined function to check the odd elements of the array
  6. void sum_and_avg();     //User defined function to find the sum and average of elements of the array
  7. void max_and_min();    //User defined function to the maximum and minimum element of the array
  8. void remove_duplicates();   //User defined function to remove duplicate elements of the array
  9. void reverse();                 //User defined function to reverse the array

  10. int main()
  11. {
  12.     int option;           //Declaring variables 
  13.     char choice;      //Declaring variable
  14.     do{
  15.     //Displaying Menu options
  16.                     printf("\n _______________MENU__________");
  17.      
  18.               printf("\n 1. Print the even-valued elements of the array");

  19.     printf("\n 2. Print the odd-valued elements of the array");

  20.     printf("\n 3. Calculate and print the sum and average of the elements of array");

  21.     printf("\n 4. Print the maximum and minimum element of array");

  22.     printf("\n 5. Remove the duplicates from the array");

  23.     printf("\n 6. Print the array in reverse order");
  24.    
  25.     printf("\n 7. Exit");
  26.     printf("\n\n  Enter Your Choice : ");//Inputting the choice from the user
  27.       scanf("%d", &option);
  28.     switch(option)//Checking the entered choice
  29.     {
  30.     case 1:  
  31.         even();//Calling the function
  32.         break;
  33.                case 2:  
  34.         odd();//Calling the function
  35.         break;
  36. case 3:  
  37.                   sum_and_avg();//Calling the function
  38.               break;
  39. case 4:  
  40.                      max_and_min();//Calling the function
  41.                      break;
  42.       case 5: 
  43.                            remove_duplicates();//Calling the function
  44.                            break;
  45.       case 6: 
  46.                       reverse();//Calling the function
  47.                       break;
  48. case 7:
  49.         printf("Exiting Program........");
  50.         exit(1);        
  51.                default:
  52.             printf("\n Invalid Choice !");
  53.             break;
  54. }
  55. printf("Do you want to continue (Y/N)? ");
  56. choice=getch();
  57. }
  58. while(choice=='Y' || choice=='y');//repeat the loop until user wants to exit the program 
  59. }


  60. //Defining the function even()
  61. void even()
  62. {  
  63.               char op;
  64.       int  i, num,even=0;//Initializations and declaration of variables
  65.  
  66.     printf("\n  Enter the size of an array : ");//Inputtin the size of the array
  67.     scanf("%d", &num);
  68.    
  69.     int array[num];//Initializing array of same size
  70.     printf("\n  Enter the elements of the array : ");//Inoutting the elements in the array
  71.     for (i = 0; i < num; i++)
  72.     {
  73.                   scanf("%d", &array[i]);
  74.                }
  75.               printf("\n  Even numbers in the array are : ");//Displaying the even elements of the array
  76.               for (i = 0; i < num; i++)
  77.               {
  78.                   if (array[i] % 2 == 0)
  79.                      {
  80.             even++;
  81.                           printf("%d \t", array[i]);
  82.                      }
  83.         }
  84.     
  85.      
  86. }//End of function even()

  87. //Defining the function odd()
  88. void odd()
  89. {
  90.                char op;//Initialization of variable
  91.   int i, num,odd=0;//Initialization and Declaration of Variables
  92.  
  93.     printf("\n  Enter the size of an array : ");//Initializations and declaration of variables
  94.     scanf("%d", &num);
  95.    
  96.     int array[num];//Initializing array of same shirt
  97.     printf("\n  Enter the elements of the array : ");//Inputtin the size of the array
  98.     for (i = 0; i < num; i++)
  99.     {
  100.                   scanf("%d", &array[i]);
  101.                }
  102.                 
  103.                 printf("\n  Odd numbers in the array are: ");//Displaying the odd elements of the array
  104.         
  105. for (i = 0; i < num; i++)
  106.                {
  107.                       if (array[i] % 2 != 0)
  108.                     {
  109.             odd++;
  110.                           printf("%d \t", array[i]);
  111.                      }
  112.               }
  113.    
  114. }//End of function odd()



  115. //Defining the function sum_and_avg()
  116. void sum_and_avg()
  117. {
  118.    char op;
  119.    int size, i, sum = 0;//Initialization and Declaration of Variables

  120.     printf("\n  Enter the size of the arrays : ");//Inputting the size of the array
  121.     scanf("%d", &size);

  122.     int arr[size];//Initializing the array of entered size
  123.     printf("\n  Enter the elements of the array :");//Inputting the elements in the array
  124.     
  125.     //Calculating the Sum of elements of the array
  126.     for (i = 0; i < size; i++)
  127.     {
  128.         scanf("%d", &arr[i]);
  129.         sum += arr[i];
  130.     }

  131.     printf("\n  The sum of the array is : %d \n", sum);//Displaying the sum of the elements of the array
  132.     printf("\n  The average of the array is : %f", (float)sum / size);//Displaying the average of the     elements                                                                                                                                                                                  of the array
  133.     
  134. }//End of function sum_and_avg()


  135. //Defining the function max_min()
  136. void max_and_min()
  137. {
  138.     char op;
  139.      int i, max, min, size;//Declaration of Variables

  140.     printf("\n  Enter the size of the arrays : ");//Inputting the size of the array
  141.     scanf("%d", &size);

  142.     int arr[size];
  143.     printf("\n  Enter the elements of the array : ");//Inputting the elements in the array
  144.     
  145. for(i=0; i<size; i++)
  146.     {
  147.         scanf("%d", &arr[i]);
  148.     }


  149.     /* Assume first element as maximum and minimum */
  150.     max = arr[0];
  151.     min = arr[0];

  152.     /* Find maximum and minimum in all array elements*/
  153.     for(i=1; i<size; i++)
  154.     {
  155.         /* If current element is greater than max */
  156.         if(arr[i] > max)
  157.         {
  158.             max = arr[i];
  159.         }

  160.         /* If current element is smaller than min */
  161.         if(arr[i] < min)
  162.         {
  163.             min = arr[i];
  164.         }
  165.     }

  166.     /* Print maximum and minimum element */
  167.     printf("\n  Maximum element = %d\n", max);
  168.     printf("\n  Minimum element = %d", min);
  169.     
  170.     

  171. }//end of function max_and_min()


  172. //Defining the function remove_duplicates()
  173. void remove_duplicates()
  174. {
  175.     char op;
  176. int i, j, k, n;//Declaration of variables
  177.     
  178.     printf("\n   Enter array size: ");//Inputting the size of the array
  179.     scanf("%d", &n);
  180.     
  181.     int arr[n];//Initializing an array of entered size
  182.     
  183.     printf("\n   Enter %d array elements: ", n);//Entering the elements into the array
  184.    
  185.     for(i = 0; i < n; i++)
  186.     {
  187.         scanf("%d", &arr[i]);
  188.     }

  189.     printf("\n   Original array is: ");//Displaying the Original array
  190.     for(i = 0; i < n; i++)
  191.     {
  192.         printf(" %d", arr[i]);
  193.     }

  194.     printf("\n   New array is: ");//Displayin the new array after removing duplicate items
  195.     
  196.     //Calculating the duplicate items and deleting those elements
  197.     for(i = 0; i < n; i++)
  198.     {
  199.         for(j = i+1; j < n; )
  200.         {
  201.             if(arr[j] == arr[i])
  202.             {
  203.                 for(k = j; k < n; k++)
  204.                 {
  205.                     arr[k] = arr[k+1];
  206.                 }
  207.                 n--;
  208.             }
  209.             else
  210.             {
  211.                 j++;
  212.             }
  213.         }
  214.     }

  215.     for(i = 0; i < n; i++)
  216.     {
  217.         printf("%d ", arr[i]);//Displaying elements
  218.     }
  219.    
  220. }//end of function remove_duplicates()


  221. void reverse()
  222. {
  223.     char op;//Declarations of Variables
  224.     int size, i; //Declarations of Variables

  225.     /* Input size of array */
  226.     printf("\n  Enter size of the array : ");
  227.     scanf("%d", &size);
  228.     
  229.     int arr[size];
  230.     /* Input array elements */
  231.     printf("\n  Enter elements in array: ");
  232.     for(i=0; i<size; i++)
  233.     {
  234.         scanf("%d", &arr[i]);
  235.     }
  236.     /*Print array in reversed order*/
  237.     printf("\n  Array in reverse order: ");
  238.     for(i = size-1; i>=0; i--)
  239.     {
  240.         printf("%d ", arr[i]);
  241.     }   
  242. }


Output:


_______________MENU__________

 1. Print the even-valued elements of the array

 2. Print the odd-valued elements of the array

 3. Calculate and print the sum and average of the elements of array

 4. Print the maximum and minimum element of array

 5. Remove the duplicates from the array

 6. Print the array in reverse order

 7. Exit

 

  Enter Your Choice : 1

 

  Enter the size of an array : 5

 

  Enter the elements of the array : 10

11

12

13

14

 

  Even numbers in the array are : 10    12      14      Do you want to continue

(Y/N)?

 _______________MENU__________

 1. Print the even-valued elements of the array

 2. Print the odd-valued elements of the array

 3. Calculate and print the sum and average of the elements of array

 4. Print the maximum and minimum element of array

 5. Remove the duplicates from the array

 6. Print the array in reverse order

 7. Exit

 

  Enter Your Choice : 2

 

  Enter the size of an array : 5

 

  Enter the elements of the array : 10

12

11

13

14

 

  Odd numbers in the array are: 11      13      Do you want to continue (Y/N)?

 

 _______________MENU__________

 1. Print the even-valued elements of the array

 2. Print the odd-valued elements of the array

 3. Calculate and print the sum and average of the elements of array

 4. Print the maximum and minimum element of array

 5. Remove the duplicates from the array

 6. Print the array in reverse order

 7. Exit

 

  Enter Your Choice : 3

 

  Enter the size of the arrays : 5

 

  Enter the elements of the array :10

11

12

13

14

 

  The sum of the array is : 60

 

  The average of the array is : 12.000000Do you want to continue (Y/N)?

_______________MENU__________

 1. Print the even-valued elements of the array

 2. Print the odd-valued elements of the array

 3. Calculate and print the sum and average of the elements of array

 4. Print the maximum and minimum element of array

 5. Remove the duplicates from the array

 6. Print the array in reverse order

 7. Exit

 

  Enter Your Choice : 4

 

  Enter the size of the arrays : 5

 

  Enter the elements of the array : 10

11

12

13

14

 

  Maximum element = 14

 

  Minimum element = 10 Do you want to continue (Y/N)?

_______________MENU__________

 1. Print the even-valued elements of the array

 2. Print the odd-valued elements of the array

 3. Calculate and print the sum and average of the elements of array

 4. Print the maximum and minimum element of array

 5. Remove the duplicates from the array

 6. Print the array in reverse order

 7. Exit

  Enter Your Choice : 5

   Enter array size: 5

   Enter 5 array elements: 10

11

12

13

14

 

   Original array is:  10 11 12 13 14

   New array is: 10 11 12 13 14 Do you want to continue (Y/N)?

1. Print the even-valued elements of the array

 2. Print the odd-valued elements of the array

 3. Calculate and print the sum and average of the elements of array

 4. Print the maximum and minimum element of array

 5. Remove the duplicates from the array

 6. Print the array in reverse order

 7. Exit

 

  Enter Your Choice : 6

 

  Enter size of the array : 5

 

  Enter elements in array: 10

11

12

13

14

 

  Array in reverse order: 14 13 12 11 10 Do you want to continue (Y/N)?

 _______________MENU__________

 1. Print the even-valued elements of the array

 2. Print the odd-valued elements of the array

 3. Calculate and print the sum and average of the elements of array

 4. Print the maximum and minimum element of array

 5. Remove the duplicates from the array

 6. Print the array in reverse order

 7. Exit

 

  Enter Your Choice : 7

Exiting Program........


1 comment: