SlideShare ist ein Scribd-Unternehmen logo
1 von 16
May 13, 2012 TWO-DIMENSIONAL ARRAYS (MATRIX)




Type array [size1][size2];



          Row           A[ i ][ j ]
   Column




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




                                                1. int
                                                   2. float
                                            3. string
                                         4. char




                                                              Int const size1=?,size2=?;


      1   Programming C++ ©by ARAM-CSD
May 13, 2012 TWO-DIMENSIONAL ARRAYS (MATRIX)

                                              x     x
                                                                     x       x



Int array[size1][size2];

          int i,j;                       i,j,n,k,l,m,

                                                                                             main()

                                                                                                      {




                                                                                 cin, cout,if,
      for(i=0;i<size1;i++)
      {
      for(j=0;j<size2;j++)
      {
      _____
      _____
      }}
                                                                     size1         i
                                                                                   size1
                                                                         size2       j
                                                                                   size2
                                  n=2                   A2x2                           cin>>
      for(i=0;i<n;i++)
      {
      for(j=0;j<n;j++)
      {
      cin>>a[i][j];
      }}
            :                                      4           2x2       2


Cin>>a[i][j];
1234
      2   Programming C++ ©by ARAM-CSD
May 13, 2012 TWO-DIMENSIONAL ARRAYS (MATRIX)

 a[j=0]                      a[i=0]
                                                                      a[i=0][j=0]
                                                            j i
                                          <<” “   <<endl   cin>>

cin>>a[i][j]<<endl<<” “;




cin>>a[i][j];

}}

cout<<endl<<” “;



                                                                   cout<<

       for(i=0;i<n;i++)
       {
       for(j=0;j<n;j++)
       {
       cout<<a[i][j]<<” “;
       }
       cout<<endl;
       }



                                      1             2
                                      3             4
                                                     i
                                                                                    j

                                                                             getch();
                                                                                     }



       3   Programming C++ ©by ARAM-CSD
May 13, 2012 TWO-DIMENSIONAL ARRAYS (MATRIX)




//write C++ program to read and print the matrix A3x3 .
#include <iostream.h>
#include <conio.h>
                               Header
                                               123456789

int const n=3;
int a[n][n];              Declaration
int I,j;                                       1    2     3
for(i=0;i<n;i++)                               4    5     6
{
for(j=0;j<n;j++)                               7    8     9
{                             INPUT
cin>>a[i][j];
}}
cout<<endl;

for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cout <<a[i][j]<<” “;           OUTPUT
}
cout<<endl;
}

   getch();
   }                       END




         4    Programming C++ ©by ARAM-CSD
May 13, 2012 TWO-DIMENSIONAL ARRAYS (MATRIX)




     1- Write C++ program to read and print out the Matrix A3x3 .then print sum of all rows.
#include <iostream.h>
                                  123456789
#include <conio.h>
int const n=3;
int a[n][n];
int sum[n];                           1 2 3
int i,j;
                                      4     5 6
main()
{                                     7     8 9

for(i=0;i<n;i++)                      Sum of row (1) = 6
{
                                      Sum of row (2) = 15
for(j=0;j<n;j++)
{                                     Sum of row (3) = 24
cin>>a[i][j];
}}
cout<<endl;

for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cout<<a[i][j]<<" ";
}
cout<<endl;
}
cout<<endl;
for(i=0;i<n;i++)
{
 sum[i]=0;
for(j=0;j<n;j++)
{
sum[i]=sum[i]+a[i][j];
}
cout<<"Sum of row ("<<(i+1)<<")="<<sum[i]<<endl;
}
getch();
}



         5   Programming C++ ©by ARAM-CSD
May 13, 2012 TWO-DIMENSIONAL ARRAYS (MATRIX)



2-Write C++ program to read and print out the Matrix A3x3 .then print sum of all columns.
#include <iostream.h>
                                  123456789
#include <conio.h>
int const n=3;
int a[n][n];
int sum[n];                           1     2 3
int i,j;
main()                                4     5 6
{
                                      7     8 9
for(i=0;i<n;i++)
                                      Sum of column (1) = 12
{
for(j=0;j<n;j++)
                                      Sum of column (2) = 15
{
cin>>a[i][j];                         Sum of column (3) = 18
}}
cout<<endl;

for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cout<<a[i][j]<<" ";
}
cout<<endl;
}
cout<<endl;
for(j=0;j<n;j++)
{
 sum[j]=0;
for(i=0;i<n;i++)
{
sum[j]=sum[j]+a[i][j];
}
cout<<"Sum of column ("<<(j+1)<<")="<<sum[j]<<endl;
}
getch();
}




         6   Programming C++ ©by ARAM-CSD
May 13, 2012 TWO-DIMENSIONAL ARRAYS (MATRIX)




   3-write a program to read and printout Matrix Anxm, then find the sum of each row
   in A and Multiplication of each column in A.
#include <iostream.h>
#include <conio.h>
int const n=3,m=2;
int a[n][m];
int i,j,sum[n],mult[m];
main()
{
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
cin>>a[i][j];
}}
cout<<endl;
cout<<"A1"<<"t"<<"A2"<<endl;
cout<<"___________"<<endl;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
cout<<a[i][j]<<"t";
}
cout<<endl;
}
cout<<"____________"<<endl;
for(i=0;i<n;i++)
{ sum[i]=0;
for(j=0;j<m;j++)
{
sum[i]=sum[i]+a[i][j];
}
cout<<"Sum of row ["<<(i+1)<<"]="<<sum[i]<<endl;
}
cout<<"_________________n"<<endl;
for(j=0;j<m;j++)
{ mult[j]=1;
for(i=0;i<n;i++)
{
mult[j]=mult[j]*a[i][j];
}
cout<<"Mult of col ["<<(j+1)<<"]="<<mult[j]<<endl;
}
getch();      7 Programming C++ ©by ARAM-CSD
}
May 13, 2012 TWO-DIMENSIONAL ARRAYS (MATRIX)




 4- Write program to create an array A2x2. first row are EVEN and second row are ODD.
#include <iostream.h>
#include <conio.h>                                          1 2 3 4
int const n=2;
int a[n][n];                                                __      __
int i,j;
main()                                                      1         2
{
for(i=0;i<n;i++)                                            3         4
{
for(j=0;j<n;j++)                                            ___________
{
cin>>a[i][j];
                                                            __      __
}}
cout<<endl;
cout<<"__"<<"t"<<"__"<<endl;
                                                            2         4
for(i=0;i<n;i++)
{
                                                            1         3
for(j=0;j<n;j++)
{
cout<<a[i][j]<<"t";
}
cout<<endl;
}
cout<<"n__________________n";

cout<<"__"<<"t"<<"__"<<endl;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(a[i][j]%2==0)
cout<<a[i][j]<<"t";
}}
cout<<endl;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(a[i][j]%2==1)
cout<<a[i][j]<<"t";
}}
getch();
}
         8   Programming C++ ©by ARAM-CSD
May 13, 2012 TWO-DIMENSIONAL ARRAYS (MATRIX)



    5-Write C++ program to read and print out the Matrix A3x3 when Diagonal equal to Zero.

#include <iostream.h>
#include <conio.h>
int const n=3;
                                         1 2 3 4 5 6 7 8 9
int a[n][n];
int i,j;                                 0   2       3
main()
{
for(i=0;i<n;i++)                         4   0       6
{
for(j=0;j<n;j++)                         7   8       0
{
cin>>a[i][j];
}}
cout<<endl;

for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i==j)
a[i][j]=0;
}}
cout<<endl;

for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cout<<a[i][j]<<" ";
}
cout<<"nn";
}
getch();
}
      9   Programming C++ ©by ARAM-CSD
May 13, 2012 TWO-DIMENSIONAL ARRAYS (MATRIX)



6-Write C++ program to read and print out the Matrix A3x3 .then print sum of lower triangular.

#include <iostream.h>
#include <conio.h>
                                     1 2 3 4 5 6 7 8 9
int const n=3;
int a[n][n];                         1 2 3
int i,j,l;
main()                               4 5 6
{                                    7 8 9
for(i=0;i<n;i++)
{                                    Sum of Lower Triangular=11
for(j=0;j<n;j++)
{
cin>>a[i][j];
}}
cout<<endl;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cout<<a[i][j]<<" ";
}
cout<<"nn";
}
l=0;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i<j)
l=l+a[i][j];
}
}
cout<<"Sum of Lower Triangular="<<l;
getch();
}

      10   Programming C++ ©by ARAM-CSD
May 13, 2012 TWO-DIMENSIONAL ARRAYS (MATRIX)



7- Write C++ program to read and print out the Matrix A3x3 .then print sum of Upper triangular.

#include <iostream.h>
#include <conio.h>
                                              1 2 3 4 5 6 7 8 9
int const n=3;
int a[n][n];                                  1 2       3
int i,j,u;
main()                                        4     5    6
{                                             7     8    9
for(i=0;i<n;i++)
{                                             Sum of Upper Triangular=19
for(j=0;j<n;j++)
{
cin>>a[i][j];
}}
cout<<endl;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cout<<a[i][j]<<" ";
}
cout<<"nn";
}
u=0;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i>j)
u=u+a[i][j];
}
}
cout<<"Sum of Upper Triangular="<<l;
getch();
}

      11   Programming C++ ©by ARAM-CSD
May 13, 2012 TWO-DIMENSIONAL ARRAYS (MATRIX)



8- Write C++ program read and print the matrix A3x3, and find the Identity of the matrix.
#include <iostream.h>
#include <conio.h>                          1 2 3 4 5 6 7 8 9
int const n=3;
int a[n][n];
int i,j,k;                                  1 2 3
main()                                      4 5 6
{
                                            7 8 9
for(i=0;i<n;i++)
{                                           The identity of the Matrix
for(j=0;j<n;j++)                            ______________________
{
                                            1 0 0
cin>>a[i][j];
}}                                          0 1 0
cout<<endl;                                 0 0 1
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cout<<a[i][j]<<" ";
}cout<<endl;}
cout<<"nThe Identity of the Matrixn"<<"_____________________n";
k=0;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{if(i==j)
a[i][j]=1;
else
a[i][j]=0;
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{cout<<a[i][j]<<" ";
}cout<<"n";
}
getch();
}


     12   Programming C++ ©by ARAM-CSD
May 13, 2012 TWO-DIMENSIONAL ARRAYS (MATRIX)



9- Write C++ program read and print the matrix A3x3, and find the Trace of the matrix.
#include <iostream.h>
#include <conio.h>                        1 2 3 4 5 6 7 8 9
int const n=3;
int a[n][n];
int i,j,k;                                1 2 3
main()                                    4 5 6
{
                                          7 8 9
for(i=0;i<n;i++)
{                                         The Trace of the Matrix=15
for(j=0;j<n;j++)
{
cin>>a[i][j];
}}
cout<<endl;

for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cout<<a[i][j]<<" ";
}
cout<<endl;
}
cout<<endl;

k=0;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i==j)
{
k=k+a[i][j];
}
}}
cout<<"nThe Trace of the Matrix="<<k;

getch();
}


      13   Programming C++ ©by ARAM-CSD
May 13, 2012 TWO-DIMENSIONAL ARRAYS (MATRIX)

10- Write C++ program read and print the matrix A3x3, and make the lower triangular=0.
 #include <iostream.h>
 #include <conio.h>                      1 2 3 4 5 6 7 8 9
 int const n=3;
 int a[n][n];
                                         1 2 3
 int i,j;
 main()                                  4 5 6
 {                                       7 8 9
 for(i=0;i<n;i++)
 {
 for(j=0;j<n;j++)                        1   0   0
 {                                       4   5   0
 cin>>a[i][j];
                                         7   8   9
 }}
 cout<<endl;
 for(i=0;i<n;i++)
 {
 for(j=0;j<n;j++)
 {
 cout<<a[i][j]<<" ";
 }cout<<endl;}
 cout<<endl;
 for(i=0;i<n;i++)
 {
 for(j=0;j<n;j++)
 {if(i<j)
 a[i][j]=0;
 cout<<a[i][j]<<” “;
 }
 cout<<endl;
 }
 getch();
 }




     14   Programming C++ ©by ARAM-CSD
May 13, 2012 TWO-DIMENSIONAL ARRAYS (MATRIX)

11- Write C++ program read and print the matrix A3x3, then check whether is symmetric or
not.
 #include <iostream.h>
 #include <conio.h>                      1 2 3 4 5 6 7 8 9
 int const n=3;
 int a[n][n];
 int i,j,k;                              1 2 3
 main()                                  4 5 6
 {
                                         7 8 9
 for(i=0;i<n;i++)
 {                                       The Matrix is not symmetric
 for(j=0;j<n;j++)
 {
 cin>>a[i][j];
 }}
 cout<<endl;
 for(i=0;i<n;i++)
 {
 for(j=0;j<n;j++)
                                         1 2 3 2 5 6 3 6 9
 {
 cout<<a[i][j]<<" ";
 }cout<<endl;}                           1 2 3
 cout<<endl;
                                         2 5 6
 k=0;
 for(i=0;i<n;i++)                        3 6 9
 {                                       The Matrix is symmetric
 for(j=0;j<n;j++)
 {if(a[i][j]==a[i][j])
 k=k+1;
 }}
 if(k==n*n)
 cout<<”The Matrix is symmetric”;
 else
 cout<<”The Matrix is not symmetric”;
 getch();
 }




     15   Programming C++ ©by ARAM-CSD
May 13, 2012 TWO-DIMENSIONAL ARRAYS (MATRIX)

12- Write C++ program read and print the matrix A3x3, and make the Upper triangular=0.
#include <iostream.h>
#include <conio.h>
                                         1 2 3 4 5 6 7 8 9
int const n=3;
int a[n][n];                             1 2 3
int i,j;
main()
                                         4 5 6
{                                        7 8 9
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
                                         1   2   3
{                                        0   5   6
cin>>a[i][j];                            0   0   9
}}
cout<<endl;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cout<<a[i][j]<<" ";
}cout<<endl;}
cout<<endl;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{if(i>j)
a[i][j]=0;
cout<<a[i][j]<<” “;
}
cout<<endl;
}
getch();
}




     16   Programming C++ ©by ARAM-CSD

Weitere ähnliche Inhalte

Was ist angesagt?

C Code and the Art of Obfuscation
C Code and the Art of ObfuscationC Code and the Art of Obfuscation
C Code and the Art of Obfuscationguest9006ab
 
Rainer Grimm, “Functional Programming in C++11”
Rainer Grimm, “Functional Programming in C++11”Rainer Grimm, “Functional Programming in C++11”
Rainer Grimm, “Functional Programming in C++11”Platonov Sergey
 
Image Cryptography and Steganography
Image Cryptography and SteganographyImage Cryptography and Steganography
Image Cryptography and SteganographyMohammad Amin Amjadi
 
Ejercicios de programacion
Ejercicios de programacionEjercicios de programacion
Ejercicios de programacionJeff Tu Pechito
 
Class list data structure
Class list data structureClass list data structure
Class list data structureKatang Isip
 
Bartosz Milewski, “Re-discovering Monads in C++”
Bartosz Milewski, “Re-discovering Monads in C++”Bartosz Milewski, “Re-discovering Monads in C++”
Bartosz Milewski, “Re-discovering Monads in C++”Platonov Sergey
 
The International Journal of Engineering and Science
The International Journal of Engineering and ScienceThe International Journal of Engineering and Science
The International Journal of Engineering and Sciencetheijes
 
2020 1학기 정기스터디 2주차
2020 1학기 정기스터디 2주차2020 1학기 정기스터디 2주차
2020 1학기 정기스터디 2주차Moonki Choi
 
CS253: Minimum spanning Trees (2019)
CS253: Minimum spanning Trees (2019)CS253: Minimum spanning Trees (2019)
CS253: Minimum spanning Trees (2019)Jinho Choi
 
Cn os-lp lab manual k.roshan
Cn os-lp lab manual k.roshanCn os-lp lab manual k.roshan
Cn os-lp lab manual k.roshanriturajj
 
Yoyak ScalaDays 2015
Yoyak ScalaDays 2015Yoyak ScalaDays 2015
Yoyak ScalaDays 2015ihji
 
Datastructure tree
Datastructure treeDatastructure tree
Datastructure treerantd
 
Низкоуровневые оптимизации .NET-приложений
Низкоуровневые оптимизации .NET-приложенийНизкоуровневые оптимизации .NET-приложений
Низкоуровневые оптимизации .NET-приложенийAndrey Akinshin
 
An Introduction to Coding Theory
An Introduction to Coding TheoryAn Introduction to Coding Theory
An Introduction to Coding TheoryAlexanderWei11
 

Was ist angesagt? (19)

Computer Practical XII
Computer Practical XIIComputer Practical XII
Computer Practical XII
 
C Code and the Art of Obfuscation
C Code and the Art of ObfuscationC Code and the Art of Obfuscation
C Code and the Art of Obfuscation
 
662305 LAB13
662305 LAB13662305 LAB13
662305 LAB13
 
PRACTICAL COMPUTING
PRACTICAL COMPUTINGPRACTICAL COMPUTING
PRACTICAL COMPUTING
 
Rainer Grimm, “Functional Programming in C++11”
Rainer Grimm, “Functional Programming in C++11”Rainer Grimm, “Functional Programming in C++11”
Rainer Grimm, “Functional Programming in C++11”
 
Image Cryptography and Steganography
Image Cryptography and SteganographyImage Cryptography and Steganography
Image Cryptography and Steganography
 
Monad
MonadMonad
Monad
 
Ejercicios de programacion
Ejercicios de programacionEjercicios de programacion
Ejercicios de programacion
 
Class list data structure
Class list data structureClass list data structure
Class list data structure
 
Bartosz Milewski, “Re-discovering Monads in C++”
Bartosz Milewski, “Re-discovering Monads in C++”Bartosz Milewski, “Re-discovering Monads in C++”
Bartosz Milewski, “Re-discovering Monads in C++”
 
The International Journal of Engineering and Science
The International Journal of Engineering and ScienceThe International Journal of Engineering and Science
The International Journal of Engineering and Science
 
Container adapters
Container adaptersContainer adapters
Container adapters
 
2020 1학기 정기스터디 2주차
2020 1학기 정기스터디 2주차2020 1학기 정기스터디 2주차
2020 1학기 정기스터디 2주차
 
CS253: Minimum spanning Trees (2019)
CS253: Minimum spanning Trees (2019)CS253: Minimum spanning Trees (2019)
CS253: Minimum spanning Trees (2019)
 
Cn os-lp lab manual k.roshan
Cn os-lp lab manual k.roshanCn os-lp lab manual k.roshan
Cn os-lp lab manual k.roshan
 
Yoyak ScalaDays 2015
Yoyak ScalaDays 2015Yoyak ScalaDays 2015
Yoyak ScalaDays 2015
 
Datastructure tree
Datastructure treeDatastructure tree
Datastructure tree
 
Низкоуровневые оптимизации .NET-приложений
Низкоуровневые оптимизации .NET-приложенийНизкоуровневые оптимизации .NET-приложений
Низкоуровневые оптимизации .NET-приложений
 
