Question

In: Computer Science

IN C Write a function in the form: void play( int key, int duration) // duration...

IN C

Write a function in the form:

  void play( int key, int duration) // duration units are tenths of a second

which generates and prints samples of sin(w*t) for t=0,1,2,...,n-1 which represent a tone corresponding to piano key number key, where:

  n = (duration/10.0)*8000

  w = (2π440rkey-49)/8000

  r = 21/12

In the main program call your function to play the first three notes of three blind mice.

Solutions

Expert Solution

#code for above statement

Solution:

#include <stdio.h>
#include <math.h>
//incluuding the math header to file to compute the sin function
#define PI 3.14159265
//constant variable pi
//declaring the function play with parameters key and duration
void play(int key,int duration);
int main()
{
   //calling the play function passing for example key=2 and duration=10
   play(2,1);
}
//function of play
void play(int key,int duration)
{
   //declaring variable for n and t r and w
   int n,t;
   double r,w;
   //calucluating the n,r, according to the given information
   n=(duration/10.0)*80000;
   r=21/12;
   w=((2*PI*440*r*key)-(49))/8000;
   printf("%d",n);
   //for loop to find all the values of t from 0 to n-1
   for(t=0;t<n;t++)
   {
       printf("%lf\n",sin(w*t));
   }
}

#screenshot of the program

#output of the given program

I hope this is helpful to you!

Thank you!

- 0 C:\Users\sandeep\Desktop\a.c- [Executing) - Dev-C++ 5.11 File Edit Search View Project Execute Tools AStyle Window 2 * BB EE Help 1 :: 0 :: ~ * d TDM-GCC 4.9.2 32-bit Release v a.c 2 3 4 5 6 7 8 9 #include <stdio.h> #include <math.h> //incluuding the math header to file to compute the sin function #define PI 3.14159265 //constant variable pi /declaring the function play with parameters key and duration void play (int key, int duration); int main() 10 //calling the play function passing for example key=2 and duration=10 play (2,1); 12 } 13 14 //function of play void play(int key, int duration), 7/declaring variable for n and tr and w int n,t; double r,w; //calucluating the n,r, according to the given information n=(duration/10.0)*80000; r=21/12; w=((2*PI*440*r*key)-(49)/8000; printf("%d",n); //for loop to find all the values of t from 0 to n-1 for(t=0;t<n;t++) printf("%1f\n", sin(w*t));

-ox C:\Users\sandeep\Desktop\a.exe 0.359053 0.868555 0.986170 0.658831 0.034232 -0.605813 -0.972518 -0.900429 -0.422072 0.246722 0.804196 0.998822 0.742785 0.151608 -0.507973 -0.938360 -0.945365 -0.525826 0.130962 0.728661 0.997592 0.816416 0.266878 -0.403074 -0.891161 -0.977162 -0.622273 0.013382 0.642999 0.982499 0.878701 0.378439 -0.292573 -0.831577 -0.995379 -0.710072 -0.104384 0.548401 Process exited after 7.434 seconds with return value 8000 Press any key to continue ...


Related Solutions

4, Make the table project with C++. Write a function with the following interface: void multiplyTable(int...
4, Make the table project with C++. Write a function with the following interface: void multiplyTable(int num) This function should display the multiplication table for values from 1...num. For example, if the function is passed 10 when it is called, it should display the following: 1 2 3 4 5 6 7 8 9 10 2 4 6 8 10 12 14 16 18 20 3 6 9 12 15 18 21 24 27 30 4 8 12 16 20...
Write a method with the following header: public static void showGradeDistribution(int a, int b, int c,...
Write a method with the following header: public static void showGradeDistribution(int a, int b, int c, int d, int f) It should print a graph (using asterisks) for each of the letters entered in the reverse order of the parameter list and with a label. In addition, if A and B grades sum is equal or exceeds that of grades C and D and F, the message “Strong class!” should be displayed. For example a method call of: showGradeDistribution(5,7,4,4,3); Would...
C++ A void function named NextLeapYear() that takes an int reference parameter. If the parameter is...
C++ A void function named NextLeapYear() that takes an int reference parameter. If the parameter is positive, the function will assign it the next leap year after it; otherwise, the function will assign 4 to it.
Part 1.1 Write a function whose prototype is void exchange ( /*inout*/ int *p, /*inout*/ int...
Part 1.1 Write a function whose prototype is void exchange ( /*inout*/ int *p, /*inout*/ int *q ) that takes two pointers to integer variables and exchanges the values in these variables. Part 1.2 Write a function whose prototype is char lastChar ( /*in*/ const char *str ) that takes a nonempty C-String as parameter and returns the last character in the string. For example the call lastChar ("srjc") will return the character c. Part 1.3 Define an array of...
Make a function definition in C for the following: void insert (double *b, int c, double...
Make a function definition in C for the following: void insert (double *b, int c, double s, int pos); //Insert value s at position pos in array. //needs: // c > 0, pos >= 0, and pos <= c-1. Elements b[0]...b[c-1] exist. //this will do: //Elements from indexes pos up to c-2 have been moved up to indexes pos+1 up to c-1. The value s has been copied into b[pos]. //Note: the data that was in b[c-1] when the function...
Write a MIPS function using a MARS 4.5 complier named void makeRandomArray(int arr[], int size) that...
Write a MIPS function using a MARS 4.5 complier named void makeRandomArray(int arr[], int size) that generates and stores a specified number of random numbers each of whose values are between 0 and 1000 in an array
In c++ Rewrite the following recursive method into an iterative function.  void mystery (int n) {  ...
In c++ Rewrite the following recursive method into an iterative function.  void mystery (int n) {    cout<<"? ";    if (n > 0)      mystery (n - 1); }
Programmer defined Function / Arrays: 1.Write a function for printing an array. void print_array (int arr[],...
Programmer defined Function / Arrays: 1.Write a function for printing an array. void print_array (int arr[], int size); 2. Write a function to generate an array of random integers. void random_array (int arr[], int size, int range); The scale of numbers should be 1 to range. 3. Write a function to find the sum of a sequence of number. int sum (int arr[], int size); 4. Write a function rotate(arr[], d, n) that rotates arr[] of size n by d...
C++ Write the C++ code for a void function that prompts the user to enter a...
C++ Write the C++ code for a void function that prompts the user to enter a name, and then stores the user's response in the string variable whose address is passed to the function. Name the function getName.
What is time Complexity each of the following function? 1- void function(int n) {             for (int...
What is time Complexity each of the following function? 1- void function(int n) {             for (int i=n/2; i<=n; i++)                           for (int j=1; j<=n/2; j++)                                     for (int k=1; k<=n; k = k * 2)                                                 print ”Hello”; } 2- void function(int n) {             for (int i=n/2; i<=n; i++)                           for (int j=1; j<=n; j = 2 * j)                                     for (int k=1; k<=n; k = k * 2)                                                 print ”Hello”; } 3- void function(int n) {             for (int i=1; i<=n; i++)                           for (int j=1;...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT