⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 q17.cpp

📁 一个对数组进行操作的源代码
💻 CPP
字号:
     // Q17
     #include <iostream.h>
     #include <conio.h>
     #include <iomanip.h>
     void INPUT(int A[][5], int);
     void DISPLAY(int A[][5], int);
     void DIAGONAL(int A[][5], int,int);
     int ARDiagonal(int [][5], int);
     int BRDiagonal(int [][5], int);
     int ALDiagonal(int [][5], int);
     int BLDiagonal(int [][5], int);
     void main()
     { clrscr();
	 int A[5][5], n, total=0;
	 do
	 { cout<<"Enter number of rows & columns of the square matrix ";
	   cin>>n;
	 } while(n<1 || n>5);
	 INPUT(A, n);
	 DISPLAY(A, n);
	 cout<<endl;
	 for(int i=0; i<n; i++)
	 { int sR=0;
	   for(int j=0; j<n; j++)
	   { sR+=A[i][j];
	     total+=A[i][j];
	   }
	   cout<<"Sum of row "<<i+1<<" is "<<sR<<endl;
	 }
	 for(i=0; i<n; i++)
	 { int sC=0;
	   for(int j=0; j<n; j++)
	    sC+=A[j][i];
	    cout<<"Sum of column "<<i+1<<" is "<<sC<<endl;
	 }
	 DIAGONAL(A, n, total);
	 cout<<"Sum of elements above right diagonal is "<<ARDiagonal(A, n)<<endl;
	 cout<<"Sum of elements above left diagonal is "<<ALDiagonal(A, n)<<endl;
	 cout<<"Sum of elements below right diagonal is "<<BRDiagonal(A, n)<<endl;
	 cout<<"Sum of elements below left diagonal is "<<BLDiagonal(A, n)<<endl;
	 getch();
     }

     void INPUT(int A[][5], int r)
     { for(int i=0; i<r; i++)
	 { for(int j=0; j<r; j++)
	   { cout<<"Enter element "<<i+1<<"X"<<j+1<<": ";
	     cin>>A[i][j];
	   }
	 }
     }

     void DISPLAY(int A[][5], int c)
     { for(int i=0; i<c; i++)
	 { for(int j=0; j<c; j++)
	   cout<<setw(10)<<A[i][j];
	   cout<<endl;
	 }
     }

     void DIAGONAL(int A[][5], int n, int total)
     { int lD=0, rD=0;
	 for(int i=0; i<n; i++)
	 { rD+=A[i][i];
	   lD+=A[n-1-i][i];
	 }
	 cout<<"The sum of right diagonal is "<<rD<<endl;
	 cout<<"The sum of left diagomal is "<<lD<<endl;
	 cout<<"The sum of non-diagonal elements is ";
	 if(n%2==0)
	 cout<<total-rD-lD;
	 else
	 cout<<total-rD-lD+A[n/2][n/2];
	 cout<<endl;
     }

     int ARDiagonal(int A[][5], int n)
     { int s=0;
	 for(int i=0; i<n-1; i++)
	  for(int j=i+1; j<n; j++)
	  s+=A[i][j];
	 return (s);
     }

     int ALDiagonal(int A[][5], int n)
     { int s=0;
	 for(int i=0; i<n-1; i++)
	  for(int j=0; j<n-1-i; j++)
	  s+=A[i][j];
	 return (s);
     }

     int BRDiagonal(int A[][5], int n)
     { int s=0;
	 for(int i=1; i<n; i++)
	  for(int j=0; j<i; j++)
	  s+=A[i][j];
	 return (s);
     }

     int BLDiagonal(int A[][5], int n)
     { int s=0;
	 for(int i=1; i<n; i++)
	  for(int j=n-i; j<n; j++)
	  s+=A[i][j];
	 return (s);
     }













⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -