C++ Understanding Multi Dimensional Arrays (CUMDA)

A multidimensional array is an array that has more than one index type or more than one subscript number, for example having two, three and so on.

1. Defining Array Types in C++

tipe_data nama_variabel_array[jumlah_elemen_baris][jumlah_elemen_kolom];

Example:

int matrik[3][4];

A two-dimensional array variable is defined with the name matrix which has 3 row elements and 4 column elements.

2. Array element depiction

3. Operations on Arrays

matrik[1][2] = 23;

This means that the input data 23 is in the matrix array row 1 and column 2.

matrik[2][3] = matrik[1][2];

This means inputting data into the matrix array row 2, column 3 with the data in the matrix array row 1, column 2.

To display data:

cout<<

4. Addition of 2 Matrices in C++

Example of a program to calculate the sum of two matrices, the condition for summing two matrices is that both matrices have the same order, if 2 rows 2 columns, then the opposite of the sum must also be the same 2 rows 2 columns.

Program:

#include <iostream.h>
#include <conio.h>

/**
*bundet.com
*Penjumlahan 2 Matrix
*/

void main()
{
 int a[10][10], b[10][10], c[10][10];
 int brs, klm;
  clrscr();
 cout<<"Masukan jumlah baris : ";
 cin>>brs;
 cout<<"Masukan jumlah kolom : ";
 cin>>klm;
 for(int i=0;i<brs;i++) //input matrik A
 {
 for(int j=0;j<klm;j++)
  {
  cout<<"Matrik A ["<<i<<"]["<<j<<"] = ";
  cin>>a[i][j];
  }
 }

 for(int i=0;i<brs;i++) //input matrik B
 {
 for(int j=0;j<klm;j++)
  {
  cout<<"Matrik B ["<<i<<"]["<<j<<"] = ";
  cin>>b[i][j];
  }
 }

 for(int i=0;i<brs;i++) //menjumlahkan matrik A dengan B
 {
 for(int j=0;j<klm;j++)
  {
  c[i][j]= a[i][j]+b[i][j];
  }
 }

 clrscr();
 cout<<"Matrik A"<<endl; //menampilkan matrik A
 for(int i=0;i<brs;i++)
 {
 for(int j=0;j<klm;j++)
  {
  cout<<"Matrik A ["<<i<<"]["<<j<<"] = ";
  cout<<a[i][j]<<" ";
  }
  cout<<endl;
 }
 cout<<"Matrik B"<<endl; //menampilkan matrik B
 for(int i=0;i<brs;i++)
 {
 for(int j=0;j<klm;j++)
  {
  cout<<"Matrik B ["<<i<<"]["<<j<<"] = ";
  cout<<b[i][j]<<" ";
  }
  cout<<endl;
 }
 cout<<"Matrik C"<<endl; //menampilkan matrik C
 for(int i=0;i<brs;i++)
 {
 for(int j=0;j<klm;j++)
  {
  cout<<"Matrik C ["<<i<<"]["<<j<<"] = ";
  cout<<c[i][j]<<" ";
  }
  cout<<endl;
 }
getch();
}

Output:

Source:

© STMIK El Rahma Yogyakarta. Compiled by Mr. Eko Riswanto, ST, M.Cs,

C++ 2 Dimensional Array Matrix Sort Rows, Columns, Start-End Elements

Case Study1

Create a program for a 2-dimensional matrix with random input and display it in the following way:

  • a) Sort in the same column.
  • b) Sort on the same line.
  • c) Sort from the first element to the end.

In this case, there are many iteration processes used to create a matrix element that we will determine, then to solve the core problem, both in questions a, b, and c, it uses an exchange process like when we first learned about Programming Algorithms, and in the exchange process it also uses a selection process using an algorithm to determine the max and min values. For more information, please understand the source code below:

Solution:

Source code:

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<iomanip.h>

/**
*bundet.com
*Array 2 Dimensi, Matrix Urut Baris, Kolom, Elemen Awal-Akhir, Cetak Serentak
*/

