Question

Please code using C++ Thank you Write a function that receives a 2d array of type...

Please code using C++ Thank you

Write a function that receives a 2d array of type double and returns the average of all elements in the 2d array

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
//Creating an 3 by 3 array
double arr[3][3];

//Declaring variables
double sum=0.0,average=0.0;
int count=0;

/* This for loop will get the numbers entered
* by the user and populates them into an array
*/
for(int i=0;i<3;i++)
{
   for(int j=0;j<3;j++)
   {
       //Getting the number entered by the user
   cout<<"Enter Number "<<i<<","<<j<<":";
   cin>>arr[i][j];  
   }
}

/* This for loop will calculates
* the sum of all the elements in the array
*/
for(int i=0;i<3;i++)
{
   for(int j=0;j<3;j++)
   {
   //Sum of all the elements
   sum+=arr[i][j];  
   count++;
   }
}      

//Calculates the average
average=sum/count;

//Dispalying the average
cout<<"The Average of all the elements is :"<<std::fixed<<std::setprecision(2)<<average<<endl;

   return 0;
}

_______________________

output:

C:AProgram Files (x86)\Dev-CpplMinGW64 binlAverageOf2DArray.exe Enter Number ,011.1 Enter Number 0.1:22.2 Enter Number .2:33.

________Thank You

Add a comment
Know the answer?
Add Answer to:
Please code using C++ Thank you Write a function that receives a 2d array of type...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ ( Please Read) Write a function prototype and a function definition called getTotal that receives...

    C++ ( Please Read) Write a function prototype and a function definition called getTotal that receives an  array of type double, the number of rows in the array. It returns the total of all elements in the array.

  • USING C++: Write a function that given a 2D dynamic array of integers, will return the...

    USING C++: Write a function that given a 2D dynamic array of integers, will return the number of elements whose absolute value is smaller than a given value x. The function should take as arguments (in this order): a pointer to the array (i.e., 2D dynamic array) the number of rows the number of columns the given value The function should return the number of elements smaller in absolute value than epsilon E.g. if A={{0.2, -0.1, 3.4},{4.4, 0, -2}} and...

  • 1. Write code for a function that receives two parameters (a,and b) by value and two...

    1. Write code for a function that receives two parameters (a,and b) by value and two more parameters (c and d) by reference. All parameters are double. The function works by assigning c to (a/b) and assigning d to (a*b). The function has no return value. From main, use scanf to get two numbers, then call the function, and then display both outcome values to the output in a printf statement. 2. After part 1 is completed, write code to...

  • programing C,already write part of code (a) Write a function print.array that receives an array of...

    programing C,already write part of code (a) Write a function print.array that receives an array of real numbers and the number of el stored in the array. This function must display the elements of the array in order with each one on a line by itself in a field width of 7 with two decimal places. (See sample output. Recall the format specifier would be "%7.21f"). (b) Sometimes we want to treat an array as a mathematical vector and compute...

  • C++ Programme Write a function that receives, as arguments, a pointer to a double array, as...

    C++ Programme Write a function that receives, as arguments, a pointer to a double array, as well as the number of elements: in the array(int). The function must use pointer operations and calculate the standard deviation of the elements in the array. The function returns the standard deviation to the calling statementſ Use the function in main( to display its operation. | Example: array = (11.2, 2.4, 3.13, 16.4, 5.8, 9.22, 4.9, 10.5, 6.5, 2.99) std Dev(array) = 4.249367 ZWI...

  • Write a program using C in Microsoft visual studios. Write a function that receives an array...

    Write a program using C in Microsoft visual studios. Write a function that receives an array of integers and the array length and prints one integer per line (Hint: Use \n for a line break). Left align all of the integers and use a field width of 10. Populate an array of 10 elements from the user and pass the array and its length to the function.

  • Please write a C++ code for 2D matrix by using STACK push and pop function.

    Please write a C++ code for 2D matrix by using STACK push and pop function.

  • 1. Write a C function named find that receives a one-dimensional array of type integer named...

    1. Write a C function named find that receives a one-dimensional array of type integer named arr and its size of type integer. The function fills the array with values that are the power of four (4^n) where n is the index. After that, the function must select a random index from the array and move the array elements around the element stored in the randomly selected index Example arr = [1, 4, 16, 64, 256) if the randomly selected...

  • create a file homework_part_1.c a) Implement the function initialize_array that receives two parameters: an array of...

    create a file homework_part_1.c a) Implement the function initialize_array that receives two parameters: an array of integers and the array size. Use a for loop and an if statement to put 0s in the odd positions of the array and 5s in the even positions. You must use pointers to work with the array. Hint: review pointers as parameters. b) Implement the function print_array that receives as parameters an array of integers and the array size. Use a for statements...

  • Please send this C++ code with ( // ) Comments 1. In main, you have the...

    Please send this C++ code with ( // ) Comments 1. In main, you have the following array declared: int arr[5] = {1, 5, 3, 2} a. What are the elements in the array? Create a function called displayArr that accepts the array and the size of the array, and displays every element in the array. b. Create a function called findAverage that accepts the array and the size of the array as arguments, and returns the average of all...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT