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
sakhassan
Experienced poster
Posts: 105
Joined: Sat Mar 11, 2006 9:42 am
Location: cse,DU

Post by sakhassan »

trailing spaces means spaces after the words ... for eg.

"Hello World........" where "." represents space :) ... Hope u understand .....

use 'gets' instead of 'scanf' .....
Time that gone is gone forever ...

Debashis Maitra
Learning poster
Posts: 62
Joined: Sun Jul 09, 2006 8:31 am
Location: University of Dhaka
Contact:

Post by Debashis Maitra »

After a lot of PE finally i got AC

thnx jan bhai for his input

thnx everyone
Akash chhoyar swopno
Dream to touch the sky

blodstone
New poster
Posts: 6
Joined: Sun Nov 19, 2006 5:47 am

Post by blodstone »

I think I did waht all you guys post here, trailing spaces, new line etc but why still got PE? Someone help me please.

Code: Select all

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

int main(){
	freopen("input.in","r",stdin);
	freopen("output.out","w",stdout);
	vector<string> x;
	string buf;
	int i=0;
	int max=0;
	while(true){
		getline(cin,buf,'\n');
		i=buf.length();
		if(max<i) max=i;
		x.push_back(buf);
		if(feof(stdin)) break;	
	}
	for(int i=0;i<max;i++){
		for(int j=x.size()-1;j>=0;j--){
			if(i<x[j].length()){
				if(x[j][i]==32) cout << " ";
				else
				cout << x[j][i];
			}
			else
				cout << " ";
		}
		cout << endl;
	}
	return 0;
}

Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:

Post by Jan »

Replace your code with the following part (you know where :D )...

Code: Select all

while(getline(cin,buf,'\n')){ 
      i=buf.length();
      if(max<i) max=i;
      x.push_back(buf);
   }
Hope it helps.
Ami ekhono shopno dekhi...
HomePage

S.M. Arifuzzaman
New poster
Posts: 10
Joined: Sun Feb 11, 2007 1:01 pm
Location: [CSE,BUET],DHAKA,BANGLADESH.

490 wa plz help

Post by S.M. Arifuzzaman »

I don't know what's the wrong here. here's my code

Code: Select all

#include<stdio.h>

int main()
{
	//freopen("in.txt","r",stdin);
	//freopen("ot.txt","w",stdout);
	char arr[101][101]={' '},ch;
	int i=0,j=0,maxj=-1;   //maxj for maximum column length
	while(scanf("%c",&ch)!=EOF)
	{
		
		if(ch=='\n')
		{
			arr[i][j]=' ';
			i++;
			if(j>maxj)
				maxj=j;
			j=0;

		}
		

		else
		{
		
		
			arr[i][j]=ch;
			j++;
		
		
		}
			
	
	}
	j--;
	if(j>maxj)
				maxj=j;

	for(int m=0;m<=maxj;m++){
		for(int k=i;k>=0;k--)
			if(arr[k][m]!='\n')
				printf("%c",arr[k][m]);
		printf("\n");}



	return 0;
}
I have tried some sets of input posted in the forum by 'JAN' and 'mogers'
and got desired output. then why wrong????
I am so crazy now..... plz help(newcomer)
S. M. Arifuzzaman......
He dreamXtreme..................
But good4nothing................................

helloneo
Guru
Posts: 516
Joined: Mon Jul 04, 2005 6:30 am
Location: Seoul, Korea

Post by helloneo »

Hello..
If there is a thread already, don't create a new thread.. just use old one to post..

By the way, your array does not contain '\n'
What is this supposed to mean..?

Code: Select all

         if(arr[k][m]!='\n') 
            printf("%c",arr[k][m]); 

S.M. Arifuzzaman
New poster
Posts: 10
Joined: Sun Feb 11, 2007 1:01 pm
Location: [CSE,BUET],DHAKA,BANGLADESH.

Post by S.M. Arifuzzaman »

Initially i didn't substitute '\n' with ' '(space)<i think it was a mistake, isn't?>. so the two lines was included.
anyway then i substituted '\n' with ' '(space) but carelessly the two lines u indicated remained.
however, it is all the same the line "if(arr[k][m]!='\n')" remains or not. I just want to print arr[k][m]. Then why error?

***sorry for bad english
S. M. Arifuzzaman......
He dreamXtreme..................
But good4nothing................................

S.M. Arifuzzaman
New poster
Posts: 10
Joined: Sun Feb 11, 2007 1:01 pm
Location: [CSE,BUET],DHAKA,BANGLADESH.

Post by S.M. Arifuzzaman »

Hi, I m a newcomer facing wa in 490, but don't know why. I have checked my code with some sets of input posted in the forum by 'JAN' etc. my code produces expected output. so why wa????

Code: Select all

#include<stdio.h>

