10062 - Tell me the frequencies!
Moderator: Board moderators
10062
what's wrong with my code?
[c]
#include<stdio.h>
void main()
{
int f[256];
char line[1001];
int i,min,idx;
int ctr;
/*
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
*/
while(gets(line)!=NULL)
{
for(i=0;i<256;i++)
{
f=0;
}
for(i=0;line!='\0';i++)
{
f[line]++;
}
do{
ctr=0;
min=255;
for(i=32;i<128;i++)
{
if(f!=0 && f<=min)
{
min=f;
idx=i;
ctr++;
}
}
if(!ctr)
break;
printf("%d %d\n",idx,min);
f[idx]=0;
}while(1);
printf("\n");
}
/*
fcloseall();
*/
}
[/c]
I've tested it and it's seems fine to me....
any critical input?
[c]
#include<stdio.h>
void main()
{
int f[256];
char line[1001];
int i,min,idx;
int ctr;
/*
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
*/
while(gets(line)!=NULL)
{
for(i=0;i<256;i++)
{
f=0;
}
for(i=0;line!='\0';i++)
{
f[line]++;
}
do{
ctr=0;
min=255;
for(i=32;i<128;i++)
{
if(f!=0 && f<=min)
{
min=f;
idx=i;
ctr++;
}
}
if(!ctr)
break;
printf("%d %d\n",idx,min);
f[idx]=0;
}while(1);
printf("\n");
}
/*
fcloseall();
*/
}
[/c]
I've tested it and it's seems fine to me....
any critical input?
-
- New poster
- Posts: 10
- Joined: Wed Dec 17, 2003 3:12 pm
- Location: Dhaka
- Contact:
10062 Why wrong answer?
I got wrong answer. Can somebody check my code.
[cpp]
The code is removed.
[/cpp]
[cpp]
The code is removed.
[/cpp]
Last edited by Kamanashish on Tue Oct 26, 2004 3:18 pm, edited 1 time in total.
Work hard.
-
- New poster
- Posts: 44
- Joined: Fri Feb 20, 2004 5:52 pm
10062 - Tell me the frequences
Hi.
Here is the code I keep getting WA for:
[c]
#include <stdio.h>
int main()
{
char line[1001];
int freq[96], i, j, max;
while(fgets(line, 1000, stdin))
{
line[strlen(line) - 1] = '\0';
max = 0;
memset(freq, 0, sizeof(freq));
for (i = 0; i < strlen(line); i++)
{
if (line >= 32 && line < 128)
{
freq[line - 32]++;
if (freq[line - 32] > max) max = freq[line - 32];
}
}
for (i = 1; i <= max; i++)
for (j = 96; j >= 0; j--)
if (freq[j] == i) printf("%d %d\n", j + 32, i);
printf("\n");
}
return 0;
}
[/c]
Can anyone tell me where the mistake is, or at least give me some more test data?
Thanks.
Here is the code I keep getting WA for:
[c]
#include <stdio.h>
int main()
{
char line[1001];
int freq[96], i, j, max;
while(fgets(line, 1000, stdin))
{
line[strlen(line) - 1] = '\0';
max = 0;
memset(freq, 0, sizeof(freq));
for (i = 0; i < strlen(line); i++)
{
if (line >= 32 && line < 128)
{
freq[line - 32]++;
if (freq[line - 32] > max) max = freq[line - 32];
}
}
for (i = 1; i <= max; i++)
for (j = 96; j >= 0; j--)
if (freq[j] == i) printf("%d %d\n", j + 32, i);
printf("\n");
}
return 0;
}
[/c]
Can anyone tell me where the mistake is, or at least give me some more test data?
Thanks.
-
- Guru
- Posts: 1080
- Joined: Thu Dec 19, 2002 7:37 pm
Re: 10062 - Tell me the frequences
[quote="Tomislav Novak"]Hi.
Here is the code I keep getting WA for:
[c]
#include <stdio.h>
int main()
{
char line[1001];
// make it a little bigger, 1024 will do
int freq[96], i, j, max;
Here is the code I keep getting WA for:
[c]
#include <stdio.h>
int main()
{
char line[1001];
// make it a little bigger, 1024 will do
int freq[96], i, j, max;
I have give this input in your program:
AAAAAAAAABBBBBBBBBBBBCCCDDDDDDEEEEEEEEFFFFFFaaaaaaaarrrrrrrrrrrrrrrrrrrrrrrrrr
and it give output:
67 3
114 6
70 6
97 8
69 8
65 9
66 12
68 26
you see here 114(r) is 6 and 68(D) is 26
but output should be 68 6 and 114 26
try to overcome this problam.
note:you don't need to declare c[1001] two time. just remove the first declaration.
AAAAAAAAABBBBBBBBBBBBCCCDDDDDDEEEEEEEEFFFFFFaaaaaaaarrrrrrrrrrrrrrrrrrrrrrrrrr
and it give output:
67 3
114 6
70 6
97 8
69 8
65 9
66 12
68 26
you see here 114(r) is 6 and 68(D) is 26
but output should be 68 6 and 114 26
try to overcome this problam.
![:)](./images/smilies/icon_smile.gif)
note:you don't need to declare c[1001] two time. just remove the first declaration.
when i compile your code using g++ compiler it give this message:
" the `gets' function is dangerous and should not be used."
But I think it should not be a reason for WA.
for all input (i test) your code gives same output as my AC code give.
there may be some lack in gets function.
I solve it using getchar();
So, try again![:wink:](./images/smilies/icon_wink.gif)
" the `gets' function is dangerous and should not be used."
But I think it should not be a reason for WA.
for all input (i test) your code gives same output as my AC code give.
![:-?](./images/smilies/icon_confused.gif)
there may be some lack in gets function.
I solve it using getchar();
So, try again
![:wink:](./images/smilies/icon_wink.gif)
10062 > Tell me the frequencies! > Code changed.
Greetings!.
Can anyone PLEASE tell me why I got "Runtime Error (SIGSEGV)" in this problem??![:o](./images/smilies/icon_eek.gif)
The limits for the "ordinal" of the characters read are 33 to 127??. No character read, except for eoLn and eoF, will be out of those limits??.
since there is a Max = 1000 for the characters in a line, the maximum value for a char will be 1000, rite??.
Thanks in advance.
The message on my e-mail is:
[pascal]{ Bernardo E. L
Can anyone PLEASE tell me why I got "Runtime Error (SIGSEGV)" in this problem??
![:o](./images/smilies/icon_eek.gif)
The limits for the "ordinal" of the characters read are 33 to 127??. No character read, except for eoLn and eoF, will be out of those limits??.
since there is a Max = 1000 for the characters in a line, the maximum value for a char will be 1000, rite??.
Thanks in advance.
The message on my e-mail is:
My code:Your program has died with signal 11 (SIGSEGV). Meaning:
Invalid memory reference
Before crash, it ran during 0.045 seconds.
[pascal]{ Bernardo E. L
Last edited by _.B._ on Wed Apr 14, 2004 9:26 pm, edited 1 time in total.
_.
OUCH!.
OUCH!.WR wrote:What about blanks (ASCII 32)??!!
Seems to be that I forgot how to substract
![:o](./images/smilies/icon_eek.gif)
Thanks again WR!.
Got AC/PE. Will check for the PE now.
Keep posting!, and I'll re-study mathe basics
![:lol:](./images/smilies/icon_lol.gif)
_.
10062 tell me frequences!!!! WA 10062
hello
why j get wa ??
#include<stdio.h>
char stat [512];
void pw(void)
{
int i,j;
for(i=1;i!=1024;i++)
{
j=500;
while(j--)
{
if(stat[j]==i)printf("%d %d\n",j,stat[j]);
}
}
}
main()
{
int i;
char ch;
while(1)
{
ch=getchar();
if(ch=='\n'|| ch=='\r'){pw(); for(i=0;i!=512;i++)stat=0; printf("\n");}
else stat[ch]++;
if(ch==EOF){pw(); break;}
}
}
why j get wa ??
#include<stdio.h>
char stat [512];
void pw(void)
{
int i,j;
for(i=1;i!=1024;i++)
{
j=500;
while(j--)
{
if(stat[j]==i)printf("%d %d\n",j,stat[j]);
}
}
}
main()
{
int i;
char ch;
while(1)
{
ch=getchar();
if(ch=='\n'|| ch=='\r'){pw(); for(i=0;i!=512;i++)stat=0; printf("\n");}
else stat[ch]++;
if(ch==EOF){pw(); break;}
}
}