1. Web Hosting Master
Join Date
Feb 2003
Location
AR
Posts
2,381

## Bit 'o C++ Help please

First off, YES this is homework. I'm well aware that homework is meant to be done by me, and I've done most of it, but this one bit is giving me a headache (can't think straight today...damned allergies)

I've got the following two dimensional array:

Code:
```2 	3 	38 	21 	19 	3
3 	23 	49 	37 	83 	-7
14 	37 	20 	2 	4 	22
30 	2 	2 	3 	223 	22
18 	43 	29 	37 	34 	23
81 	34 	92 	73 	43 	32```
I've already made the program to calculate the sum of all the rows, the sum of all the columns, the sum of the major diagonal, the sum of the minor diagonal, and the last part is driving me crazy.

I need to calculate the sum of each concentric square
What this means is that I need to calculate the sum of 2+3+38+21+19+3+(-7)+22+22+23+32+43+73+92+34+81+18+30+14+3

I then need to calculate the sum of 23+49+37+83+4+223+34+37+29+43+2+37

Then 20+2+2+3

This all needs to be done with loops.

For example, to calculate the major diagonal, I did

Code:
```int sum = 0;
for (int c=0; c<csize; c++ ) {
sum = sum + a[c][c];
}
cout << "Sum Of The Major Diagonal: " << sum << endl;```
For the life of me, though, I just can't think of how to manage the concentric squares. I'm not asking for somebody to do it for me, but some hints would be nice
Thanks,
Kevin
Last edited by ThatScriptGuy; 09-06-2006 at 04:25 PM.

2. Web Hosting Master
Join Date
Feb 2003
Location
AR
Posts
2,381
Also, I would like to mention that csize is the number of columns in the array.
Kevin

3. Web Hosting Master
Join Date
Aug 2002
Location
Posts
662
That's a fun problem, there are several ways to tackle this, some interesting properties exhibit themselves if you lay all your rows into a line-array and see what "concentric" is on a single line.

To get anything with a concentric pattern, the array has to be square.

Now that this is laid out, the concentric square contains (width-1)^2 elements. So in an array of size 5, your largest concentric square has 4^2 (16) elements. Further, your steps each reduce the periphery by a factor of two. So if you have a square array of size 5, your first step will be 5, then 3. Lastly, we stop when the periphery is of size 3. Smaller than that, you have lost concentricity.

So!

Now it's just an easy game of mapping the periphery to our formula, I'll keep it simple using loops as you requested, you can drum up the elaborate solution on your own as an exercise and impress your prof!

Code:
```int main(void) {
int sqarr [6][6] = {{ 2, 3, 38, 21, 19, 3 },
{ 3, 23, 49, 37, 83, -7 },
{ 14, 37, 20, 2, 4, 22 },
{ 30, 2, 2, 3, 223, 22 },
{ 18, 43, 29, 37, 34, 23 },
{ 81, 34, 92, 73, 43, 32 } };

int csize = 6, periphery, sum = 0;

// each iteration represents a circle
for( int i = 0 ; i < csize ; i++ ){
// no longer a circle?
if( csize - (i*2) < 2 )
break;

periphery = csize - (i * 2);

// add the top and bottom row
for( int j = 0 ; j < periphery ; j++ ){
sum += sqarr[i][i + j];
sum += sqarr[csize - 1 - i][i + j];
}

// add the side columns of the concentric areas
for( int k = 0 ; k < periphery - 2 ; k++ ){
sum += sqarr[i+k+1][i];
sum += sqarr[i+k+1][csize - 1 -i];
}

}

cout << sum;
}```
Good luck!
Regards.
Alex

4. Web Hosting Master
Join Date
Feb 2003
Location
AR
Posts
2,381
OH Goodness gracious.....I'm going out on a limb and gonna say that we weren't expected to figure that one out. Now that it's explained, though, it does make sense. Just a lot of logical thinking to get to the answer.....
Thanks for those tips..
Kevin

5. Web Hosting Master
Join Date
Feb 2003
Location
AR
Posts
2,381
Well poo. What you wrote, in english, makes sense, but when I try to transfer that to code, it just doesn't work for me (I tried to do it without using your code).....Finally, after I wasn't able to do it for about a day, I looked at your code and the first thing I saw was that it only output the sum one time. I need the sum of each concetric circle to be output. Nevertheless, I put your code at the bottom, changing some variables, and it doesn't work (confused me)

Here is what I've got:
Code:
```/*
2d.cc -- Program to illustrate the use of 2d arrays

Author: Larry Morell

Modification History
Date       Action
09/05/06 -- Original version

*/
using namespace std;
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

int main () {
int a[6][6] =
{
{2, 3, 38, 21, 19, 3},
{3, 23, 49, 37, 83, -7},
{14, 37, 20, 2, 4, 22},
{30, 2, 2, 3, 223, 22},
{18, 43, 29, 37, 34, 23},
{81, 34, 92, 73, 43, 32}
};
const int csize = 6;
const int rsize= sizeof(a)/sizeof(int)/csize;

// Print the array in 2-dimensions
for (int r=0; r < rsize; r++ ) {
cout << setw(5) << a[r][0];
for (int c=1; c < csize; c++) { // Add a[r][c] to sum
cout << " "<< setw(rsize) <<a[r][c];
}
cout << endl;
}
//We therefore want to calculate six row sums:
//Calculate sum of rows:
for (int r=0; r < rsize; r++ ) {
int sum = 0;
for (int c=0; c < csize; c++) { // Add a[r][c] to sum
sum = sum + a[r][c];
}
cout << "Row " << r << " sum: " << sum << endl;
}

//And csize column sums:
//Calculate sum of rows:
for (int c=0; c < csize; c++ ) {
int sum = 0;
for (int r=0; r < csize; r++) { // Add a[r][c] to sum
sum = sum + a[r][c]; } cout << "Col " << c << " sum: " << sum << endl;
}

//Calculate sum of the major diagonal
int sum = 0;
for (int c=0; c<csize; c++ ) {
sum = sum + a[c][c];
}
cout << "Sum Of The Major Diagonal: " << sum << endl;

//Calculate sum of the minor diagonal
sum = 0;
for (int c=csize-1; c>=0;  c-- ) {
sum = sum + a[5-c][c];
}
cout << "Sum Of The Minor Diagonal: " << sum << endl;

//Calculate the sum of each concentric square
int periphery;
//each iteration represents a circle
for(int i=0; i<csize; i++){
//no longer a circle?
if(csize - (i*2) < 2)
break;
periphery = csize - (i*2);

//add the top and bottom row
for(int j=0; j<periphery; j++){
sum += a[i][i+j];
sum += a[csize-1-i][i+j];
}

//add the side columns of the concentric areas
for(int k=0; k<periphery - 2; k++){
sum += a[i+k+1][i];
sum += a[i+k+1][csize-1-i];
}
}
cout << sum;
return 0;
}```
Any help is appreciated.
Kevin

6. Web Hosting Master
Join Date
Aug 2002
Location
Posts
662
If you want the sum of each circle, just move the sum inside the loop.

Code:
```int main(void) {
int sqarr [6][6] = {{ 2, 3, 38, 21, 19, 3 },
{ 3, 23, 49, 37, 83, -7 },
{ 14, 37, 20, 2, 4, 22 },
{ 30, 2, 2, 3, 223, 22 },
{ 18, 43, 29, 37, 34, 23 },
{ 81, 34, 92, 73, 43, 32 } };

int csize = 6, periphery, sum;

// each iteration represents a circle
for( int i = 0 ; i < csize ; i++ ){
sum = 0;
// no longer a circle?
if( csize - (i*2) < 2 )
break;

periphery = csize - (i * 2);

// add the top and bottom row
for( int j = 0 ; j < periphery ; j++ ){
sum += sqarr[i][i + j];
sum += sqarr[csize - 1 - i][i + j];
}

// add the side columns of the concentric areas
for( int k = 0 ; k < periphery - 2 ; k++ ){
sum += sqarr[i+k+1][i];
sum += sqarr[i+k+1][csize - 1 -i];
}
cout << sum << endl;
}

}```

7. Web Hosting Master
Join Date
Feb 2003
Location
AR
Posts
2,381
I thought I had tried that......guess not...
Thanks again for your help...Now to go make it look pretty
Kevin

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•