Question

a) Write a function called print_doubles that prints the first n elements of an array of...

a) Write a function called print_doubles that prints the first n elements of an array of doubles. Assume the array is long enough.
Example:

double v[5] = {1,2,3,4,5};
print_doubles(v, 5); // prints [1,2,3,4,5]

b) Write a function with the following signature:

void get_min_max(const double values[], int n, double *pmin, double *pmax);
that returns in output parameters *pmin the value of the minimum element in array values and in *pmax the maximum element. Parameter n represents the size of array values.

c) Write a function with the following signature:
void moving_average3(const double x[], int n, double averages[]);

that computes in array averages the moving average (with window size 3) of the numbers in array x. Output values averages[i] will be the arithmetic average of values x[i], x[i+1], and x[i+2]. Thus, the number of elements in output array averages will be n – 2.

Example:

#define N 8

double values[N] = {1, 2, 3, 4, 5, 6, 7, 8};

double mavgs[N];

moving_average3(values, N, mavgs);
print_doubles(mavgs, N - 2);
// prints: [2.000000, 3.000000, 4.000000, 5.000000, 6.000000, 7.000000]

d) Write a function with the following signature:

void moving_average(const double x[], int n, int wsz, double averages[]);
that computes in array averages the moving average (with window size wsz) of the numbers in array x.

Example:

#define N 8

double values[N] = {1, 2, 3, 4, 5, 6, 7, 8};
double mavgs[N]; // N is max. size regardless of window sizeint winsz = 5;
moving_average(values, N, winsz, mavgs);
print_doubles(mavgs, N - winsz + 1);
// prints: [3.000000, 4.000000, 5.000000, 6.000000]

e) Write a main function that illustrates how the functions from parts a) – c) are used.

I was able to figure out letter a and b but im confused on how to do the moving_average3 and moving_average.

please answer in c language.

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

Please find the code below:

#include<stdio.h>                                                                   // header file

void print_doubles(double v[],int n)                                               // function print_doubles, it takes the array as parameter and the number of elements to print
{
   int i;                                                                           // taking a looping variable i
  
   for(i=0;i<n;i++)                                                               // looping i using for loop and print each element till the index value is n
   {
       printf("%lf\n",v[i]);
   }
  
}

void get_min_max(const double values[],int n,double *pmin,double *pmax)               // function get_min_max, it takes array, n and pmin and pmax pointer variable as parameter
{
   int i;                                                                           // looping variable i
      
   *pmin=values[0];                                                               // initializing pmin and pmax to the 1st element of the array
   *pmax=values[0];
  
   for(i=1;i<n;i++)                                                               // looping till n
   {
       if(values[i]<*pmin)                                                           // if any value in the array is less than pmin
       *pmin=values[i];                                                           // then that value is new pmin
      
       if(values[i]>*pmax)                                                           // if any value in the array is greater than pmax
       *pmax=values[i];                                                           // then that value is the new pmax      
   }
}

void moving_average3(const double x[],int n,double averages[])                       // this function takes our original array, n and averages array as parameter
{
   int i=0;                                                                       // initialize looping variable to 0
   double window=3;                                                               // initialize window variable to 3
   double sum=0;                                                                   // and sum variable to 0
  
   for(i=0;i<n;i++)                                                               // loop i from 0 till n
   {
       sum=x[i]+x[i+1]+x[i+2];                                                       // add the element to which i is pointing, the next element and the 2nd next element
       averages[i]=sum/window;                                                       // calculate averahe as sum/window, and store in averages array
   }
  
  
   printf("\n\nThe moving average when the window is 3\n");                       // call the print_double() and print the details
   print_doubles(averages,n-2);
}

void moving_average(const double x[],int n,int wsz,double averages[])               // this function takes the original array, n, the window size and averages array      
{
   int i=0;                                                                       // initialize loop variable i to 0
   int j=0;                                                                       // initialize loop variable j to 0, this variable will take care of the window
   int sum=0;                                                                       // and sum to 0
  
   for(i=0;i<n;i++)                                                               // loop i from 0 till n
   {
       for(j=0;j<wsz;j++)                                                           // loop j from 0 till window      
       {
           sum=sum+x[i+j];                                                           // calculate sum as sum + x[i+j], where i holds the current value, and j holds the window value
       }
       averages[i]=sum/wsz;                                                       // calculate average as sum/window
       sum=0;                                                                       // reinitialize sum to 0
   }
  
   printf("\n\nThe moving average when the window is %d \n",wsz);                   // call print_double() and print the details
   print_doubles(averages,n-wsz+1);
}

void main()
{
   double v[20]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
  
   printf("The first 7 elements in the array are:\n");
   print_doubles(v,7);
  
   double pmin,pmax;
  
  
   get_min_max(v,10,&pmin,&pmax);
  
   printf("\n\nThe minimum element in the 1st 10 elements is:");
   printf("%lf",pmin);
   printf("\n\nThe maximum element in the 1st 10 elements is:");
   printf("%lf",pmax);
  
   double averages[20];
  
   moving_average3(v,15,averages);
   moving_average(v,15,5,averages);
}


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

Output:

Add a comment
Know the answer?
Add Answer to:
a) Write a function called print_doubles that prints the first n elements of 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
  • Consider the following program that reads a number of nonnegative integers into an array and prints...

    Consider the following program that reads a number of nonnegative integers into an array and prints the contents of the array.   Complete the missing parts, add new function prototypes and function definitions, and test the program several times. Add the following to the program: Write a void function that prints the list of nonnegative integers in reverse. Write a void function that prints all the numbers stored in the list that are greater than 10. It will also print the...

  • Write a C++ function that prints an array, skipping over a number of elements. Use the...

    Write a C++ function that prints an array, skipping over a number of elements. Use the following header: void printArraySkip(int array[], int elems, int skip) { } Where 'array' is an input array of 'elem' integer elements, and 'skip' is the number of elements to skip over. For example, if the function is called like this: int array[6] = { 5, 7, 11, 12, 18, 19 }; printArraySkip(array, 6, 2); The output would be: 11 19 in c++. Thanks

  • (C++ program )Write a function that accepts an int array and the array’s size as arguments. The function should create a new array that is one element larger than the argument array. The first element...

    (C++ program )Write a function that accepts an int array and the array’s size as arguments. The function should create a new array that is one element larger than the argument array. The first element of the new array should be set to 0. Element 0 of the argument array should be copied to the element 1 of the new array. Element 1 of the argument array should be copied to element 2 of the new array, and so forth....

  • 17. Write a non-member function called centroid(param) that takes a static array of points and the...

    17. Write a non-member function called centroid(param) that takes a static array of points and the size of the array and return the centroid of the array of points. If there is no center, return the origins private: double x, y, z public: /Constructors Point(); Point(double inX, double inY, double inZ = 0); Point(const Point& inPt); / Get Functions double getX() const; double getY) const; double getZ) const; Set Functions void setX(double inX); void setY(double inY); void setZ(double inZ); void...

  • I need a c++ code please. 32. Program. Write a program that creates an integer constant...

    I need a c++ code please. 32. Program. Write a program that creates an integer constant called SIZE and initialize to the size of the array you will be creating. Use this constant throughout your functions you will implement for the next parts and your main program. Also, in your main program create an integer array called numbers and initialize it with the following values (Please see demo program below): 68, 100, 43, 58, 76, 72, 46, 55, 92, 94,...

  • P1) Write a complete C program that prints out the word YES, if its string command...

    P1) Write a complete C program that prints out the word YES, if its string command line argument contains the sequence the somewhere in it. It prints out the word NO otherwise. Both the word the and partial sequences like in the words theatre or brother qualify. Note: You can use string functions or not but if you do the only ones allowed are strcpy and strlen. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ P2) What is the output of the following program (one answer per...

  • code in c please Concepts to Practice User-written functions math.h Description For the prelab assignment, you...

    code in c please Concepts to Practice User-written functions math.h Description For the prelab assignment, you are to write a function to calculate Power (exponentiation). Your function will be called MyPower. MyPower() will raise a given double type number to a positive integer power. You may NOT call any function from math.h such as pow) in your implementation of MyPower) The main) function in your program should look exactly like this: int main(void) int i,j; for (i-1;i<5;i++) { for (j-1;j<5;j++)...

  • Write a C++ function printArray(A, m, n) that prints an m × n two dimensional array...

    Write a C++ function printArray(A, m, n) that prints an m × n two dimensional array A of integers, declared to be “int** A,” to the standard output. Each of the m rows should appear on a separate line.

  • Pointer arithmetic: a) Write a printString function that prints a string (char-by-char) using pointer arithmetics and...

    Pointer arithmetic: a) Write a printString function that prints a string (char-by-char) using pointer arithmetics and dereferencing. b) Write a function “void computeMinMax(double arr[], int length, double * min, double * max)” that finds the minimum and the maximum values in an array and stores the result in min and max. You are only allowed to use a single for loop in the function (no while loops). Find both the min and the max using the same for loop. Remember...

  • 1.Write code for a Java method, printArray, that takes an int array as parameter and prints...

    1.Write code for a Java method, printArray, that takes an int array as parameter and prints it out, one element per line. public static void printArray (int[] values){ Methods that do NOT return a result have “void” as return type. Notice how an array parameter type is passed, int [] }// end printArray 2.Write code for a Java method that takes in as input parameter an integer number, say num, and returns a double array of size num with 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