739 - Soundex Indexing

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

Moderator: Board moderators

mamun
A great helper
Posts: 286
Joined: Mon Oct 03, 2005 1:54 pm
Location: Bangladesh
Contact:

Post by mamun »

Code: Select all

for(int i=0;i<10;i++) 
cout<<ch1;
Since you have declared i in the for loop so its scope is only in this for loop. You can't reuse i after it without redeclaring it.This is what you are doing in main(). So it can be like

Code: Select all

for(int i=0;i<10;i++) 
cout<<ch1; 
cout<<"NAME"; 
for(int i=0;i<21;i++) 
cout<<ch1;
or

Code: Select all

int i;
for(i=0;i<10;i++) 
cout<<ch1; 
cout<<"NAME"; 
for(i=0;i<21;i++) 
cout<<ch1;
The latter method is preferable by me.
Use appropriate tag for posting.

Munni
New poster
Posts: 9
Joined: Tue Jan 03, 2006 4:59 pm

739 PE plz hlp me

Post by Munni »

I hv solved the CE, but still it is giving PE.I can't understand where the prblm is.Plz hlp me.

/*My code*/
#include<stdio.h>
#include<iostream.h>
#include<string.h>
char compare(char ch)
{

if((ch=='B')||(ch=='P')||(ch=='F')||(ch=='V'))
return '1';


if((ch=='C')||(ch=='S')||(ch=='K')||(ch=='G')||(ch=='J')||(ch=='Q')||(ch=='X')||(ch=='Z'))
return '2';


if((ch=='D')||(ch=='T'))
return '3';

if((ch=='L'))
return '4';


if((ch=='M')||(ch=='N'))
return '5';

if((ch=='R'))
return '6';


return '0';




}

int main(void)
{

char ch,str2[5],ch1=' ',ch2;
char tmp;
char str1[30];
int num=0,j,k,i;
for(i=1;i<10;i++)
cout<<ch1;
cout<<"NAME";
for(i=1;i<21;i++)
cout<<ch1;
cout<<"SOUNDEX CODE";
cout<<"\n";
while((ch=getchar())!=EOF)

{
if(ch=='\n')
{
strcpy(str2,"0000");
ch2=' ';
tmp=' ';
str1[num]='\0';
for(i=1;i<10;i++)
cout<<ch1;
cout<<str1;
for(i=1;i<25-num;i++)
cout<<ch1;
j=0;
k=0;
str2[j]=str1[k];
ch2=compare(str1[0]);
tmp=ch2;
j++;
k++;
while((j<=3)&&(k<num))
{

ch2=compare(str1[k]);
if(tmp!=ch2)
{
str2[j]=ch2;
if(ch2!='0')
j++;
tmp=ch2;
k++;

}
else
k++;

}
str2[4]='\0';
cout<<str2;
num=0;
cout<<"\n";

}

else
{

str1[num]=ch;
num++;

}


}

for(i=1;i<20;i++)
cout<<ch1;
cout<<"END OF OUTPUT";

return 0;


}

mamun
A great helper
Posts: 286
Joined: Mon Oct 03, 2005 1:54 pm
Location: Bangladesh
Contact:

Post by mamun »

You're supposed to print second column from 35th column. You're doing it 1 column before.

Munni
New poster
Posts: 9
Joined: Tue Jan 03, 2006 4:59 pm

739 PE

Post by Munni »

Dear solver, i hv changed my code , but still it is PE.Will u plz explain whr the prblm is.

Munni
New poster
Posts: 9
Joined: Tue Jan 03, 2006 4:59 pm

Post by Munni »

Thnks .I hv fix my problem nd got AC.

Zaspire
New poster
Posts: 36
Joined: Sun Apr 23, 2006 2:42 pm
Location: Russia

739: Anybody explain the Difference between two codes AC,WA

Post by Zaspire »

This is AC

Code: Select all

#include <stdio.h>

char res;

bool Code(char c)
{
	res='a';
	if (c=='A'||c=='E'||c=='I'||c=='O'||c=='U'||c=='H'||c=='W'||c=='Y') return false;
	if (c=='B'||c=='F'||c=='P'||c=='V') {res='1'; return true;}
	if (c=='C'||c=='G'||c=='J'||c=='K'||c=='Q'||c=='S'||c=='X'||c=='Z') {res='2'; return true;}
	if (c=='D'||c=='T') {res='3'; return true;}
	if (c=='L') {res='4'; return true;}
	if (c=='M'||c=='N') {res='5'; return true;}
	if (c=='R') {res='6'; return true;}
	res=' ';
	return false;
}

