Computer Science Canada

ACSL question

Author:  AsianSensation [ Sun Jun 22, 2003 9:32 pm ]
Post subject:  ACSL question

I am doing some old ACSL question, actually, this one to be exact:

http://www.acsl.org/acsl/96-97/pdf/jr/prog2.pdf

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]++;

                }
                else if (type == "b")
                {
                        pos [posx-1][posy-1]++;
                        pos [posx][posy-1]++;
                        pos [posx-1][posy-2]++;
                        pos [posx][posy-2]++;
                        pos [posx-1][posy-3]++;
                }
                else if (type == "c")
                {
                        pos [posx-1][posy-1]++;
                        pos [posx-1][posy]++;
                        pos [posx-2][posy-1]++;
                        pos [posx-3][posy-1]++;
                        pos [posx-4][posy-1]++;
                }
        }

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;
}

Author:  SilverSprite [ Mon Jun 23, 2003 9:01 am ]
Post 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..

Author:  AsianSensation [ Mon Jun 23, 2003 3:47 pm ]
Post subject: 

so instead of using count++, I should use +=1?

Author:  Catalyst [ Mon Jun 23, 2003 3:48 pm ]
Post subject: 

in ur prog u have

code:

count++1;


you only need

code:

count++;

hould be

Author:  SilverSprite [ Mon Jun 23, 2003 4:21 pm ]
Post subject: 

Ok what part of "the 1 shouldnt be there" dont you get asian:P *sigh* hopeless

Author:  AsianSensation [ Mon Jun 23, 2003 4:45 pm ]
Post 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

code:

char type [2];
cin >> type;

if (type == "a")
cout >> "Yay!"
else
cout >> "Boo!"


When I tried to use that, it outputted "Boo!" every single time, even if I entered "a".

Author:  SilverSprite [ Mon Jun 23, 2003 7:34 pm ]
Post subject: 

Nope the if statements arent the problem.. Do you know the difference between a char and a string.. doesnt look like it...

Author:  Homer_simpson [ Mon Jun 23, 2003 8:35 pm ]
Post subject: 

y not use string instead of characters?

Author:  AsianSensation [ Mon Jun 23, 2003 8:54 pm ]
Post 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.

Author:  Homer_simpson [ Mon Jun 23, 2003 9:25 pm ]
Post 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...

Author:  rizzix [ Tue Jun 24, 2003 1:48 pm ]
Post 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
    ...

Author:  SilverSprite [ Tue Jun 24, 2003 7:43 pm ]
Post 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..

Author:  AsianSensation [ Wed Jul 02, 2003 9:55 pm ]
Post subject: 

ok, then can someone give me a tutorial, or a site on how to use strings?

Author:  UBC_Wiskatos [ Wed Jul 02, 2003 10:26 pm ]
Post subject:  Re: ACSL question

AsianSensation wrote:
I am doing some old ACSL question, actually, this one to be exact:

http://www.acsl.org/acsl/96-97/pdf/jr/prog2.pdf

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;

                }
                else if (type == "b")
                {
                        pos [posx-1][posy-1]++1;
                        pos [posx][posy-1]++1;
                        pos [posx-1][posy-2]++1;
                        pos [posx][posy-2]++1;
                        pos [posx-1][posy-3]++1;
                }
                else if (type == "c")
                {
                        pos [posx-1][posy-1]++1;
                        pos [posx-1][posy]++1;
                        pos [posx-2][posy-1]++1;
                        pos [posx-3][posy-1]++1;
                        pos [posx-4][posy-1]++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:

code:

#include <iostream>

#define BLOCK_A 1
#define BLOCK_B 2
#define BLOCK_C 3

using namespace std;

int main ()
{

        int iTotal, posx, posy, iBlockType;
        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;
                }
        }
 
        cout << "Input the total number: ";
        cin >> iTotal;

        for (int i=0;i<iTotal; i++)
        {
                cout << "Type of block (A: 1, B: 2, C: 3): ";
                cin >> iBlockType;
                cout << "X-Coord: ";
                cin >> posx;
                cout << "Y-Coord: ";
                cin >> posy;

                switch(iBlockType)
                {
                        case BLOCK_A:
                        {
                                pos[posx][posy] = 1;
                                pos[posx][posy-1] = 1;
                                pos[posx][posy-2] = 1;
                                pos[posx+1][posy-2] = 1;
                                pos[posx+2][posy-2] = 1;
                        } break;
                        case BLOCK_B:
                        {
                                pos[posx][posy] = 1;
                                pos[posx+1][posy] = 1;
                                pos[posx][posy-1] = 1;
                                pos[posx+1][posy-1] = 1;
                                pos[posx][posy-2] = 1;
                        } break;
                        case BLOCK_C:
                        {
                                pos[posx][posy] = 1;
                                pos[posx-1][posy] = 1;
                                pos[posx-2][posy] = 1;
                                pos[posx-3][posy] = 1;
                                pos[posx][posy+1] = 1;
                        } break;
                        default:
                        {
                                cout << "Invalid input!" << endl;
                        }
                }
        }

        for (int k=0; k<8; k++)
        {
                for (int j=0; j<8; j++)
                {
                        if (pos[k][j] == 1)
                        {
                                count++;
                        }
                }
        }

        cout << count;
       
        return(0);
}

Author:  rizzix [ Thu Jul 03, 2003 9:50 am ]
Post 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];

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: ";
   cin >> tot;

   for (int i=0;i<tot; i++)
   {
      cout << "Type of block (A: 1, B: 2, C: 3): ";
      cin >> iBlockType;
      cout << "X-Coord: ";
      cin >> posx;
      cout << "Y-Coord: ";
      cin >> 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]++;

      }
      else if (type == "b")
      {
         pos [posx-1][posy-1]++;
         pos [posx][posy-1]++;
         pos [posx-1][posy-2]++;
         pos [posx][posy-2]++;
         pos [posx-1][posy-3]++;
      }
      else if (type == "c")
      {
         pos [posx-1][posy-1]++;
         pos [posx-1][posy]++;
         pos [posx-2][posy-1]++;
         pos [posx-3][posy-1]++;
         pos [posx-4][posy-1]++;
      }
   }

for (int k=0; k<8; k++)
{
   for (int j=0; j<8; j++)
   {
      if (pos[k][j] != 0)
      {
         count++;
      }
   }
}

  cout << count << endl;
  system("PAUSE");
  return 0;
}


EDIT: actually i changed it a bit to make it more compatible with newer compilers Confused

Author:  AsianSensation [ Thu Jul 03, 2003 9:24 pm ]
Post subject: 

ok, I changed the code around(see edit), but still, it won't output anything, the cursor just flashes. UBC_Wiskatos's works, except I don't know that much about what he did, apparently, I skipped the case section of the tutorial.

so what's wrong with it now?

Author:  UBC_Wiskatos [ Thu Jul 03, 2003 10:10 pm ]
Post subject: 

Try this:

I don't think your original array values correspond to the shapes, I changed them in my code, but they're in their original form here. Anyway, the problem seems to be with the if seeing if the char is equal to a, b, or c. I think this should work.

I think the core problem is that you use double quotes instead of single quotes, since single quotes are for (single-character) chars.

code:

#include <iostream>
using namespace std;

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;

   cout << "Input the total number: ";
   cin >> tot;

   for (int i=0;i<tot; i++)
   {
      cout << "Type of block (A, B, C): ";
      cin >> type;
      cout << "X-Coord: ";
      cin >> posx;
      cout << "Y-Coord: ";
      cin >> 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]++;

      }
      else if (type == 'b')
      {
         pos [posx-1][posy-1]++;
         pos [posx][posy-1]++;
         pos [posx-1][posy-2]++;
         pos [posx][posy-2]++;
         pos [posx-1][posy-3]++;
      }
      else if (type == 'c')
      {
         pos [posx-1][posy-1]++;
         pos [posx-1][posy]++;
         pos [posx-2][posy-1]++;
         pos [posx-3][posy-1]++;
         pos [posx-4][posy-1]++;
      }
   }

for (int k=0; k<8; k++)
{
   for (int j=0; j<8; j++)
   {
      if (pos[k][j] != 0)
      {
         count++;
      }
   }
}

  cout << count << endl;
  return 0;
}

Author:  SilverSprite [ Thu Jul 03, 2003 10:58 pm ]
Post subject: 

you did that AGAIN!! i told you not to!! and so did so many other ppl asian

Author:  rizzix [ Fri Jul 04, 2003 10:35 am ]
Post subject: 

lol

Author:  AsianSensation [ Fri Jul 04, 2003 7:59 pm ]
Post subject: 

man.......Im so messed up it's not even funny.....
I found out the problem

code:

for (int k=0; k<8; k++)


instead, I had this:

code:

for (int k=0; k<8; i++)


man....Im soooo out of it.....

maybe the SAT stuff is detrimental to my health...

Author:  UBC_Wiskatos [ Fri Jul 04, 2003 9:08 pm ]
Post subject: 

LOL, jeez man, I told you about that error a few posts back! Those SATs are going to kill you. Very Happy


: