490 - Rotating Sentences

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
Shahid
Learning poster
Posts: 68
Joined: Fri Oct 26, 2001 2:00 am
Location: Dhaka, Bangladesh
Contact:

problem 490 - rotating sentences

Post by Shahid »

hi ,i am getting runtime error from the judge. can anyone help me?
another question: isn't the problem statement ensures that tabs are not given as input, or it should be ignored in analyzing the input?

here is my code:

[c] /*@BEGIN_OF_SOURCE_CODE*/





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


void main()
{
char in[100][100] = {0};

int i= 0, j = 0, k, l, max = 0;

for( ; ; )
{
gets(in);
if(!strcmp(in, NULL))
break;

if(strlen(in) > max)
max = strlen(in);
i++;
}

for(j = 0; j < max; j++)
{
for(k = i-1; k >= 0; k--)
if(strlen(in[k]) > j)
printf("%c", in[k][j]);
else
printf(" ");

printf("\n");
}

}
/*@END_OF_SOURCE_CODE*/
[/c]
thanx in advance for helping
Stefan Pochmann
A great helper
Posts: 284
Joined: Thu Feb 28, 2002 2:00 am
Location: Germany
Contact:

Post by Stefan Pochmann »

One problem might be that sentences can have 100 characters, but you only have space for 99 characters (don't forget the '\0' character that marks the end of the string).
Shahid
Learning poster
Posts: 68
Joined: Fri Oct 26, 2001 2:00 am
Location: Dhaka, Bangladesh
Contact:

Post by Shahid »

i increased the array size , but the result is same :-?
Stefan Pochmann
A great helper
Posts: 284
Joined: Thu Feb 28, 2002 2:00 am
Location: Germany
Contact:

Post by Stefan Pochmann »

Maybe it's a bad idea to compare to the string at address NULL, since you're not allowed to access that address. Try comparing it to the empty string "".
Shahid
Learning poster
Posts: 68
Joined: Fri Oct 26, 2001 2:00 am
Location: Dhaka, Bangladesh
Contact:

Post by Shahid »

right u r, stefan. i just solved removing the null......
thanx for ur help. but one more question. isn't a empty string = "\0" = NULL...then whats the problem null is doing?
Ivor
Experienced poster
Posts: 150
Joined: Wed Dec 26, 2001 2:00 am
Location: Tallinn, Estonia

Post by Ivor »

When you pass "\0" C actually allocates memory for this one symbol, and passes the address. When you try to compare to NULL, you're trying to compare your string to the one at address NULL (or 0) and that's giving you the runtime error. It's the same if you tried to compare to string at some arbitrary address.

Ivor
mido
Learning poster
Posts: 78
Joined: Sun Jun 16, 2002 9:48 pm
Location: Cairo,Egypt

490 runtime error

Post by mido »

I get a runtime error with this...this almost always happens if I use string functions..anyways, here's my code if you wanna look :
[cpp]
#include <iostream.h>
#include <string.h>

char *str2[100];

int
i=0,
j=0,
max=0,
count=0;

void Rem_Tab(char*& x)
{
for (int i=0;i<strlen(x);i++)
{
while (x==9)
{
for (int j=i;j<strlen(x)-1;j++)
{
x[j]=x[j+1];
}
x[j]=0;
}
}
}

void main()
{
for (i=0;i<100;i++)
str2=new char[100];
i=0;
while (cin.getline(str2,110,'\n'))
{
count++;
if (strlen(str2)>max)
max=strlen(str2);
Rem_Tab(str2);
i++;
}
for (i=0;i<max;i++)
{
for (j=count-1;j>=0;j--)
{
if (i<strlen(str2[j]))
cout<<str2[j];
else
cout<<" ";
}
cout<<endl;
}
cout.flush();
}[/cpp]
mido
Learning poster
Posts: 78
Joined: Sun Jun 16, 2002 9:48 pm
Location: Cairo,Egypt

Post by mido »

Looks like I just had to extend the arrays somewhat.. oh well :)
razibcse
New poster
Posts: 50
Joined: Mon Jul 22, 2002 3:17 am
Location: SUST,BANGLADESH
Contact:

490: Why WA

Post by razibcse »

490: I always get WA...why this happens?Please tell me....Language:C


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

void main()
{
char w[110][110],ww[110][110];
long t,x,a,b,c,d,i[110],k;
a=0;
while(gets(w[a]))
{
t=0;
for(x=0;x<strlen(w[a]);x++)
if(w[a][x]!=' ')
{
t++;
ww[a][t]=w[a][x];
}
i[a]=t;
a++;
}

b=a;
k=0;
for(a=0;a<b;a++)
if(i[a]>k)
k=i[a];

for(d=0;d<=k;d++)
{
for(c=(b-1);c>=0;c--)
printf("%c",ww[c][d]);

printf("\n");
}

}
ttmoptic
New poster
Posts: 1
Joined: Tue Aug 27, 2002 7:51 pm

490 why WA

Post by ttmoptic »

I cannot see why I am getting WA. Language is c++

Code: Select all

#include <iostream>
#include<string>
#include <vector>
using namespace std;

void main(void)

{
        char ch;
        vector<string> sent;
        for(cin.get(ch);cin;cin.get(ch))
        {
            string word;
            word+=ch;
            for(cin.get(ch);ch!='\n';cin.get(ch))
                    word+=ch;
            sent.push_back(word);
        }
        int max=0;
        for(int i=0;i<sent.size();i++)
                if(sent[i].length()>max)
                        max=sent[i].length();
        for(int i=0;i<sent.size();i++)
                if(sent[i].length()<max)
                        sent[i].resize(max);
        for(int i=0;i<max;i++)
        {
                for(int j=sent.size()-1;j>=0;j--)
                                cout << sent[j][i];
                cout << endl;
        }
}
arc16
Learning poster
Posts: 62
Joined: Sun Aug 04, 2002 1:05 am
Location: Indonesia

Post by arc16 »

you have small mistake. try this:

Code: Select all

input:
a
bb
ccc
dddd
eeeee
ffff
ggg

your output:
gfedcb
gfedc
 fed
  e

correct output:
gfedcba
gfedcb
gfedc
 fed
  e
Hisoka
Experienced poster
Posts: 120
Joined: Wed Mar 05, 2003 10:40 am
Location: Indonesia

Post by Hisoka »

Code: Select all

input:
aaaaaa
bbbbbb
ccccccc
output:
cba
cba
cba
cba
cba
cba
and it doesn't NULL char. and only space char in back of the input.
like this:
b=space char.
akuborangbkerenbbbbbbbbbbbbbbbbbb... :wink:
little joey
Guru
Posts: 1080
Joined: Thu Dec 19, 2002 7:37 pm

Post by little joey »

Well, I don't know why, but your program can't handle input like this:

Code: Select all

1234

12
1234567890
1...56..9
...4.....0
123

12345
It should give:

Code: Select all

1.1.111.1
2.2..22.2
3.3..3..3
4..4.4..4
5...55...
....66...
.....7...
.....8...
....99...
...0.0...
Here spaces are replaced by dots, for readability.
Your program prints some things and then goes into an endles loop...

Hope it helps.
Hisoka
Experienced poster
Posts: 120
Joined: Wed Mar 05, 2003 10:40 am
Location: Indonesia

Post by Hisoka »

I think you don't understand the problem. input can more than 2 sentence. And your program only can store 2 input. And your algo program is wrong.
Dominik Michniewski
Guru
Posts: 834
Joined: Wed May 29, 2002 4:11 pm
Location: Wroclaw, Poland
Contact:

Post by Dominik Michniewski »

little joey, are you seru that you should print spaces at end of line? When I print it I got WA ...

Dominik MIchniewski
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)
Post Reply

Return to “Volume 4 (400-499)”