我对最终的void函数有问题,我试图将其赋给声明为int l的变量,但由于函数的返回类型为void而我不能,并且由于行和列总和,我的确得到了相同的结果和总和,我对应该怎么做或如何分别计算行或列感到困惑,这里是个问题
编写一个菜单驱动的C ++程序,以对大小为m x n的二维数组A进行以下操作。您应该使用用户定义的函数,该函数接受二维数组A及其大小m和n作为参数。选项是:
将元素输入大小为m x n的矩阵 显示大小为m x n的矩阵的元素 大小为m x n的矩阵的所有元素之和 显示大小为m x n的矩阵的逐行求和 显示大小为m x n的矩阵的列式和 创建大小为n x n
的矩阵B的转置#include<iostream>
using namespace std;
void showcases();
void input_element(int A[][20], int& m, int& n);
void display_matrix(int A[][20], int& m, int& n);
int sum(int A[][20], int& m, int& n);
int row_sum(int A[][20], int& m, int& n);
int col_sum(int A[][20], int& m, int& n);
void transp(int A[][20], int B[][20], int& m, int& n);
int main()
{
int a = 0, b = 0, choice = 0, A[10][20], B[10][20], n = 0, x = 0, y = 0, z = 0, l = 0;
for (int k = 0; choice <= 6; k++)
{
showcases();
x = sum(A, a, b);
y = row_sum(A, a, b);
z = col_sum(A, a, b);
cin >> choice;
if (choice == 1) {
input_element(A, a, b);
}
if (choice == 2) {
display_matrix(A, a, b);
}
if (choice == 3) {
cout << "The sum of the matrix elements is " << x << endl;
}
if (choice == 4) {
cout << "The sum of the matrix rows is " << y << endl;
}
if (choice == 5) {
cout << "The sum of the matrix columns is " << z << endl;
}
if (choice == 6) {
transp(A, B, a, b);
cout << endl;
}
if (choice >= 7 || choice <= 0) {
cout << "Your choice is invalid" << endl;
}
}
system("pause");
return 0;
}
void input_element(int A[][20], int& m, int& n)
{
int row, col;
cout << "Enter the number of rows: ";
cin >> m;
cout << "Enter the number of columns: ";
cin >> n;
for (int r = 0; r < m; r++)
for (int colum = 0; colum < n; colum++)
{
cout << "Enter the elements for the array: ";
cin >> A[r][colum];
}
}
void display_matrix(int A[][20], int& m, int& n)
{
for (int r = 0; r < m; r++)
for (int colum = 0; colum < n; colum++)
{
cout << "Entered element is " << ": ";
cout << A[r][colum];
cout << endl;
}
}
int sum(int A[][20], int& m, int& n)
{
int s = 0, r, colum;
for (r = 0; r < m; r++)
{
for (colum = 0; colum < n; colum++)
{
s += A[r][colum];
}
}
return s;
}
int row_sum(int A[][20], int& m, int& n)
{
int r_s = 0, r, colum;
for (r = 0; r < m; r++)
{
for (colum = 0; colum < n; colum++)
{
r_s += A[r][colum];
}
}
return r_s;
}
int col_sum(int A[][20], int& m, int& n)
{
int col_s(0), colum, r;
for (r = 0; r < m; r++)
{
for (colum = 0; colum < n; colum++)
{
col_s += A[colum][r];
}
}
return col_s;
}
void transp(int A[][20], int B[][20], int& m, int& n)
{
int r, colum;
for (r = 0; r < m; r++)
{
for (colum = 0; colum < n; colum++)
B[r][colum] = A[colum][r];
}
for (r = 0; r < m; r++)
{
for (colum = 0; colum < n; colum++)
cout << B[r][colum];
}
return;
}
void showcases()
{
cout << "Menu" << endl;
cout << "1. input elements" << endl;
cout << "2. Display matrix" << endl;
cout << "3. Sum of matrix elements" << endl;
cout << "4. Sum of matrix rows" << endl;
cout << "5. Sum of matrix elements" << endl;
cout << "6. The transpose of elements" << endl;
cout << "7. Exit" << endl;
cout << "Choose among the options above: ";
}
我需要行和列的总和不同,并且我试图固定转置部分
答案 0 :(得分:0)
这是代码
#include<iostream>
using namespace std;
// protype
void showcases();
void accept(int Array[][20], int& a, int& b);
void outputs(int Array[][20], int& a, int& b);
int sum(int Array[][20], int& a, int& b);
void row_sum(int Array[][20], int& a, int& b);
void col_sum(int Array[][20], int& a, int& b);
void transposed(int Array[][20], int& a, int& b);
int main()
{
// variable declaration
int a = 0, b = 0, choice = 0, A[10][20], n = 0, x = 0, y = 0, z = 0;
// For loop execution
for (int k = 0; choice <= 6; k++)
{
x = sum(A, a, b);
showcases();
cin >> choice;
cout << "----------------------------------------------" << endl;
cout << "----------------------------------------------" << endl;
if (choice == 1) {
accept(A, a, b);
cout << "-----------------------------------------------------------" << endl << endl;
}
if (choice == 2) {
outputs(A, a, b);
cout << "-----------------------------------------------------------" << endl << endl;
}
if (choice == 3) {
cout << "The sum of the matrix elements is " << x << endl;
cout << "-----------------------------------------------------------" << endl << endl;
}
if (choice == 4) {
col_sum(A, a, b);
cout << "-----------------------------------------------------------" << endl << endl;
}
if (choice == 5) {
row_sum(A, a, b);
cout << "-----------------------------------------------------------" << endl << endl;
}
if (choice == 6) {
cout << "Elements of matrix after transpose are: " << endl;
transposed(A, a, b);
cout << endl;
cout << "-----------------------------------------------------------" << endl << endl;
}
if (choice >= 7 || choice <= 0) {
cout << "Your choice is invalid" << endl;
cout << "-----------------------------------------------------------" << endl << endl;
return -1;
}
}
system("pause");
return 0;
}
// To input the elements
void accept(int Array[][20], int& a, int& b)
{
cout << "Enter number of rows for matrix: ";
cin >> a;
cout << "Enter number of columns for matrix: ";
cin >> b;
for (int i = 0; i < a; i++)
{
for (int j = 0; j < b; j++)
{
// to keep a track of your position in array
cout << "Enter elements of matrix " << "[" << i << "]" << " [" << j << "]: ";
cin >> Array[i][j];
}
}
}
// TO output the elements
void outputs(int Array[][20], int& a, int& b)
{
for (int i = 0; i < a; i++)
{
for (int j = 0; j < b; j++)
{
cout << Array[i][j] << " ";
}
cout << endl;
}
}
// To find the total sum
int sum(int Array[][20], int& a, int& b)
{
int s = 0, i, j;
for (i = 0; i < a; i++)
{
for (j = 0; j < b; j++)
{
s += Array[i][j];
}
}
return s;
}
// To find the row sum
//you only need to find the row sum
void row_sum(int Array[][20], int& a, int& b)
{
int row_s = 0;
for (int i = 0; i < a; i++)
{
for (int j = 0; j < b; j++)
{
row_s += Array[i][j];
}
cout << "sum of" << i + 1 << " Row is " << row_s;
row_s = 0;
cout << endl;
}
return;
}
// To find the column sum
//Same you were suming up all the elements
void col_sum(int Array[][20], int& a, int& b)
{
int col_s = 0;
for (int i = 0; i < a; i++)
{
for (int j = 0; j < b; j++)
{
col_s += Array[j][i];
}
cout << "sum of" << i + 1 << " column is " << col_s;
col_s = 0;
cout << endl;
}
return;
}
// To transpose (To change the elements of rows and columns) the matrix
// you do not need to call b to store A
void transposed(int Array[][20], int& a, int& b)
{
for (int i = 0; i < a; i++)
{
for (int j = 0; j < b; j++)
{
cout << Array[j][i] << " ";
}
cout << endl;
}
return;
}
// To display (To showcase) the available choiced
void showcases()
{
cout << "Menu" << endl;
cout << "1. input elements" << endl;
cout << "2. Display matrix" << endl;
cout << "3. Sum of matrix elements" << endl;
cout << "4. Sum of matrix rows" << endl;
cout << "5. Sum of matrix elements" << endl;
cout << "6. The transpose of elements" << endl;
cout << "7. Exit" << endl;
cout << "Choose among the options above: ";
}