210 likes | 353 Vues
This guide explains how to compute the average of each row in a 2D array in C++. Using a function named `RowAverage`, we iterate through each row of a 2D array containing float values. The function sums the values in the row and divides by the total number of columns to find the average. The computed average is then printed to the console. Examples demonstrated include calculations for a specific 2D array, showcasing how to handle varying data entries to achieve accurate results.
E N D
Row Average (2D Array) void RowAverage(float x[ ][4], int i) { int j; float total=0.0; for(j=0;j<4;j++) { total=total+x[i][j]; } cout<<“Row”<<j<<“Average=”<<total/4.0<<endl; return; }
Row Average (2D Array) void RowAverage(float x[ ][4], int i) { int j; float total=0.0; for(j=0;j<4;j++) { total=total+x[i][j]; } cout<<“Row”<<j<<“Average=”<<total/4.0<<endl; return; } 0 1 2 3 12.6 15.6 16.2 13.5 0 21.3 24.5 25.8 x 1 27.4 33.2 31.5 37.1 32.7 2 i=1
Row Average (2D Array) void RowAverage(float x[ ][4], int i) { int j; float total=0.0; for(j=0;j<4;j++) { total=total+x[i][j]; } cout<<“Row”<<j<<“Average=”<<total/4.0<<endl; return; } 0 1 2 3 12.6 15.6 16.2 13.5 0 21.3 24.5 25.8 x 1 27.4 33.2 31.5 37.1 32.7 2 i=1 j=?
Row Average (2D Array) void RowAverage(float x[ ][4], int i) { int j; float total=0.0; for(j=0;j<4;j++) { total=total+x[i][j]; } cout<<“Row”<<j<<“Average=”<<total/4.0<<endl; return; } 0 1 2 3 12.6 15.6 16.2 13.5 0 21.3 24.5 25.8 x 1 27.4 33.2 31.5 37.1 32.7 2 i=1 j=? total=0.0
Row Average (2D Array) void RowAverage(float x[ ][4], int i) { int j; float total=0.0; for(j=0;j<4;j++) { total=total+x[i][j]; } cout<<“Row”<<j<<“Average=”<<total/4.0<<endl; return; } 0 1 2 3 12.6 15.6 16.2 13.5 0 21.3 24.5 25.8 x 1 27.4 33.2 31.5 37.1 32.7 2 i=1 j=0 total=0.0
Row Average (2D Array) void RowAverage(float x[ ][4], int i) { int j; float total=0.0; for(j=0;j<4;j++) { total=total+x[i][j]; } cout<<“Row”<<j<<“Average=”<<total/4.0<<endl; return; } 0 1 2 3 12.6 15.6 16.2 13.5 0 21.3 24.5 25.8 x 1 27.4 33.2 31.5 37.1 32.7 2 i=1 j=0 total=0.0
Row Average (2D Array) void RowAverage(float x[ ][4], int i) { int j; float total=0.0; for(j=0;j<4;j++) { total=total+x[i][j]; } cout<<“Row”<<j<<“Average=”<<total/4.0<<endl; return; } 0 1 2 3 12.6 15.6 16.2 13.5 0 21.3 24.5 25.8 x 1 27.4 33.2 31.5 37.1 32.7 2 i=1 j=0 total=21.3
Row Average (2D Array) void RowAverage(float x[ ][4], int i) { int j; float total=0.0; for(j=0;j<4;j++) { total=total+x[i][j]; } cout<<“Row”<<j<<“Average=”<<total/4.0<<endl; return; } 0 1 2 3 12.6 15.6 16.2 13.5 0 21.3 24.5 25.8 x 1 27.4 33.2 31.5 37.1 32.7 2 i=1 j=1 total=21.3
Row Average (2D Array) void RowAverage(float x[ ][4], int i) { int j; float total=0.0; for(j=0;j<4;j++) { total=total+x[i][j]; } cout<<“Row”<<j<<“Average=”<<total/4.0<<endl; return; } 0 1 2 3 12.6 15.6 16.2 13.5 0 21.3 24.5 25.8 x 1 27.4 33.2 31.5 37.1 32.7 2 i=1 j=1 total=21.3
Row Average (2D Array) void RowAverage(float x[ ][4], int i) { int j; float total=0.0; for(j=0;j<4;j++) { total=total+x[i][j]; } cout<<“Row”<<j<<“Average=”<<total/4.0<<endl; return; } 0 1 2 3 12.6 15.6 16.2 13.5 0 21.3 24.5 25.8 x 1 27.4 33.2 31.5 37.1 32.7 2 i=1 j=1 total=21.3
Row Average (2D Array) void RowAverage(float x[ ][4], int i) { int j; float total=0.0; for(j=0;j<4;j++) { total=total+x[i][j]; } cout<<“Row”<<j<<“Average=”<<total/4.0<<endl; return; } 0 1 2 3 12.6 15.6 16.2 13.5 0 21.3 24.5 25.8 x 1 27.4 33.2 31.5 37.1 32.7 2 i=1 j=2 total=48.7
Row Average (2D Array) void RowAverage(float x[ ][4], int i) { int j; float total=0.0; for(j=0;j<4;j++) { total=total+x[i][j]; } cout<<“Row”<<j<<“Average=”<<total/4.0<<endl; return; } 0 1 2 3 12.6 15.6 16.2 13.5 0 21.3 24.5 25.8 x 1 27.4 33.2 31.5 37.1 32.7 2 i=1 j=2 total=48.7
Row Average (2D Array) void RowAverage(float x[ ][4], int i) { int j; float total=0.0; for(j=0;j<4;j++) { total=total+x[i][j]; } cout<<“Row”<<j<<“Average=”<<total/4.0<<endl; return; } 0 1 2 3 12.6 15.6 16.2 13.5 0 21.3 24.5 25.8 x 1 27.4 33.2 31.5 37.1 32.7 2 i=1 j=2 total=73.2
Row Average (2D Array) void RowAverage(float x[ ][4], int i) { int j; float total=0.0; for(j=0;j<4;j++) { total=total+x[i][j]; } cout<<“Row”<<j<<“Average=”<<total/4.0<<endl; return; } 0 1 2 3 12.6 15.6 16.2 13.5 0 21.3 24.5 25.8 x 1 27.4 33.2 31.5 37.1 32.7 2 i=1 j=3 total=73.2
Row Average (2D Array) void RowAverage(float x[ ][4], int i) { int j; float total=0.0; for(j=0;j<4;j++) { total=total+x[i][j]; } cout<<“Row”<<j<<“Average=”<<total/4.0<<endl; return; } 0 1 2 3 12.6 15.6 16.2 13.5 0 21.3 24.5 25.8 x 1 27.4 33.2 31.5 37.1 32.7 2 i=1 j=3 total=73.2
Row Average (2D Array) void RowAverage(float x[ ][4], int i) { int j; float total=0.0; for(j=0;j<4;j++) { total=total+x[i][j]; } cout<<“Row”<<j<<“Average=”<<total/4.0<<endl; return; } 0 1 2 3 12.6 15.6 16.2 13.5 0 21.3 24.5 25.8 x 1 27.4 33.2 31.5 37.1 32.7 2 i=1 j=3 total=99.0
Row Average (2D Array) void RowAverage(float x[ ][4], int i) { int j; float total=0.0; for(j=0;j<4;j++) { total=total+x[i][j]; } cout<<“Row”<<j<<“Average=”<<total/4.0<<endl; return; } 0 1 2 3 12.6 15.6 16.2 13.5 0 21.3 24.5 25.8 x 1 27.4 33.2 31.5 37.1 32.7 2 i=1 j=4 total=99.0
Row Average (2D Array) void RowAverage(float x[ ][4], int i) { int j; float total=0.0; for(j=0;j<4;j++) { total=total+x[i][j]; } cout<<“Row”<<j<<“Average=”<<total/4.0<<endl; return; } 0 1 2 3 12.6 15.6 16.2 13.5 0 21.3 24.5 25.8 x 1 27.4 33.2 31.5 37.1 32.7 2 i=1 j=4 total=99.0
Row Average (2D Array) void RowAverage(float x[ ][4], int i) { int j float total=0.0; for(j=0;j<4;j++) { total=total+x[i][j]; } cout<<“Row”<<j<<“Average=”<<total/4.0<<endl; return; } 0 1 2 3 12.6 15.6 16.2 13.5 0 21.3 24.5 25.8 x 1 27.4 33.2 31.5 37.1 32.7 2 i=1 j=4 total=99.0 Row 1 Average=24.8
Row Average (2D Array) void RowAverage(float x[ ][4], int i) { int j float total=0.0; for(j=0;j<4;j++) { total=total+x[i][j]; } cout<<“Row”<<j<<“Average=”<<total/4.0<<endl; return; } 0 1 2 3 12.6 15.6 16.2 13.5 0 21.3 24.5 25.8 x 1 27.4 33.2 31.5 37.1 32.7 2 i=1 j=4 total=99.0 Row 1 Average=24.8
Row Average (2D Array) void RowAverage(float x[ ][4], int i) { int j float total=0.0; for(j=0;j<4;j++) { total=total+x[i][j]; } cout<<“Row”<<j<<“Average=”<<total/4.0<<endl; return; } 0 1 2 3 12.6 15.6 16.2 13.5 0 21.3 24.5 25.8 x 1 27.4 33.2 31.5 37.1 32.7 2 i=1 j=4 total=99.0 Row 1 Average=24.8