1 / 21

Calculating Row Average for 2D Arrays in C++

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.

loe
Télécharger la présentation

Calculating Row Average for 2D Arrays in C++

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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; }

  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

  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=?

  4. 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

  5. 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

  6. 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

  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=0 total=21.3

  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=1 total=21.3

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

  14. 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

  15. 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

  16. 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

  17. 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

  18. 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

  19. 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

  20. 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

  21. 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

More Related