499 - What's The Frequency, Kenneth?

All about problems in Volume 4. 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
FlyDeath
Learning poster
Posts: 73
Joined: Wed Jan 02, 2002 2:00 am
Location: Taiwan

Post by FlyDeath » Sat Jan 26, 2002 5:56 pm

I really got mad with this problem,i fixed it many times and still got WA,can someone help me?Here's my code:


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

void main()
{
char temp[100000];
long times[256];
char temp2;
long i;
long len;
long max=0;
for(i=0;i<256;i++)
times=0;
while(scanf("%s%c",&temp,&temp2)!=EOF)
{
len=strlen(temp);
for(i=0;i<len;i++)
if((temp>=65&&temp<=90)||(temp>=97&&temp<=122))
times[temp]++;
if(temp2=='n')
{
max=0;
for(i=65;i<=90;i++)
if(times>max)
max=times;
for(i=97;i<=122;i++)
if(times>max)
max=times;
if(max!=0)
{
for(i=65;i<=90;i++)
if(times[i]==max)
printf("%c",i);
for(i=97;i<=122;i++)
if(times[i]==max)
printf("%c",i);
printf(" ");
printf("%ld",max);
printf("n");
}
for(i=0;i<256;i++)
times[i]=0;
}
}
}

Adrian Kuegel
Guru
Posts: 724
Joined: Wed Dec 19, 2001 2:00 am
Location: Germany

Post by Adrian Kuegel » Sat Jan 26, 2002 11:49 pm

A line can end with <space><newline>. In this case your Program doesnt work.
Why dont you use
scanf("%[^n]",temp);
A line is never longer than 500 characters, since my program reads only up to that size.
And in the case max==0 you should print all letters

rmukadam
New poster
Posts: 4
Joined: Wed May 01, 2002 2:51 pm

Q499

Post by rmukadam » Wed May 01, 2002 2:59 pm

Hi, the question seems straight forward but somehow I keep on getting WA. Here is my code:

void main(void){

char line[500];
long Counter[53];
char letters[53] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','\n'};
int i,j;

bool FLAG = true;
int Largest;

while(!cin.eof()){

for(i=0;i<53;++i)
Counter[i] = 0;

Largest = 0;

cin.getline(line,500);
for(i=0;i<500;++i){

switch (line[i]){
case 'A':
Counter[0] = Counter[0] + 1;
break;
case 'B':
Counter[1] = Counter[1] + 1;
break;
case 'C':
Counter[2] = Counter[2] + 1;
break;
ETC... case 'z':
Counter[51] = Counter[51] + 1;
break;
default:
break;

}
}

//Determining Largest and printing:
for(i=0;i<52;++i){
for(j=0;j<52;++j){
if(Counter[i] < Counter[j]){
FLAG = false;
break;
}

}

if(FLAG){
cout << letters[i];
Largest = Counter[i];
}
else
FLAG = true;

}
cout << " " << Largest << endl;
}
}

I wonder where am i going wrong......Any suggestions or help will be appreciated!!! :D

tp
New poster
Posts: 4
Joined: Sun Feb 03, 2002 2:00 am

Re: Q499

Post by tp » Wed May 01, 2002 5:47 pm

I don't write C++ but there seems to be a problem with your for cycle -

cin.getline(line,500);
for (i=0; i < 500;++i){ <-- should be (i = 0; i < strlen(line); ++i)
switch (line) { blaah... }
}

Easiest way to solve problem 499 would be to create an array - for example
int frequencies[256], and increase its elements according to the chars read
in (in your case it would look like frequencies[line]++) that you should put
into your for cycle... then you just find the most often occuring chars from
the array ranges ['A'-'Z'] and ['a'-'z']. The code will be much cleaner :)

razibcse
New poster
Posts: 50
Joined: Mon Jul 22, 2002 3:17 am
Location: SUST,BANGLADESH
Contact:

499:Why WA

Post by razibcse » Tue Aug 06, 2002 2:39 am

Why this gets WA..please let me know if anyone kindly sees it..

Code: Select all

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

void main()
{
char w[1002],ch[1002],y[1002],t1;
long q,h,f,t,l,n[1002],a,b,c,d,i,m,x;
while(gets(w))
{

d=0;
b=strlen(w);
for(a=0;a<b;a++)
 {
 t=1;
 if(w[a]==' ') continue;
 for(c=a+1;c<b;c++)
  {
  if(w[a]==w[c])
    {
    t++;
    for(i=c;i<(b-1);i++)
      w[i]=w[i+1];
      b--;
      c--;

    }
   }
   n[d]=t;
   ch[d]=w[a];
   d++;
  }
 l=d;
 h=0;
 for(d=0;d<l;d++)
   if(n[d]>h)
    h=n[d];
  f=0;
  for(d=0;d<l;d++)
    if(h==n[d])
      {
      y[f]=ch[d];
      f++;
      }
  m=f;
  for(q=0;q<m;q++)
   for(x=q+1;x<m;x++)
     if(y[q]>y[x])
       {
       t1=y[q];
       y[q]=y[x];
       y[x]=t1;
       }
 for(f=0;f<m;f++)
  printf("%c",y[f]);
  printf(" %ld\n",h);
}
}
[c][/c]