void main() {
    int a[10][10], b[10][10], brs, klm, x,y,z;
    randomize();
   cout << "SOAL NO. 1"<<endl<<endl;
   cout << "Masukkan jumlah baris: "; cin >> brs;
    cout << "Masukkan jumlah kolom: "; cin >> klm;
    cout << endl;

//soal A
   for(int i=0; i<brs; i++) {
        for(int j=0; j<klm; j++) {
            a[i][j]=random(50);
        }
    }
    for(int i=0; i<brs; i++) {
        for(int j=0; j<klm; j++) {
            for(int k=0; k<klm; k++) {
                if(a[i][j] < a[k][j]) {
                    y=a[i][j];
                    a[i][j]=a[k][j];
                    a[k][j]=y;
                }
            }
        }
    }
   cout << "JAWABAN SOAL 1A" << endl <<endl;
    for(int i=0; i<brs; i++) {
        for(int j=0; j<klm; j++) {
      cout << setw(5) << a[i][j];
        }
        cout << endl;
    }

cout<<endl<<endl;
//soal 1B
    for(int i=0; i<brs; i++) {
        for(int j=0; j<klm; j++) {
            a[i][j]=random(50);
        }
    }
    for(int i=0; i<brs; i++) {
        for(int j=0; j<klm; j++) {
            for(int k=0; k<klm; k++) {
                if(a[i][j] < a[i][k]) {
                    x=a[i][j];
                    a[i][j]=a[i][k];
                    a[i][k]=x;
                }
            }
        }
    }
   cout << "JAWABAN SOAL 1B" << endl <<endl;
    for(int i=0; i<brs; i++) {
        for(int j=0; j<klm; j++) {
      cout << setw(5) << a[i][j];
        }
        cout << endl;
    }

cout<<endl<<endl;
//soal 1C
    for(int i=0; i<brs; i++) {
        for(int j=0; j<klm; j++) {
            a[i][j]=random(50);
        }
    }
    for(int i=0; i<brs; i++) {
        for(int j=0; j<klm; j++) {
            for(int k=0; k<klm; k++) {
                for(int l=0; l<klm; l++) {
                    if(a[i][j] < a[k][l]) {
                    z=a[i][j];
                    a[i][j]=a[k][l];
                    a[k][l]=z;
                    }
                }
            }
        }
    }
   cout << "JAWABAN SOAL 1C" << endl <<endl;
    for(int i=0; i<brs; i++) {
        for(int j=0; j<klm; j++) {
      cout << setw(5) << a[i][j];
        }
        cout << endl;
    }
    getch();
}

Case Study2

In the previous material, we discussed how to sort numbers in a matrix based on columns and rows.

The challenge this time is, how to sort the numbers in the matrix based on all elements, including columns and rows. This allows us to combine the elements of the algorithms that we have created before, you could say the challenge this time is to reunite some program extractions, or like playing a puzzle.

Solution:

Source code:

#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
#include <iomanip.h>

/**
*bundet.com
*Array 2 Dimensi Matrix Urut Kolom, Baris, Elemen Awal-Akhir.
*/

void main(){
int matrix[10][10];
int tampung[100];

randomize();
 for(int i=0;i<10;i++)
 for(int j=0;j<10;j++)
 matrix[i][j]=random(20);

 for(int i=0;i<10;i++)
 {
 for(int j=0;j<10;j++)
 cout<<setw(5)<<matrix[i][j];
 cout<<endl;
 }

int x=0;
//menampung baris ke-0...
 for(int i=0;i<10;i++)
 for(int j=0;j<10;j++)
   {
 tampung[x]=matrix[i][j];
 x++;
 }

//mengurutkan nilai di tampung
int tamp;
 for(int i=0;i<100;i++)
 for(int j=0;j<100;j++)
 if (tampung[i]<tampung[j])
   {
 tamp=tampung[i];
 tampung[i]=tampung[j];
 tampung[j]=tamp;
 }

 int a=0;
 for(int i=0;i<10;i++)
 for(int j=0;j<10;j++)
   {
 matrix[i][j]=tampung[a];
 a++;
 }

 cout<<"\nMatrix setelah diurutkan perbaris adalah\n";
 for(int i=0;i<10;i++)
   {
 for(int j=0;j<10;j++)
 cout<<setw(5)<<matrix[i][j];
 cout<<endl;
 }

getch();
}

Hope this is useful & happy learning!

Question 1

FRANS PURBA Jun 17 2015, 11:44:00

// min kalo mau ubah posisi array seperti ini
1 2 4
5 6 7
// masing2 nilai baris di geser sebanyak 1x jadi:
4 1 2
7 5 6

please help me min

C++ 2D Array Matrix Output Prime Replace 0, Other Multiples of 2 & 3 Replace 1

Case study:

1. Create a 2-dimensional matrix C++ program with random input, then display it in the following way:

  • Replace the non-prime matrix entries with 0.
  • Replace the matrix contents that are not multiples of 2 and 3 with the number 1.
  • In this case we try to apply the concept of matrix formation algorithms, prime numbers, and even or odd numbers.

Solution:

Source code:

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<iomanip.h>

/**
*bundet.com
*Array 2 Dimensi Matrix Input Random, Output Prima Ganti 0, Selain Kelipatan 2 dan 3 Ganti 1
*/

void main() {
    int a[10][10], brs, klm,y;
    randomize();
   cout << "SOAL NO. 2"<<endl<<endl;
   cout << "Masukkan jumlah baris: "; cin >> brs;
    cout << "Masukkan jumlah kolom: "; cin >> klm;
    cout << endl;

//soal 2A

   for(int i=0; i<brs; i++) {
        for(int j=0; j<klm; j++) {
            a[i][j]=random(50);
        }
    }

   for(int i=0; i<brs; i++) {
        for(int j=0; j<klm; j++) {
            y=0;
            for(int p=1; p<=a[i][j]; p++) {
                if(a[i][j]%p==0) {
                    y++;
                }
            }
            if(y!=2 || a[i][j]==0) {
                a[i][j]=0;
            }

        }
    }

   cout << "JAWABAN SOAL 2A" << endl <<endl;
    for(int i=0; i<brs; i++) {
        for(int j=0; j<klm; j++) {
      cout << setw(5) << a[i][j];
        }
        cout << endl;
    }

cout<<endl<<endl;
//soal 2B

    for(int i=0; i<brs; i++) {
        for(int j=0; j<klm; j++) {
            a[i][j]=random(50);
        }
    }

    for(int i=0; i<brs; i++) {
        for(int j=0; j<klm; j++) {
            if((a[i][j]%2!=0) || (a[i][j]%3!=0) || (a[i][j]==0)) {
                a[i][j]=1;
            }
        }
    }
   cout << "JAWABAN SOAL 2B" << endl <<endl;
    for(int i=0; i<brs; i++) {
        for(int j=0; j<klm; j++) {
      cout << setw(5) << a[i][j];
        }
        cout << endl;
    }
    getch();
}

Hope this is useful & happy learning!

Question 1

**YULIANTO BASTIAN**Oct 20, 2016, 21:15:00 ask

code untuk menampilkan data
1 6 11 16
2 7 12 17
3 8 13 18
4 9 14 19

5 10 15 20
dengan 1x looping saja

thx...

Response 1

Wow, great assignment bro, happy studying!

C++ 2 Dimensional Array Matrix Random Input Output Other Than Prime Replace 0

Case study

This experiment is still about Matrix and 2-dimensional arrays, where I will try to change the numbers other than prime numbers to 0, so that the matrix display only shows the numbers 0 and prime numbers, for more details see the example below:

Source Code:

#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
#include <iomanip.h>

/**
*bundet.com
*Contoh Program C++ Matriks
*Selain Bilangan Prima Ganti 0
*/

void main()
{
int matrix[10][10];
int tampung[10];
randomize();

for(int i=0;i<10;i++)
for(int j=0;j<10;j++)
matrix[i][j]=random(20);

cout<<"\nIsi matrix asli hasil acak :\n";

for(int i=0;i<10;i++)
{
for(int j=0;j<10;j++)
cout<<setw(5)<<matrix[i][j];
cout<<endl;
}

int x, jml;
for(int i=0;i<10;i++)
for(int j=0;j<10;j++)
{
x=matrix[i][j];
for(int a=1;a<=x;a++)
if(x%a==0)
jml++;
if(jml>2 || x==1)
matrix[i][j]=0;
jml=0;
}

cout<<"\nMatrix bilangan prima selain itu ganti 0 :\n";
for(int i=0;i<10;i++)
{
for(int j=0;j<10;j++)
cout<<setw(5)<<matrix[i][j];
cout<<endl;
}

getch();
}

Hope this is useful & happy learning!

C++ 2 Dimensional Array Matrix Random Input Prime Output Replace 0

Case study

This experiment is still about matrices and 2-dimensional arrays, where we will try to change the prime numbers to zero (0), so that no prime numbers are displayed in the matrix example below.


Prime Matrix Replaced by Zero

Source Code:

#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
#include <iomanip.h>

/**
*bundet.com
*Contoh Program Matriks pada C++
*Bilangan Prima Diganti 0
*/

void main()
{
 int matrix[10][10];
 int tampung[10];
 randomize();

 for(int i=0;i<10;i++)
 for(int j=0;j<10;j++)
 matrix[i][j]=random(20);

 cout<<"\nIsi matrix asli hasil acak :\n";

 for(int i=0;i<10;i++)
 {
 for(int j=0;j<10;j++)
 cout<<setw(5)<<matrix[i][j];
 cout<<endl;
 }

 int x, jml;
 for(int i=0;i<10;i++)
 for(int j=0;j<10;j++)
 {
 x=matrix[i][j];
 for(int a=1;a<=x;a++)
 if(x%a==0)
 jml++;
 if(jml>2 || x==1)
 matrix[i][j];
 else
 matrix[i][j]=0;
 jml=0;
 }

 cout<<"\nMatrix bilangan prima ganti 0 :\n";
 for(int i=0;i<10;i++)
 {
 for(int j=0;j<10;j++)
 cout<<setw(5)<<matrix[i][j];
 cout<<endl;
 }

getch();
}

Hope this is useful & happy learning!

C++ 2 Dimensional Array Looping On Matrix Row Sort

You need to know that the loop in C++ 2-dimensional array is absolute, so what can you do, you all have to memorize it, because there is no other model or variant for the concept of looping 2-dimensional arrays. If you all think you can make another version, even prove it, then you are great and can even beat the greatness of this person,  😃

Ok, let's get straight to the point, please see this screenshot:

Source code:

#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
#include <iomanip.h>

/**
*bundet.com
*Matrix urut baris
*/

void main()
{
 int matrix[10][10];
 int tampung[10];
 randomize();

 for(int i=0;i<10;i++)
 for(int j=0;j<10;j++)
 matrix[i][j]=random(20);

 for(int i=0;i<10;i++)
 {
 for(int j=0;j<10;j++)
 cout<<setw(5)<<matrix[i][j];
 cout<<endl;
 }

 int x=0;
 //menampung baris ke-0...
 for(int b=0;b<10;b++)
 {
 for(int i=0;i<10;i++)
 for(int j=0;j<10;j++)
 if(j==b)
 {
 tampung[x]=matrix[i][j];
 x++;
 }

 //mengurutkan nilai di tampung
 int tamp;
 for(int i=0;i<10;i++)
 for(int j=0;j<10;j++)
 if(tampung[i]<tampung[j])
 {
 tamp=tampung[i];
 tampung[i]=tampung[j];
 tampung[j]=tamp;
 }

 int a=0;
 for(int i=0;i<10;i++)
 for(int j=0;j<10;j++)
 if(j==b)
 {
 matrix[i][j]=tampung[a];
 a++;
 }
 a=0;x=0;
 }
 //endfor
 cout<<"\nMatrix setelah diurutkan perbaris adalah:\n";
 for(int i=0;i<10;i++)
 {
 for(int j=0;j<10;j++)
 cout<<setw(5)<<matrix[j][i];
 cout<<endl;
 }
getch();
}

Hope this is useful & happy learning!

C++ 2 Dimensional Array Input-Output Concept


Sales Datalog C++ Example

Source Code:

#include <iostream.h>
#include <conio.h>
#define ROWS 3
#define COLS 2

/*
*bundet.com
*Wawan Beneran
*Konsep Array 2 Dimensi (Baris & Kolom) - DataLog Penjualan
*/

void main()
{
int tahun= 2010;
int cell = 10;

int penjualan[ROWS][COLS];
//input data penjualan
for (int i=0; i < ROWS; i++)
{
cout << "\nPenjualan Tahun " << i+tahun;
cout << "\n--------------------\n";
for (int j=0; j < COLS; j++)
{
cout << "Data ke-" << j+1;
cout << " = ";
cin >> penjualan[i][j];
}
}

//cetak header tabel
cout <<endl<<endl;
cout << "Data penjualan pertahun\n";
cout << "-----------------------\n";
//output data penjualan
for (int i=0; i < ROWS; i++)
{ cout << "\nTahun " << tahun+i;
cout << "\n----------------\n";
for (int j=0; j < COLS; j++)
{ cout << j+1 << ") ";
cout << penjualan[i][j] << endl;
}
}
getch();
}

Hope this is useful & happy learning!


Post a Comment

Previous Next

نموذج الاتصال