Help in 2D C++......

This is a discussion on Help in 2D C++...... within the C++ Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <conio.h> #include <string.h> #include <iostream> #include <iomanip> #include <cmath> using namespace std; void call(int array[5][6], const ...

  1. #1
    "PALINDROME"
    Join Date
    Nov 2010
    Posts
    59

    Help in 2D C++......

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    #include <iostream>
    #include <iomanip>
    #include <cmath>
    using namespace std;
     
    void call(int array[5][6], const int i){
         
      int colRowSum[6] = {0};
      int colSumRow[5] = {0};
     
     
      //sum to column row
      for(int i = 0; i < 5; i++){
         for( int j = 0; j < 6; j++){
            colRowSum[j] += array[i][j];
             colSumRow[i] += array[i][j];
          }
      }
     
      //print array
    for(int i = 0; i != 5; ++i){
    	  for(int j = 0; j != 6; ++j){
    		  cout.width(9);
    		  cout << array[i][j];
    	  }
     
              cout.width(9);
              cout << colSumRow[i];
     
    	  cout << endl;
      }
     
     
     
      //print result
      for(int n = 0; n != 6; ++n){
    	  cout.width(9);
    	  cout << colRowSum[n] <<" ";
      }
      for (int s = 0; s != 5; ++s){
          cout.width(9);
          cout <<colSumRow[s];
      }
     
      cout << endl;
    }
     
    int main(){
        int array[ 5 ][ 6 ] = { { 141,   567,   434,   100,   269,   324, },
                                { 578,   458,   562,   564,   305,   245, },
                                { 381,   427,   561,   591,   595,   542, },
                                { 427,   536,   491,   204,   502,   253, },
                                { 392,   482,   521,   316,   318,   495, } };
     
    	call (array,5);
     
        system("pause");
    	return 0;
     
    }

    here is the correct output...i want the program to be like this....

    100 101 102 103 104 105 615
    106 107 108 109 110 111 651
    112 113 114 115 116 117 687
    118 119 120 121 122 123 723
    124 125 126 127 128 129 759
    560 565 570 575 580 585 3435

    i really need your help!>...
    Last edited by [Student]; 07-12-2011 at 08:43 AM.

  2. #2
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    you really haven't done a very good job of explaining the problem. "help to align the numbers and remove the extra that doesn't belong" puts too much onus on the people you're asking to help to understand your issue.

    Code:
     //print array
    for(int i = 0; i != 5; ++i){
    	  for(int j = 0; j != 6; ++j){
    		  cout.width(9);
    		  cout << array[i][j];
    	  }
     
              cout.width(9);
              cout << colSumRow[i];
     
    	  cout << endl;
      }
     
     
     
      //print result
      for(int n = 0; n != 6; ++n){
    	  cout.width(9);
    	  cout << colRowSum[n] <<" ";
      }
      for (int s = 0; s != 5; ++s){
          cout.width(9);
          cout <<colSumRow[s];
      }
     
      cout << endl;
    istill dont' realyl know what your complaint is, but from what i can piece together, the highlighted lines above seems suspect.

  3. #3
    "PALINDROME"
    Join Date
    Nov 2010
    Posts
    59
    Hey I get it now.....i solve the misunderstanding issue.....but there 's a problem....

    this is my output but missing of the final sum of R and C....

    100 101 102 103 104 105 615
    106 107 108 109 110 111 651
    112 113 114 115 116 117 687
    118 119 120 121 122 123 723
    124 125 126 127 128 129 759
    560 565 570 575 580 585

    100 101 102 103 104 105 615
    106 107 108 109 110 111 651
    112 113 114 115 116 117 687
    118 119 120 121 122 123 723
    124 125 126 127 128 129 759
    560 565 570 575 580 585 3435 <-- how to get this sum of 3435 from R and C

    Code:
    #include <iostream>
    
    using namespace std;
     
    void call(int array[][6], const int i){
         
      int colRowSum[6] = {0};
      int colSumRow[5] = {0};
     
      
      //sum to column row
      for(int i = 0; i < 5; i++){
         for( int j = 0; j < 6; j++){
            colRowSum[j] += array[i][j];
             colSumRow[i] += array[i][j];
          }
      }
     
      //print array
    for(int i = 0; i != 5; ++i){
    	  for(int j = 0; j != 6; ++j){
                  cout.width(6);
    		  cout << array[i][j]<<"  ";
    	  }
    	      cout.width(6);
              cout << colSumRow[i]<<"  ";
     
    	  cout << endl;
      }
     
     
     
      //print result
      for(int n = 0; n != 6; ++n){
              cout.width(6);
    	  cout << colRowSum[n]<<"  ";
      }
      for (int s = 0; s != 0; ++s){
          cout.width(7);
          cout <<colSumRow[s]<<"  ";
      }
      
      cout << endl;
    }
     
    int main(){
        int array[ 5 ][ 6 ] = { { 141,   567,   434,   100,   269,   324, },
                                { 578,   458,   562,   564,   305,   245, },
                                { 381,   427,   561,   591,   595,   542, },
                                { 427,   536,   491,   204,   502,   253, },
                                { 392,   482,   521,   316,   318,   495, } };
     
    	call (array,5);
        cout<<endl;
        
        system("pause");
    	return 0;
     
    }

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,761
    > 560 565 570 575 580 585 3435 <-- how to get this sum of 3435 from R and C
    You add up either
    - every element of the array (easy, you already have a loop doing this)
    - all of colRowSum
    - all of colSumRow
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21