add data in col, 2D arrays

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2005
Posts: 5
Reputation: csi_a_m is an unknown quantity at this point 
Solved Threads: 0
csi_a_m csi_a_m is offline Offline
Newbie Poster

add data in col, 2D arrays

 
0
  #1
Apr 19th, 2005
I need help adding the data in columns and rows of 10 in a 2d array. I got the rows added, but could someone please help out with how to sum the data in the columns. Please look at my code, and thanks in advance
  1. #include <iostream> // I/O TO DOS SCREEN
  2. #include <fstream> // FILE I/O
  3. #include <stdlib.h> // STANDARD LIBRARY
  4. #include <iomanip> // I/O FORMATTING
  5.  
  6. using namespace std;
  7.  
  8. //
  9. // PROTOTYPE STATEMENTS
  10. //
  11. void function_name();
  12.  
  13. //
  14. // GLOBAL VARIABLE DECLARATIONS
  15. //
  16. const int SIZE = 10;
  17. float sum = 0;
  18.  
  19. int main()
  20. {
  21. int two_di_arr[ SIZE ][ SIZE ];
  22. ifstream infile("Two_Dimensional_Data_In.txt");
  23.  
  24. for( int row = 0; row < SIZE; row++ )
  25. {
  26. for( int col = 0; col < SIZE; col++ )
  27. {
  28. infile >> two_di_arr[ row ][ col ];
  29. sum += two_di_arr[ row ][ col ]; //This is where I sum the rows
  30. cout << setw( 6 ) << two_di_arr[ row ][ col ] << " ";
  31. }
  32. cout << sum / SIZE;
  33. cout << endl;
  34.  
  35. }
  36.  
  37.  
  38. system("pause");
  39. return 0;
  40. }
  41. // END MAIN() //////////////////////////////
  42.  
  43. // FUNCTION DEFINITIONS
  44.  
  45. void function_name()
  46. {
  47.  
  48. }
  49. ////////////////////////////////////////////
Code tags added. -Narue
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,622
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 714
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: add data in col, 2D arrays

 
0
  #2
Apr 19th, 2005
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. int a[5][5] = {
  8. {1, 2, 3, 4, 5},
  9. {6, 7, 8, 9,10},
  10. {11,12,13,14,15},
  11. {16,17,18,19,20},
  12. };
  13.  
  14. // Row major order
  15. for ( int i = 0; i < 5; i++ ) {
  16. int sum = 0;
  17.  
  18. for ( int j = 0; j < 5; j++ )
  19. sum += a[i][j];
  20.  
  21. cout<< sum <<'\t';
  22. }
  23.  
  24. cout<<endl;
  25.  
  26. // Column major order
  27. for ( int i = 0; i < 5; i++ ) {
  28. int sum = 0;
  29.  
  30. for ( int j = 0; j < 5; j++ )
  31. sum += a[j][i];
  32.  
  33. cout<< sum <<'\t';
  34. }
  35.  
  36. cout<<endl;
  37. }
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 5
Reputation: csi_a_m is an unknown quantity at this point 
Solved Threads: 0
csi_a_m csi_a_m is offline Offline
Newbie Poster

Re: add data in col, 2D arrays

 
0
  #3
Apr 20th, 2005
Thanks alot, that helped....
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC