Question

for this assignment you will be creating a series of functions for manipulating an array of...

for this assignment you will be creating a series of functions for manipulating an array of data. The data will consist of randomly generated doubles. You should make the following functions:

  • generate() - This function fills an array with random doubles in a specified range. This function takes four inputs: an array of doubles, an integer representing the size of the array, and two doubles representing the lower an upper bounds of the range random values. For example, the function call:
    generate(data,100,-5.0,5.0);
    should fill the array called data[] with 100 random values between -5.0 and 5.0.
  • print() - This function should print an array of values. The function takes two inputs: an array and the size of the array. Exactly how the values are printed is up to you, but its probably best not to print each value on its own line.
  • average() - This function returns the average of an array of values. The function takes two inputs: an array and the size of the array, and it returns a double.
  • min() This function returns the smallest value from an array of values. The function takes two inputs: an array and the size of the array, and it returns a double.
  • max() This function returns the largest value from an array of values. The function takes two inputs: an array and the size of the array, and it returns a double.
  • addX() This function adds a value to every element in an array. function takes three inputs: an array, the size of the array, and the value to be added to each element of the array. For example, if you have an array [5.5, 6.6, 7.7] and you call:
    add(array, 3, 2.5)
    the array should become [8, 9.1, 10.2], that is each element had 2.5 added to it.
  • num_less() This function returns the number of values that are lower than the average value of an array of values. That is it counts how many values are less than the average and returns that number. The function takes two inputs: an array and the size of the array, and it returns an integer (the number of values below the average).
  • num_more() This function returns the number of values that are higher than the average value of an array of values. That is it counts how many values are less than the average and returns that number. The function takes two inputs: an array and the size of the array, and it returns an integer (the number of values above the average).
  • num_less() (same name as the function above) This function returns the number of values that are lower than a threshold value in an array of values. That is it counts how many values are less than the threshold value. The threshold value is passed to the function. The function takes three inputs: an array, the size of the array, and a double which is the threshold value. It returns an integer, which is the number of values below the threshold.
  • range() This returns the range of values in the array, i.e. the difference between the largest and smallest values in the array. The function takes two inputs: an array and the size of the array. It returns a double, which is the range.
  • Extra Credit: std_dev() This function returns the standard deviation of the values in the array. Note in the formula on the linked web page the Greek symbol is mu, which is the average (or mean) of the data. The square root function is defined in the cmath library, which you can include. The function takes two inputs: an array and the size of the array. It returns a double, which is the standard deviation.

Make sure to write a main() function that tests each of the functions. It should use the generate() function to fill the array. If you can't finish all of the functions make a note of which ones you did finish in the code comments.

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

Thanks for the question, here is the complete code including the extra credit part.

thank you !!

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

#include<iostream>
#include <cmath>
#include<cstdlib>

using namespace std;


void generate(double data[], int size, int lower, int upper){
  
   for(int i=0; i<size;i++){
       data[i] = ((double) rand() / (RAND_MAX))*(upper-lower) + lower;
   }  
}

void print(double data[], int size){
   for(int i=0; i<size;i++){
       cout<<data[i]<<" ";
   }
   cout<<endl<<endl;
  
}

double average(double data[], int size){
   double total = 0;
   for(int i=0; i<size;i++){
       total+=data[i];
   }
   return total/size;
}

double min(double data[], int size){
   double min_val=0;
   for(int i=0; i<size; i++){
       if(i==0)min_val=data[0];
       else if(min_val>data[i])min_val=data[i];
       }
  
   return min_val;
}

double max(double data[], int size){
   double max_val=0;
   for(int i=0; i<size; i++){
       if(i==0)max_val=data[0];
       else if(max_val<data[i])max_val=data[i];
       }
  
   return max_val;
}

void addX(double data[], int size, double delta){
   for(int i=0; i<size; i++){
       data[i]+=delta;
      
   }
}

int num_less(double data[], int size){
  
   double avg = average(data,size);
   int count=0;
   for(int i=0; i<size; i++){
       count+=data[i]<avg?1:0;
       }
  
   return count;
}

int num_more(double data[], int size){
  
   double avg = average(data,size);
   int count=0;
   for(int i=0; i<size; i++){
       count+=data[i]>avg?1:0;
       }
  
   return count;
}

int num_less(double data[], int size, double threshold){
  
  
   int count=0;
   for(int i=0; i<size; i++){
       count+=data[i]<threshold?1:0;
       }
  
   return count;
}

double range(double data[], int size){
  
   return max(data,size) - min(data,size);
}


double std_dev(double data[], int size){
   double avg = average(data,size);
   double squared_sum=0;
   for(int i=0; i<size;i++){
       squared_sum   += (data[i]-avg)*(data[i]-avg);
   }
      
   return pow(squared_sum/size,0.5);
}

int main(){
  
   int size=20;
   double data[size];
   generate(data,size,-5.0,5.0);
   print(data,size);
   cout<<"Average: "<<average(data,size)<<endl<<endl;
   cout<<"Min Value: "<<min(data,size)<<endl<<endl;
   cout<<"Max Value: "<<max(data,size)<<endl<<endl;
   addX(data,size,1);
   cout<<"Added 1 to all elements\n\n";
   print(data,size);
   cout<<"Num less than average: "<<num_less(data,size)<<endl<<endl;
   cout<<"Num more than average: "<<num_more(data,size)<<endl<<endl;
   cout<<"Num more than 0: "<<num_less(data,size,0)<<endl<<endl;
   cout<<"Range: "<<range(data,size)<<endl<<endl;
   cout<<"Standard Deviation: "<<std_dev(data,size)<<endl<<endl;
  
}

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

Add a comment
Know the answer?
Add Answer to:
for this assignment you will be creating a series of functions for manipulating an array of...
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
  • 1. Write a function named findTarget that takes three parameters: numbers, an array of integers -...

    1. Write a function named findTarget that takes three parameters: numbers, an array of integers - size, an integer representing the size of array target, a number The function returns true if the target is inside array and false otherwise 2. Write a function minValue that takes two parameters: myArray, an array of doubles size, an integer representing the size of array The function returns the smallest value in the array 3. Write a function fillAndFind that takes two parameters:...

  • In C Write a couple of functions to process arrays. Note that from the description of...

    In C Write a couple of functions to process arrays. Note that from the description of the function you have to identify what would be the return type and what would be part of the parameter. display(): The function takes an int array and it’s size and prints the data in the array. sumArray(): It takes an int array and size, and returns the sum of the elements of the array. findMax(): It takes an int array and size, and...

  • Do the following: - Write and test a function that takes an array of doubles and...

    Do the following: - Write and test a function that takes an array of doubles and returns the average of the values in the array - Write and test a function that takes an array of doubles and returns number of values in the array that are above the average of the values in the array - Write and test a function that takes an array of Strings and returns number of values in the array that start with an...

  • In c++ 1. Write a function named findTarget that takes three parameters - numbers: an array...

    In c++ 1. Write a function named findTarget that takes three parameters - numbers: an array of integers size: an integer representing the size of array target: a number The function returns true if the target is inside the array and false otherwise 2. Write a function min Valve that takes two parameters: myArray an array of doubles - size: an integer representing the size of array The function returns the smallest value in the array 3 Wrile a funcion...

  • For this c++ assignment, Overview write a program that will process two sets of numeric information....

    For this c++ assignment, Overview write a program that will process two sets of numeric information. The information will be needed for later processing, so it will be stored in two arrays that will be displayed, sorted, and displayed (again). One set of numeric information will be read from a file while the other will be randomly generated. The arrays that will be used in the assignment should be declared to hold a maximum of 50 double or float elements....

  • #include <stdio.h> // Define other functions here to process the filled array: average, max, min, sort,...

    #include <stdio.h> // Define other functions here to process the filled array: average, max, min, sort, search // Define computeAvg here // Define findMax here // Define findMin here // Define selectionSort here ( copy from zyBooks 11.6.1 ) // Define binarySearch here int main(void) { // Declare variables FILE* inFile = NULL; // File pointer int singleNum; // Data value read from file int valuesRead; // Number of data values read in by fscanf int counter=0; // Counter of...

  • CST-117 In-Class Assignment 7 A method stub, or signature, is the first line of a method....

    CST-117 In-Class Assignment 7 A method stub, or signature, is the first line of a method. Write a class that contains the following “stubs” for methods used in measurement conversion. You do not have to implement the methods. Here’s an example: Write a void method that takes an integer for the number of millimeters and displays the number of meters. Correct response: public void showMeters(int numMillimeters){} Write a method that takes two integers and displays their sum. Write a method...

  • Map, Filter, and Reduce are three functions commonly used in functional programming. For this assignment you...

    Map, Filter, and Reduce are three functions commonly used in functional programming. For this assignment you will be implementing all three, along with three minor functions that can be passed to them. Map The map function takes as arguments a function pointer and a integer vector pointer. It applies the function to every element in the vector, storing the results in-order in a new vector. It then returns a pointer to a new vector. You should pass your square function...

  • Description For this assignment, you will maintain an array of bank accounts. You will declare a...

    Description For this assignment, you will maintain an array of bank accounts. You will declare a struct type called bankAcct and have an array of bankAcct with maximum size of 20, the members of the struct are shown below struct bankAcct { string first , last ; double amount ; long acctNo ; short pin ; }; Each member is described below • string first stores the user’s first name • string last stores the user’s last name • double...

  • Note: None of these functions should use cout. It is OK to use cout while debugging...

    Note: None of these functions should use cout. It is OK to use cout while debugging to check how your function is working, but your final submission should not contain cout in any function but main. Head ==== Name the source file for this section head.cpp. Write a function named "head" which takes an array of integers as an argument, and returns the first element in the array. Write a function named main which outputs the result of testing your...

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