Diagonal of a matrix (2 dimension array) with c++

#include<iostream.h>
#include<conio.h>
void main()
{
   cout<<"========================================================\n";
   cout<<"        Tugas Algoritma Pemrograman Semester 2          \n";
   cout<<"            Pencarian diagonal pada matrix              \n";
   cout<<"========================================================\n\n";
   cout<<"Tekan sembarang tombol untuk melanjutkan...";
   getch();
   awal:
   clrscr();

    int i,j;
   int dimat;
   char lagi;

//input data
    cout<<"Masukkan dimensi matrix : ";
    cin>>dimat;
   if ((dimat<2)||(dimat>9))
   {
       cout<<"Dimensi antara 2-9, tekan sembarang tombol utk mengulang";
      getch();
      goto awal;
   }
    int matrik[9][9];
    int diag[2][9];
   cout<<"\n\n" ;
   for(i=0;i<9;i++)
   {
       for(j=0;j<9;j++)
          matrik[i][j]=0;
   }

   for(i=0;i<dimat;i++)
   {
       for(j=0;j<dimat;j++)
       {
            cout<<"Masukkan nilai matrix "<<"["<<i<<"]["<<j<<"] : " ;
         cin>>matrik[i][j];
      }
   }

//print matrix
   clrscr();
   cout<<"Matrix "<<dimat<<"x"<<dimat<<" :\n\n";
   for(i=0;i<dimat;i++)
   {
       for(j=0;j<dimat;j++)
            cout<<matrik[i][j]<<" ";
      cout<<"\n";
   }
//get diagonal
    bool isi1;
   bool isi2;
    int lastidx1=0;
   int lastidx2=dimat-1;
   for(i=0;i<dimat;i++)
   {
      isi1=false;
      isi2=false;
       for(j=0;j<dimat;j++)
      {
         if(i==lastidx1)
         {
             if(j==lastidx1)
             {
                 diag[0][lastidx1]=matrik[i][j];
                 isi1=true;
             }
             if(j==lastidx2)
             {
                 diag[1][lastidx1]=matrik[i][j];
                isi2=true;
             }
             if((isi1==true)&&(isi2==true))
             {
                 lastidx1++;
                 lastidx2--;
             }
         }
      }
   }
//print diagonal
    cout<<"\n\nDiagonalnya adalah :\n\n";
   for(i=0;i<=1;i++)
   {
       if(i==0)
          cout<<"Diagonal A : ";
      else if (i==1)
          cout<<"Diagonal B : ";
       for(j=0;j<dimat;j++)
            cout<<diag[i][j]<<" ";
      cout<<"\n";
   }
   cout<<"\n\n\n========================================================\n";
   cout<<"          dibuat oleh :  R.Radian Baratasena            \n";
   cout<<"                    NRP : 14511069                      \n";
   cout<<"                 Dosen : AMRONI, M.KOM                  \n";
   cout<<"              STIKOM POLTEK Cirebon  2012               \n";
   cout<<"========================================================\n\n";
    cout<<"\n\n\nHitung lagi? [y/n]: ";
   cin>>lagi;
   if ((lagi=='y')||(lagi=='Y'))
      goto awal;
}


notes :

- dynamic array dimension (based on user input), if user input is 7, then the array dimension is 7x7.

- dynamic array datas (based on user input), if the dimension is 7, then the array consist of 49 datas.

- this program is for education purpose only.

Comments

Popular posts from this blog

Hot Keyboard Pro 3.2.700 Incl Serial Key

Windows 7 ultimate SP1 32-bit free download

Borland Turbo C++ 4.5 download