9 Replies - 1357 Views - Last Post: 16 March 2010 - 02:05 AM Rate Topic: -----

#1 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

number of elements in an array

Posted 15 March 2010 - 09:20 PM

I created 8 arrays to hold coordinates. I found that it would be far simpler to make one massive array that can hold these 8 arrays. Is it possible for me to get the number of elements in one of the subArrays of the master array? I know many of you may be tempted to tell me "Of course you can get the size, you have an array holding them!" Yes, I realize this. However, this file is simply a test I created for a much bigger program. In the bigger program, I won't have that array, nor will I have variables holding the subarrays. Again, you might ask "why won't you have that array?" but I can only respond with "why ask why?" My question is, can I find the number of elements in these subarrays or is it impossible?

P.S. I did try to do some digging on the topic through google and the site's search function. I've seen people ask similar questions, but I didn't see any that really helped me. Thanks for reading everyone!

#include <cstdlib>
#include <iostream>
#define arSize(array) sizeof(array)/(sizeof(int)*2)
using namespace std;

void populateArray(int masterArr[8][10][2], int arrToFill[][2], short thePose, short count)
{     
    int x = 0;
    int y = thePose * 128;
    for (int frame = 0; frame < count; frame++)
    {
        arrToFill[frame][0] = x;
        arrToFill[frame][1] = y;
        masterArr[thePose][frame][0] = x;
        masterArr[thePose][frame][1] = y;
        x += 128;
                
    }
}

void printSubAry(int myArray[][2], int size)
{
     
     for(int x=0; x < size; x++)
     {
             cout <<"["<< myArray[x][0]<<","<<myArray[x][1]<<"]";
     } 
     cout<<"\n";
}

void printMasterAry(int myArray[8][10][2], int sizeArray[8])
{
     for(int pose=0; pose < 8; pose++)
     { 
             for(int frame=0; frame < sizeArray[pose]; frame++)
              {
                     cout <<"["<< myArray[pose][frame][0]<<","<<myArray[pose][frame][1]<<"]";
              } 
              cout<<"\n";
     }
}


int main( int argc, char* args[] )
{
    int standingArr[4][2];             
    int walkingArr[8][2];
    int cheeringArr[8][2];
    int punch1Arr[3][2];
    int punch2Arr[3][2];
    int punch3Arr[5][2];
    int throwAttackArr[10][2];
    int spawnArr[9][2];
    //pose/frame/coord
    int masterArr[8][10][2];  //an attempt at a 3d array
    //(sizeof(myArray)/sizeof(int))/2
 
    int sizeAry[8] = {4,8,8,3,3,5,10,9};
 
    enum animation {standing,walking,cheering, punch1, 
                 punch2, punch3, throwAttack, spawn};//the possible poses
                 
    int x;//The pose a sprite currently has

    populateArray(masterArr,standingArr,standing,4);
    populateArray(masterArr,walkingArr,walking,8);
    populateArray(masterArr,cheeringArr,cheering,8);
    populateArray(masterArr,punch1Arr,punch1,3);
    populateArray(masterArr,punch2Arr,punch2,3);
    populateArray(masterArr,punch3Arr,punch3,5);
    populateArray(masterArr,throwAttackArr,throwAttack,10);
    populateArray(masterArr,spawnArr,spawn,9);
    
    
    
    cout<<"\nStanding Arr:\n";
    printSubAry(standingArr,4);
    
    cout<<"\nWalking Arr:\n";
    printSubAry(walkingArr,8);
    
    cout<<"\nCheering Arr:\n";
    printSubAry(cheeringArr,8);
    
    cout<<"\npunch1 Arr:\n";
    printSubAry(punch1Arr,3);
    
    cout<<"\npunch2 Arr:\n";
    printSubAry(punch2Arr,3);
    
    cout<<"\npunch3 Arr:\n";
    printSubAry(punch3Arr,5);
    
    cout<<"\nThrow Attack Arr:\n";
    printSubAry(throwAttackArr,10);
    
    cout<<"\nSpawn Arr:\n";
    printSubAry(spawnArr,9);
    
    cout<<"\nMaster Arr:\n";
    printMasterAry(masterArr,sizeAry);
    
    cout<<"Size of masterArr[0]\n"<<arSize(masterArr[0])<<"\nSize of standingArr\n"<<arSize(standingArr);
    
    cin >> x;
    
}


Is This A Good Question/Topic? 0
  • +

Replies To: number of elements in an array

#2 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: number of elements in an array

Posted 15 March 2010 - 10:45 PM

With all that chatty chat, it's not exactly clear what you will have, but if you have something like int arr[4][2];:

int arr[4][2];
std::cout << "Size of array in bytes = " << sizeof(arr) << std::endl;
std::cout << "Number of elements in array = " << sizeof(arr)/sizeof(int) << endl;



Does that apply to your situation?
Was This Post Helpful? -1
  • +
  • -

#3 Guest_c.user*


Reputation:

Re: number of elements in an array

Posted 15 March 2010 - 11:01 PM

atraub said:

I found that it would be far simpler to make one massive array that can hold these 8 arrays.

these arrays are different
one array has some number of equal elements

int n[10];
there are ten elements and every is int, they are equal

correct your size macro
#define arSize(array) ( sizeof array / sizeof array[0] )
this is for a one-dimensional array

#define arSize(array) ( sizeof array / sizeof array[0][0] )
this is for a two-dimensional array

even if you make a union with all these arrays, it will not look nicely

for your purpose you could do a structure with pointers to arrays, I think

This post has been edited by c.user: 15 March 2010 - 11:02 PM

Was This Post Helpful? 0

#4 no2pencil  Icon User is online

  • Toubabo Koomi
  • member icon

Reputation: 5226
  • View blog
  • Posts: 27,000
  • Joined: 10-May 07

Re: number of elements in an array

Posted 15 March 2010 - 11:03 PM

I would initialize the array with '\0' in every element, & then after the array elements are filled, you can verify the final char by finding the 1st '\0' in each array, & thus find the size.
Was This Post Helpful? 0
  • +
  • -

#5 Guest_c.user*


Reputation:

Re: number of elements in an array

Posted 15 March 2010 - 11:49 PM

no2pencil said:

I would initialize the array with '\0' in every element

his array is an integer array
if one of the elements will be zero (because it can be zero in row 1, 2, 3, 0, 5, 7, ...) it will give wrong info about size
Was This Post Helpful? 0

#6 no2pencil  Icon User is online

  • Toubabo Koomi
  • member icon

Reputation: 5226
  • View blog
  • Posts: 27,000
  • Joined: 10-May 07

Re: number of elements in an array

Posted 15 March 2010 - 11:51 PM

Hrm, int huh? Fill it with chars & then atoi() on it when it comes time to use the values.

Maybe?
Was This Post Helpful? 0
  • +
  • -

#7 Guest_c.user*


Reputation:

Re: number of elements in an array

Posted 15 March 2010 - 11:58 PM

how he can fill it with chars if int can permit at least 32767 and char permits 255 only
if he want a row 1, 2, 158, 0, 22174, -3, 0 or any other...:)
Was This Post Helpful? 0

#8 Guest_c.user*


Reputation:

Re: number of elements in an array

Posted 16 March 2010 - 12:24 AM

some implementation

#include <stdio.h>
#include <stdlib.h>

enum {
    DIMMAX = 10
};

struct array {
    int n[DIMMAX][2];
    int size;
};

typedef struct array SubArr;

int main(void) /* ANSI C89 */
{

    SubArr master[8];
    int i, j;
    
    master[0].size = 3;
    master[1].size = 3;
    master[2].size = 4;
    master[3].size = 5;
    master[4].size = 8;
    master[5].size = 8;
    master[6].size = 9;
    master[7].size = 10;
    
    for (i = 0; i < master[0].size; i++)
        for (j = 0; j < 2; j++)
            master[0].n[i][j] = 3;
    
    for (i = 0; i < master[1].size; i++)
        for (j = 0; j < 2; j++)
            master[1].n[i][j] = 3;
    
    for (i = 0; i < master[2].size; i++)
        for (j = 0; j < 2; j++)
            master[2].n[i][j] = 4;

    ...
    
    exit(EXIT_SUCCESS);
}


This post has been edited by c.user: 16 March 2010 - 12:29 AM

Was This Post Helpful? 0

#9 rs4  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 29
  • View blog
  • Posts: 153
  • Joined: 01-February 09

Re: number of elements in an array

Posted 16 March 2010 - 01:59 AM

The answer to your question depends on how you allocate your memory. If you allocate an array at compile time then yes simply using sizeof() will work.

Consider this code
#include <iostream>
using namespace std;

int main(){
	int x[9][5][7];
	cout<<sizeof(x)<<endl
		<<sizeof(x[0])<<endl
		<<sizeof(x[0][0])<<endl
		<<sizeof(x[0][0][0]);
	return 0;
}


The output is this(on 32-bit system, sizeof(int) == 4)

1260 Given by 9*5*7*4
140 Given by 5*7*4
28 Given by 7*4
4 Given by 4 Size of int.

So finding the size of any sub part of that array is easy.
Size of 1st dimension == sizeof(x)/sizeof(x[0]) == 1260/140 == 9
Size of 2nd dimension == sizeof(x[0])/sizeof(x[0][0]) == 140/28 == 5

and so on.

Now if you want to use dynamic memory allocation sizeof() doesn't work, and there is no other function to take its place that I'm aware of. But you can use the first element of the array to tell you how many elements are in the array, and then start accessing from [1].

Now in your bigger problem where your not going to have an array well I have no idea what your going to need to do.

Hope this helps
Was This Post Helpful? 0
  • +
  • -

#10 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: number of elements in an array

Posted 16 March 2010 - 02:05 AM

This is exactly why to use std::vector and not an array.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1