RockBand.com


View Full Version : Anyone here with good c++ knowledge?



Eman311
09-21-2007, 10:45 AM
I'm stuck on these two problems

Write a function with the signature: int binaryToInt(int k) which accepts k boolean inputs (as 0 or 1), and returns the int value represented by those k bits. The most significant bit should be the one which is entered first. That is, if the user enters the sequence 1010, the return value should be 10(ten). Use recursion in this function. Don't use exponents or powers. Write a test for your function, and add that test to main().


and

The "well-tempered" scale is a musical scale in which the frequency of any pitch is a constant multiple of the pitch before. There are twelve pitches in an octave, and the change frequency of a pitch is exactly twice the frequency of the pitch one octave below. Write a function with the signature double wellTempered(int pitch, int octave) with the property that wellTempered(0,0) == 27.5, wellTempered(x,y+1) == 2 * wellTempered(x,y), and wellTempered(x+1,y) == wellTempered(x,y) times the twelfth root of 2. Don't use recursion in this function. Use the pow function in cmath. Try to make your function as simple as possible. (Hint, use exponential identities -- you only need one call to pow)

devo193
09-21-2007, 10:57 AM
What the hell kinda math are you takin?

Eman311
09-21-2007, 11:14 AM
That's programming, not math.

Xzyliac
09-21-2007, 12:09 PM
I gave up on C++ a while ago though my good ol' buddy who is writing the code for our game speaks very fluent C++. Unfortunate for you he's out of the country.

ZkDotNet
09-21-2007, 02:53 PM
If you figure out the first one, can you let me know? It's actually bothering me. I can think of several ways of doing it, but I can't think of a single way recursion would be useful.

The second one confuses me a little too. Are you allowed to have constants that define what (0,0) is? Or, were you given an algorithm that would explain how it's 27.5?

kanerules
09-21-2007, 03:06 PM
Guess it didn't pay off to copy everything off of the guy next to me, back in Grade 10 Computer Science. :P But hey, I passed the class with an 89% anyways, so can't complain. :P

Kang_Zircon
09-21-2007, 03:08 PM
Eman, you have single-handedly talked me out of ever wanting to learn more about programming than I currently do. I read those two problems and immediately thought to myself "hmm, I think I'll practice painting more..."

I think the only meaning I could really glean from any of that was the term "recursion." Maybe it's because I learned programming in Java, or maybe it's because I simply have no business being a programmer.

no_direction_home
09-21-2007, 03:11 PM
i would help you but i only know lame ass pascal

Chthonic
09-22-2007, 12:34 AM
Sorry, bud. I know my way around C# and PHP, but I've never bothered to learn about bitwise operations. Guess I should someday, if I want to get better at this stuff.

Eman311
09-22-2007, 12:42 AM
If you figure out the first one, can you let me know? It's actually bothering me. I can think of several ways of doing it, but I can't think of a single way recursion would be useful.

The second one confuses me a little too. Are you allowed to have constants that define what (0,0) is? Or, were you given an algorithm that would explain how it's 27.5?

I'll keep you updated on the first one. I'm definetly a lot close, and have some people helping me out.

The second, i think I have to find an algorithm to define the frequency for any possible input while at the same time keeping that initial value.

Eman311
09-22-2007, 01:57 AM
well so far for the first one i have



#include <iostream>

using namespace std;

int binarytoInt (int k)
{
int total = 0;
string str1;
cout << "Enter a string of" << k <<"bits : 0 or 1 ";
cin >> str1;
for( int i=k;i>0;i--)
{
int x = str1.find(i);
total += 2*i;
}
cout << total;
return 0;
}

int main()
{
binarytoInt(4);
return 0;
}


doesn't work, but its gotta be close.

jq71586
09-22-2007, 02:07 AM
well so far for the first one i have



#include <iostream>

using namespace std;

int binarytoInt (int k)
{
int total = 0;
string str1;
cout << "Enter a string of" << k <<"bits : 0 or 1 ";
cin >> str1;
for( int i=k;i>0;i--)
{
int x = str1.find(i);
total += 2*i;
}
cout << total;
return 0;
}

int main()
{
binarytoInt(4);
return 0;
}


doesn't work, but its gotta be close.



*head explodes*