int main()
{
#ifndef ONLINE_JUDGE
	freopen("in.txt","r",stdin);
	freopen("out.txt","w",stdout);
#endif
	int size,length;
	char in[80],out[6],*t1,*t2,last;
	puts("         NAME                     SOUNDEX CODE");
	while (scanf("%s",in)!=EOF)
	{
		printf("         %s",in);
		t1=in;
		t2=out;
		size=0;
		*t2=*t1;
		length=1;
		Code(*t1);
		last=res;
		t1++;
		t2++;
		for (;(*t1!='\0')&&(length<4);t1++,size++)
		{
			if (Code(*t1)&&(last!=res))
			{
				*t2=res;
				t2++;
				length++;
			}
			if (res!=' ')last=res;
		}
		for (;length<4;t2++,length++)
			*t2='0';
		*t2='\0';
		for (;*t1!='\0';t1++,size++) ;
		for (;size<24;size++) putchar(' ');
		puts(out);
	}
	puts("                   END OF OUTPUT");
	return 0;
}
[/code]

Zaspire
New poster
Posts: 36
Joined: Sun Apr 23, 2006 2:42 pm
Location: Russia

Post by Zaspire »

But why this WA?

Code: Select all

#include <stdio.h>

char res;

bool Code(char c)
{
	res='a';
	if (c=='A'||c=='E'||c=='I'||c=='O'||c=='U'||c=='H'||c=='W'||c=='Y') return false;
	if (c=='B'||c=='F'||c=='P'||c=='V') {res='1'; return true;}
	if (c=='C'||c=='G'||c=='J'||c=='K'||c=='Q'||c=='S'||c=='X'||c=='Z') {res='2'; return true;}
	if (c=='D'||c=='T') {res='3'; return true;}
	if (c=='L') {res='4'; return true;}
	if (c=='M'||c=='N') {res='5'; return true;}
	if (c=='R') {res='6'; return true;}
	res=' ';
	return false;
}

int main()
{
#ifndef ONLINE_JUDGE
	freopen("in.txt","r",stdin);
	freopen("out.txt","w",stdout);
#endif
	int size;
	char in[80],out[6],*t1,*t2,last;
	puts("         NAME                     SOUNDEX CODE");
	while (scanf("%s",in)!=EOF)
	{
		out[4]='\0';
		t1=in;
		t2=out;
		size=0;
		*t2=*t1;
		Code(*t1);
		last=res;
		t1++;
		t2++;
		printf("         %s",in);
		for (;(*t1!='\0')&&(*t2!='\0');t1++,size++)
		{
			if (Code(*t1)&&(last!=res))
			{
				*t2=res;
				t2++;
			}
			if (res!=' ')last=res;
		}
		for (;*t2!='\0';t2++)
			*t2='0';
		for (;*t1!='\0';t1++,size++) ;
		for (;size<24;size++) putchar(' ');
		puts(out);
	}
	puts("                   END OF OUTPUT");
	return 0;
}

Zaspire
New poster
Posts: 36
Joined: Sun Apr 23, 2006 2:42 pm
Location: Russia

Post by Zaspire »

I don't initializing memory(out).
That why WA.

Moha
Experienced poster
Posts: 216
Joined: Tue Aug 31, 2004 1:02 am
Location: Tehran
Contact:

Post by Moha »

Please remove your code now!

vijay03
New poster
Posts: 33
Joined: Wed Sep 13, 2006 6:46 pm
Contact:

Cant find the error!

Post by vijay03 »

My code gives the correct answer for all the input in the problem statement and the inputs i could find on this board, but OJ still says my code gives WA. Could anyone please tell me whats the mistake in my code?

Code: Select all

Removed after ACC
Last edited by vijay03 on Fri Nov 24, 2006 11:57 am, edited 1 time in total.

vijay03
New poster
Posts: 33
Joined: Wed Sep 13, 2006 6:46 pm
Contact:

WA!

Post by vijay03 »

Could someone please tell me whats wrong with my code? Is there some kind of a formatting error?

ALEXANDRIA_2
New poster
Posts: 10
Joined: Tue Nov 07, 2006 2:03 pm
Location: Jakarta

Post by ALEXANDRIA_2 »

hello vijay03

try to increase your array size from b[5] to b[25]






hope u get AC soon...
Impossible is nothing

vijay03
New poster
Posts: 33
Joined: Wed Sep 13, 2006 6:46 pm
Contact:

Accepted

Post by vijay03 »

Thanks a lot Alexandria! Got ACC. :D

Fuad Hassan EWU
New poster
Posts: 38
Joined: Tue Jul 17, 2007 3:21 pm
Location: East West University

Post by Fuad Hassan EWU »

I am Constantly getting PE. plz help me. :roll:

Code: Select all


DELETED AFTER AC

:wink:
Eagle er moto daana meley urbo

zyxw
New poster
Posts: 24
Joined: Sat Mar 22, 2008 5:49 am
Location: Chennai
Contact:

Strange !!

Post by zyxw »

I was constantly getting WA.
Although problem statement doesn't say so, when i included '\n' after "END OF OUTPUT", i got AC :)
I am not totally useless, because I can still be used as a bad example :P

Post Reply

Return to “Volume 7 (700-799)”