anyways, I did the question in C++, but I dont know why it doesn't work, I did it in turing, and it worked, and I don't know what's wrong with it. could someone please help me?
btw, the question required input from file, but since i don't know how to do that, i made it keyboard input.
code:
#include <iostream.h>
int main ()
{
int tot, posx, posy;
int count =0;
int pos [8][8];
for (int x= 0; x<8; x++)
{
for (int y= 0; y<8; y++)
{
pos [x][y] = 0;
}
}
char type [1];
cout << "Input the total number" << endl;
cin >> tot;
for (int i=0;i<tot; i++)
{
cin >> type >> posx >> posy;
if (type == "a")
{
pos [posx-1][posy-1]++;
pos [posx-1][posy-2]++;
pos [posx-1][posy-3]++;
pos [posx][posy-3]++;
pos [posx+1][posy-3]++;
for (int k=0; k<8; i++)
{
for (int j=0; j<8; j++)
{
if (pos[k][j] != 0)
{
count ++1;
}
}
}
cout << count;
return 0;
}
Sponsor Sponsor
SilverSprite
Posted: Mon Jun 23, 2003 9:01 am Post subject: (No subject)
Ok i dont know the question becomes i'm too lazy to read it but the problem might be all the pos[wahtever][whatever]++1 and the count++1; You shouldnt have the ones there. Also watch your array subscripts dont go out of range.. c++ doesnt warn you about those..
AsianSensation
Posted: Mon Jun 23, 2003 3:47 pm Post subject: (No subject)
so instead of using count++, I should use +=1?
Catalyst
Posted: Mon Jun 23, 2003 3:48 pm Post subject: (No subject)
in ur prog u have
code:
count++1;
you only need
code:
count++;
hould be
SilverSprite
Posted: Mon Jun 23, 2003 4:21 pm Post subject: (No subject)
Ok what part of "the 1 shouldnt be there" dont you get asian:P *sigh* hopeless
AsianSensation
Posted: Mon Jun 23, 2003 4:45 pm Post subject: (No subject)
.....man, Im so stupid.....*smaking self over head
anyways, I dont think that was the problem, I think the if statement are the problem
When I tried to use that, it outputted "Boo!" every single time, even if I entered "a".
SilverSprite
Posted: Mon Jun 23, 2003 7:34 pm Post subject: (No subject)
Nope the if statements arent the problem.. Do you know the difference between a char and a string.. doesnt look like it...
Homer_simpson
Posted: Mon Jun 23, 2003 8:35 pm Post subject: (No subject)
y not use string instead of characters?
Sponsor Sponsor
AsianSensation
Posted: Mon Jun 23, 2003 8:54 pm Post subject: (No subject)
uh......string?
nope, still haven't learned string yet....
but I asked bugz, and he said i could use
code:
char type;
cin >> type;
if (type == 'a')
that worked, but still, the program isnt working, i'll go and try and fix it, and maybe learn string while im at it.
Homer_simpson
Posted: Mon Jun 23, 2003 9:25 pm Post subject: (No subject)
well yeah you could check it like this i guess
if ((type[1]=='a')&&(type[2]=='\0')) then yay!!
but strings are much much easier...
rizzix
Posted: Tue Jun 24, 2003 1:48 pm Post subject: (No subject)
code:
char* this_is_a_c_type_string = "this is a string";
u see c++ is backward compatible with c so that works.
yes, use pointers to char instead.
so all u have to do for checking:
code:
if (str == "a")
...
else
...
SilverSprite
Posted: Tue Jun 24, 2003 7:43 pm Post subject: (No subject)
That's what i meant by strings and chars Asian.. double quotes for strings and single quotes for chars.. a char is only one character hence char(acter) and a string is for multiple characters hence a 'string' of characters..tut tut turing corruption..
AsianSensation
Posted: Wed Jul 02, 2003 9:55 pm Post subject: (No subject)
ok, then can someone give me a tutorial, or a site on how to use strings?
anyways, I did the question in C++, but I dont know why it doesn't work, I did it in turing, and it worked, and I don't know what's wrong with it. could someone please help me?
btw, the question required input from file, but since i don't know how to do that, i made it keyboard input.
code:
#include <iostream.h>
int main ()
{
int tot, posx, posy;
int count =0;
int pos [8][8];
for (int x= 0; x<8; x++)
{
for (int y= 0; y<8; y++)
{
pos [x][y] = 0;
}
}
char type [1];
cout << "Input the total number" << endl;
cin >> tot;
for (int i=0;i<tot; i++)
{
cin >> type >> posx >> posy;
if (type == "a")
{
pos [posx-1][posy-1]++1;
pos [posx-1][posy-2]++1;
pos [posx-1][posy-3]++1;
pos [posx][posy-3]++1;
pos [posx+1][posy-3]++1;
for (int k=0; i<8; i++)
{
for (int j=0; j<8; j++)
{
if (pos[k][j] != 0)
{
count ++1;
}
}
}
cout << count;
return 0;
}
Hmm... I don't know what you're doing with "pos [posx-2][posy-1]++;"... or the similar array types, are you trying to increase their value? You have to be careful because an array in C++ is actually a pointer, and using ++ might actually increase the memory location, not the value of the actual address (I say might because I've never done a ++ on an array before). So what you want to do is try replacing those with "pos[posx-2][posy-1] += 1;"
Also, your last for loop initializes a low bound of integer k yet binds it by a high bound of the integer i, and increase integer i, while k remains at 0. Also, your shape definitions seemed wrong to me... Here's the rewritten code:
for (int k=0; k<8; k++)
{
for (int j=0; j<8; j++)
{
if (pos[k][j] == 1)
{
count++;
}
}
}
cout << count;
return(0);
}
rizzix
Posted: Thu Jul 03, 2003 9:50 am Post subject: (No subject)
about using ++ it's allright. dosen't affect the pointer onlyt the contents, as long as he defined the array as a 2 dimensional one and he has 2 [][] thats all.
and here's the working code without much changes:
code:
#include <iostream>
using namespace std;
int main ()
{
int tot, posx, posy;
int count = 0;
int pos [8][8];