User avatar
Ming Han
Learning poster
Posts: 77
Joined: Thu Jun 06, 2002 7:10 pm
Location: Singapore
Contact:

Solution

Post by Ming Han » Fri Oct 25, 2002 5:38 pm

If I am correct about your code,
you only check for spacing.

You are also not suppose to check those that are not "A-Z and a-z".
:: HanWorks ::

Almost Human
Learning poster
Posts: 93
Joined: Sun Jan 12, 2003 3:30 pm

499 What's the Frequency Kenneth? please help

Post by Almost Human » Thu Apr 17, 2003 8:08 am

I can't figure out why I got wrong answer... ? Did I miss anything ? Please help!

this is my code

Code: Select all

#include <stdio.h>

int main ( )
{
  char input , flag = 0 ;
  int Upper[26] , Lower[26] ;

/*  freopen ( "499.in" , "r" , stdin ) ;
  freopen ( "499.out" , "w" , stdout ) ;*/

  for ( input = 0 ; input < 26 ; input ++ )
	 Upper[input] = Lower[input] = 0 ;

  while ( 1 )
  {
	 input = fgetc ( stdin ) ;

	 if ( input >= 'a' && input <= 'z' )
		Lower[input - 'a'] ++ ;
	 else if ( input >= 'A' && input <= 'z' )
		Upper[input - 'A'] ++ ;

	 if ( input == '\n' || input == EOF )
	 {
		int max = 0 , i ;

		for ( i = 0 ; i < 26 ; i ++ )
		  if ( Upper[i] > max ) max = Upper[i] ;
		for ( i = 0 ; i < 26 ; i ++ )
		  if ( Lower[i] > max ) max = Lower[i] ;

		if ( flag ) printf ( "\n" ) ;

		for ( i = 0 ; i < 26 ; i ++ )
		  if ( Upper[i] == max ) printf ( "%c" , i + 'A' ) ;
		for ( i = 0 ; i < 26 ; i ++ )
		  if ( Lower[i] == max ) printf ( "%c" , i + 'a' ) ;

		printf ( " %i" , max ) ;

		flag = 1 ;

		for ( i = 0 ; i < 26 ; i ++ )
		  Upper[i] = Lower[i] = 0 ;
	 }

	 if ( input == EOF ) break ;
  }

  return 0 ;
}

Eric
Learning poster
Posts: 83
Joined: Wed Sep 11, 2002 6:28 pm
Location: Hong Kong

Post by Eric » Thu Apr 17, 2003 10:52 am

[cpp] if ( input >= 'a' && input <= 'z' )
Lower[input - 'a'] ++ ;
else if ( input >= 'A' && input <= 'z' )
Upper[input - 'A'] ++ ;[/cpp]

Oh...you have a typing error.
You have typed 'z' instead of 'Z' in the nested loop. :wink:

Almost Human
Learning poster
Posts: 93
Joined: Sun Jan 12, 2003 3:30 pm

Post by Almost Human » Thu Apr 17, 2003 12:46 pm

Thanks Eric!!

At least 1 mistake had been discovered. I've tried to submitted again, and it's still "wrong answer"

Can anybody help me please ?

Code: Select all

#include <stdio.h>

int main ( )
{
  char input , flag = 0 ;
  int Upper[26] , Lower[26] ;

/*  freopen ( "499.in" , "r" , stdin ) ;
  freopen ( "499.out" , "w" , stdout ) ;*/

  for ( input = 0 ; input < 26 ; input ++ )
	 Upper[input] = Lower[input] = 0 ;

  while ( 1 )
  {
	 input = fgetc ( stdin ) ;

	 if ( input >= 'a' && input <= 'z' )
		Lower[input - 'a'] ++ ;
	 else if ( input >= 'A' && input <= 'Z' )
		Upper[input - 'A'] ++ ;

	 if ( input == '\n' || input == EOF )
	 {
		int max = 0 , i ;

		for ( i = 0 ; i < 26 ; i ++ )
		  if ( Upper[i] > max ) max = Upper[i] ;
		for ( i = 0 ; i < 26 ; i ++ )
		  if ( Lower[i] > max ) max = Lower[i] ;

		if ( flag ) printf ( "\n" ) ;

		for ( i = 0 ; i < 26 ; i ++ )
		  if ( Upper[i] == max ) printf ( "%c" , i + 'A' ) ;
		for ( i = 0 ; i < 26 ; i ++ )
		  if ( Lower[i] == max ) printf ( "%c" , i + 'a' ) ;

		printf ( " %i" , max ) ;

		flag = 1 ;

		for ( i = 0 ; i < 26 ; i ++ )
		  Upper[i] = Lower[i] = 0 ;
	 }

	 if ( input == EOF ) break ;
  }

  return 0 ;
}

Eric
Learning poster
Posts: 83
Joined: Wed Sep 11, 2002 6:28 pm
Location: Hong Kong

Post by Eric » Thu Apr 17, 2003 1:01 pm

[cpp] if ( flag ) printf ( "\n" ) ;[/cpp]
Why don't you print the new line immediately after the number max?
But, I don't know if this would cause WA.

willy
New poster
Posts: 11
Joined: Fri Aug 01, 2003 2:20 pm
Location: Singapore

Q499

Post by willy » Fri Aug 01, 2003 2:26 pm

My program seems correct, can anyone tell me what's wrong?
#include <stdio.h>
#include <string.h>
#include <ctype.h>

int main()
{
int count[255],max,length,a,now;
char abc[255],alpha[255];
strcpy(alpha,"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
while(gets(abc)!=NULL)
{
max = 0;
memset(count,0,sizeof(count));
length = strlen(abc);
for (a = 0; a < length; a++)
{
if (isalpha(abc[a]))
{
if (isupper(abc[a]))
{
now = abc[a] - 'A';
count[now]++;
if (count[now] > max)
max = count[now];
}
else
{
now = abc[a] - 'a' + 26;
count[now]++;
if (count[now] > max)
max = count[now];
}
}
}
for (a = 0; a < 25+25; a++)
if (count[a] == max)
printf("%c",alpha[a]);
printf(" %d\n",max);}
}

return 0;
}[c][/c][cpp][/cpp]

willy
New poster
Posts: 11
Joined: Fri Aug 01, 2003 2:20 pm
Location: Singapore

Q499

Post by willy » Fri Aug 01, 2003 2:26 pm

My program seems correct, can anyone tell me what's wrong?
#include <stdio.h>
#include <string.h>
#include <ctype.h>

int main()
{
int count[255],max,length,a,now;
char abc[255],alpha[255];
strcpy(alpha,"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
while(gets(abc)!=NULL)
{
max = 0;
memset(count,0,sizeof(count));
length = strlen(abc);
for (a = 0; a < length; a++)
{
if (isalpha(abc[a]))
{
if (isupper(abc[a]))
{
now = abc[a] - 'A';
count[now]++;
if (count[now] > max)
max = count[now];
}
else
{
now = abc[a] - 'a' + 26;
count[now]++;
if (count[now] > max)
max = count[now];
}
}
}
for (a = 0; a < 25+25; a++)
if (count[a] == max)
printf("%c",alpha[a]);
printf(" %d\n",max);}
}

return 0;
}[c][/c][cpp][/cpp]

willy
New poster
Posts: 11
Joined: Fri Aug 01, 2003 2:20 pm
Location: Singapore

Q499

Post by willy » Fri Aug 01, 2003 2:48 pm

My program seems correct, can anyone tell me what's wrong?
#include <stdio.h>
#include <string.h>
#include <ctype.h>

int main()
{
int count[255],max,length,a,now;
char abc[255],alpha[255];
strcpy(alpha,"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
while(gets(abc)!=NULL)
{
max = 0;
memset(count,0,sizeof(count));
length = strlen(abc);
for (a = 0; a < length; a++)
{
if (isalpha(abc[a]))
{
if (isupper(abc[a]))
{
now = abc[a] - 'A';
count[now]++;
if (count[now] > max)
max = count[now];
}
else
{
now = abc[a] - 'a' + 26;
count[now]++;
if (count[now] > max)
max = count[now];
}
}
}
for (a = 0; a < 25+25; a++)
if (count[a] == max)
printf("%c",alpha[a]);
printf(" %d\n",max);}
}

return 0;
}[c][/c][cpp][/cpp]

Master
Learning poster
Posts: 82
Joined: Thu Oct 10, 2002 1:15 pm
Location: St. Johns, Canada
Contact:

Post by Master » Wed Aug 13, 2003 6:53 am

I have not analysys your code. But you can try the followings

here you have to find the frequency of alphabet not other character such as space, comma(,) etc.

you need a sorting process and this should be stable. I used bubble sort for this problem.

M H Rasel
CUET Old Sailor

User avatar
UFP2161
A great helper
Posts: 277
Joined: Mon Jul 21, 2003 7:49 pm
Contact:

Post by UFP2161 » Wed Aug 13, 2003 7:08 am

[cpp]for (a = 0; a < 25+25; a++)[/cpp]
There are 26 letters in the English alphabet? =P

Post Reply

Return to “Volume 4 (400-499)”