Hello, im not sure how to do this as i tried comparing the two vectors but the d
ID: 3714931 • Letter: H
Question
Hello, im not sure how to do this as i tried comparing the two vectors but the dimensions are not the same, not sure how to go about it.
Define a third row vector, Button, that is the same length as the frequency and duration vectors. Then write code to compare each element of the frequency vector to the 4 x 2 matrix to identify which button number to assign to each element of the Button vector. For example, the Button vector for the 8 notes shown in Figure 7 would be Button = [2,2,3,1,2,3,2,2].
Note Frequency Duration Button Frequency 440 392 330 262 392 392 330 440 392 330 392 392 0.5 0.5 0.5 0.5 1 1 0.5 0.5 1 4Explanation / Answer
#include <vector>
#include <map>
using namespace std;
int main()
{
// 4x2 Matrix conating the Frequency and corresponding Button
// The first part of the pair is the frequency and the second the the button
map<int, int> buttons =
{
make_pair(440, 1),
make_pair(392, 2),
make_pair(330, 3),
make_pair(262, 4)
};
vector<char> Notes = { 'G', 'G', 'E', 'A', 'G', 'E', 'G', 'G' };
vector<int> Frequencies = { 392, 392, 330, 440, 392, 330, 392, 392 };
vector<double> Duration = { 0.5, 0.5, 0.5, 0.5, 1, 1, 0.5, 0.5 };
// Vector used to store the Button values
vector<int> Button;
// For each frequency from the vector of frquencies, find corresponding button
// value and store it in the Button vector
for (int f : Frequencies)
{
Button.emplace_back(buttons[f]);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.