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

using namespace std;

int main()
{
int A[10][10],m,n,x,y,sum=0;
//Create a Matrix
cout << "Enter number of rows and columns in Matrix A : \n";
cin>>n>>m;
cout << "Enter elements of Matrix A : \n";
for(x=1;x<n+1;++x)
for(y=1;y<m+1;++y)
cin>>A[x][y];
//Find sum of each row
for(x=1;x<n+1;++x)
{
A[x][m+1]=0;
for(y=1;y<m+1;++y)
A[x][m+1]=A[x][m+1]+A[x][y];
}
//Find sum of each column
for(y=1;y<m+1;++y)
{
A[n+1][y]=0;
for(x=1;x<n+1;++x)
A[n+1][y]+=A[x][y];
}
cout << "\nMatrix A, Row Sum (Last Column)" << " and Column Sum (Last Row) : \n";
for(x=1;x<n+1;++x)
{
for(y=1;y<m+2;++y)
cout << A[x][y] << "     ";
cout << "\n";
}
//Print sum of each column
x=n+1;
for(y=1;y<m+1;++y)
cout << A[x][y] << "     ";
cout << "\n";
if(m==n)
{
for(x=1;x<m+1;x++)
for(y=1;y<n+1;y++)
if(x==y)
sum+=A[x][y];
else
if(y==m-(x+1))
sum+=A[x][y];
}
cout << "Sum of diagonal elements is : " << sum << endl;
getch();
return 0;
}
``````

## All 6 Replies

``````How to add the menu case option for that code? example: 1.Sum Row
2.Sum column
3.Diagonal sum
4.exit
``````

Hey,

I'll give you an example.. Then you can just relevant insert the code where you need it.

``````int choice = 0;

cout << "Please enter an option: \n";
cout << "\t\t 1) Sum Row\n";
cout << "\t\t 2) Sum Column\n";
cout << "\t\t 3) Diagonal Sum\n";
cout << "\t\t 4) Quit\n";
cin >> choice;

switch(choice)
{
case 1:
// Sum row code
break;

case 2:
// Sum col row
break;

case 3:
// diagonal sum
break;

case 4:
// quite
return 0;
break;

default:
cout << "Option unknown";
break;

}
``````

thanks in advance has answered my question :), I've input all, is it true the code like the below???
, and I got an error messages "6 D:\Dev-Cpp\Untitledp.cpp expected unqualified-id before '{' token" , how can I fix it?

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

using namespace std;
int choice = 0;
{
cout << "Please enter an option: \n";
cout << "\t\t 1) Sum Row\n";
cout << "\t\t 2) Sum Column\n";
cout << "\t\t 3) Diagonal Sum\n";
cout << "\t\t 4) Quit\n";
cin >> choice;
switch(choice)
{
case 1:
// Sum row code

cout << "Enter number of rows and columns in Matrix A : \n";
cin>>n>>m;
cout << "Enter elements of Matrix A : \n";
for(x=1;x<n+1;++x)
for(y=1;y<m+1;++y)
cin>>A[x][y];

for(x=1;x<n+1;++x)
{
A[x][m+1]=0;
for(y=1;y<m+1;++y)
A[x][m+1]=A[x][m+1]+A[x][y];
}
break;

case 2:
// Sum col row
for(y=1;y<m+1;++y)
{
A[n+1][y]=0;
for(x=1;x<n+1;++x)
A[n+1][y]+=A[x][y];
}
cout << "\nMatrix A, Row Sum (Last Column)" << " and Column Sum (Last Row) : \n";
for(x=1;x<n+1;++x)
{
for(y=1;y<m+2;++y)
cout << A[x][y] << "     ";
cout << "\n";
}
x=n+1;
for(y=1;y<m+1;++y)
cout << A[x][y] << "     ";
cout << "\n";
if(m==n)
{
for(x=1;x<m+1;x++)
for(y=1;y<n+1;y++)
if(x==y)
sum+=A[x][y];
else
if(y==m-(x+1))
sum+=A[x][y];
}

break;
case 3:
// diagonal sum
cout << "Sum of diagonal elements is : " << sum << endl;

break;
case 4:
// quite
return 0;
break;
default:
cout << "Option unknown";
break;
}
``````

Apologies.

I purposly left out main, so, you need to include main..

``````int main()
{

}
``````

As you had it before, just implement my code. :)

Kinda like this (NOT COMPLETE):

``````#include <iostream>
using namespace std;

int main()
{
int choice = 0;
int A[10][10],m,n,x,y,sum=0;
//Create a Matrix
cout << "Enter number of rows and columns in Matrix A : \n";
cin>>n>>m;
cout << "Enter elements of Matrix A : \n";
for(x=1;x<n+1;++x)
for(y=1;y<m+1;++y)
cin>>A[x][y];

cout << "Please enter an option: \n";
cout << "\t\t 1) Sum Row\n";
cout << "\t\t 2) Sum Column\n";
cout << "\t\t 3) Diagonal Sum\n";
cout << "\t\t 4) Quit\n";
cin >> choice;

switch(choice)
{
case 1:
for(x=1;x<n+1;++x)
{
A[x][m+1]=0;
for(y=1;y<m+1;++y)
A[x][m+1]=A[x][m+1]+A[x][y];
}
break;

case 2:
for(y=1;y<m+1;++y)
{
A[n+1][y]=0;
for(x=1;x<n+1;++x)
A[n+1][y]+=A[x][y];
}
cout << "\nMatrix A, Row Sum (Last Column)" << " and Column Sum (Last Row) : \n";
for(x=1;x<n+1;++x)
{
for(y=1;y<m+2;++y)
cout << A[x][y] << "     ";
cout << "\n";
}
break;

case 3:
// diagonal sum
break;

case 4:
// quite
return 0;
break;

default:
cout << "Option unknown";
break;

}

}
``````

Hope this helps.

thanks for the answer :), but when I select the first menu program is exit, how to keep program run and can the calculation sum of each row, each column, and sum of diagonal 1 and diagonal 2, please help me..,

So you want the program to iterate until the user enter's quits?

If this is the case, just add a loop:

Before:
int choice = 0;

``````   while(choice != 4)
{
``````

And then close the bracket off at the end of the program:

``````}
``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.