int main()
{
	//freopen("in1.txt","r",stdin);
	//freopen("ot.txt","w",stdout);
	char arr[500][500]={' '},ch;
	int i=0,j=0,maxj=-1;   //maxj for maximum column length
	while(scanf("%c",&ch)!=EOF)
	{
		
		if(ch=='\n')
		{
			arr[i][j]=' ';
			i++;
			if(j>maxj)
				maxj=j;
			j=0;

		}
		

		else
		{
		
		
			arr[i][j]=ch;
			j++;
		
		
		}
			
	
	}
	j--;
	if(j>maxj)
				maxj=j;

	for(int m=0;m<=maxj;m++){
		for(int k=i;k>=0;k--)
			
				printf("%c",arr[k][m]);
		printf("\n");}



	return 0;
}

SO plz plz plz help me. I m getting crazy..........
S. M. Arifuzzaman......
He dreamXtreme..................
But good4nothing................................

S.M. Arifuzzaman
New poster
Posts: 10
Joined: Sun Feb 11, 2007 1:01 pm
Location: [CSE,BUET],DHAKA,BANGLADESH.

Post by S.M. Arifuzzaman »

:cry:
is there nobody to help me.
then what can i do?
what should be done now???
:( :( :( :( :( :( :( :( :( :(
S. M. Arifuzzaman......
He dreamXtreme..................
But good4nothing................................

mf
Guru
Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland
Contact:

Post by mf »

Every line in input files, including the last one, is terminated by '\n'.
So, your program prints an unnecessary blank first column, that's why you get WA.

S.M. Arifuzzaman
New poster
Posts: 10
Joined: Sun Feb 11, 2007 1:01 pm
Location: [CSE,BUET],DHAKA,BANGLADESH.

Post by S.M. Arifuzzaman »

I hv done some modifications according to u(mf). but still wa. modifications are indicated in the code as comments.

Code: Select all

#include<stdio.h>

int main()
{
//	freopen("in.txt","r",stdin);
//	freopen("ot.txt","w",stdout);
	char arr[1000][1000]={' '},ch;
	int i=0,j=0,maxj=-1;   //maxj for maximum column length
	while(scanf("%c",&ch)!=EOF)
	{
		
		if(ch=='\n')
		{
			//arr[i][j]=' ';  //modification 1
			i++;
			j--;   //modification 2:
                           //newly included as per mf to deselect '\n' from printing
			if(j>maxj)
				maxj=j;
			j=0;

		}
		

		else
		{
		
		
			arr[i][j]=ch;
			j++;
		
		
		}
			
	
	}
//	j--;         //modification 3: 
                     //these 3 lines removed as even last line  contains '\n'
//	if(j>maxj)
///				maxj=j;

	for(int m=0;m<=maxj;m++){
		for(int k=i;k>=0;k--)
			
				printf("%c",arr[k][m]);
		printf("\n");}



	return 0;
}
S. M. Arifuzzaman......
He dreamXtreme..................
But good4nothing................................

mf
Guru
Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland
Contact:

Post by mf »

Still, your program prints a blank first column. Moreover, it prints \0 instead of some spaces.

Also, why not just let gets() handle all that business with lines and line terminators.
That's so much simpler:

Code: Select all

for (n = 0; gets(arr[n]) != NULL; n++);
// you've read n lines of input into arr[0], ..., arr[n-1]

S.M. Arifuzzaman
New poster
Posts: 10
Joined: Sun Feb 11, 2007 1:01 pm
Location: [CSE,BUET],DHAKA,BANGLADESH.

Post by S.M. Arifuzzaman »

I am busy with my exam now, so I m temporarily retired. Anyway thnx mf for his suggestion. I expect to start very soon with his suggestion.
S. M. Arifuzzaman......
He dreamXtreme..................
But good4nothing................................

lena
New poster
Posts: 28
Joined: Mon Mar 05, 2007 5:44 pm

490 PE~~~Help

Post by lena »

#include<stdio.h>
#include<string.h>
char a[100][102];
int n;
int len[100];
char res[120];
int main()
{
// freopen("490.txt","r+",stdin);
// freopen("490_O.txt","w+",stdout);
n = 0;
int i,j;
for(i=0;i<100;i++)
for(j=0;j<102;j++)
a[j]=0;
while(gets(a[n])&& strlen(a[n]) !=0 ){


n++;
}

//intf("%d\n",n);
for(i = 0;i<n;i++)len = strlen(a);
int ml = len[0];
for(i=0;i<n;i++) if(len>ml) ml = len;

for(i=0;i<ml;i++)
{
for(j=n-1;j>=0;j--)
if(a[j] == 0 || a[j] == '\t')
printf(" ");
else
printf("%c",a[j]);

if(i!=ml-1)
printf("\n");
}
//while(true){}
return 0;
}

Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:

Post by Jan »

Search your problem first. Don't open a new thread if there is one already.
Ami ekhono shopno dekhi...
HomePage

Post Reply

Return to “Volume 4 (400-499)”