Bakkster
09-22-2007, 02:14 AM
int binarytoInt (int k)
{
int total = 0;
string str1;
cout << "Enter a string of" << k <<"bits : 0 or 1 ";
cin >> str1;
for( int i=k;i>0;i--)
{
int x = str1.find(i);
total += 2*i;
}
cout << total;
return 0;
}

You need to recurse (call binaryToInt from itself). You're close, just replace the for loop like this:

for( int i=k;i>0;i--)
{
int x = str1.find(i);
total += 2*i;
}
should become:

total += binaryToInt(k-1)
So binaryToInt will grab exactly one bit at a time, each time. Then it calls itself for k-1 to get the next bit.

The second one is more straightforward 27.5*(2^y)*(1/12th root of 2^x) Simplify and put that into one pow() call.

Hope that helps :cool:

Eman311
09-22-2007, 02:37 AM
I love you

if i call binarytoInt again, it asks for another input. How can i call each bit while sidestepping that part?

edit: i think i know. I need to write a test function. The binarytoInt function shouldn't have any input, it should just do what I need it to do.

I don't know, I R CONFUSED

Xzyliac
09-22-2007, 12:55 PM
*head explodes*

LOL!

Quite ****ing true.

ZkDotNet
09-22-2007, 03:56 PM
[Edited cuz i was wrong]

Okay.. first, tackle the last requirement. (Consider it a ghetto use of test driven development.) (My language constructs will be different. I'm a C# developer, so I'll use that or pseudo-code. There'll be some things I'm going to assume you know how to do in C++, such as casting, etc.)

Write the test first in the main function:


int main()
{
int testValue = binaryToInt(1010);
if (testValue == 10)
cout >> "Yay, you rock!";
else
cout >> "Boo, you suck!";
}


Next, there really shouldn't be any asking for input in the binaryToInt function. The input is the argument you're passing to the function.

Now, you'll need to loop through the digits. The ideal way would be to turn those into an array of boolean values (since, that's what they really represent). I don't have any bright ideas for that, so cast it to a string instead and continue as you were.

I'll just write the ghetto-pseudo-code:


int binaryToInt(int k)
{
int returnValue = 0;
string bits = (string)k; // Cast the int 'k' to a string
if (bits.size() == 1)
{
return (bits.find("1") == 0) ? 1 : 0;
}
returnValue += (bits.find("1") == 0) ? (bits.size() - 1) ^ 2 : 0;
returnValue += binaryToInt((int)bits.substr(1));
return returnValue;
}



I doubt that'll work out of the box. I tried to use C++ constructs where possible, but I'm not particularly familiar with casting in C++.

Like my real job, I did a poor job of commenting. Let me know if any of it is unclear.

Eman311
09-23-2007, 12:34 AM
Wow thanks a lot man. The only errors that come up are

"Can't convert from type into to string" and vice versa.

after a little fixing of minor errors

Eman311
09-23-2007, 06:24 AM
think you can help with this?

The "well-tempered" scale is a musical scale in which the frequency of any pitch is a constant multiple of the pitch before. There are twelve pitches in an octave, and the change frequency of a pitch is exactly twice the frequency of the pitch one octave below. Write a function with the signature double wellTempered(int pitch, int octave) with the property that wellTempered(0,0) == 27.5, wellTempered(x,y+1) == 2 * wellTempered(x,y), and wellTempered(x+1,y) == wellTempered(x,y) times the twelfth root of 2. Don't use recursion in this function. Use the pow function in cmath. Try to make your function as simple as possible. (Hint, use exponential identities -- you only need one call to pow)

JesusSilencio
09-23-2007, 07:23 AM
The second one is more straightforward 27.5*(2^y)*(1/12th root of 2^x) Simplify and put that into one pow() call.

He basically said it there. You just need to simplify it into one pow() call. so it would be something like this.

double wellTempered(int x, int y)
{
double a = 27.5 * 2^(y+x/12);
return a;
}

edit: Except you would use the pow() function rather than the carrot.

MotleyPriest111
09-23-2007, 07:27 AM
http://www.encyclopediadramatica.com/images/thumb/f/f5/Howdoishotweb.jpg/646px-Howdoishotweb.jpg

Eman311
09-23-2007, 08:03 AM
Looks like we've got a funny man here.