Advertisement
shammya

class -1 loop and array

May 10th, 2021
689
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.34 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. /*
  6. 1. Accept ‘m’ integer number and store them in an array arr[m],
  7. 2. then divide it into two arrays odd and even.
  8. 3. Write a program in the programming language C and
  9. 4.(1) display the contents of odd array and even array
  10. 5.(2) display the maximum number of arr[m]
  11. 6.(3) display the maximum number of even array
  12. 7.(4) display the multiplication value of minimum and maximum numbers of odd array.
  13.  
  14. */
  15.  
  16. int main(){
  17.  
  18.    // # 1 task
  19.   int array_size,odd_size=0,even_size=0,max_num_array = -1,max_num_even=-1,min_num_odd=10000,max_num_odd=-1;
  20.   // array size taken
  21.   printf("Enter arry size \n");
  22.   scanf("%d",&array_size) ;
  23.  
  24.   //array creation
  25.   int arr[array_size];
  26.  
  27.   printf("Enter arry elements \n");
  28.  
  29.     // array input taken
  30.     int i;
  31.     for(i=0;i<array_size;i++)
  32.     {
  33.         scanf("%d",&arr[i]);
  34.         //odd-even check
  35.         if(arr[i]%2==0)
  36.             even_size++;
  37.         else
  38.             odd_size++;
  39.  
  40.         // maximum check
  41.         if(arr[i]>max_num_array)
  42.             max_num_array = arr[i];
  43.     }
  44.  
  45.    
  46.  
  47.     // #2 task odd and even array can be of size different
  48.  
  49.     //even and odd array creation
  50.     int even_array[even_size];
  51.     int odd_array[odd_size];
  52.  
  53.     //value insertion
  54.  
  55.     int j,k;
  56.     for(i=0,j=0,k=0;i<array_size;i++)
  57.     {
  58.         if(arr[i]%2==0)
  59.         {
  60.             even_array[k] = arr[i];
  61.             // maximum check for even
  62.             if(even_array[k]>max_num_even)
  63.                 max_num_even = even_array[k];
  64.              k++;
  65.         }
  66.         else
  67.         {
  68.             odd_array[j] = arr[i];
  69.            
  70.             if(odd_array[j]>max_num_odd)
  71.                 max_num_odd = odd_array[j];
  72.             if(odd_array[j]<min_num_odd)
  73.                 min_num_odd = odd_array[j];
  74.             j++;
  75.             }
  76.     }
  77.  
  78.     // 10000+12222 = 5 sec 10 year smart human coder
  79.     // 10-9 sec 1 sec  computer fast
  80.    
  81.  
  82.     printf(" *** main array *** \n");
  83.     for(i=0;i<array_size;i++)
  84.     {
  85.         printf("%d ",arr[i]);
  86.     }
  87.  
  88.     printf("\n *** odd array ***\n");
  89.     for(i=0;i<odd_size;i++)
  90.     {
  91.         printf("%d ",odd_array[i]);
  92.     }
  93.  
  94.     printf(" \n ***  even array *** \n");
  95.     for(i=0;i<even_size;i++)
  96.     {
  97.         printf("%d ",even_array[i]);
  98.     }
  99.  
  100.     // maximuma array element print
  101.  
  102.     printf("\n maximum main array element is = %d ",max_num_array);
  103.     printf("\n maximum even array element is = %d ",max_num_even);
  104.     printf("\n the multiplication value of minimum and maximum numbers of odd array = %d",(max_num_odd*min_num_odd));
  105.  
  106.  
  107. }
  108.  
  109. // index  - 0   1   2   3    4    5
  110. // values - 50  30  55  56   79    80 - array use case - list  list id = array index  list value  = array value
  111.  
  112. // odd - 1,3 ,5,7,9 -9*1
  113. // even -
  114. /*
  115.  
  116.    #1  age theke jani- int arr[5] = {1,2,3,4,5};
  117.    #2 user input dibe
  118.     int array[5];
  119.     array[0] = 10;
  120.     array[1] = 10;
  121.     array[2] = 10;
  122.     array[3] = 10;
  123.     array[4] = 10;
  124.  
  125.                  i
  126.     main = 1,3,5,8,7,9,4
  127.                 j    
  128.     odd = 1,3,5
  129.              k  
  130.     even = 8
  131.  
  132.  
  133.  
  134.     // nested if else
  135.  
  136.     if
  137.         if
  138.         else
  139.     else if
  140.         if
  141.         else if
  142.         else
  143.     else
  144.  
  145.  
  146.     // max or min checking logic
  147.     arr = 1,2,3,4,5,7,1
  148.     max_num_array = 7
  149.     arr[i] = 1
  150. */
  151.  
  152.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement