| | |
add data in col, 2D arrays
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Feb 2005
Posts: 5
Reputation:
Solved Threads: 0
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
Code tags added. -Narue
C++ Syntax (Toggle Plain Text)
#include <iostream> // I/O TO DOS SCREEN #include <fstream> // FILE I/O #include <stdlib.h> // STANDARD LIBRARY #include <iomanip> // I/O FORMATTING using namespace std; // // PROTOTYPE STATEMENTS // void function_name(); // // GLOBAL VARIABLE DECLARATIONS // const int SIZE = 10; float sum = 0; int main() { int two_di_arr[ SIZE ][ SIZE ]; ifstream infile("Two_Dimensional_Data_In.txt"); for( int row = 0; row < SIZE; row++ ) { for( int col = 0; col < SIZE; col++ ) { infile >> two_di_arr[ row ][ col ]; sum += two_di_arr[ row ][ col ]; //This is where I sum the rows cout << setw( 6 ) << two_di_arr[ row ][ col ] << " "; } cout << sum / SIZE; cout << endl; } system("pause"); return 0; } // END MAIN() ////////////////////////////// // FUNCTION DEFINITIONS void function_name() { } ////////////////////////////////////////////
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; int main() { int a[5][5] = { {1, 2, 3, 4, 5}, {6, 7, 8, 9,10}, {11,12,13,14,15}, {16,17,18,19,20}, }; // Row major order for ( int i = 0; i < 5; i++ ) { int sum = 0; for ( int j = 0; j < 5; j++ ) sum += a[i][j]; cout<< sum <<'\t'; } cout<<endl; // Column major order for ( int i = 0; i < 5; i++ ) { int sum = 0; for ( int j = 0; j < 5; j++ ) sum += a[j][i]; cout<< sum <<'\t'; } cout<<endl; }
I'm here to prove you wrong.
![]() |
Similar Threads
- ootball league table - add data (C)
- VB - How to open an Excel Doc w/ existing data & add data in specific cells? (VB.NET)
- Combined data type arrays. (C++)
- how to add data (ASP)
- how to override arrays and objects??? (Java)
Other Threads in the C++ Forum
- Previous Thread: Directory Trees
- Next Thread: Context Help Error 129
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion count database delete deploy desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph homeworkhelp homeworkhelper iamthwee ifstream input int integer lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






