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

Username:   Password: 
 RegisterRegister   
 Array of pointers
Index -> Programming, C++ -> C++ Help
Goto page 1, 2, 3  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Tubs




PostPosted: Wed Nov 16, 2005 11:47 pm   Post subject: Array of pointers

Is the best way to change a array belonging to the main function inside a different function by using enumerated types & a pointer to it?
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Thu Nov 17, 2005 12:02 am   Post subject: (No subject)

Well, you have an array declared inside your main function.

c:
int main()
{
   int foo[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

   return 0;
}


What you really have is a glorified pointer that points to the beginning of the array.

This pointer can be passed to another function. But, since you just have a glorified pointer, no information about the length of the array gets passed with it. So we have a separate parameter for the length of the array.

Then inside the function, we can iterate over the array.

c:
void increment_all(int *input_array, int size_of_array)
{
   int index;

   for (index = 0; index < size_of_array; index++)
   {
      input_array[index]++;
   }
}


And back in main we can use that.

c:
#include <stdio.h>

void increment_all(int *input_array, int size_of_array)
{
   int index;

   for (index = 0; index < size_of_array; index++)
   {
      input_array[index]++;
   }
}

int main()
{
   int index;
   int foo[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

   increment_all(foo, 10);

   for (index = 0; index < 10; index++)
   {
      printf("%d\n", foo[index]);
   }

   return 0;
}
Tubs




PostPosted: Thu Nov 17, 2005 12:24 am   Post subject: (No subject)

I should have been more specific. How about multi dementional arrays? (2 in the case I am working with)
wtd




PostPosted: Thu Nov 17, 2005 12:58 am   Post subject: (No subject)

Ok. A two-dimensional array is an array of arrays. Each of those arrays is a pointer to its beginning. Thus the two-dimensional array is just a glorified pointer to a glorified pointer.

However, there is a complication. If we have a multidimensional array, we cannot pass declare the function as taking a pointer to a pointer.

Here's some working code to consider:

c:
#include <stdio.h>

void increment_all(int (*input_array)[5], int y_dimension)
{
   int x_index, y_index;

   for (y_index = 0; y_index < y_dimension; y_index++)
   {
      for (x_index = 0; x_index < 5; x_index++)
      {
         input_array[y_index][x_index]++;
      }
   }
}

int main()
{
   int x_index, y_index;
   int foo[2][5] = {{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}};

   increment_all(foo, 2);

   for (y_index = 0; y_index < 2; y_index++)
   {
      for (x_index = 0; x_index < 5; x_index++)
      {
         printf("%d\n", foo[y_index][x_index]);
      }
   }

   return 0;
}
Tubs




PostPosted: Thu Nov 17, 2005 1:07 am   Post subject: (No subject)

Isn't C silly. You rock my socks wtd.
wtd




PostPosted: Thu Nov 17, 2005 1:13 am   Post subject: (No subject)

Tubs wrote:
Isn't C silly.


Yes.

Tubs wrote:
You rock my socks wtd.


Glad to help.

Thank you for asking a relatively focused question. These are so much easier to approach than, "here's my hundred lines of code... what's wrong?!"

Smile
wtd




PostPosted: Thu Nov 17, 2005 1:19 am   Post subject: (No subject)

Yes, C is silly. Fortunately there are other fish in the sea.

code:
# let foo = [|[|1; 2; 3|]; [|4; 5; 6; 7|]|];;
val foo : int array array = [|[|1; 2; 3|]; [|4; 5; 6; 7|]|]
# let increment_all arr =
     for y = 0 to Array.length arr - 1 do
        for x = 0 to Array.length arr.(y) - 1 do
           arr.(y).(x) <- arr.(y).(x) + 1
        done
     done;;
val increment_all : int array array -> unit = <fun>
# increment_all foo;;
- : unit = ()
# foo;;
- : int array array = [|[|2; 3; 4|]; [|5; 6; 7; 8|]|]
Tubs




PostPosted: Thu Nov 17, 2005 1:44 am   Post subject: (No subject)

*backs away slowly towards the door*

*runs*
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Thu Nov 17, 2005 2:08 am   Post subject: (No subject)

Here's just the function:

code:
let increment_all arr =
   for y = 0 to Array.length arr - 1 do
      for x = 0 to Array.length arr.(y) - 1 do
         arr.(y).(x) <- arr.(y).(x) + 1
      done
   done


That isn't so bad, is it? Smile
Tubs




PostPosted: Thu Nov 17, 2005 12:31 pm   Post subject: (No subject)

Not trying to be a nuisance and post my 100 line program and ask why its not working, but I really don't know how to solve this problem. Since this program uses reading / writing from .txt files it crashes inside my IDE, but says that there are no errors. However, when i try to run the .exe that has a windows error as well so I am left with absolutely no idea on what is failing (I suspect it has something to do with the borders of the array). The .txt file is a 25x25 square of X's, with -'s along the border.

dev c++ Mad

code:

#include <stdio.h>
#define ROWS 25
#define COLS 25

void generate(char (*grid)[25], int y_dim);

int main(int argc, char *argv[])
{


int x, y, k, j, rep;
char cells[ROWS][COLS], i;

FILE *in;         //Pointers to open and close the file

in = fopen( "data.txt", "r" );    // open a file for reading

for(y = 0; y < 25; ++y)

{
    for(x = 0; x < 25; ++x)

        {
        fscanf(in, "%c", &cells[x][y] );
        }
}

fclose (in);

for (k = 0; k < COLS; ++k)
      {
      printf ("%c", cells[j][k]);
      }

printf ("Enter 1 to generate again> ");
scanf ("%d", &rep);

if (rep == 1)
{

rep = 0;

do
{

  generate(cells, COLS - 1);

  for (j = 0; j < ROWS; ++j)

    {

    for (k = 0; k < COLS; ++k)
      {
      printf ("%c", cells[j][k]);
      }

  printf ("\n");

     }

printf ("Enter 1 to generate again> ");
scanf ("%d", &rep);

} while (rep = 1);
}

system ("pause");

  return 0;
}


void generate(char *grid[25], int y_dim)
{
   int x_i, y_i, friends = 0;

   for (y_i = 1; y_i < y_dim; y_i++)

   {
      for (x_i = 1; x_i < 24; x_i++)

      {

        if (grid[y_i - 1][x_i - 1] == 'O')
          friends++;

        if (grid[y_i - 1][x_i] == 'O')
          friends++;

        if (grid[y_i - 1][x_i + 1] == 'O')
          friends++;

        if (grid[y_i][x_i - 1] == 'O')
          friends++;

        if (grid[y_i][x_i + 1] == 'O')
          friends++;

        if (grid[y_i + 1][x_i - 1] == 'O')
          friends++;

        if (grid[y_i + 1][x_i] == 'O')
          friends++;

        if (grid[y_i + 1][x_i + 1] == 'O')
          friends++;

       /// Conditions

        if (grid[y_i][x_i] == 'O')

          // no survival code due to no changes

          if (friends >= 4 || friends < 2)  // death
             grid[y_i][x_i] = 'X';


        if (grid[y_i][x_i] == 'X')

          if (friends = 3)   // birth
            grid[y_i][x_i] == 'O';


      }

   }

}
Tubs




PostPosted: Thu Nov 17, 2005 12:32 pm   Post subject: (No subject)

OH ya this is the game of life. If that helps.
wtd




PostPosted: Thu Nov 17, 2005 12:57 pm   Post subject: (No subject)

Amazingly enough, well-formatted code is easier to work with.
Tubs




PostPosted: Thu Nov 17, 2005 12:59 pm   Post subject: (No subject)

Whats wrong with that?
wtd




PostPosted: Thu Nov 17, 2005 1:02 pm   Post subject: (No subject)

c:
#include <stdio.h>
#define ROWS 25
#define COLS 25

void generate(char (*grid)[25], int y_dim);

int main(int argc, char *argv[])
{
   int x, y, k, j, rep;
   char cells[ROWS][COLS], i;

   FILE *in;         //Pointers to open and close the file

   in = fopen("data.txt", "r");    // open a file for reading

   for (y = 0; y < 25; ++y)
   {
      for (x = 0; x < 25; ++x)
      {
         fscanf(in, "%c", &cells[x][y]);
      }
   }

   fclose (in);

   for (k = 0; k < COLS; ++k)
   {
      printf("%c", cells[j][k]);
   }

   printf("Enter 1 to generate again> ");
   scanf("%d", &rep);

   if (rep == 1)
   {
      rep = 0;

      do
      {
         generate(cells, COLS - 1);

         for (j = 0; j < ROWS; ++j)
         {
            for (k = 0; k < COLS; ++k)
            {
               printf("%c", cells[j][k]);
            }

            printf("\n");
         }

         printf("Enter 1 to generate again> ");
         scanf("%d", &rep);
      } while (rep = 1);
   }

   system ("pause");

   return 0;
}


void generate(char *grid[25], int y_dim)
{
   int x_i, y_i, friends = 0;

   for (y_i = 1; y_i < y_dim; y_i++)
   {
      for (x_i = 1; x_i < 24; x_i++)
      {
        if (grid[y_i - 1][x_i - 1] == 'O')
          friends++;

        if (grid[y_i - 1][x_i] == 'O')
          friends++;

        if (grid[y_i - 1][x_i + 1] == 'O')
          friends++;

        if (grid[y_i][x_i - 1] == 'O')
          friends++;

        if (grid[y_i][x_i + 1] == 'O')
          friends++;

        if (grid[y_i + 1][x_i - 1] == 'O')
          friends++;

        if (grid[y_i + 1][x_i] == 'O')
          friends++;

        if (grid[y_i + 1][x_i + 1] == 'O')
          friends++;

       /// Conditions

        if (grid[y_i][x_i] == 'O')

          // no survival code due to no changes

        if (friends >= 4 || friends < 2)  // death
           grid[y_i][x_i] = 'X';


        if (grid[y_i][x_i] == 'X')
           if (friends = 3)   // birth
              grid[y_i][x_i] == 'O';


      }
   }
}
Tubs




PostPosted: Thu Nov 17, 2005 1:03 pm   Post subject: (No subject)

I dont know how to do super cool stuff like that Sad
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 3  [ 45 Posts ]
Goto page 1, 2, 3  Next
Jump to:   


Style:  
Search: