This is a C programming. Use only If else statement to solve this. EE285 Lab 3:
ID: 3889072 • Letter: T
Question
This is a C programming. Use only If else statement to solve this.
EE285 Lab 3: Musical Note Generator Part One This lab will last two weeks. This week, you will write a simple program that takes in a musical note (A-G), an optional modifier of sharp (#) or flat (b), and an octave and return the frequency of the note in Hertz. Part one is due next week, after which we will introduce new material that builds on what you will learn today See the Note Table link on Blackboard to help you create functions that return a note's frequency (in particular, note the "Equations used for this table" link at the bottom). Each line represents a 1% step" in musical terminology and they are equally apart. The first column gives the note's name, its octave number, and whether it's flat or sharp. Sample output is given below A 4 440 Hz F# 5 739.99 Bb2 116.54 Instead of using a bunch of conditionals for this lab, try to parse the input using the scanf statement below to separate the note into different variables and use the equations from the website to generate frequencies for any note (e.g. G 20 should still return a valid frequency) scanf ("3ctc%d", &letter;, &modifier;, &octave;); Remember that the equations listed use half steps, not full steps, so think about that while writing your code. Another piece of advice is to use A 4 as your base note, as it is defined as exactly 440 Hz. Finally, note that while the letters start over at G, the octave rises at C (B3 is followed by Ca, not Ca). At some point, you will need to raise something to a power. You can do this by including math.h and using the function "pow(base, power)". Make sure to link the math library when compiling by adding "-Im" to the end of the compilation command Deliverables Demonstrate your code to a TA. Submit your code on Blackboard. Hold onto your code as you will use it next weekExplanation / Answer
/*
The musical scale is defined in such a way where each octave has notes from C D E F G H A B. Except between E F and B C, all ther notes have a note in between which is called sharp or flat. # denotes sharp and b denotes flat. So, D# and Eb basically denote the same note. To go from one note to another, we call it a half step. We set our base note to A4 with frequency 440Hz and use the formula f=f1*2^(n/12), where f1 is 440 and n is the number of half steps required to go to the note from A4. Right side is defined positive, left side negative.
*/
#include <stdio.h>
int main()
{
int octave;
char note,modifier;
scanf("%c%c%d",¬e,&modifier,&octave); //Input the complete note
freq(note,modifier,octave); //pass the value to the frequency calculator function
return 1;
}
void freq(char no,char mod,int oc)
{
//base note used is A 4
int c=1,d=3,e=5,f=6,g=8,a=10,b=12; //each note is given a corresponding number based on the keyboard notes
int n,o,m;
if(no=='A') n=a; //the note is given a number based on the previous definition
else if(no=='B') n=b;
else if(no=='C') n=c;
else if(no=='D') n=d;
else if(no=='E') n=e;
else if(no=='F') n=f;
else n=g;
o=oc-4; //find difference in number of octaves
if(mod=='#') m=1; //modifier is given +1 if its sharp or -1 if flat
else if(mod=='b') m=-1;
else m=0;
int hs=o*12+(n-a+m); //to calculate number of half steps using the logic
//No of half steps= (difference in octaves)+(difference in notes)+(difference in modifiers)
double f0=440.0; //Base frequency
double a0=1.0594630944; //12th root of 2
double ans=f0*pow(a0,hs);
printf("%4f",ans);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.