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...
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...
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.
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...
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;...
R5.18The following function swaps two integers, without requiring a temporary variable(C++): void tricky_swap(int& a, int& b)...
R5.18The following function swaps two integers, without requiring a temporary variable(C++): void tricky_swap(int& a, int& b) { a = a - b; b = a + b; a = b - a; } However, it fails in one important case, namely when calling tricky_swap(x, x). Explain what should happen and what actually happens.
Consider the following program written in C syntax: void swap(int a, int b) { int temp;...
Consider the following program written in C syntax: void swap(int a, int b) { int temp; temp = a; a = b; b = temp;} void main() { int value = 2, list[5] = {1, 3, 5, 7, 9}; swap(value, list[0]); swap(list[0], list[1]); swap(value, list[value]); } For each of the following parameter-passing methods, what are all of the values of the variables value and list after each of the three calls to swap? Passed by value Passed by reference Passed...
Using c++, write a program that will display your name as a void function then will...
Using c++, write a program that will display your name as a void function then will perform the following by user-defined functions: a. to compute for the sum of two numbers (n1, n2) using function.
Assume that struct Node{        int item;        Node* link; }; Write function void list_remove(NodePtr& prev_ptr);...
Assume that struct Node{        int item;        Node* link; }; Write function void list_remove(NodePtr& prev_ptr); The function will remove the node after the node pointed by prev_ptr. c++
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT