Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 ACSL question
Index -> Programming, C++ -> C++ Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
AsianSensation




PostPosted: 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;
}
Sponsor
Sponsor
Sponsor
sponsor
SilverSprite




PostPosted: 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




PostPosted: Mon Jun 23, 2003 3:47 pm   Post subject: (No subject)

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




PostPosted: 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




PostPosted: 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




PostPosted: 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

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".
SilverSprite




PostPosted: 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




PostPosted: Mon Jun 23, 2003 8:35 pm   Post subject: (No subject)

y not use string instead of characters?
AsianSensation




PostPosted: 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




PostPosted: 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




PostPosted: 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




PostPosted: 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




PostPosted: 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?
UBC_Wiskatos




PostPosted: 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);
}
rizzix




PostPosted: 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];

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
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 21 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: