450 - Little Black Book

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

htl
Experienced poster
Posts: 185
Joined: Fri Jun 28, 2002 12:05 pm
Location: Taipei, Taiwan

450

Post by htl » Wed Jul 31, 2002 1:09 pm

Why does this code get WA? Is anything I didn't consider yet? ex. the data may be longer than 100 chars.(But it will cause RE, isn't it?)
[c]
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct list
{
char title[5],firstn[10],lastn[10],addr[100],depart[50],home[20],work[20],box[10];
}data;
int comp(const void*,const void*);
void main(void)
{
int n,x,count,y,z;
char depart[50],s[1000];
data *in[10000];
scanf("%d\n",&n);
count=0;
for(x=0;x<n;x++)
{
gets(depart);
while(gets(s)!=NULL)
{
if(!strlen(s))
break;
in[count]=(data *)malloc(sizeof(data));
for(y=0,z=0;depart[y]!='\0';y++)
in[count]->depart[z++]=depart[y];
in[count]->depart[z]='\0';
for(y=0,z=0;s[y]!=',';y++)
in[count]->title[z++]=s[y];
in[count]->title[z]='\0';
for(y++,z=0;s[y]!=',';y++)
in[count]->firstn[z++]=s[y];
in[count]->firstn[z]='\0';
for(y++,z=0;s[y]!=',';y++)
in[count]->lastn[z++]=s[y];
in[count]->lastn[z]='\0';
for(y++,z=0;s[y]!=',';y++)
in[count]->addr[z++]=s[y];
in[count]->addr[z]='\0';
for(y++,z=0;s[y]!=',';y++)
in[count]->home[z++]=s[y];
in[count]->home[z]='\0';
for(y++,z=0;s[y]!=',';y++)
in[count]->work[z++]=s[y];
in[count]->work[z]='\0';
for(y++,z=0;s[y]!='\0';y++)
in[count]->box[z++]=s[y];
in[count]->box[z]='\0';
count++;
}
}
qsort(in,count,sizeof(data *),comp);
for(x=0;x<count;x++)
{
printf("----------------------------------------\n");
printf("%s %s %s\n",in[x]->title,in[x]->firstn,in[x]->lastn);
printf("%s\n",in[x]->addr);
printf("Department: %s\n",in[x]->depart);
printf("Home Phone: %s\n",in[x]->home);
printf("Work Phone: %s\n",in[x]->work);
printf("Campus Box: %s\n",in[x]->box);
}
for(x=0;x<count;x++)
free(in[x]);
}
int comp(const void *a,const void *b)
{
return strcmp((*((data **)a))->lastn,(*((data **)b))->lastn);
}
[/c]

Fresh
New poster
Posts: 46
Joined: Mon Apr 15, 2002 10:42 am
Contact:

...

Post by Fresh » Wed Jul 31, 2002 3:24 pm

I think the problem is the way u handle/split the input. Try use dynamic memory and strtok.

[cpp]

// your structure
typedef struct list
{ char *title, *firstn, *lastn, *addr;
char *depart, *home, *work, *box;
}data;



i = 0;
while(gets(s)!=NULL)
{
if (!strlen(s)==0) break;

in->depart = (char*)malloc(strlen(p) + 1);
strcpy(in->depart,depart);

q = 0;
char *p = strtok(s,",\n");
while (p!=NULL)
{ if (i==0)
{ in->title = (char*)malloc(strlen(p) + 1);
strcpy(in->title,p);
}
else if (q == 1)
...
...
p = strtok(NULL,",\n");

if (q >= 6) q = -1;
q++;
}
i++;
}
[/cpp]

-novice :roll:

htl
Experienced poster
Posts: 185
Joined: Fri Jun 28, 2002 12:05 pm
Location: Taipei, Taiwan

Post by htl » Wed Jul 31, 2002 6:13 pm

Sorry for asking the syntax question. But I don't know why every data set finally is the same. Btw, do I have to modify the comp() to fit the new type definition of the data?
[c]
//data reading part
scanf("%d\n",&n);
count=0;
for(x=0;x<n;x++)
{
gets(depart);
len=strlen(depart);
while(gets(s)!=NULL)
{
if(!strlen(s))
break;
in[count]->depart=(char *)malloc(sizeof(char)*(len+1));
strcpy(in[count]->depart,depart);
temp=strtok(s,",");
in[count]->title=(char *)malloc(sizeof(char)*(strlen(temp)+1));
strcpy(in[count]->title,temp);
temp=strtok(0,",");
in[count]->firstn=(char *)malloc(sizeof(char)*(strlen(temp)+1));
strcpy(in[count]->firstn,temp);
temp=strtok(0,",");
in[count]->lastn=(char *)malloc(sizeof(char)*(strlen(temp)+1));
strcpy(in[count]->lastn,temp);
temp=strtok(0,",");
in[count]->addr=(char *)malloc(sizeof(char)*(strlen(temp)+1));
strcpy(in[count]->addr,temp);
temp=strtok(0,",");
in[count]->home=(char *)malloc(sizeof(char)*(strlen(temp)+1));
strcpy(in[count]->home,temp);
temp=strtok(0,",");
in[count]->work=(char *)malloc(sizeof(char)*(strlen(temp)+1));
strcpy(in[count]->work,temp);
temp=strtok(0,",");
in[count]->box=(char *)malloc(sizeof(char)*(strlen(temp)+1));
strcpy(in[count]->box,temp);
count++;
}
}

//comp function
int comp(const void *a,const void *b)
{
return strcmp((*((data **)a))->lastn,(*((data **)b))->lastn);
}
[/c]

SilVer DirectXer
New poster
Posts: 39
Joined: Wed Jan 22, 2003 11:02 am

450 - Little Black Book

Post by SilVer DirectXer » Fri Mar 07, 2003 12:55 pm

[cpp]
#include <stdlib.h>
#include <iostream.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
int GET_all_information(void);
struct rec{
char dept[300];
char pre_name[300];
char first_name[300];
char last_name[1000];
char address[300];
char telH[300];
char telO[300];
char box[300];
};
typedef struct rec recf;
recf record[5000];
unsigned int num_of_dep;
char dept[1000];
char dummy[2];
unsigned int current_record=0;
unsigned int current_dep;
void bubble_sort (recf *list, unsigned int left,unsigned int right );
void display_result(void);
void main(void)
{
unsigned int i;
while(1)
{ current_record=0;
cin>>num_of_dep;
if (feof(stdin))
exit(0);

for (current_dep=1;current_dep<=num_of_dep;current_dep++)
{

gets(dept);
GET_all_information();

}

bubble_sort(record,0,current_record-1);
display_result();

}
}

int GET_all_information(void)
{
unsigned int i=0;
char temp_char;
strcpy(record[current_record].dept,dept);
temp_char=getchar();
if (temp_char=='\n')
return 0;
while (temp_char!=',')
{
record[current_record].pre_name[i++]=temp_char;
temp_char=getchar();
}
temp_char=getchar();i=0;

while (temp_char!=',')
{
record[current_record].first_name[i++]=temp_char;
temp_char=getchar();
}
temp_char=getchar();i=0;

while (temp_char!=',')
{
record[current_record].last_name[i++]=temp_char;
temp_char=getchar();
}
temp_char=getchar();i=0;

while (temp_char!=',')
{
record[current_record].address[i++]=temp_char;
temp_char=getchar();
}
temp_char=getchar();i=0;

while (temp_char!=',')
{
record[current_record].telH[i++]=temp_char;
temp_char=getchar();
}
temp_char=getchar();i=0;

while (temp_char!=',')
{
record[current_record].telO[i++]=temp_char;
temp_char=getchar();
}
temp_char=getchar();i=0;

while (temp_char!='\n')
{
record[current_record].box[i++]=temp_char;
temp_char=getchar();
}
current_record++;
GET_all_information();

return 0;
}


void bubble_sort (recf *list, unsigned int left, unsigned int right )
{
unsigned int i,j,k;
recf temp;
for (i=0;i<right;i++)
for (j=0;j<=right-i-1;j++)
{
k=strcmp(list[j+1].last_name,list[j].last_name);
if (k<0)
{
temp=list[j];
list[j]=list[j+1];
list[j+1]=temp;
}
}

}


void display_result(void)
{
unsigned int i;

for(i=0;i<current_record;i++)
{
cout<<"----------------------------------------"<<endl;
cout<<record.pre_name<<" "<<record.first_name <<" "<<record.last_name<<endl;
cout<<record.address<<endl;
cout<<"Department: "<<record.dept<<endl;
cout<<"Home Phone: "<<record.telH<<endl;
cout<<"Work Phone: "<<record.telO<<endl;
cout<<"Campus Box: "<<record.box<<endl;
}
}



[/cpp]

O.....what's wrong with this easy task?

User avatar
saiqbal
New poster
Posts: 36
Joined: Wed Aug 07, 2002 4:52 pm
Location: Dhaka, Bangladesh
Contact:

RE:450

Post by saiqbal » Fri Mar 07, 2003 2:54 pm

check your bubble sort method. your program's output for even sample input doesn't matches.

thanx
-sohel

SilVer DirectXer
New poster
Posts: 39
Joined: Wed Jan 22, 2003 11:02 am

Post by SilVer DirectXer » Fri Mar 07, 2003 7:57 pm

[cpp]
#include <stdlib.h>
#include <iostream.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
int GET_all_information(void);
int fncompare (const void * a, const void * b );
struct rec{
char dept[300];
char pre_name[300];
char first_name[300];
char last_name[1000];
char address[300];
char telH[300];
char telO[300];
char box[300];
};
typedef struct rec recf;
recf record[5000];
int num_of_dep;
char dept[1000];
char dummy[2];
int current_record=0;
int current_dep;
void display_result(void);
void main(void)
{
int i;

while (1)
{
current_record=0;
cin>>num_of_dep;
if (feof(stdin))
break;
for (current_dep=1;current_dep<=num_of_dep;current_dep++)
{
gets(dept);
GET_all_information();
}
qsort(record,current_record,sizeof(recf),fncompare);
display_result();
}
}

int GET_all_information(void)
{
int i=0;
char temp_char;
strcpy(record[current_record].dept,dept);
temp_char=getchar();
if (temp_char=='\n')
return 0;
while (temp_char!=',')
{
record[current_record].pre_name[i++]=temp_char;
temp_char=getchar();
}
record[current_record].pre_name='\0';
temp_char=getchar();i=0;

while (temp_char!=',')
{
record[current_record].first_name[i++]=temp_char;
temp_char=getchar();
}
record[current_record].first_name='\0';
temp_char=getchar();i=0;

while (temp_char!=',')
{
record[current_record].last_name[i++]=temp_char;
temp_char=getchar();
}
record[current_record].last_name='\0';
temp_char=getchar();i=0;

while (temp_char!=',')
{
record[current_record].address[i++]=temp_char;
temp_char=getchar();
}
record[current_record].address='\0';
temp_char=getchar();i=0;

while (temp_char!=',')
{
record[current_record].telH[i++]=temp_char;
temp_char=getchar();
}
record[current_record].telH='\0';
temp_char=getchar();i=0;

while (temp_char!=',')
{
record[current_record].telO[i++]=temp_char;
temp_char=getchar();
}
record[current_record].telO='\0';
temp_char=getchar();i=0;

while (temp_char!='\n')
{
record[current_record].box[i++]=temp_char;
temp_char=getchar();
}
record[current_record].box='\0';
current_record++;
GET_all_information();

return 0;
}


void bubble_sort (recf *list, int left, int right )
{
int i,j,k;
recf temp;
for (i=0;i<right;i++)
for (j=0;j<=right-i-1;j++)
{
k=strcmp(list[j+1].last_name,list[j].last_name);
if (k<0)
{
temp=list[j];
list[j]=list[j+1];
list[j+1]=temp;
}
}
}


void display_result(void)
{
int i;

for(i=0;i<current_record;i++)
{
cout<<"----------------------------------------"<<endl;
cout<<record.pre_name<<" "<<record.first_name <<" "<<record.last_name<<endl;
cout<<record[i].address<<endl;
cout<<"Department: "<<record[i].dept<<endl;
cout<<"Home Phone: "<<record[i].telH<<endl;
cout<<"Work Phone: "<<record[i].telO<<endl;
cout<<"Campus Box: "<<record[i].box<<endl;
}
}



int fncompare(const void *a,const void *b)
{
return strcmp((*(recf*)a).last_name , (*(recf*)b).last_name);
}
[/cpp]

i am edited the code to use it buildin quick sort...
however, it still WA even sample input can run without problem.

User avatar
saiqbal
New poster
Posts: 36
Joined: Wed Aug 07, 2002 4:52 pm
Location: Dhaka, Bangladesh
Contact:

Re: 450

Post by saiqbal » Sat Mar 08, 2003 6:30 am

im wondering why u r getting WA instead of TLE. i checked both the codes u paste here and none actually terminates after end of input. i guess u better check it.

-sohel

SilVer DirectXer
New poster
Posts: 39
Joined: Wed Jan 22, 2003 11:02 am

Post by SilVer DirectXer » Mon Mar 10, 2003 10:19 am

umum....

actually i am not really know how should i terminate the input.....

how?

User avatar
saiqbal
New poster
Posts: 36
Joined: Wed Aug 07, 2002 4:52 pm
Location: Dhaka, Bangladesh
Contact:

Post by saiqbal » Mon Mar 10, 2003 4:07 pm

easy!! just comment out the while loop in the main method. there will be only one set of input in the input file.

good luck
-sohel

SilVer DirectXer
New poster
Posts: 39
Joined: Wed Jan 22, 2003 11:02 am

Post by SilVer DirectXer » Wed Mar 12, 2003 10:14 am

[cpp]
#include <stdlib.h>
#include <iostream.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
int GET_all_information(void);
int fncompare (const void * a, const void * b);
struct rec{
char dept[300];
char pre_name[300];
char first_name[300];
char last_name[1000];
char address[300];
char telH[300];
char telO[300];
char box[300];
};
typedef struct rec recf;
recf record[5000];
int num_of_dep;
char dept[1000];
char dummy[2];
int current_record=0;
int current_dep;
void display_result(void);
void main(void)
{
int i;

current_record=0;
cin>>num_of_dep;
for (current_dep=1;current_dep<=num_of_dep;current_dep++)
{
gets(dept);
GET_all_information();
}
qsort(record,current_record,sizeof(recf),fncompare);
display_result();
}


int GET_all_information(void)
{
int i=0;
char temp_char;
strcpy(record[current_record].dept,dept);
temp_char=getchar();
if (temp_char=='\n')
return 0;
while (temp_char!=',')
{
record[current_record].pre_name[i++]=temp_char;
temp_char=getchar();
}
record[current_record].pre_name='\0';
temp_char=getchar();i=0;

while (temp_char!=',')
{
record[current_record].first_name[i++]=temp_char;
temp_char=getchar();
}
record[current_record].first_name='\0';
temp_char=getchar();i=0;

while (temp_char!=',')
{
record[current_record].last_name[i++]=temp_char;
temp_char=getchar();
}
record[current_record].last_name='\0';
temp_char=getchar();i=0;

while (temp_char!=',')
{
record[current_record].address[i++]=temp_char;
temp_char=getchar();
}
record[current_record].address='\0';
temp_char=getchar();i=0;

while (temp_char!=',')
{
record[current_record].telH[i++]=temp_char;
temp_char=getchar();
}
record[current_record].telH='\0';
temp_char=getchar();i=0;

while (temp_char!=',')
{
record[current_record].telO[i++]=temp_char;
temp_char=getchar();
}
record[current_record].telO='\0';
temp_char=getchar();i=0;

while (temp_char!='\n')
{
record[current_record].box[i++]=temp_char;
temp_char=getchar();
}
record[current_record].box='\0';
current_record++;
GET_all_information();

return 0;
}

void display_result(void)
{
int i;

for(i=0;i<current_record;i++)
{
cout<<"----------------------------------------"<<endl;
cout<<record.pre_name<<" "<<record.first_name <<" "<<record.last_name<<endl;
cout<<record[i].address<<endl;
cout<<"Department: "<<record[i].dept<<endl;
cout<<"Home Phone: "<<record[i].telH<<endl;
cout<<"Work Phone: "<<record[i].telO<<endl;
cout<<"Campus Box: "<<record[i].box<<endl;
}
}

int fncompare(const void *a,const void *b)
{
return strcmp((*(recf*)a).last_name , (*(recf*)b).last_name);
}
[/cpp]

However, i still WA with this.....

