Advertisement
Guest User

Random fill array Integer

a guest
Dec 5th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.54 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. using namespace std;
  4.  
  5. bool podredi(int i,int j){
  6.     return(i<j);
  7. }
  8. void fillarray(int ina[],int in,int R,int s,int a=0){
  9.     if (R == 1)
  10.     for (int i=0;i<in;i++){
  11.         ina[i]=rand() % s;
  12.     }
  13.     else if (R== 0)
  14.         for (int i=0;i<in;i++){
  15.             cout<<"["<<i<<"] = ";
  16.             cin>>a;
  17.             ina[i]=a;
  18.         }
  19.     else
  20.         cout<<"Wrong random switch"<<endl;
  21. }
  22. void printar(int a[],int m){
  23.  
  24.     for(int i=0; i<m; i++){
  25.        if (a[i]!=0)
  26.         cout<<a[i]<<endl;
  27.     }}
  28. int main()
  29. {
  30. //By viktor korunoski 05-12-2019
  31.     int n,r=0,s=0,zb;
  32.     char p;
  33.     float c=0;
  34.     cout << "Number elements in the array: ";
  35.     cin>>n;
  36.     cout<<endl;
  37.     cout<<"Random? Y/N"<<endl;
  38.     cin>>p;
  39.     zb=0;
  40.     if ((p == 'Y') or (p== 'y')){
  41.         r=1;
  42.         cout<<"From 0 to ";
  43.         cin>>s;
  44.         cout<<endl;
  45.     }
  46.     else if((p == 'N') or (p == 'n'))
  47.         r=0;
  48.     else
  49.         cout<<"Wrong random swithch"<<endl;
  50.     int b[n],ap[n],apm[n];
  51.     fillarray(b,n,r,s);
  52.     for (int i=0;i<n;i++){
  53.         apm[i]=0;
  54.         ap[i]=0;
  55.         zb=zb+b[i];
  56.     }
  57.  
  58.     c=zb/(n-1);
  59.     for(int i=0; i<n; i++){
  60.         if(b[i]<c){
  61.             ap[i]=b[i];
  62.         }
  63.         else
  64.             apm[i]=b[i];
  65.     }
  66.     sort(ap,ap+n,podredi);
  67.     sort(apm,apm+n);
  68.     cout<<"Larger then the avreage  "<<endl;
  69.     printar(ap,n);
  70.     cout<<" "<<endl;
  71.     cout<<"Smaller than the avreage "<<endl;
  72.     printar(apm,n);
  73.     cout<<" "<<endl;
  74.     return 0;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement