All about problems in Volume IV. If there is a thread about your problem, please use it. If not, create one with its number in the subject.
Moderator: Board moderators
by @ce » Tue May 29, 2012 5:02 pm
Getting correct for sample input but getting WA...plzz help me find the error in my code.
- Code: Select all
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
char arr[100][101];
int i = -1, max = -1;
while(gets(arr[++i]))
{
int l = 0;
while(arr[i][l++] != '\0');
if(l> max)
max = l;
}
for(int p = 0; p<max;p++)
{
for(int q = i; q>=0; q--)
{
if(arr[q][p] != '\0')
printf("%c", arr[q][p]);
}
if(p != max-1)
printf("\n");
}
//system("pause");
}
Last edited by
@ce on Tue Jun 19, 2012 8:19 am, edited 1 time in total.
-@ce
-

@ce
- Learning poster
-
- Posts: 61
- Joined: Mon May 28, 2012 8:46 am
- Location: Ranchi, India
by brianfry713 » Fri Jun 01, 2012 12:33 am
Try the sample I/O again.
-
brianfry713
- Guru
-
- Posts: 1771
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
by @ce » Fri Jun 01, 2012 10:21 am
I am still not finding anything wrong

Input
- Code: Select all
Rene Decartes once said,
"I think, therefore I am."
Output generated by my code:-
- Code: Select all
"R
Ie
n
te
h
iD
ne
kc
,a
r
tt
he
es
r
eo
fn
oc
re
e
s
Ia
i
ad
m,
.
"
Last edited by
@ce on Tue Jun 19, 2012 8:20 am, edited 1 time in total.
-@ce
-

@ce
- Learning poster
-
- Posts: 61
- Joined: Mon May 28, 2012 8:46 am
- Location: Ranchi, India
by brianfry713 » Fri Jun 01, 2012 11:42 pm
Your problem might be that arr is uninitialized and then you check it for '\0'. On my machine (and possibly the judge's) that gave different results.
-
brianfry713
- Guru
-
- Posts: 1771
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
by @ce » Sun Jun 03, 2012 3:11 pm
@brianfry....java version of the same code is also giving WA...neither i could correct the above C++ code
- Code: Select all
import java.util.Scanner;
class Main
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
String arr[] = new String[100];
int i = -1, max = -1;
while(in.hasNextLine())
{
arr[++i] = in.nextLine();
int l = arr[i].length();
if(l > max)
max = l;
}
for(int p = 0; p<=max-1;p++)
{
for(int q = i; q>=0; q--)
{
if(arr[q].length() > p)
System.out.print(arr[q].charAt(p));
}
if(p != max-1)
System.out.println();
}
}
}
Last edited by
@ce on Tue Jun 19, 2012 8:20 am, edited 1 time in total.
-@ce
-

@ce
- Learning poster
-
- Posts: 61
- Joined: Mon May 28, 2012 8:46 am
- Location: Ranchi, India
by brianfry713 » Mon Jun 04, 2012 10:49 pm
For your JAVA code, try the input:
- Code: Select all
"I think, therefore I am."
Rene Decartes once said,
Output should be:
- Code: Select all
R"
eI
n
et
h
Di
en
ck
a,
r
tt
eh
se
r
oe
nf
co
er
e
s
aI
i
da
,m
.
"
There should be a newline at the end of the last line.
-
brianfry713
- Guru
-
- Posts: 1771
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
by xtine.m » Thu Jun 14, 2012 4:38 am
I'm having the same problem. I can't seem to see what's wrong with my output. Please help.
- Code: Select all
#include <stdio.h>
int main(){
char input[100][100] = {'\0'};
int counter[100] = {0};
char ch;
int a = 0;
int b = 0;
int c = 0;
int h;
int i;
int nos = 0;
int high = 0;
while (scanf ("%c", &ch) != EOF) {
if (ch != '\n') {
if (ch != '\t'){
input[a][b] = ch;
b++;
}
} else {
counter[c] = b;
if (high <= b) {
high = b;
}
c++;
b = 0;
a++;
input[a][b] = ch;
}
nos = a;
}
a = nos - 1;
for (h = 0;h < high;) {
for (i = a; i >= 0; i--) {
printf("%c", input[i][h]);
}
printf("\n");
h++;
}
return 0;
}
-
xtine.m
- New poster
-
- Posts: 6
- Joined: Tue May 08, 2012 5:36 pm
by brianfry713 » Thu Jun 14, 2012 10:17 pm
For the sample input, you're printing null characters at the end of the last two lines.
-
brianfry713
- Guru
-
- Posts: 1771
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
by xtine.m » Fri Jun 22, 2012 11:55 am
brianfry713 wrote:For the sample input, you're printing null characters at the end of the last two lines.
hi! are you referring to the first sentence? since it has lesser characters it prints null characters. should it print a space? I edited my last code so it will print a space. so i added this code. but I still get a wrong answer.
- Code: Select all
for (j = 0; j < c; j++) {
temp = counter[j];
if (high > temp) {
min = high - temp;
for (k = min; k >= 0 ;) {
Tmin = temp+k;
input[j][Tmin] = ' ';
k--;
}
}
}
-
xtine.m
- New poster
-
- Posts: 6
- Joined: Tue May 08, 2012 5:36 pm
by brianfry713 » Fri Jun 22, 2012 11:34 pm
There should be spaces, not null characters. Post your full code.
-
brianfry713
- Guru
-
- Posts: 1771
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
by xtine.m » Mon Jun 25, 2012 3:43 am
Here's my full code:
- Code: Select all
#include <stdio.h>
int main(){
char input[100][100] = {'\0'};
int counter[100] = {0};
char ch;
int a = 0;
int b = 0;
int c = 0;
int h;
int i;
int j;
int k;
int temp;
int min = 0;
int Tmin = 0;
int nos = 0;
int high = 0;
while (scanf ("%c", &ch) != EOF) {
if (ch != '\n') {
if (ch != '\t'){
input[a][b] = ch;
b++;
}
} else {
counter[c] = b;
if (high <= b) {
high = b;
}
c++;
b = 0;
a++;
input[a][b] = ch;
}
nos = a;
}
for (j = 0; j < c; j++) {
temp = counter[j];
if (high > temp) {
min = high - temp;
for (k = min; k >= 0 ;) {
Tmin = temp+k;
input[j][Tmin] = ' ';
k--;
}
}
}
printf("\n");
a = nos - 1;
for (h = 0;h < high;) {
for (i = a; i >= 0; i--) {
printf("%d", input[i][h]);
}
printf("\n");
h++;
}
return 0;
}
-
xtine.m
- New poster
-
- Posts: 6
- Joined: Tue May 08, 2012 5:36 pm
by brianfry713 » Mon Jun 25, 2012 11:59 pm
Line 58 should be %c not %d.
-
brianfry713
- Guru
-
- Posts: 1771
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
by @ce » Tue Jun 26, 2012 2:23 pm
What's wrong??
My code :-
- Code: Select all
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
char arr[100][101];
int i = -1, max = -1;
for(int f = 1;f<100;f++)
arr[f][0] = '\0';
while(gets(arr[++i]))
{
int l = 0;
while(arr[i][l++] != '\0');
if(l> max)
max = l;
}
for(int p = 0; p<max;p++)
{
for(int q = i-1; q>=0; q--)
{
if(arr[q][p] != '\0')
printf("%c", arr[q][p]);
else
printf(" ");
}
printf("\n");
}
}
Input
- Code: Select all
"I think, therefore I am."
Rene Decartes once said,
Output
- Code: Select all
R"
eI
n
et
h
Di
en
ck
a,
r
tt
eh
se
r
oe
nf
co
er
e
s
aI
i
da
,m
.
"
-@ce
-

@ce
- Learning poster
-
- Posts: 61
- Joined: Mon May 28, 2012 8:46 am
- Location: Ranchi, India
by brianfry713 » Tue Jun 26, 2012 11:54 pm
You're printing an extra line at the end with two spaces.
-
brianfry713
- Guru
-
- Posts: 1771
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
by @ce » Wed Jun 27, 2012 7:39 pm
I tried this....still WA
My code :-
- Code: Select all
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
char arr[100][101];
int i = -1, max = -1;
for(int f = 1;f<100;f++)
arr[f][0] = '\0';
while(gets(arr[++i]))
{
int l = 0;
while(arr[i][l++] != '\0');
if(l> max)
max = l;
}
for(int p = 0; p<max-1;p++)
{
for(int q = i-1; q>=0; q--)
{
if(arr[q][p] != '\0')
printf("%c", arr[q][p]);
else
printf(" ");
}
printf("\n");
}
printf("\n");
}
-@ce
-

@ce
- Learning poster
-
- Posts: 61
- Joined: Mon May 28, 2012 8:46 am
- Location: Ranchi, India
Return to Volume IV
Who is online
Users browsing this forum: Google [Bot] and 0 guests