SilVer DirectXer
New poster
Posts: 39
Joined: Wed Jan 22, 2003 11:02 am

Post by SilVer DirectXer » Sat Mar 15, 2003 8:20 pm

someone tell me to sort by first name if their last name is the same...
but i am still WA with that.

User avatar
SeaViperz
New poster
Posts: 4
Joined: Mon Dec 30, 2002 9:44 am

450 Little Black Book TLE

Post by SeaViperz » Thu Mar 20, 2003 11:41 am

hi everyone, can someone tell me why I get TLE? Thanks!
[cpp]
#include <stdio.h>
#include <string.h>
class Guy{
public:
char dept[100],title[10],fname[100],lsname[100],hfone[10],wfone[10],box[10],hadd[150];
void setGuy(char input[2000],char cdept[100]){
char *p;
strcpy(dept,cdept);
p=strtok(input,","); strcpy(title,p);
p=strtok(NULL,","); strcpy(fname,p);
p=strtok(NULL,","); strcpy(lsname,p);
p=strtok(NULL,","); strcpy(hadd,p);
p=strtok(NULL,","); strcpy(hfone,p);
p=strtok(NULL,","); strcpy(wfone,p);
p=strtok(NULL,","); strcpy(box,p);
return;
}
void printGuy(){
printf("--------------------\n");
printf("%s %s %s\n",title,fname,lsname);
printf("%s\n",hadd);
printf("Department: %s\n",dept);
printf("Home Phone: %s\n",hfone);
printf("Work Phone: %s\n",wfone);
printf("Campus Box: %s\n",box);
return;
}
};
int main(){
Guy guys[10000],swap;
char currdept[100],ln[2000]={'\0'};
int i,depts,c,j,currguy=0;
bool sorted;
scanf("%d",&depts);
gets(currdept);
for(i=0;i<depts;i++){
gets(currdept);
while(gets(ln)){
if(strlen(ln)==0) break;
guys[currguy++].setGuy(ln,currdept);
}
}
sorted=false;
while(!sorted){
sorted=true;
for(i=0;i<currguy-1;i++){
if( strcmp( guys.lsname, guys[i+1].lsname )>0){
sorted=false;
swap=guys;
guys=guys[i+1];
guys[i+1]=swap;
}
}
}
for(i=0;i<currguy;i++) guys.printGuy();
return 0;
}
[/cpp]
*****SeaViperz*****
Draw your fangs and let's duel.
For my poison serves forever as my fuel!

Dominik Michniewski
Guru
Posts: 834
Joined: Wed May 29, 2002 4:11 pm
Location: Wroclaw, Poland
Contact:

Post by Dominik Michniewski » Tue Jun 03, 2003 2:45 pm

Could anyone tell me, why this code gives me WA ?
I try with one set of input, now I try with more than one, but still WA ...
Please help me if anyone can !

Best regards
DM

Code: Select all

cut off after Accepted
Last edited by Dominik Michniewski on Fri Aug 27, 2004 9:34 am, edited 1 time in total.
If you really want to get Accepted, try to think about possible, and after that - about impossible ... and you'll get, what you want ....
Born from ashes - restarting counter of problems (800+ solved problems)

User avatar
sohel
Guru
Posts: 856
Joined: Thu Jan 30, 2003 5:50 am
Location: New York

450 ; why WA

Post by sohel » Sat Nov 01, 2003 7:46 am

Can someone give me any critical input for this problem.
This problem looks straight forward, but I keep getting WA.

Thanks.

deddy one
Experienced poster
Posts: 120
Joined: Tue Nov 12, 2002 7:36 pm

Post by deddy one » Tue Nov 11, 2003 9:00 am

yes it is straight forward problem,
just check your outputting format.
i.e :

print exactly 40 dash

[c]printf ("%s %s %s\n",data.title,data.name1,data.name2);
printf ("%s\n",data.add);
printf ("Department: %s\n",data.dept);
printf ("Home Phone: %s\n",data.hphone);
printf ("Work Phone: %s\n",data.wphone);
printf ("Campus box: %s\n",data.cbox);[/c]

if that doesn't fix it, then your problem maybe lie
on how you use your sorting method

hope this help.

Post Reply

Return to “Volume 4 (400-499)”