can any body tell me a udf program in c++ which accepts a squared integer matrix with odd dimensions(like 33,55....) & display the sum of the middle row and middle column elements.
please help me if you can................

Dani AI

Generated

The goal is simply to accept an odd-sized square matrix, find the middle index, and compute two sums: the middle row and the middle column. The code posted in #4 has several problems (old headers like <iostream.h> and conio.h, void main(), a hard-coded 3x3 size, and incorrect loop use such as for(i==1)), while correctly points out that the middle index is n/2 (integer division for 0-based indexing) and rightly called out the for(j==1) mistake. ’s suggestion to show test cases is a good practice.

Algorithm summary:

  • Read integer n and validate n > 0 and n % 2 == 1.
  • Read n*n integers into a 2D container.
  • Compute mid = n / 2.
  • Sum the middle row: sumRow = sum of a[mid][j] for j = 0..n-1.
  • Sum the middle column: sumCol = sum of a[i][mid] for i = 0..n-1.
    Note: if a single combined total of unique elements on the middle row+column is required, use sumRow + sumCol - a[mid][mid] to avoid double counting the center element.

Correct, minimal C++ (modern, no conio, dynamic n):

#include <iostream>
#include <vector>

int main() {
    int n;
    if (!(std::cin >> n)) return 0;
    if (n <= 0 || n % 2 == 0) {
        std::cout << "expecting positive odd n\n";
        return 0;
    }
    std::vector<std::vector<int>> a(n, std::vector<int>(n));
    for (int i = 0; i < n; ++i)
        for (int j = 0; j < n; ++j)
            std::cin >> a[i][j];

    int mid = n / 2;
    long long sumRow = 0, sumCol = 0;
    for (int j = 0; j < n; ++j) sumRow += a[mid][j];
    for (int i = 0; i < n; ++i) sumCol += a[i][mid];

    std::cout << "middle row sum: " << sumRow << '\n';
    std::cout << "middle column sum: " << sumCol << '\n';
    std::cout << "combined unique sum: " << (sumRow + sumCol - a[mid][mid]) << '\n';
    return 0;
}

Troubleshooting tips: test with small matrices (3x3, 5x5), check for off-by-one errors (0-based vs 1-based), validate input format (stdin vs file), and use a larger integer type for sums if element values can be large. This approach generalizes beyond 3x3 and fixes the logic problems shown in the thread.

Recommended Answers

All 5 Replies

When you ask a question, try to write it in a good form.. otherwise you won't get much help. first search yourself on forum, tell us how much have you worked, where are you stuck and things..
no one is going to spoon feed you here..

well as for the "mathametical" question:
if you have 5 rows it shows in c++ as 0-1-2-3-4
so you need to get the '2' row which can be done by just dividing by 2 (5 / 2 =2 - if you are using integers)
another example if there are 3 rows - 0-1-2
3/2 = 1 which is the row number 1 .

as for the c++ programming you have to study on your own

#include<iostream.h>
#include<conio.h>
void main()
{
  int a[3][3],i,j,sumr=0,sumc=0;
 clrscr();
 cout<<"enter the array of size 3*3";
 for(i=0;i<3;i++)
 {
     for(j=0;j<3;j++)
     cin>>a[i][j];
     }
for(i==1)
{ 
   for(j=0;j<3;j++)
   sumr+=a[i][j];
   }
 for(i=0;i<3;i++)
 {
    for(j==1)
    sumc+=a[i][j];
    }
cout<<"sum of elements of middle row="<<sumr;
cout<<"sum of elements of middle column="<<sumc;
getch();
}

is the above program correct???

Make test cases and test it, have you coded it or not?

For me I would leave out at least the conio, clrscr and getch bit. And how are you reading the 33 or 55 square matrixes when it says input 3x3 matrix?

I can not imagine anybody typing in zillion useless numbers to get result only from middle column and one whole line, so I think input should be without prompting (coming from file through indirection, as this is main program, not function). You only need to save one total for column and one total for the row, two scalar variables.

for(j==1)

This does not make sense.

Be a part of the DaniWeb community

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