## 499 - What's The Frequency, Kenneth?

Moderator: Board moderators

FlyDeath
Learning poster
Posts: 73
Joined: Wed Jan 02, 2002 2:00 am
Location: Taiwan
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;
long times;
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;
}
}
}

Guru
Posts: 724
Joined: Wed Dec 19, 2001 2:00 am
Location: Germany
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

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

### Q499

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

void main(void){

char line;
long Counter;
char letters = {'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 = Counter + 1;
break;
case 'B':
Counter = Counter + 1;
break;
case 'C':
Counter = Counter + 1;
break;
ETC... case 'z':
Counter = Counter + 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!!! tp
New poster
Posts: 4
Joined: Sun Feb 03, 2002 2:00 am

### Re: Q499

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, 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
Contact:

### 499:Why WA

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,ch,y,t1;
long q,h,f,t,l,n,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]

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

### Solution

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

this is my code

Code: Select all

``````#include <stdio.h>

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

/*  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
[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. Almost Human
Learning poster
Posts: 93
Joined: Sun Jan 12, 2003 3:30 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 , Lower ;

/*  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
[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

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

int main()
{
int count,max,length,a,now;
char abc,alpha;
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

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

int main()
{
int count,max,length,a,now;
char abc,alpha;
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

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

int main()
{
int count,max,length,a,now;
char abc,alpha;
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
Contact:
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

UFP2161
A great helper
Posts: 277
Joined: Mon Jul 21, 2003 7:49 pm
Contact:
[cpp]for (a = 0; a < 25+25; a++)[/cpp]
There are 26 letters in the English alphabet? =P