Question

In: Computer Science

In this lab we will write 3 functions: GenData: This function will take an integer parameter...

In this lab we will write 3 functions:

GenData: This function will take an integer parameter and return a vector with that many random integers generated in the range of 0 and 100 (rand()%101). Seed the random number generator with 22.

Mean(): This function will take a vector and return the mean.
Variance(): This function will take a vector and return the population variance, as: [Sum for values[( x_i - Mean )^2]] / (number of items)

In Main:

  • Use GenData to generate a vector with 100 random values.
  • Use Mean() to calculate the mean of the vector, and print formatted with 2 decimal points as below:
    • "Mean=25.11"
  • Use Variance() to calculate the variance of the vector, and print formatted with 2 decimal points as below:
    • "Variance=25.11"

Example Output:
83, 89, 54, 38, 2, 84, 47, 88, 78, 55,
Mean=51.62
Variance=905.08

Given:

#include <iostream>
#include <vector>
#include <cstdlib>
#include <iomanip>
#include <cmath>
using namespace std;

vector<int> GenData(int n){
//should generate n random values and return them.
}

double Mean(vector<int> d){
unsigned int i;

//return mean of all values in vector.
}
/*Variance function
sum +=(your current X value - mean)^2
return variance - > [ sum/total size of vector]
*/


int main(){
int i;
vector<int> d;
d = GenData(100);

for (/*loop for 10 values*/){
cout << d.at(i) << ", " ;
}
return 0;
}
in c++ pls

Solutions

Expert Solution

Code:

#include <iostream>
#include <vector>
#include <cstdlib>
#include <iomanip>
#include <cmath>
using namespace std;

vector<int> GenData(int n) {
        //should generate n random values and return them.
        srand(22); // set seed to 22
        vector <int> v; 
        // generate n numbers
        for(int i=0;i<n;i++){
                int x = rand()%101;
                v.push_back(x);
        }
        return v;
}

double Mean(vector<int> d) {
        int n = d.size(); // no. of elements in vector
        unsigned int i = 0; // use to store sum
        for(int j=0;j<n;j++){
                i+= d[j]; // add values to sum
        }
        // mean = sum of all no. / size
        double mean = i/(n*1.0);
        //return mean of all values in vector.
        return mean;
}

/*Variance function
sum +=(your current X value - mean)^2
return variance - > [ sum/total size of vector]
*/
double Variance(vector <int> d){
        double mean = Mean(d); // find out mean
        double sum = 0;
        int n = d.size(); // no. of elements
        for(int i=0;i<n;i++){
                sum += (d[i]-mean)*(d[i]-mean); // add ( x_i - Mean )^2
        }
        // divide by no. of items to find variance
        double var = sum/n;
        return var;
}


int main() {
        int i;
        vector<int> d;
        d = GenData(100);

        for (int i=0;i<10;i++) {
                cout << d.at(i) << ", " ;
        }
        cout<<endl;
        double mean = Mean(d);
        double var = Variance(d);
        cout<<"Mean="<<mean<<endl;
        cout<<"Variance="<<var<<endl;
        return 0;
}

Code Screenshot:

Code output:

==========================

Code along with screenshots and comment has been added.

Please upvote.


Related Solutions

Write a function convert_date that takes an integer as a parameter and returns three integer values...
Write a function convert_date that takes an integer as a parameter and returns three integer values representing the input converted into days, month and year (see the function docstring). Write a program named t03.py that tests the function by asking the user to enter a number and displaying the output day, month and year. Save the function in a PyDev library module named functions.py A sample run for t03.py: Enter a date in the format MMDDYYYY: 05272017 The output will...
Using C++ Write a template function that accepts an integer parameter and returns its integer square...
Using C++ Write a template function that accepts an integer parameter and returns its integer square root. The function should return -1, if the argument passed is not integer. Demonstrate the function with a suitable driver program .
Write a Racket function that will take a list of numbers as a parameter and return...
Write a Racket function that will take a list of numbers as a parameter and return true if they all are positive, and false otherwise. You are NOT required to check the type of elements in the list. (please do on racket language)
Write a Racket function that will take a list of numbers as a parameter and return...
Write a Racket function that will take a list of numbers as a parameter and return true if they all are positive, and false otherwise. You are NOT required to check the type of elements in the list.
6.28 LAB: Convert to binary - functions Write a program that takes in a positive integer...
6.28 LAB: Convert to binary - functions Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is: As long as x is greater than 0 Output x % 2 (remainder is either 0 or 1) x = x / 2 Note: The above algorithm outputs the 0's and 1's in reverse order. You will need to write a second...
Write a function, namely shape(s) that takes a positive integer s as the parameter and draws...
Write a function, namely shape(s) that takes a positive integer s as the parameter and draws a square of length s. The square should be filled by a randomized color. Then write another function, that calls the shape function to draw 4 squares as a 2x2 table. Please answer in python.
write a C++ function called inc whose parameter is an integer reference type and that increases...
write a C++ function called inc whose parameter is an integer reference type and that increases the parameter by one when it is called.
6.32 LAB: Exact change - functions Write a program with total change amount as an integer...
6.32 LAB: Exact change - functions Write a program with total change amount as an integer input that outputs the change using the fewest coins, one coin type per line. The coin types are dollars, quarters, dimes, nickels, and pennies. Use singular and plural coin names as appropriate, like 1 penny vs. 2 pennies. Ex: If the input is: 0 or less, the output is: no change Ex: If the input is: 45 the output is: 1 quarter 2 dimes...
Problem: Grading Function #Write a function called getScore() that does not take any parameter. Inside the...
Problem: Grading Function #Write a function called getScore() that does not take any parameter. Inside the function, it asks the user to input scores for 3 items (Each of the items is out of 100). The final score is calculated by 20% of item1 + 30% of item2 + 50% of item3. After calculating the final score, the function returns it to the caller. The scores are floating-point numbers. #Write another function called getLetterGrade() that takes a float parameter and...
Write code in C please. #1 Write a function multiples() which will take an integer input...
Write code in C please. #1 Write a function multiples() which will take an integer input and it will print out all the multiples of this number starting from 2 but not including itself. For example, multiples(10) will print 2, 5 and multiples(100) will print 2, 4, 5, 10, 20, 25, 50 #2 Write and test a Fibonacci() function that uses a loop instead of recursion to calculate Fibonacci numbers.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT