dirtydevil123

Array Assignment 1 All

Oct 9th, 2021 (edited)
687
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.28 KB | None | 0 0
  1. Question:1
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8.   int a;
  9.   cout<<"Plsease inter the array size : ";
  10.   cin>>a;
  11.   int arr[a];
  12.  
  13.   cout<<"enter the array input : "<< endl;
  14.  
  15.   for(int i=0;i<a ;i++){
  16.  
  17.     cin>>arr[i];
  18.   }
  19.   cout<<endl;
  20.  
  21.  
  22.   for(int j=0;j<a; j++){
  23.  
  24.     cout<<arr[j];
  25.     cout<<" ";
  26.   }
  27.  
  28.   cout<<endl;
  29.  
  30.   for(int k=(a-1);k>=0; k--){
  31.  
  32.     cout<<arr[k];
  33.     cout<<" ";
  34.   }
  35.  
  36.     return 0;
  37. }
  38.  
  39. Question:1 End--------------------------------------------------------------------------------------------------------------
  40.  
  41. Question:2
  42.  
  43. #include <iostream>
  44. #include <bits/stdc++.h>
  45. using namespace std;
  46.  
  47. int main()
  48. {
  49.  
  50.   int arr[100];
  51.   int big=0,small=100,sum=0;
  52.     srand(time(0));
  53.   for(int i=0;i<100; i++){
  54.     arr[i]=rand()%100;
  55.   }
  56.  
  57.   for(int j=0;j<100; j++){
  58.         {
  59.    if( big<arr[j])
  60.     big=arr[j];
  61.   }
  62.   if( small>arr[j])
  63.     small=arr[j];
  64.   }
  65.  
  66.   cout<<"100 random integers  :"<<endl;
  67.   for(int k=0;k<100; k++){
  68.     cout<<arr[k]<<" ";
  69.     sum=sum+arr[k];
  70.  
  71.   }
  72.  
  73.   double avg=0,s=sum;
  74.   avg=s/100;
  75.   cout<<endl;
  76.    cout<<"Largest value : "<<big<<endl;
  77.    cout<<"Smallest value : "<<small<<endl;
  78.    cout<<"average value : "<<avg<<endl;
  79.  
  80.  
  81.  
  82.     return 0;
  83. }
  84.  
  85. Question:2 End-----------------------------------------------------------------------------------------------
  86.  
  87. Question:3
  88.  
  89. #include <iostream>
  90. #include <bits/stdc++.h>
  91. using namespace std;
  92.  
  93. int main()
  94. {
  95.  
  96.   int arr[25],a,b;
  97.   cout<<"input the first number : ";
  98.   cin>>a;
  99.    cout<<"input the second number (2nd num > 1st num) : ";
  100.    cin>>b;
  101.     srand(time(0));
  102.   for(int i=1;i<=25; i++){
  103.            int srand = rand()%(b-a + 1) + a;
  104.             arr[i]=srand;
  105.             cout<<arr[i]<<" ";
  106.    }
  107.     return 0;
  108. }
  109.  
  110. Question:3 end-----------------------------------------------------------------------------------------------
  111.  
  112. Question:4
  113.  
  114. #include <iostream>
  115. #include <bits/stdc++.h>
  116. using namespace std;
  117.  
  118. int main()
  119. {
  120. int a,b,c;
  121. int x=0;
  122. cout<<"print array size : ";
  123. cin>>a;
  124. int arr[a];
  125.  
  126.  srand(time(0));
  127.   for(int i=0;i<a; i++){
  128.     arr[i]=rand()%10;
  129.   }
  130.  
  131.   for(int k=0;k<a; k++){
  132.     cout<<arr[k]<<" ";
  133.  
  134.   }
  135. cout<<endl;
  136.   cout<<"Input the number to Search in the array : ";
  137.   cin>>b;
  138.  
  139.  for (int j=0;j<=a;j++) {
  140.                 //x=counter
  141.             if (arr[j]==b){
  142.                     cout<<"The number found in index "<<j<<endl;
  143.                     x++;
  144.         }
  145.  
  146.  }
  147.  
  148.         cout<<"and the number was found : "<<x<<" times ";
  149.  
  150.  
  151.     return 0;
  152. }
  153.  
  154. Question : 4 end-----------------------------------------------------------------------------------------
  155.  
  156. Question : 5
  157.  
  158. #include <iostream>
  159. #include <bits/stdc++.h>
  160. using namespace std;
  161.  
  162. int main()
  163. {
  164. int a,b,c,t;
  165. float arr[5],arr2[5];
  166.  
  167.  srand(time(0));
  168.   for(int i=0;i<5; i++){
  169.     arr[i]=rand()%10;
  170.     arr2[i]=rand()%10;
  171.   }
  172. cout<<"first array before sorting : ";
  173.   for(int k=0;k<5; k++){
  174.     cout<<arr[k]<<" ";
  175.   }
  176.   cout<<endl;
  177.   cout<<"second array before sorting : ";
  178.   for(int k=0;k<5; k++){
  179.     cout<<arr2[k]<<" ";
  180.   }
  181.   cout<<endl;
  182.   for(int k=0;k<5; k++){
  183.         for(int j=k+1;j<5;j++){
  184.             if(arr[k]>arr[j]){
  185.                  t= arr[k];
  186.                 arr[k] = arr[j];
  187.                 arr[j] = t;
  188.         }
  189.   }}
  190.   cout<<"first array in ascending order : ";
  191.   for(int k=0;k<5; k++){
  192.    cout <<arr[k]<<" ";
  193.   }
  194.   for(int k=0;k<5; k++){
  195.         for(int j=k+1;j<5;j++){
  196.             if(arr2[k]<arr2[j]){
  197.                  t= arr2[k];
  198.                 arr2[k] = arr2[j];
  199.                 arr2[j] = t;
  200.         }
  201.   }
  202.   }cout<<endl;
  203.   cout<<"Second array in descending order : ";
  204.   for(int k=0;k<5; k++){
  205.     cout<<arr2[k]<<" ";
  206.   }
  207.     return 0;
  208. }
  209.  
  210. Question : 5 end-------------------------------------------------------------------------------------------
  211.  
  212. Question : 6
  213.  
  214. #include <iostream>
  215. #include <bits/stdc++.h>
  216. using namespace std;
  217.  
  218. int main()
  219. {
  220. int arr[10],a=1,b,c,d=1,i=0;
  221.  
  222. cout<<"insert 10 array input : "<<endl;
  223. for(int i=0;i<10;i++){
  224.     cin>>arr[i];
  225. }
  226.  for (int i = 0; i < 10; i++)
  227.  
  228.   if (a == 1 || d == 1) {
  229.     if (arr[i] < arr[i+1])
  230.       d = 0;
  231.     else if (arr[i] > arr[i+1])
  232.       a = 0;
  233.   }
  234.  
  235.   if (a == 1)
  236.     cout<<"The array is sorted in ascending order";
  237.   else if (d == 1)
  238.     cout<<"The array is sorted in descending order ";
  239.   else
  240.     cout<<"The array is not sorted ";
  241.     return 0;
  242. }
  243.  
  244. Question : 6 end -----------------------------------------------------------------------------------------
  245.  
  246. Question : 7
  247.  
  248. #include<iostream>
  249. #include<conio.h>
  250. #include <bits/stdc++.h>
  251. using namespace std;
  252. int main()
  253. {
  254.  
  255.     float arr[11];
  256.     int big=0,big1=0,small=10,small1=0,t=0;
  257.     float mid=11/2;
  258.     float median;
  259. srand(time(0));
  260.   for(int i=0;i<11; i++){
  261.     arr[i]=rand()%10;
  262.     cout<<arr[i]<<" ";
  263.   }
  264.  
  265.    for(int i=0;i<11; i++){
  266.     if(big<arr[i]){
  267.         big=arr[i];
  268.     }
  269.  
  270.     if(small>arr[i]){
  271.         small=arr[i];
  272.     }
  273.    }
  274.  
  275.   for(int i=0;i<11; i++){
  276.     if(big1<arr[i] && big>arr[i] ){
  277.         big1=arr[i];
  278.     }
  279.  
  280.     if(big1>arr[i] && small!=arr[i]){
  281.         small1=arr[i];
  282.     }
  283.  
  284.   }
  285.   for(int k=0;k<11; k++){
  286.         for(int j=k+1;j<11;j++){
  287.             if(arr[k]>arr[j]){
  288.                  t= arr[k];
  289.                 arr[k] = arr[j];
  290.                 arr[j] = t;
  291.         }
  292.   }}
  293.   {
  294.  
  295.        median = (arr[(11-1)/2]+arr[11/2])/2.0;
  296.  
  297.  
  298.  
  299.     }
  300.  
  301.  
  302.   cout<<endl;
  303.   cout<<"First minimun "<<small<<endl;
  304.   cout<<"Second mimunum "<<small1<<endl;
  305.   cout<<"First maximum "<<big<<endl;
  306.   cout<<"Second Maximum "<<big1<<endl;
  307.    cout << "The median is: " << median << endl;
  308.  
  309.  
  310.     return 0;
  311. }
  312.  
  313.  
  314. Question : 7 end------------------------------------------------------------------------------------------------------
  315.  
  316. Question : 8
  317.  
  318. #include <iostream>
  319. #include <bits/stdc++.h>
  320. using namespace std;
  321.  
  322. int main()
  323. {
  324.     int arr[10],evenArr[10],oddArr[10],a=0,b=0,c,even=0,evenArr1[10],counter=0;
  325.  
  326. srand(time(0));
  327.   for(int i=0;i<10; i++){
  328.     arr[i]=rand()%10;
  329.   cout<<arr[i]<<" ";
  330.   }
  331.   cout<<endl;
  332.   for(int i=0;i<10;i++)
  333.  
  334.   {
  335.       if(arr[i]%2==0){
  336.              evenArr[a]=arr[i];
  337.              a++;
  338.       }
  339.       if(arr[i]%2!=0){
  340.              oddArr[b]=arr[i];
  341.              b++;
  342.       }
  343.       }
  344.   cout<<endl;
  345.  
  346.   for(int i=0;i<a;i++){
  347.     cout<<evenArr[i]<<" ";
  348.   }
  349.   cout<<endl;
  350.   for(int i=0;i<b;i++){
  351.     cout<<oddArr[i]<<" ";
  352.   }
  353.  
  354.     return 0;
  355. }
  356.  
  357. Question  : 8 end-----------------------------------------------------------------------------------------
  358.  
  359. Question :9
  360.  
  361. #include<iostream>
  362. #include<conio.h>
  363. using namespace std;
  364. int main()
  365. {
  366.  
  367.     int arr[100],i,n;
  368.     cout<<"Enter a Number :";
  369.     cin>>n;
  370.  
  371.     arr[0]=0;
  372.     arr[1]=1;
  373.  
  374.     for(i=2;i<n;i++)
  375.       arr[i]=arr[i-1]+arr[i-2];
  376.  
  377.     cout<<"So the "<<n<<" Fibonaccii numbers are:"<<endl;
  378.     for (i=0;i<n;i++)
  379.     cout<<arr[i]<<" ";
  380.  
  381.     return 0;
  382. }
  383.  
  384. Question :9 end-----------------------------------------------------------------------------------------------
  385.  
  386. Question :10
  387.  
  388. #include <iostream>
  389. #include <bits/stdc++.h>
  390. using namespace std;
  391.  
  392. int main()
  393. {
  394.   int i,s=0,j,p;
  395.  
  396.   int arr[10];
  397.    srand(time(0));
  398.   for(int i=0;i<10; i++){
  399.     arr[i]=rand()%100;
  400.     cout<<arr[i]<<" ";
  401.   }
  402. cout<<endl;
  403. for (i = 0; i < 10; i++) {
  404.         j = 2;
  405.         p = 1;
  406.         while (j < arr[i]) {
  407.             if (arr[i] % j == 0) {
  408.                 p = 0;
  409.                 break;
  410.             }
  411.             j++;
  412.         }
  413.         if (p == 1) {
  414.             cout << arr[i] << " ";
  415.                     s++;
  416.         }
  417.     }
  418.     cout<<"prime number count : "<<s;
  419.  
  420.   return 0;
  421. }
  422.  
  423. Question : 10 end-----------------------------------------------------------------------------------------------
  424.  
  425. Question : 11
  426.  
  427. #include <iostream>
  428. using namespace std;
  429. int main()
  430. {
  431. int a[10], n, i,j;
  432. cout<<"Input the  number You want to convert ";
  433. cin>>n;
  434. j=n;
  435. for(i=0; n>0; i++)
  436. {
  437. a[i]=n%2;
  438. n= n/2;
  439. }
  440. cout<<"Binary number of "<<j<<" = ";
  441. for(i=i-1 ;i>=0 ;i--)
  442. {
  443. cout<<a[i];
  444. }
  445. return 0;
  446. }
  447.  
  448. Question: 11 end---------------------------------------------------------------------------------------------------
  449.  
  450. Question: 12
  451.  
  452. #include <iostream>
  453. #include <bits/stdc++.h>
  454. using namespace std;
  455. int main()
  456. {
  457.     int arr[10],a,b,c,j;
  458. srand(time(0));
  459.   for(int i=0;i<10; i++){
  460.     arr[i]=rand()%10;
  461.  
  462.     cout<<arr[i]<<" ";
  463.   }
  464.   cout<<endl;
  465.   cout<<endl;
  466.     int result = 0;
  467.     for (int i=0; i<10; i++){
  468.       for  (int j=0;j<10;j++){
  469.                 if (arr[i] == arr[j])
  470.                     result++;
  471.         }
  472.         cout<<arr[i]<<" was found "<<result<<" times"<<endl;
  473.         result=0;
  474.     }
  475.  
  476.  
  477.  
  478.  
  479. return 0;
  480. }
  481.  
  482. Question : 12 end---------------------------------------------------------------------------------------------------------
  483.  
  484. Question :13
  485.  
  486.  
  487.  
  488. #include <iostream>
  489. #include <bits/stdc++.h>
  490. using namespace std;
  491.  
  492. const int R=5;
  493. const int C=3;
  494.  
  495. int main() {
  496.     int r=0, c=0;
  497.     int arr[R][C];
  498.  
  499.     srand(time(0));
  500.     for (r=0; r<R; r++)
  501.          for (c=0;c<C;c++)
  502.                arr[r][c]=rand()%(50-30 + 1) + 30;
  503.  
  504.  
  505.     for (c=0;c<C;c++) {
  506.          for (r=0;r<R;r++){
  507.               cout << arr[c][r] << endl;
  508.          }
  509.         cout << endl;
  510.     }
  511.  
  512.     return 0;
  513. }
  514.  
  515.  
  516. Question :13 end-------------------------------------------------------------------------------------------------------
  517.  
Add Comment
Please, Sign In to add comment