Tag Archives: #coding

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

 }

mastery 16

 

here is my code for mastery 16 showing the use of “else” as a conditional.

here is my code for masteries 15 and 16

<iostream>

using namespace std;

 

int main(){

  int a, c, d;

  char b;

 

  cout <<“does it move? 1) yes 2) no”;

  cin >> (a);

  if (a == 1) {

    cout << “should it move? 1)yes 2)no”;

    cin>>(c);

    if (c == 1) {

      cout << “then no problem 8)”;

 

    }

    else {

      cout << “then use duct tape”;

     }

 

 

 

    }

    else {

      cout <<“Should it move? 1)yes 2)no”;

      cin >>(d);

      if (d==1) {

        cout << “then use DW40”;

    }

    else {

      cout <<“then no problem 8)”;

    }

  }

  return 0;

}