Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ programing Write all of this code in the main, do not create separate method

ID: 3684592 • Letter: C

Question

C++ programing

Write all of this code in the main, do not create separate methods. Develop a 2-D array with 3 rows and 4 columns. Populate it 12 random integers in the range 1 to 9, including the end points. Print the contents of the array, appropriately labeled, with 3 rows and 4 columns. For readability, include a space between each number and use setw function. Your printout should look like the following, with the x's representing numbers in the array x x x x x x x x x x x x Create a ID array called rowAvg, size must be equal to 2D array number of rows Calculate the average each row, save in the rowAvg Create a ID array called columnAvg, size must be equal to 2D array number of rows Calculate the average each column, save in the columnAvg Calculate the average of all numbers (totalAvg) Print rowAvg and columnAvg arrays and totalAvg.

Explanation / Answer

#include<iostream>
#include<cstdlib>
using namespace std;

int main(){

   //declaring a 2-D array of 3x4
   int arr[3][4];
  
   //populating random numbers in range 1-9
   for(int i=0; i<3; i++){
       for(int j=0; j<4; j++){
           arr[i][j] = rand()%9 + 1; // random number in range 1-9
       }
   }
  
   //printing the content od array
   for(int i=0; i<3; i++){
       for(int j=0; j<4; j++){
           cout<<arr[i][j]<<" ";
       }
       cout<<" ";
   }
  
   // creating 1-D rowAvg array
   double rowAvg[3];
  
   // calculating each row average and saving into rowAvg
   for(int i=0; i<3; i++){
       double sum = 0;
       for(int j=0; j<4; j++){
           sum = sum +arr[i][j];
       }
       rowAvg[i] = sum/3.0;
   }
  
   //printing rowAvg
   for(int i=0; i<3; i++){
       cout<<"rowAvg["<<i<<"] = "<<rowAvg[i]<<endl;
   }
   // creating 1-D columnAvg array
   double columnAvg[4];
  
   // calculating each column average and saving into columnAvg
   for(int i=0; i<4; i++){
       double sum = 0;
       for(int j=0; j<3; j++){
           sum = sum +arr[j][i];
       }
       columnAvg[i] = sum/4.0;
   }
  
   //printing columnAvg
   for(int i=0; i<4; i++){
       cout<<"columnAvg["<<i<<"] = "<<columnAvg[i]<<endl;
   }
   //calculating total sum
   double totalSum = 0;
   for(int i=0; i<3; i++){
       for(int j=0; j<4; j++){
           totalSum = totalSum+arr[i][j];
       }
   }
  
   // ptinting total avg
   cout<<endl<<endl<<"Total average : "<<totalSum/12.0<<endl;
   return 0;
}

/*

Sample run:

2   8   1   8  
6   8   2   4  
7   2   6   5  
rowAvg[0] = 6.33333
rowAvg[1] = 6.66667
rowAvg[2] = 6.66667
columnAvg[0] = 3.75
columnAvg[1] = 4.5
columnAvg[2] = 2.25
columnAvg[3] = 4.25


Total average : 4.91667

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote