mastery 24

++  

Arrays are a good way to store a determined amount of numbers given by the user. You can either enter the size of the array, and that will be the maximum f numbers that will fit in. Just so, you can create a variable in order to ask the user for the size of the array and that way you can ask as many numbers as the user wants to provide. When using arrays in functions, you need to replace your variables in the main() code, so you can ask the user for the size of the array and then ask for each number using a “for” loop or a “do while” loop.

here is an example:

<iostream>

<cstdlib>

 using namespace std;

 

 int average(int x[], int size){

   int sum=0, average;

 

   for(int i=0; i<size; i++){

     sum=sum+x[i];

   }

   average=sum/size;

   return average;

 

 }

 int main(){

   int size;

   int a[size];

   cout<<“give me the amount of numbers “<<endl;

   cin>>size;

   for(int i=0; i<size; i++){

     cout <<“give me a number “;

     cin>> a[i];

 }

 cout<<average(a,size);

 }

CC BY 4.0 mastery 24 by carlos green is licensed under a Creative Commons Attribution 4.0 International License.

Comments are closed.