C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 08-10-2003, 04:38 AM   #1
Registered User
 
Join Date: Nov 2002
Posts: 34
Unhappy a segfaulting algorythm

This is a "string2Hz" algorythm ; It is used to return the Hz of a
string is entered. This is the syntax:
[key][octave](optional [in/decrement (+/-)])
So for example:
C1
a1
a1+
c1+

These for example have to return error:
a+1
q1
cc1+


(error is 0 at this code)
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define A1BASE 440
char tmpchar[255];

int octave_man(void) // gets complete Hz-number of string tmpchar ... .
{
	char *tmpchar2,*tmpchar3,*tmpchar4,*tmpchar5;
	char *alphabad[] = { "a","b","c","d","e","f","g" };
	int inti,inti2;
	strncpy(tmpchar2,tmpchar,2); // kick possible garbage out, this mks it "user-friendly"
	if((strcmp("a1",tmpchar)) == 0) //we use tmpchar to make it bug-free ... . 
		return A1BASE;	// we don't need a huge calculation to define A1-value !!!
	for(inti=0;inti<=7;inti++) // count up the scale
	{
		if((strncmp(tmpchar2,alphabad[inti],0)) == 0)
		{
			strncpy(tmpchar3,tmpchar2,0); // seperation
			for(inti2 = 0;inti2<=6;inti2++) // count up the octave
			{
				sprintf(tmpchar4,"%s%d",tmpchar3,inti2); // tmp-usement
				if((strncmp(tmpchar2,tmpchar4,2)) ==0 )
				{
					sprintf(tmpchar4,"%d",inti2); // real association!
					break;
				}
			}
			sprintf(tmpchar5,"%s%d",tmpchar3,tmpchar4);
			if((strcmp(tmpchar5,tmpchar2)) == 0)
				return ((A1BASE + (A1BASE/7*inti))*inti2);	// return tone+octave as absolute Hz
			strcat(tmpchar5,"+");
			if((strcmp(tmpchar5,tmpchar2)) == 0)
				return ((A1BASE + (A1BASE/7*inti))+(A1BASE/14)*inti2);//return tone+octave+ as absolute Hz
			sprintf(tmpchar5,"%s%d-",tmpchar3,tmpchar4);
			if((strcmp(tmpchar5,tmpchar2)) == 0)
				return ((A1BASE + (A1BASE/7+inti))-(A1BASE/14)*inti2);//return tone+octave- as absolute Hz
		}		
		// if we leave the for(); loop generally, somebody entered something very
		// stupid! IT IS NOT VERY INTELIGENT TO POINT THIS OUT AFTER CALC SO MUCH :)
	}
//recapitulation: alphabad	: the scale
//		  tmpchar2 	: whole string
//		  tmpchar3 	: tone
//		  tmpchar4	: octave
//		  tmpchar5	: is just a tmp-char :)
//		  init		: integer used for the scale
//		  inti2		: integer used for the octave
	return 0;
}
int main(int argc, char *argv[])
{
	int integer;
	scanf("%s",tmpchar);
	if((integer =octave_man()) == 0)
	{
		printf("error!\n");
		return 1;
	}
	else
	{
		printf("%d\n",integer);
		return 0;
	}
}

A short appendix: The math behind it:
a1 = 440 Hz, a2 = 880! So b1 is (a1+(a1/7*key))*octave ,
so (A1 + (A1/7*1)) *1 = 502.857 (503 = B1 )!

A general formula to calculate a full key is:
(A1 + (A1/7*x)) *y
(X = Number of the key: a is 1 , b is 2, c is 3 ,... | Y = Octave )


This means:
a1 b1 c2 d2 e2 f2 g2 a2
440 503 566 629 691 754 817 880


If I have to calculate with incrementation, I just do:
(A1 + (A1/7*x) + (A1/14) *y
---------


Please, could somebody reply and tell me where make
octave_man() errors?
demonus is offline   Reply With Quote
Old 08-10-2003, 05:24 AM   #2
Registered User
 
Join Date: Nov 2002
Posts: 34
I see that it "takes some time", maybe the local time of the local heroes like salem is not very familiar with Europe , right ?

Could you please send me a reply to
hexe_2003@directbox.com

???? This would be very nice
demonus is offline   Reply With Quote
Old 08-10-2003, 05:46 AM   #3
Registered User
 
Join Date: Apr 2002
Posts: 1,571
Try changing

Code:
for(inti=0;inti<=7;inti++)
to ..

Code:
for(inti=0;inti< 7;inti++)
You were going for 8 times instead of 7.

edit:

There are some really questionable things about some of your code also. If someone doesn't beat me to it I'll post them later. I need some sleep now though.
__________________
"...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers
MrWizard is offline   Reply With Quote
Old 08-10-2003, 06:34 AM   #4
....
 
Join Date: Aug 2001
Location: Groningen (NL)
Posts: 2,386
Note that the variables tmpchar2, tmpchar3, tmpchar4 and tmpchar5 are pointers to char. If you want to use them as buffers to store strings, you will need to allocate memory for them.

Also note that this will lead to a redesign of your function. In case you have allocated memory, you must also free the memory before you leave the function.
Shiro is offline   Reply With Quote
Old 08-10-2003, 07:45 AM   #5
Registered User
 
Join Date: Nov 2002
Posts: 34
OK... .
Ähm, the allocation is not neccessary , ... of course you are right, but the example you see is "over-tested" ! I used arrays first,... .

Secondly, gdb tells me errors in strncmp, strcpy, strcmp and strcpy, no matter if I use pointers and allocate something first or just use arrays ... .

@Mr Wizard: Thx ... I ´ll try this ... wait a few seconds ...
demonus is offline   Reply With Quote
Old 08-10-2003, 07:52 AM   #6
Registered User
 
Join Date: Nov 2002
Posts: 34
ok, I have some errors in it, but it is NO segfaulting function anymore ... . Arrays:
char array[N] => Range: 0 to N-1

However, if I enter a value different then a1, the function returns zero (error)... .
demonus is offline   Reply With Quote
Old 08-10-2003, 01:55 PM   #7
End Of Line
 
Hammer's Avatar
 
Join Date: Apr 2002
Posts: 6,240
Post your latest code.

>>I see that it "takes some time"
We're not here at your demand, people will answer when they're good'n'ready.
__________________
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
Hammer is offline   Reply With Quote
Old 08-11-2003, 01:48 AM   #8
Registered User
 
Join Date: Nov 2002
Posts: 34
Talking

>Post your latest code.
>
Not neccessary anymore, I already got it last night!


>>I see that it "takes some time"
>We're not here at your demand, people will answer when they're good'n'ready.

Of course you are right! I am sorry that my "argumentation" sounds like "I am an a..." !
Thx for attention

demonus
demonus is offline   Reply With Quote
Old 08-11-2003, 08:06 AM   #9
Registered User
 
Join Date: Mar 2003
Posts: 143
Your basic algorithm is wrong. Have a look at http://www.math.niu.edu/~rusin/papers/uses-math/music/ it has loads of useful (and quite interesting) info on how music works mathematically as well as a list of frequencies for different notes on a correctly tuned piano which is probably worth a look to test your algorithm, as is the discussion of algorithms and why there are 12 notes in an octave. Enjoy!
__________________
DavT
-----------------------------------------------
DavT is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
segfaulting! CMakesMeSad :( C Programming 23 07-10-2009 01:04 PM
Segfaulting Distance Program radiohead C Programming 2 01-09-2006 08:48 PM
distance algorythm help MicroFiend Game Programming 9 06-12-2004 09:28 PM
Why is it segfaulting? XSquared C Programming 7 03-30-2004 06:52 AM
parameters algorythm linuxdude C Programming 2 12-02-2003 07:18 PM


All times are GMT -6. The time now is 06:52 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22