Thread: Need help with a simple program

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    2

    Need help with a simple program

    Well i got the class c++ programming 2 and i got my first program to write and im stuck.. on the problem here is the problem.. i would appreciate any help i can get. or any ideas of how i can accomplish this. thanks

    sincerely,
    Ara

    Program 1,
    Given the following two-dimensional 6x6 square array:

    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


    Write a program that will compute:

    The sum of each row.
    The sum of each column.
    The sum of the major diagonal (coordinates that satisfy r=c)
    the sum of the minor diagonal (coordinates that satisfy 6-r=c)
    The sum of each concentric square (i.e. all points equidistant from the border)


    what i got so far. i have the rows and colums adding already i just need some help with the last three. this is what i have so far.

    Code:
    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;
    
    
    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;
    }
    
    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;
    }
    
    
    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;
    }
    
     return 0;
    }
    Last edited by t_r_a_n_s__am; 09-07-2006 at 02:06 PM.

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    CODE TAGS first, answer later.
    Sent from my iPadŽ

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Quote Originally Posted by t_r_a_n_s__am
    Well i got the class c++ programming 2 and i got my first program to write and im stuck.. on the problem here is the problem.. i would appreciate any help i can get. or any ideas of how i can accomplish this. thanks

    sincerely,
    Ara

    Program 1,
    Given the following two-dimensional 6x6 square array:

    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


    Write a program that will compute:

    The sum of each row.
    The sum of each column.
    The sum of the major diagonal (coordinates that satisfy r=c)
    the sum of the minor diagonal (coordinates that satisfy 6-r=c)
    The sum of each concentric square (i.e. all points equidistant from the border)


    what i got so far. i have the rows and colums adding already i just need some help with the last three. this is what i have so far.


    Code:
    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;
    
    
    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;
    }
    
    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;
    }
    
    
    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;
    }
    
     return 0;
    You're missing a closing brace (}).
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    2
    ok done fixed sorry should have read the sticky any help would be appreciated thanks

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    What's your question? Have you tried the other three? They follow the same basic procedure as the first two - loop through the values and only sum the ones you want. You just have to figure out the best way to loop through the values. The diagonals should be pretty easy, since you only really need one loop.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    As Daved said, for (3) and (4) use a loop like you already have:
    Code:
    for (int r=0; r < rsize; r++ ) {
         //You gave us the formula for c, so use that.
    
         //Then sum as before.
         sum = sum + a[r][c];
    For (5), I'm not sure what it's asking.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    For (5), you could start out with some code to sum the whole square (which you almost have, except sum would not be reset to zero after each row). Then add an outer loop to determine where the border starts (for your square the start of the border is going to be either 0, 1, or 2). Then your two inner loops would start at this value, instead of 0 as you are using for parts (1) and (2).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with a very simple program
    By htdefiant in forum C++ Programming
    Replies: 13
    Last Post: 08-14-2007, 01:27 PM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. [Help] Simple Array/Pointer Program
    By sandwater in forum C Programming
    Replies: 3
    Last Post: 03-30-2007, 02:42 PM
  4. simple silly program
    By verbity in forum C Programming
    Replies: 5
    Last Post: 12-19-2006, 06:06 PM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM