10528 - Major Scales

All about problems in Volume 105. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

Post Reply
B14CKD24G0N
New poster
Posts: 2
Joined: Thu Dec 09, 2010 2:40 pm

10528 Major Scales Wrong Answer Problem

Post by B14CKD24G0N »

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int hurufToInt(char* huruf) {
if (!strcmp(huruf,"C")) return 0;
else if (!strcmp(huruf,"C#")) return 1;
else if (!strcmp(huruf,"D")) return 2;
else if (!strcmp(huruf,"D#")) return 3;
else if (!strcmp(huruf,"E")) return 4;
else if (!strcmp(huruf,"F")) return 5;
else if (!strcmp(huruf,"F#")) return 6;
else if (!strcmp(huruf,"G")) return 7;
else if (!strcmp(huruf,"G#")) return 8;
else if (!strcmp(huruf,"A")) return 9;
else if (!strcmp(huruf,"A#")) return 10;
else if (!strcmp(huruf,"B")) return 11;
}

char* intToHuruf(int huruf) {
char temp[10] = "";
if (huruf == 0) strcpy(temp,"C");
else if (huruf == 1) strcpy(temp,"C#");
else if (huruf == 2) strcpy(temp,"D");
else if (huruf == 3) strcpy(temp,"D#");
else if (huruf == 4) strcpy(temp,"E");
else if (huruf == 5) strcpy(temp,"F");
else if (huruf == 6) strcpy(temp,"F#");
else if (huruf == 7) strcpy(temp,"G");
else if (huruf == 8) strcpy(temp,"G#");
else if (huruf == 9) strcpy(temp,"A");
else if (huruf == 10) strcpy(temp,"A#");
else if (huruf == 11) strcpy(temp,"B");
return temp;
}

int main() {
char data[2000]="";
bool music[12][12];
music[0][0] = 1; //C
music[0][1] = 0; //C#
music[0][2] = 1; //D
music[0][3] = 0; //D#
music[0][4] = 1; //E
music[0][5] = 1; //F
music[0][6] = 0; //F#
music[0][7] = 1; //G
music[0][8] = 0; //G#
music[0][9] = 1; //A
music[0][10] = 0; //A#
music[0][11] = 1; //B
for (int i=1; i<12; i++) {
for (int j=1; j<12;j++) {
music[j] = music[i-1][j-1];
}
music[0] = music[i-1][11];
}
bool kel = false;
while (!kel) {
gets(data);
if (strcmp(data,"END")) {
char* huruf;
bool result[12];
for (int i=0; i<12; i++) {
result = 1;
}
huruf = strtok(data," ");
while (huruf!=NULL) {
for (int i=0; i<12; i++)
result &= music[hurufToInt(huruf)];
huruf = strtok(NULL," ");
}
int jum=0;
for (int i=0; i<12; i++) {
if (result) {
if (jum) printf(" ");
jum++;
char tResult[10] = "";
strcpy(tResult,intToHuruf(i));
printf("%s",tResult);
}
}
printf("\n");
} else kel = true;
}
//getch();
return 0;
}
B14CKD24G0N
New poster
Posts: 2
Joined: Thu Dec 09, 2010 2:40 pm

Re: 10528 Major Scales Wrong Answer Problem

Post by B14CKD24G0N »

Can anyone help me?
I didn't know, why my code is get a wrong answer.
Thx.
surya ss
New poster
Posts: 22
Joined: Sat Jun 11, 2005 7:31 pm

Re: 10528 Major Scales Wrong Answer Problem

Post by surya ss »

There will be an unexpected output in the intToHuruf function
char temp[10] is a local variable, you can't return pointer to this variable since it will be used by other when the process is out of the function

PS: just try to make a more elegant code using array of string like:
const char *map[] = {"C", "C#"}; etc
and print the output using map;
B14CKD24G0N wrote:

Code: Select all

char* intToHuruf(int huruf) {
	char temp[10] = "";
	if (huruf == 0) strcpy(temp,"C");
	else if (huruf == 1) strcpy(temp,"C#");
	else if (huruf == 2) strcpy(temp,"D");
	else if (huruf == 3) strcpy(temp,"D#");
	else if (huruf == 4) strcpy(temp,"E");
	else if (huruf == 5) strcpy(temp,"F");
	else if (huruf == 6) strcpy(temp,"F#");
	else if (huruf == 7) strcpy(temp,"G");
	else if (huruf == 8) strcpy(temp,"G#");
	else if (huruf == 9) strcpy(temp,"A");
	else if (huruf == 10) strcpy(temp,"A#");
	else if (huruf == 11) strcpy(temp,"B");
	return temp;
}
Post Reply

Return to “Volume 105 (10500-10599)”