An Introduction to Coding Theory
An Introduction to Coding TheoryAn Introduction to Coding Theory
An Introduction to Coding Theory
 

Ähnlich wie 2D Array Matrix Tutorial in C

Programa.eje
Programa.ejePrograma.eje
Programa.ejeguapi387
 
PPS Arrays Matrix operations
PPS Arrays Matrix operationsPPS Arrays Matrix operations
PPS Arrays Matrix operationsSreedhar Chowdam
 
2 d array(part 1) || 2D ARRAY FUNCTION WRITING || GET 100% MARKS IN CBSE CS
2 d array(part 1) || 2D ARRAY FUNCTION WRITING || GET 100% MARKS IN CBSE CS2 d array(part 1) || 2D ARRAY FUNCTION WRITING || GET 100% MARKS IN CBSE CS
2 d array(part 1) || 2D ARRAY FUNCTION WRITING || GET 100% MARKS IN CBSE CSAAKASH KUMAR
 
OOP 2012 - Hint: Dynamic allocation in c++
OOP 2012 - Hint: Dynamic allocation in c++OOP 2012 - Hint: Dynamic allocation in c++
OOP 2012 - Hint: Dynamic allocation in c++Allan Sun
 
Programming Fundamentals Arrays and Strings
Programming Fundamentals   Arrays and Strings Programming Fundamentals   Arrays and Strings
Programming Fundamentals Arrays and Strings imtiazalijoono
 
Write Python for Speed
Write Python for SpeedWrite Python for Speed
Write Python for SpeedYung-Yu Chen
 
Microsoft Word Hw#1
Microsoft Word   Hw#1Microsoft Word   Hw#1
Microsoft Word Hw#1kkkseld
 
Java Simple Programs
Java Simple ProgramsJava Simple Programs
Java Simple ProgramsUpender Upr
 
ParallelProgrammingBasics_v2.pdf
ParallelProgrammingBasics_v2.pdfParallelProgrammingBasics_v2.pdf
ParallelProgrammingBasics_v2.pdfChen-Hung Hu
 
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...ssuserd6b1fd
 
Introduction to cpp (c++)
Introduction to cpp (c++)Introduction to cpp (c++)
Introduction to cpp (c++)Arun Umrao
 
C Prog - Pointers
C Prog - PointersC Prog - Pointers
C Prog - Pointersvinay arora
 

Ähnlich wie 2D Array Matrix Tutorial in C (20)

Computer Programming- Lecture 9
Computer Programming- Lecture 9Computer Programming- Lecture 9
Computer Programming- Lecture 9
 
Programa.eje
Programa.ejePrograma.eje
Programa.eje
 
PPS Arrays Matrix operations
PPS Arrays Matrix operationsPPS Arrays Matrix operations
PPS Arrays Matrix operations
 
array2d.ppt
array2d.pptarray2d.ppt
array2d.ppt
 
2 d array(part 1) || 2D ARRAY FUNCTION WRITING || GET 100% MARKS IN CBSE CS
2 d array(part 1) || 2D ARRAY FUNCTION WRITING || GET 100% MARKS IN CBSE CS2 d array(part 1) || 2D ARRAY FUNCTION WRITING || GET 100% MARKS IN CBSE CS
2 d array(part 1) || 2D ARRAY FUNCTION WRITING || GET 100% MARKS IN CBSE CS
 
OOP 2012 - Hint: Dynamic allocation in c++
OOP 2012 - Hint: Dynamic allocation in c++OOP 2012 - Hint: Dynamic allocation in c++
OOP 2012 - Hint: Dynamic allocation in c++
 
Programming Fundamentals Arrays and Strings
Programming Fundamentals   Arrays and Strings Programming Fundamentals   Arrays and Strings
Programming Fundamentals Arrays and Strings
 
Arrays
ArraysArrays
Arrays
 
Write Python for Speed
Write Python for SpeedWrite Python for Speed
Write Python for Speed
 
Microsoft Word Hw#1
Microsoft Word   Hw#1Microsoft Word   Hw#1
Microsoft Word Hw#1
 
Vcs16
Vcs16Vcs16
Vcs16
 
Java Simple Programs
Java Simple ProgramsJava Simple Programs
Java Simple Programs
 
C lab excellent
C lab excellentC lab excellent
C lab excellent
 
C and Data Structures Lab Solutions
C and Data Structures Lab SolutionsC and Data Structures Lab Solutions
C and Data Structures Lab Solutions
 
C and Data Structures
C and Data Structures C and Data Structures
C and Data Structures
 
C programs
C programsC programs
C programs
 
ParallelProgrammingBasics_v2.pdf
ParallelProgrammingBasics_v2.pdfParallelProgrammingBasics_v2.pdf
ParallelProgrammingBasics_v2.pdf
 
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
 
Introduction to cpp (c++)
Introduction to cpp (c++)Introduction to cpp (c++)
Introduction to cpp (c++)
 
C Prog - Pointers
C Prog - PointersC Prog - Pointers
C Prog - Pointers
 

Kürzlich hochgeladen

The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 

Kürzlich hochgeladen (20)

The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 

2D Array Matrix Tutorial in C

  • 1. May 13, 2012 TWO-DIMENSIONAL ARRAYS (MATRIX) Type array [size1][size2]; Row A[ i ][ j ] Column #include <iostream.h> #include <conio.h> #include <math.h> #include <cstring.h> 1. int 2. float 3. string 4. char Int const size1=?,size2=?; 1 Programming C++ ©by ARAM-CSD
  • 2. May 13, 2012 TWO-DIMENSIONAL ARRAYS (MATRIX) x x x x Int array[size1][size2]; int i,j; i,j,n,k,l,m, main() { cin, cout,if, for(i=0;i<size1;i++) { for(j=0;j<size2;j++) { _____ _____ }} size1 i size1 size2 j size2 n=2 A2x2 cin>> for(i=0;i<n;i++) { for(j=0;j<n;j++) { cin>>a[i][j]; }} : 4 2x2 2 Cin>>a[i][j]; 1234 2 Programming C++ ©by ARAM-CSD
  • 3. May 13, 2012 TWO-DIMENSIONAL ARRAYS (MATRIX) a[j=0] a[i=0] a[i=0][j=0] j i <<” “ <<endl cin>> cin>>a[i][j]<<endl<<” “; cin>>a[i][j]; }} cout<<endl<<” “; cout<< for(i=0;i<n;i++) { for(j=0;j<n;j++) { cout<<a[i][j]<<” “; } cout<<endl; } 1 2 3 4 i j getch(); } 3 Programming C++ ©by ARAM-CSD
  • 4. May 13, 2012 TWO-DIMENSIONAL ARRAYS (MATRIX) //write C++ program to read and print the matrix A3x3 . #include <iostream.h> #include <conio.h> Header 123456789 int const n=3; int a[n][n]; Declaration int I,j; 1 2 3 for(i=0;i<n;i++) 4 5 6 { for(j=0;j<n;j++) 7 8 9 { INPUT cin>>a[i][j]; }} cout<<endl; for(i=0;i<n;i++) { for(j=0;j<n;j++) { cout <<a[i][j]<<” “; OUTPUT } cout<<endl; } getch(); } END 4 Programming C++ ©by ARAM-CSD
  • 5. May 13, 2012 TWO-DIMENSIONAL ARRAYS (MATRIX) 1- Write C++ program to read and print out the Matrix A3x3 .then print sum of all rows. #include <iostream.h> 123456789 #include <conio.h> int const n=3; int a[n][n]; int sum[n]; 1 2 3 int i,j; 4 5 6 main() { 7 8 9 for(i=0;i<n;i++) Sum of row (1) = 6 { Sum of row (2) = 15 for(j=0;j<n;j++) { Sum of row (3) = 24 cin>>a[i][j]; }} cout<<endl; for(i=0;i<n;i++) { for(j=0;j<n;j++) { cout<<a[i][j]<<" "; } cout<<endl; } cout<<endl; for(i=0;i<n;i++) { sum[i]=0; for(j=0;j<n;j++) { sum[i]=sum[i]+a[i][j]; } cout<<"Sum of row ("<<(i+1)<<")="<<sum[i]<<endl; } getch(); } 5 Programming C++ ©by ARAM-CSD
  • 6. May 13, 2012 TWO-DIMENSIONAL ARRAYS (MATRIX) 2-Write C++ program to read and print out the Matrix A3x3 .then print sum of all columns. #include <iostream.h> 123456789 #include <conio.h> int const n=3; int a[n][n]; int sum[n]; 1 2 3 int i,j; main() 4 5 6 { 7 8 9 for(i=0;i<n;i++) Sum of column (1) = 12 { for(j=0;j<n;j++) Sum of column (2) = 15 { cin>>a[i][j]; Sum of column (3) = 18 }} cout<<endl; for(i=0;i<n;i++) { for(j=0;j<n;j++) { cout<<a[i][j]<<" "; } cout<<endl; } cout<<endl; for(j=0;j<n;j++) { sum[j]=0; for(i=0;i<n;i++) { sum[j]=sum[j]+a[i][j]; } cout<<"Sum of column ("<<(j+1)<<")="<<sum[j]<<endl; } getch(); } 6 Programming C++ ©by ARAM-CSD
  • 7. May 13, 2012 TWO-DIMENSIONAL ARRAYS (MATRIX) 3-write a program to read and printout Matrix Anxm, then find the sum of each row in A and Multiplication of each column in A. #include <iostream.h> #include <conio.h> int const n=3,m=2; int a[n][m]; int i,j,sum[n],mult[m]; main() { for(i=0;i<n;i++) { for(j=0;j<m;j++) { cin>>a[i][j]; }} cout<<endl; cout<<"A1"<<"t"<<"A2"<<endl; cout<<"___________"<<endl; for(i=0;i<n;i++) { for(j=0;j<m;j++) { cout<<a[i][j]<<"t"; } cout<<endl; } cout<<"____________"<<endl; for(i=0;i<n;i++) { sum[i]=0; for(j=0;j<m;j++) { sum[i]=sum[i]+a[i][j]; } cout<<"Sum of row ["<<(i+1)<<"]="<<sum[i]<<endl; } cout<<"_________________n"<<endl; for(j=0;j<m;j++) { mult[j]=1; for(i=0;i<n;i++) { mult[j]=mult[j]*a[i][j]; } cout<<"Mult of col ["<<(j+1)<<"]="<<mult[j]<<endl; } getch(); 7 Programming C++ ©by ARAM-CSD }
  • 8. May 13, 2012 TWO-DIMENSIONAL ARRAYS (MATRIX) 4- Write program to create an array A2x2. first row are EVEN and second row are ODD. #include <iostream.h> #include <conio.h> 1 2 3 4 int const n=2; int a[n][n]; __ __ int i,j; main() 1 2 { for(i=0;i<n;i++) 3 4 { for(j=0;j<n;j++) ___________ { cin>>a[i][j]; __ __ }} cout<<endl; cout<<"__"<<"t"<<"__"<<endl; 2 4 for(i=0;i<n;i++) { 1 3 for(j=0;j<n;j++) { cout<<a[i][j]<<"t"; } cout<<endl; } cout<<"n__________________n"; cout<<"__"<<"t"<<"__"<<endl; for(i=0;i<n;i++) { for(j=0;j<n;j++) { if(a[i][j]%2==0) cout<<a[i][j]<<"t"; }} cout<<endl; for(i=0;i<n;i++) { for(j=0;j<n;j++) { if(a[i][j]%2==1) cout<<a[i][j]<<"t"; }} getch(); } 8 Programming C++ ©by ARAM-CSD
  • 9. May 13, 2012 TWO-DIMENSIONAL ARRAYS (MATRIX) 5-Write C++ program to read and print out the Matrix A3x3 when Diagonal equal to Zero. #include <iostream.h> #include <conio.h> int const n=3; 1 2 3 4 5 6 7 8 9 int a[n][n]; int i,j; 0 2 3 main() { for(i=0;i<n;i++) 4 0 6 { for(j=0;j<n;j++) 7 8 0 { cin>>a[i][j]; }} cout<<endl; for(i=0;i<n;i++) { for(j=0;j<n;j++) { if(i==j) a[i][j]=0; }} cout<<endl; for(i=0;i<n;i++) { for(j=0;j<n;j++) { cout<<a[i][j]<<" "; } cout<<"nn"; } getch(); } 9 Programming C++ ©by ARAM-CSD
  • 10. May 13, 2012 TWO-DIMENSIONAL ARRAYS (MATRIX) 6-Write C++ program to read and print out the Matrix A3x3 .then print sum of lower triangular. #include <iostream.h> #include <conio.h> 1 2 3 4 5 6 7 8 9 int const n=3; int a[n][n]; 1 2 3 int i,j,l; main() 4 5 6 { 7 8 9 for(i=0;i<n;i++) { Sum of Lower Triangular=11 for(j=0;j<n;j++) { cin>>a[i][j]; }} cout<<endl; for(i=0;i<n;i++) { for(j=0;j<n;j++) { cout<<a[i][j]<<" "; } cout<<"nn"; } l=0; for(i=0;i<n;i++) { for(j=0;j<n;j++) { if(i<j) l=l+a[i][j]; } } cout<<"Sum of Lower Triangular="<<l; getch(); } 10 Programming C++ ©by ARAM-CSD
  • 11. May 13, 2012 TWO-DIMENSIONAL ARRAYS (MATRIX) 7- Write C++ program to read and print out the Matrix A3x3 .then print sum of Upper triangular. #include <iostream.h> #include <conio.h> 1 2 3 4 5 6 7 8 9 int const n=3; int a[n][n]; 1 2 3 int i,j,u; main() 4 5 6 { 7 8 9 for(i=0;i<n;i++) { Sum of Upper Triangular=19 for(j=0;j<n;j++) { cin>>a[i][j]; }} cout<<endl; for(i=0;i<n;i++) { for(j=0;j<n;j++) { cout<<a[i][j]<<" "; } cout<<"nn"; } u=0; for(i=0;i<n;i++) { for(j=0;j<n;j++) { if(i>j) u=u+a[i][j]; } } cout<<"Sum of Upper Triangular="<<l; getch(); } 11 Programming C++ ©by ARAM-CSD
  • 12. May 13, 2012 TWO-DIMENSIONAL ARRAYS (MATRIX) 8- Write C++ program read and print the matrix A3x3, and find the Identity of the matrix. #include <iostream.h> #include <conio.h> 1 2 3 4 5 6 7 8 9 int const n=3; int a[n][n]; int i,j,k; 1 2 3 main() 4 5 6 { 7 8 9 for(i=0;i<n;i++) { The identity of the Matrix for(j=0;j<n;j++) ______________________ { 1 0 0 cin>>a[i][j]; }} 0 1 0 cout<<endl; 0 0 1 for(i=0;i<n;i++) { for(j=0;j<n;j++) { cout<<a[i][j]<<" "; }cout<<endl;} cout<<"nThe Identity of the Matrixn"<<"_____________________n"; k=0; for(i=0;i<n;i++) { for(j=0;j<n;j++) {if(i==j) a[i][j]=1; else a[i][j]=0; } } for(i=0;i<n;i++) { for(j=0;j<n;j++) {cout<<a[i][j]<<" "; }cout<<"n"; } getch(); } 12 Programming C++ ©by ARAM-CSD
  • 13. May 13, 2012 TWO-DIMENSIONAL ARRAYS (MATRIX) 9- Write C++ program read and print the matrix A3x3, and find the Trace of the matrix. #include <iostream.h> #include <conio.h> 1 2 3 4 5 6 7 8 9 int const n=3; int a[n][n]; int i,j,k; 1 2 3 main() 4 5 6 { 7 8 9 for(i=0;i<n;i++) { The Trace of the Matrix=15 for(j=0;j<n;j++) { cin>>a[i][j]; }} cout<<endl; for(i=0;i<n;i++) { for(j=0;j<n;j++) { cout<<a[i][j]<<" "; } cout<<endl; } cout<<endl; k=0; for(i=0;i<n;i++) { for(j=0;j<n;j++) { if(i==j) { k=k+a[i][j]; } }} cout<<"nThe Trace of the Matrix="<<k; getch(); } 13 Programming C++ ©by ARAM-CSD
  • 14. May 13, 2012 TWO-DIMENSIONAL ARRAYS (MATRIX) 10- Write C++ program read and print the matrix A3x3, and make the lower triangular=0. #include <iostream.h> #include <conio.h> 1 2 3 4 5 6 7 8 9 int const n=3; int a[n][n]; 1 2 3 int i,j; main() 4 5 6 { 7 8 9 for(i=0;i<n;i++) { for(j=0;j<n;j++) 1 0 0 { 4 5 0 cin>>a[i][j]; 7 8 9 }} cout<<endl; for(i=0;i<n;i++) { for(j=0;j<n;j++) { cout<<a[i][j]<<" "; }cout<<endl;} cout<<endl; for(i=0;i<n;i++) { for(j=0;j<n;j++) {if(i<j) a[i][j]=0; cout<<a[i][j]<<” “; } cout<<endl; } getch(); } 14 Programming C++ ©by ARAM-CSD
  • 15. May 13, 2012 TWO-DIMENSIONAL ARRAYS (MATRIX) 11- Write C++ program read and print the matrix A3x3, then check whether is symmetric or not. #include <iostream.h> #include <conio.h> 1 2 3 4 5 6 7 8 9 int const n=3; int a[n][n]; int i,j,k; 1 2 3 main() 4 5 6 { 7 8 9 for(i=0;i<n;i++) { The Matrix is not symmetric for(j=0;j<n;j++) { cin>>a[i][j]; }} cout<<endl; for(i=0;i<n;i++) { for(j=0;j<n;j++) 1 2 3 2 5 6 3 6 9 { cout<<a[i][j]<<" "; }cout<<endl;} 1 2 3 cout<<endl; 2 5 6 k=0; for(i=0;i<n;i++) 3 6 9 { The Matrix is symmetric for(j=0;j<n;j++) {if(a[i][j]==a[i][j]) k=k+1; }} if(k==n*n) cout<<”The Matrix is symmetric”; else cout<<”The Matrix is not symmetric”; getch(); } 15 Programming C++ ©by ARAM-CSD
  • 16. May 13, 2012 TWO-DIMENSIONAL ARRAYS (MATRIX) 12- Write C++ program read and print the matrix A3x3, and make the Upper triangular=0. #include <iostream.h> #include <conio.h> 1 2 3 4 5 6 7 8 9 int const n=3; int a[n][n]; 1 2 3 int i,j; main() 4 5 6 { 7 8 9 for(i=0;i<n;i++) { for(j=0;j<n;j++) 1 2 3 { 0 5 6 cin>>a[i][j]; 0 0 9 }} cout<<endl; for(i=0;i<n;i++) { for(j=0;j<n;j++) { cout<<a[i][j]<<" "; }cout<<endl;} cout<<endl; for(i=0;i<n;i++) { for(j=0;j<n;j++) {if(i>j) a[i][j]=0; cout<<a[i][j]<<” “; } cout<<endl; } getch(); } 16 Programming C++ ©by ARAM-CSD