Haval1

Homework 5

May 13th, 2018
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.06 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3. int main()
  4. {
  5.     cout<<"Write a program in C++ to execute the following\n";
  6.     cout<<"1 : Insert items to the array (Whitout using cin)\n";
  7.     int i,j,k=9,a[4][4];
  8.     for(i=0;i<=3;i++)
  9.     {
  10.         for(j=0;j<=3;j++)
  11.         {
  12.             a[i][j]=k;
  13.             cout<<a[i][j]<<"\t";
  14.         }
  15.         cout<<endl<<endl;
  16.         k-=3;
  17.     }
  18.     cout<<"--------------------------------------------------------\n";
  19.     cout<<"2 : find odd numbers in the array , putting it to the 1D array and print it.\n";
  20.     int b[7],t=0;
  21.     for(i=0;i<=3;i++)
  22.     {
  23.         for(j=0;j<=3;j++)
  24.         {
  25.             if(a[i][j]%2==1)
  26.             {
  27.                 b[t]=a[i][j];
  28.                 t++;
  29.             }
  30.         }
  31.     }
  32.     for(i=0;i<t;i++)
  33.     {
  34.         cout<<b[i]<<"\t";
  35.     }
  36.     cout<<"\n--------------------------------------------------------\n";
  37.     cout<<"3 : find even numbers in the array , putting it to the 1D array and print it.\n";
  38.     int p=0,c[7];
  39.     for(i=0;i<=3;i++)
  40.     {
  41.         for(j=0;j<=3;j++)
  42.         {
  43.             if(a[i][j]%2==0)
  44.             {
  45.                 c[p]=a[i][j];
  46.                 p++;
  47.             }
  48.         }
  49.     }
  50.     for(i=0;i<p;i++)
  51.     {
  52.         cout<<c[i]<<"\t";
  53.     }
  54.     cout<<"\n--------------------------------------------------------\n";
  55.     cout<<"4 : Divided the array SE(4,4) to two array x(2,4) , y(2,4)\n";
  56.     cout<<"X (2 , 4 )\n";
  57.     for(i=0;i<=1;i++)
  58.     {
  59.         for(j=0;j<=3;j++)
  60.         {
  61.             cout<<a[i][j]<<"\t";
  62.         }
  63.         cout<<endl<<endl;
  64.     }
  65.     cout<<"Y (2 , 4 )\n";
  66.     for(i=2;i<=3;i++)
  67.     {
  68.         for(j=0;j<=3;j++)
  69.         {
  70.             cout<<a[i][j]<<"\t";
  71.         }
  72.         cout<<endl<<endl;
  73.     }
  74.     cout<<"\n--------------------------------------------------------\n";
  75.     cout<<"5 : swap first column with third column \n";
  76.     int y;
  77.     for(i=0;i<=3;i++)
  78.     {
  79.         y=a[i][0];
  80.         a[i][0]=a[i][2];
  81.         a[i][2]=y;
  82.     }
  83.     for(i=0;i<=3;i++)
  84.     {
  85.         for(j=0;j<=3;j++)
  86.         {
  87.             cout<<a[i][j]<<"\t";
  88.         }
  89.         cout<<endl<<endl;
  90.     }
  91.     cout<<"\n--------------------------------------------------------\n";
  92.     cout<<"6 : swap first row with third column \n";
  93.     for(i=0;i<=3;i++)
  94.     {
  95.         for(j=0;j<=3;j++)
  96.         {
  97.             y=a[0][j];
  98.             a[0][j]=a[i][2];
  99.             a[i][2]=y;
  100.         }
  101.     }
  102.     for(i=0;i<=3;i++)
  103.     {
  104.         for(j=0;j<=3;j++)
  105.         {
  106.             cout<<a[i][j]<<"\t";
  107.         }
  108.         cout<<endl<<endl;
  109.     }
  110.     cout<<"\n--------------------------------------------------------\n";
  111.     cout<<"7 : swap second column with fourth row \n";
  112.     int temp;
  113.     for(i=0;i<=3;i++)
  114.     {
  115.         for(j=0;j<=3;j++)
  116.         {
  117.             temp=a[i][1];
  118.             a[i][1]=a[3][j];
  119.             a[3][j]=temp;
  120.         }
  121.     }
  122.     for(i=0;i<=3;i++)
  123.     {
  124.         for(j=0;j<=3;j++)
  125.         {
  126.             cout<<a[i][j]<<"\t";
  127.         }
  128.         cout<<endl<<endl;
  129.     }
  130.     cout<<"\n--------------------------------------------------------\n";
  131.     cout<<"8 : find the minimum and maximum number in the array and print them \n";
  132.     int max=0,mini=0;
  133.     max=a[0][0];
  134.     mini=a[i][j];
  135.     for(i=0;i<=3;i++)
  136.     {
  137.         for(j=0;j<=3;j++)
  138.         {
  139.             if(a[i][j]>max)
  140.                 max=a[i][j];
  141.             else if(a[i][j]<mini)
  142.                 mini=a[i][j];
  143.         }
  144.     }
  145.     cout<<"max : "<<max<<endl;
  146.     cout<<"mini : "<<mini<<endl;
  147.     cout<<"\n--------------------------------------------------------\n";
  148.     cout<<"9 : inverse the array\n ";
  149.     k=9;
  150.     for(i=0;i<=3;i++)
  151.     {
  152.         for(j=0;j<=3;j++)
  153.         {
  154.             a[i][j]=k;
  155.         }
  156.         k-=3;
  157.     }
  158.     for(i=0;i<=3;i++)
  159.     {
  160.         for(j=0;j<=3;j++)
  161.         {
  162.             cout<<a[j][i]<<"\t";
  163.         }
  164.         cout<<endl<<endl;
  165.     }
  166.     cout<<"\n--------------------------------------------------------\n";
  167.     cout<<"10 : arrange number in the first row in ascending \n ";
  168.     for(j=0;j<=3;j++)
  169.     {
  170.         if(a[0][j]>a[0][j+1])
  171.         {
  172.             temp=a[0][j];
  173.             a[0][j]=a[0][j+1];
  174.             a[0][j+1]=temp;
  175.         }
  176.        
  177.     }
  178.     for(i=0;i<=3;i++)
  179.     {
  180.         for(j=0;j<=3;j++)
  181.         {
  182.             cout<<a[j][i]<<"\t";
  183.         }
  184.         cout<<endl<<endl;
  185.     }
  186.     cout<<"\n--------------------------------------------------------\n";
  187.     cout<<"11 : print the upper triangle \n";
  188.     for(i=0;i<=3;i++)
  189.     {
  190.         for(j=0;j<=3;j++)
  191.         {
  192.             if(i+j<3||i+j==3)
  193.             {
  194.                 cout<<a[j][i];
  195.             }
  196.             cout<<"\t";
  197.         }
  198.         cout<<endl<<endl;
  199.     }
  200.     cout<<"\n--------------------------------------------------------\n";
  201.     cout<<"12 : print sum of number in lower triangle \n";
  202.     int sum=0;
  203.     for(i=0;i<=3;i++)
  204.     {
  205.         for(j=0;j<=3;j++)
  206.         {
  207.             if(i+j>3||i+j==3)
  208.                 sum+=a[j][i];
  209.         }
  210.     }
  211.     cout<<"Sum : "<<sum<<endl;
  212. }
Advertisement
Add Comment
Please, Sign In to add comment