Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8973713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:30:42+00:00 2026-06-15T18:30:42+00:00

I need to sum and output the rows, columns, and main diagonals. Can someone

  • 0

I need to sum and output the rows, columns, and main diagonals. Can someone please tell me what I’m doing wrong? This is the code I was given to use but I can’t seem to get it to output my sums. I put a comment in the code below where I beleive my problem exist, where it starts summing and where it stops. I would appreciate any help as I am having trouble figuring this out on my own. The output should a 3 x 3 matrix with the sums of the rows, columns and diagonals in a fourth row and column to equal 15. My output is not summing the rows, columns, and diagonls.

#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

int main()
{

int n = 3;
int row, col, r, c, i, j, k, diag=0;
int magic[19][19];
ofstream fout;
fout.open("p4-out.txt");

for(j=0 ; j<n+1 ; j++){
    for(k=0 ; k<n+1 ; k++)
    magic[j][k] = 0;}

row=1; col=(n+1)/2;
magic[row-1][col-1]=1;

for(i=2 ; i<=(n*n) ; i++){
    row-=1; col-=1;
    if(row==0 && col==0){col++; row+=2;}
    else if(row==0) row=n;
    else if(col==0) col=n;
    else if(magic[row-1][col-1]!=0)
    {col++; row+=2;}
    magic[row-1][col-1]=i;}

for(r=0; r<n; r++){                    
    for(c=0; c<n; c++)
    magic[r][n]+=magic[r][c];}

for(c=0; c<n; c++){
    for(r=0; r<n; r++)
    magic[n][c]+=magic[r][c];}

for(r=0; r<n; r++){
    magic[n][n]+=magic[r][r];}

for(r=1; r<(n-1); r++){
    c = n - r + 1;
    diag+=magic[r][c];}              

for(r=0; r<(n+1) ; r++){
    fout << endl;
    for(c=0; c<(n+1) ; c++)
    {fout << setw (5) << magic[r][c];}
    fout << endl;}
return 0;
}


                             // This my output.
                      15 <---// I need this 15 to go

        6    1    8   15                       |
                                               |
        7    5    3   15                       |
                                               |
        2    9    4   15                       |
                                               |
 [15]  15   15   15   15                       |
 ^-------------<------------<----------<-------|
 here
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T18:30:43+00:00Added an answer on June 15, 2026 at 6:30 pm

    Using proper indexing would fill up your sum rows and columns too, try:

     for(r=0; r<n; r++){                       //Where it should start summing
         for(c=0; c<n; c++){
            magic[r][n]+=magic[r][c];}   
      }
    
     for(c=0; c<n; c++){
         for(r=0; r<n; r++){
            magic[n][c]+=magic[r][c];}
     }  
     for(r=0; r<n; r++){
            magic[n][n]+=magic[r][r];
     }    
     for(r=0; r<n; r++){
            c = n - r + 1;
            diag+=magic[r][c];}              //where it should stop summing
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on this website http://www.eluminatecreative.com/new/index.html and where it says sum'r fling I need
I need help on this code. The input is stored in a text file
i need to drop columns which has value '0' in both the rows select
i have a table with 3 columns and i need to get the sum
I need some explanation for this code. This is the example code given by
Please check the code below: <?php $d=2; echo the sum of the number is.<sub>($d+1)</sub>;
I need to sum the hours worked. I have 2 fields: startTime and endTime
I need to find the sum of the ranks from 13 cards that a
I need sphinx to sort the results by the sum of an attribute. I
I need to write a non-recursive version of the function sum-squares and Use a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.